summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-03-16 05:38:17 -0300
committerEuAndreh <eu@euandre.org>2024-03-16 05:38:17 -0300
commita03abe678afd45e9d139e13c9d17a569c2853547 (patch)
treec1703f22703ed878f1d9af0f4ba1d333e9d8ad2d
parentsrc/hero.mjs: Include stacktrace in 500 log entry (diff)
downloadpapod-a03abe678afd45e9d139e13c9d17a569c2853547.tar.gz
papod-a03abe678afd45e9d139e13c9d17a569c2853547.tar.xz
src/hero.mjs: interceptors.contentType(): Generate body from status when missing
-rw-r--r--src/hero.mjs18
-rw-r--r--tests/js/hero.mjs39
2 files changed, 44 insertions, 13 deletions
diff --git a/src/hero.mjs b/src/hero.mjs
index 5169284..b378887 100644
--- a/src/hero.mjs
+++ b/src/hero.mjs
@@ -71,15 +71,23 @@ export const interceptorsFn = ({
},
contentType: async (req, next) => {
const response = await next(req);
- const [mimeType, body] = typeof response.body === "string" ?
- ["text/html", response.body] :
- ["application/json", JSON.stringify(response.body) || ""];
+ const { status, body, headers } = response;
+ assert.equal(typeof status, "number");
+ const mappings = {
+ string: () => [ "text/html", body ],
+ undefined: () => [ "text/plain", http.STATUS_CODES[status] + "\n" ],
+ FALLBACK: () => [ "application/json", JSON.stringify(body) ],
+ };
+ const type = typeof body;
+ assert.notEqual(type, "FALLBACK");
+ const [mimeType, renderedBody] =
+ (mappings[type] || mappings.FALLBACK)();
return {
...response,
- body,
+ body: renderedBody,
headers: {
"Content-Type": mimeType,
- "Content-Length": Buffer.byteLength(body),
+ "Content-Length": Buffer.byteLength(renderedBody),
...(response.headers || {})
},
};
diff --git a/tests/js/hero.mjs b/tests/js/hero.mjs
index 1c34738..8d8f811 100644
--- a/tests/js/hero.mjs
+++ b/tests/js/hero.mjs
@@ -247,7 +247,7 @@ const test_interceptorsFn = async t => {
assert.deepEqual(
contents.map(o => u.dissoc(o, "timings")),
[
- { ...req, type: "in-request" },
+ { ...req, type: "in-request" },
{ id: req.id, status, type: "in-response" },
],
);
@@ -261,20 +261,31 @@ const test_interceptorsFn = async t => {
t.start("interceptorsFn().contentType()");
await t.test("empty values", async () => {
+ await assert.rejects(
+ async () => await interceptorsFn().contentType({}, next),
+ assert.AssertionError,
+ );
assert.deepEqual(
- await interceptorsFn().contentType({}, next),
+ await interceptorsFn().contentType({
+ status: 202,
+ }, next),
{
- body: "",
+ status: 202,
+ body: "Accepted\n",
headers: {
- "Content-Type": "application/json",
- "Content-Length": 0,
+ "Content-Type": "text/plain",
+ "Content-Length": 9,
},
},
);
assert.deepEqual(
- await interceptorsFn().contentType({ body: "" }, next),
+ await interceptorsFn().contentType({
+ status: 200,
+ body: "",
+ }, next),
{
+ status: 200,
body: "",
headers: {
"Content-Type": "text/html",
@@ -286,8 +297,12 @@ const test_interceptorsFn = async t => {
await t.test("body values", async () => {
assert.deepEqual(
- await interceptorsFn().contentType({ body: { a: 1 }}, next),
+ await interceptorsFn().contentType({
+ status: 201,
+ body: { a: 1 },
+ }, next),
{
+ status: 201,
body: `{"a":1}`,
headers: {
"Content-Type": "application/json",
@@ -297,8 +312,12 @@ const test_interceptorsFn = async t => {
);
assert.deepEqual(
- await interceptorsFn().contentType({ body: "<br />" }, next),
+ await interceptorsFn().contentType({
+ status: 200,
+ body: "<br />",
+ }, next),
{
+ status: 200,
body: "<br />",
headers: {
"Content-Type": "text/html",
@@ -311,6 +330,7 @@ const test_interceptorsFn = async t => {
await t.test("header values preference", async () => {
assert.deepEqual(
await interceptorsFn().contentType({
+ status: 503,
body: "",
headers: {
"Content-Type": "we have preference",
@@ -318,6 +338,7 @@ const test_interceptorsFn = async t => {
},
}, next),
{
+ status: 503,
body: "",
headers: {
"Content-Type": "we have preference",
@@ -330,12 +351,14 @@ const test_interceptorsFn = async t => {
await t.test("headers get propagated", async () => {
assert.deepEqual(
await interceptorsFn().contentType({
+ status: 500,
body: "",
headers: {
"My": "Header",
},
}, next),
{
+ status: 500,
body: "",
headers: {
"My": "Header",