feat: add HTTP response and parsing helpers for form-based UX flows
This commit is contained in:
31
test/http.test.js
Normal file
31
test/http.test.js
Normal 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");
|
||||
});
|
||||
Reference in New Issue
Block a user