feat: add HTTP response and parsing helpers for form-based UX flows

This commit is contained in:
Codex
2026-02-18 12:54:05 +00:00
parent 4e443c3507
commit 8be203fb91
2 changed files with 134 additions and 0 deletions

31
test/http.test.js Normal file
View File

@@ -0,0 +1,31 @@
"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const {
redirect,
parseJSON,
parseFormUrlEncoded,
withQuery,
} = require("../src/lib/http");
test("redirect returns 303 and location", () => {
const response = redirect("/app");
assert.equal(response.status, 303);
assert.equal(response.headers.location, "/app");
});
test("parseJSON throws on invalid payload", () => {
assert.throws(() => parseJSON("{invalid}"), /invalid_json/);
});
test("parseFormUrlEncoded parses plus and percent encoding", () => {
const body = "title=Hello+World&body=Line%201%0ALine%202";
const parsed = parseFormUrlEncoded(body);
assert.equal(parsed.title, "Hello World");
assert.equal(parsed.body, "Line 1\nLine 2");
});
test("withQuery appends query params", () => {
assert.equal(withQuery("/login", { returnTo: "/audio/1", flash: "done" }), "/login?returnTo=%2Faudio%2F1&flash=done");
});