Skip to content

Commit

Permalink
feat: support passing headers as an object
Browse files Browse the repository at this point in the history
  • Loading branch information
eddienubes committed Aug 24, 2024
1 parent bda97e8 commit 075998b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Sage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -123,13 +124,26 @@ export class Sage<T> {
* 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
Expand Down
36 changes: 36 additions & 0 deletions test/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -484,6 +485,7 @@ describe('request', () => {
});
});
});

describe('redirects', () => {
it('should properly operate with redirects', async () => {
const res = await request(expressApp).get('/redirect');
Expand Down Expand Up @@ -512,6 +514,7 @@ describe('request', () => {
});
});
});

describe('header', () => {
it('should set and pass a single header', async () => {
const res = await request(expressApp)
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 075998b

Please sign in to comment.