diff --git a/src/Sage.ts b/src/Sage.ts index b41ddf4..1b62caf 100644 --- a/src/Sage.ts +++ b/src/Sage.ts @@ -30,6 +30,7 @@ import { createReadStream } from 'node:fs'; import { SageConfig } from './SageConfig.js'; import { SageServer } from './SageServer.js'; import { SageAssertException } from './SageAssertException.js'; +import { IncomingHttpHeaders } from 'undici/types/header.js'; /** * Greetings, I'm Sage - a chainable HTTP Testing Assistant. @@ -123,13 +124,26 @@ export class Sage { * Sets a header for the request. * Consider using this at the end of the chain if you want to override any of the defaults. * @param key - * @param value */ - set(key: string, value: string | string[]): this { + set(key: IncomingHttpHeaders): this; + set(key: string, value: string | string[]): this; + set(key: string, value: string): this; + set(key: string | IncomingHttpHeaders, value?: string | string[]): this { if (!this.request.headers) { this.request.headers = {}; } + if (typeof key === 'object' && value === undefined) { + this.request.headers = key; + return this; + } + + if (value === undefined || typeof key === 'object') { + throw new SageException( + 'When setting headers one by one, both key and value are required' + ); + } + value = wrapArray(value); // If already an array diff --git a/test/request.spec.ts b/test/request.spec.ts index d07ce1c..b811767 100644 --- a/test/request.spec.ts +++ b/test/request.spec.ts @@ -432,6 +432,7 @@ describe('request', () => { } as SageHttpResponse); }); }); + describe('application/json', () => { it('should properly call sage assistant and respond in expected format', async () => { const res = await request(expressApp) @@ -484,6 +485,7 @@ describe('request', () => { }); }); }); + describe('redirects', () => { it('should properly operate with redirects', async () => { const res = await request(expressApp).get('/redirect'); @@ -512,6 +514,7 @@ describe('request', () => { }); }); }); + describe('header', () => { it('should set and pass a single header', async () => { const res = await request(expressApp) @@ -575,7 +578,24 @@ describe('request', () => { } } as SageHttpResponse); }); + + it('should accept objects', async () => { + const res = await request(expressApp).post('/ping-pong').set({ + 'x-custom-header': 'custom-value1', + 'x-custom-header2': 'custom-value2' + }); + + expect(res).toMatchObject({ + body: { + reqHeaders: { + 'x-custom-header': 'custom-value1', + 'x-custom-header2': 'custom-value2' + } + } + } as SageHttpResponse); + }); }); + describe('cookies', () => { it('should parse cookies properly for response', async () => { const res = await request(expressApp).get('/cookie'); @@ -1005,6 +1025,22 @@ describe('request', () => { } } as SageHttpResponse); }); + + it('should accept objects', async () => { + const res = await request(expressApp).post('/ping-pong').set({ + 'x-custom-header': 'custom-value1', + 'x-custom-header2': 'custom-value2' + }); + + expect(res).toMatchObject({ + body: { + reqHeaders: { + 'x-custom-header': 'custom-value1', + 'x-custom-header2': 'custom-value2' + } + } + } as SageHttpResponse); + }); }); describe('cookies', () => {