Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { describe, it } from "node:test";
|
|
import { readLimitedJsonBody } from "./read-limited-json-body.ts";
|
|
|
|
const MAX = 64;
|
|
|
|
function jsonRequest(body: string, headers: Record<string, string> = {}): Request {
|
|
return new Request("http://localhost:28472/admin/translations/catalog", {
|
|
method: "PATCH",
|
|
headers: {
|
|
"content-type": "application/json",
|
|
...headers
|
|
},
|
|
body
|
|
});
|
|
}
|
|
|
|
describe("readLimitedJsonBody", () => {
|
|
it("parses a small JSON body", async () => {
|
|
const result = await readLimitedJsonBody(
|
|
jsonRequest(JSON.stringify({ locale: "nl", updates: { a: "b" } })),
|
|
MAX
|
|
);
|
|
assert.equal(result.ok, true);
|
|
if (result.ok) {
|
|
assert.deepEqual(result.body, { locale: "nl", updates: { a: "b" } });
|
|
}
|
|
});
|
|
|
|
it("returns 413 kind when Content-Length exceeds max", async () => {
|
|
const result = await readLimitedJsonBody(
|
|
jsonRequest("{}", { "content-length": String(MAX + 1) }),
|
|
MAX
|
|
);
|
|
assert.deepEqual(result, { ok: false, kind: "payload_too_large" });
|
|
});
|
|
|
|
it("returns 413 kind when Content-Length is non-finite", async () => {
|
|
const result = await readLimitedJsonBody(
|
|
jsonRequest("{}", { "content-length": "nope" }),
|
|
MAX
|
|
);
|
|
assert.deepEqual(result, { ok: false, kind: "payload_too_large" });
|
|
});
|
|
|
|
it("returns 413 kind when streamed body exceeds max without Content-Length", async () => {
|
|
const oversized = "x".repeat(MAX + 8);
|
|
const stream = new ReadableStream({
|
|
start(controller) {
|
|
controller.enqueue(new TextEncoder().encode(oversized));
|
|
controller.close();
|
|
}
|
|
});
|
|
const request = new Request("http://localhost:28472/admin/translations/catalog", {
|
|
method: "PATCH",
|
|
headers: { "content-type": "application/json" },
|
|
body: stream,
|
|
duplex: "half"
|
|
} as RequestInit & { duplex: "half" });
|
|
const result = await readLimitedJsonBody(request, MAX);
|
|
assert.deepEqual(result, { ok: false, kind: "payload_too_large" });
|
|
});
|
|
|
|
it("returns invalid_json for empty body", async () => {
|
|
const result = await readLimitedJsonBody(jsonRequest(""), MAX);
|
|
assert.deepEqual(result, { ok: false, kind: "invalid_json" });
|
|
});
|
|
|
|
it("returns invalid_json for malformed JSON", async () => {
|
|
const result = await readLimitedJsonBody(jsonRequest("{not-json"), MAX);
|
|
assert.deepEqual(result, { ok: false, kind: "invalid_json" });
|
|
});
|
|
});
|