Skip to content

Commit

Permalink
New response methods for headers retrieval (#222)
Browse files Browse the repository at this point in the history
* Implement response.getHeaderNames() method

* Implement response.hasHeader() method

* Fix hungry bugs in test names
  • Loading branch information
sergioregueira authored Aug 21, 2020
1 parent edfdebd commit fc7a86d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lib/mockResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,24 @@ function createResponse(options) {
return mockResponse._headers[name.toLowerCase()];
};

/**
* Function: getHeaderNames
*
* Returns an array containing the unique names of the current outgoing headers.
*/
mockResponse.getHeaderNames = function() {
return Object.keys(mockResponse._headers); // names are already stored in lowercase
};

/**
* Function: hasHeader
*
* Returns `true` if the header identified by `name` is currently set.
*/
mockResponse.hasHeader = function(name) {
return name.toLowerCase() in mockResponse._headers;
};

/**
* Function: setHeader
* Function: set
Expand Down
53 changes: 51 additions & 2 deletions test/lib/mockResponse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,29 @@ describe('mockResponse', function () {

});

describe('.getHeaderNames()', function () {
var response;

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

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

it('should return an empty array when no headers were set', function () {
expect(response.getHeaderNames()).to.deep.equal([]);
});

it('should return names of headers previously set', function () {
response.setHeader('name1', 'value1');
response.setHeader('name2', 'value2');

expect(response.getHeaderNames()).to.deep.equal(['name1', 'name2']);
});
});

describe('.getHeaders()', function () {
var response;

Expand All @@ -998,11 +1021,11 @@ describe('mockResponse', function () {
response = null;
});

it('should return an empty object when no cookies were set', function () {
it('should return an empty object when no headers were set', function () {
expect(response.getHeaders()).to.deep.equal({});
});

it('should return cookies previously set', function () {
it('should return headers previously set', function () {
response.setHeader('name1', 'value1');
response.setHeader('name2', 'value2');

Expand All @@ -1023,6 +1046,32 @@ describe('mockResponse', function () {
});
});

describe('.hasHeader()', function () {
var response;

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

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

it('should return true if the header was set', function () {
response.setHeader('name1');
expect(response.hasHeader('name1')).to.be.true;
});

it('should return false if the header is missing', function () {
expect(response.hasHeader('name1')).to.be.false;
});

it('should be case-insensitive', function () {
response.setHeader('name1');
expect(response.hasHeader('NAME1')).to.be.true;
});
});

describe('.removeHeader()', function () {
var response;

Expand Down

0 comments on commit fc7a86d

Please sign in to comment.