ts6-botflow/transport.test.mjs
Bxio 52f44b368d
All checks were successful
Testar e publicar imagem Docker / publish (push) Successful in 32s
Send WebQuery action parameters as JSON instead of form data
2026-10-10 21:06:18 +01:00

124 lines
4.0 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import http from "node:http";
import { query } from "./studio.mjs";
import { httpRequest } from "./flow-steps.mjs";
test("WebQuery uses authenticated GET and JSON POST, handles fragmented UTF-8 and server errors", async () => {
const requests = [];
const server = http.createServer(async (req, res) => {
const chunks = [];
for await (const chunk of req) chunks.push(chunk);
requests.push({
method: req.method,
path: req.url,
key: req.headers["x-api-key"],
contentType: req.headers["content-type"],
contentLength: req.headers["content-length"],
transferEncoding: req.headers["transfer-encoding"],
body: Buffer.concat(chunks).toString(),
});
res.setHeader("Content-Type", "application/json");
if (req.url.includes("channeledit")) {
let params;
try {
params = JSON.parse(Buffer.concat(chunks).toString());
} catch {}
if (
req.headers["content-type"] !== "application/json" ||
!params?.cid ||
typeof params.channel_description !== "string"
) {
res.end(
JSON.stringify({
status: { code: 1538, message: "invalid parameter" },
}),
);
return;
}
res.end(JSON.stringify({ status: { code: 0 }, body: [] }));
return;
}
if (req.url.includes("clientkick")) {
res.end(
JSON.stringify({
status: { code: 2568, message: "insufficient client permissions" },
body: [],
}),
);
return;
}
const bytes = Buffer.from(
JSON.stringify({
status: { code: 0 },
body: [{ virtualserver_name: "Olá | Portugal" }],
}),
),
split = bytes.indexOf(Buffer.from("á")) + 1;
res.write(bytes.subarray(0, split));
setImmediate(() => res.end(bytes.subarray(split)));
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const connection = {
url: `http://127.0.0.1:${server.address().port}`,
serverId: 1,
apiKey: "fixture-key",
};
try {
const result = await query(connection, "serverinfo");
assert.equal(result[0].virtualserver_name, "Olá | Portugal");
const text =
"[b]Informações do servidor[/b]\nOlá | linha\nmais = texto & + / \\ 🎵";
await query(connection, "channeledit", {
cid: 21,
channel_description: text,
});
assert.equal(requests[0].method, "GET");
assert.equal(requests[0].key, "fixture-key");
assert.equal(requests[1].method, "POST");
assert.equal(requests[1].key, "fixture-key");
assert.equal(requests[1].contentType, "application/json");
assert.equal(
Number(requests[1].contentLength),
Buffer.byteLength(requests[1].body),
);
assert.equal(requests[1].transferEncoding, undefined);
assert.deepEqual(JSON.parse(requests[1].body), {
cid: 21,
channel_description: text,
});
await assert.rejects(
() => query(connection, "clientkick", { clid: 1, reasonid: 5 }),
/2568.*permissions/,
);
const generic = await httpRequest(connection.url);
assert.equal(generic.body.body[0].virtualserver_name, "Olá | Portugal");
assert.throws(() => httpRequest("file:///tmp/test"), /HTTP/);
} finally {
await new Promise((resolve) => server.close(resolve));
}
});
test("network requests respond to flow cancellation", async () => {
const server = http.createServer(() => {});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
try {
const controller = new AbortController(),
promise = query(
{
url: `http://127.0.0.1:${server.address().port}`,
serverId: 1,
apiKey: "fixture-key",
},
"serverinfo",
{},
{ signal: controller.signal },
);
controller.abort();
await assert.rejects(() => promise, /cancelada/);
} finally {
server.closeAllConnections();
await new Promise((resolve) => server.close(resolve));
}
});