Skip to content

Commit 5e8278c

Browse files
authored
Use appendedHeaders for response-type-error: one entry per error
Agent-Logs-Url: https://github.com/counterfact/api-simulator/sessions/d4b157a0-4b1d-434f-baa8-f709fcf80a05
1 parent e9037b4 commit 5e8278c

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

src/server/dispatcher.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,13 @@ export class Dispatcher {
391391
if (!validation.valid) {
392392
return {
393393
...normalizedResponse,
394-
headers: {
395-
...normalizedResponse.headers,
396-
"response-type-error": validation.errors,
397-
},
394+
appendedHeaders: [
395+
...(normalizedResponse.appendedHeaders ?? []),
396+
...validation.errors.map((error): [string, string] => [
397+
"response-type-error",
398+
error,
399+
]),
400+
],
398401
};
399402
}
400403
}

src/server/koa-middleware.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,20 @@ export function koaMiddleware(
148148
for (const [key, value] of Object.entries(response.headers)) {
149149
if (!HEADERS_TO_DROP.has(key.toLowerCase())) {
150150
if (Array.isArray(value)) {
151-
for (const item of value) {
152-
ctx.res.appendHeader(key, item);
153-
}
151+
ctx.set(key, value);
154152
} else {
155153
ctx.set(key, value.toString());
156154
}
157155
}
158156
}
159157
}
160158

159+
if (response.appendedHeaders) {
160+
for (const [key, value] of response.appendedHeaders) {
161+
ctx.res.appendHeader(key, value);
162+
}
163+
}
164+
161165
ctx.status = response.status ?? HTTP_STATUS_CODE_OK;
162166

163167
return undefined;

src/server/registry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ interface Module {
7575
}
7676

7777
type CounterfactResponseObject = {
78+
appendedHeaders?: [string, string][];
7879
body?: Uint8Array | string;
7980
content?: {
8081
body: unknown;

test/server/dispatcher.test.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,10 +1423,11 @@ describe("given a request that contains the differently cased path", () => {
14231423
req: { path: "/widgets" },
14241424
});
14251425

1426-
const errors = response.headers?.["response-type-error"];
1427-
const firstError = Array.isArray(errors) ? errors[0] : errors;
1426+
const errorValues = response.appendedHeaders
1427+
?.filter(([key]) => key === "response-type-error")
1428+
.map(([, value]) => value);
14281429

1429-
expect(firstError).toContain("x-required-header");
1430+
expect(errorValues?.[0]).toContain("x-required-header");
14301431
});
14311432

14321433
it("adds response-type-error headers when a response header has the wrong type", async () => {
@@ -1444,10 +1445,11 @@ describe("given a request that contains the differently cased path", () => {
14441445
req: { path: "/widgets" },
14451446
});
14461447

1447-
const errors = response.headers?.["response-type-error"];
1448-
const firstError = Array.isArray(errors) ? errors[0] : errors;
1448+
const errorValues = response.appendedHeaders
1449+
?.filter(([key]) => key === "response-type-error")
1450+
.map(([, value]) => value);
14491451

1450-
expect(firstError).toContain("x-count");
1452+
expect(errorValues?.[0]).toContain("x-count");
14511453
});
14521454

14531455
it("does not add error headers when all required response headers are present and valid", async () => {
@@ -1465,7 +1467,11 @@ describe("given a request that contains the differently cased path", () => {
14651467
req: { path: "/widgets" },
14661468
});
14671469

1468-
expect(response.headers?.["response-type-error"]).toBeUndefined();
1470+
expect(
1471+
response.appendedHeaders?.find(
1472+
([key]) => key === "response-type-error",
1473+
),
1474+
).toBeUndefined();
14691475
});
14701476

14711477
it("skips response validation when validateResponses is false", async () => {
@@ -1480,7 +1486,11 @@ describe("given a request that contains the differently cased path", () => {
14801486
req: { path: "/widgets" },
14811487
});
14821488

1483-
expect(response.headers?.["response-type-error"]).toBeUndefined();
1489+
expect(
1490+
response.appendedHeaders?.find(
1491+
([key]) => key === "response-type-error",
1492+
),
1493+
).toBeUndefined();
14841494
});
14851495
});
14861496
});

0 commit comments

Comments
 (0)