Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/tools/emulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ function headerStringTransform(
) {
throw new Error('Headers must be a JSON object');
}
return parsed as Record<string, string>;
const headers: Record<string, string> = {};
for (const [key, value] of Object.entries(parsed)) {
if (typeof value !== 'string') {
throw new Error(`Header "${key}" must have a string value`);
}
headers[key] = value;
}
return headers;
} catch (error) {
throw new Error(
`Invalid JSON for headers: ${error instanceof Error ? error.message : String(error)}`,
Expand Down
33 changes: 33 additions & 0 deletions tests/tools/emulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,39 @@ describe('emulation', () => {
);
});

for (const {type, key, value} of [
{type: 'number', key: 'X-Count', value: 42},
{type: 'boolean', key: 'X-Enabled', value: true},
{type: 'nested object', key: 'X-Nested', value: {value: 'test'}},
]) {
it(`rejects a ${type} extraHttpHeaders value in schema`, () => {
const {args} = createHandlerMocks();
const schema = zod.object(emulate(args).schema);
const result = schema.safeParse({
pageId: 1,
extraHttpHeaders: JSON.stringify({[key]: value}),
});
assert.strictEqual(result.success, false);
assert.strictEqual(
result.error.issues[0].message,
`Invalid JSON for headers: Header "${key}" must have a string value`,
);
});
}

it('parses an all-string extraHttpHeaders map in schema', () => {
const {args} = createHandlerMocks();
const schema = zod.object(emulate(args).schema);
const headers = {'X-Count': '42', 'X-Enabled': 'true'};
assert.deepStrictEqual(
schema.safeParse({
pageId: 1,
extraHttpHeaders: JSON.stringify(headers),
}),
{success: true, data: {pageId: 1, extraHttpHeaders: headers}},
);
});

it('reports validation issues instead of throwing for invalid transform inputs', () => {
const {args} = createHandlerMocks();
const schema = zod.object(emulate(args).schema);
Expand Down
Loading