Skip to content

Commit

Permalink
Add appendHeader to MockResponse (#306)
Browse files Browse the repository at this point in the history
* Add the appendHeader function which is part of OutgoingMessage

* Add a test for appendHeader

* Fix function name

---------

Co-authored-by: Eugene Fidelin <eugene.fidelin@gmail.com>
  • Loading branch information
bjarkebm and eugef authored Sep 9, 2024
1 parent 845ca93 commit 3a87b42
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/mockResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,16 @@ function createResponse(options = {}) {
return this;
};

/**
* Function: appendHeader
*
* Append a header by name. If a header already exists, the new value is appended to the existing header.
*/
mockResponse.appendHeader = function appendHeader(name, value) {
mockResponse.append(name, value);
return this;
};

/**
* Function: removeHeader
*
Expand Down
25 changes: 23 additions & 2 deletions test/lib/mockResponse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ describe('mockResponse', () => {
expect(response).to.have.property('setHeader');
expect(response.setHeader).to.be.a('function');

expect(response).to.have.property('appendHeader');
expect(response.appendHeader).to.be.a('function');

expect(response).to.have.property('removeHeader');
expect(response.removeHeader).to.be.a('function');

Expand Down Expand Up @@ -923,8 +926,8 @@ describe('mockResponse', () => {
});
});

// TODO: fix in 2.0; methods should be inherited from Node OutogingMessage
describe('Node OutogingMessage methods', () => {
// TODO: fix in 2.0; methods should be inherited from Node OutgoingMessage
describe('Node OutgoingMessage methods', () => {
describe('.setHeader()', () => {
let response;

Expand All @@ -950,6 +953,24 @@ describe('mockResponse', () => {
});
});

describe('.appendHeader()', () => {
let response;

beforeEach(() => {
response = mockResponse.createResponse();
});

afterEach(() => {
response = null;
});

it('should concatenate header values, when called twice with same name', () => {
response.appendHeader('name', 'value 1');
response.appendHeader('name', 'value 2');
expect(response.getHeader('name')).to.eql(['value 1', 'value 2']);
});
});

describe('.getHeader()', () => {
let response;

Expand Down

0 comments on commit 3a87b42

Please sign in to comment.