node-mocks-http
Advanced tools
Comparing version 1.5.0 to 1.5.1
@@ -0,1 +1,6 @@ | ||
v 1.5.1 | ||
------- | ||
* Add support for the `.vary()` response method | ||
v 1.5.0 | ||
@@ -2,0 +7,0 @@ ------- |
@@ -34,2 +34,3 @@ 'use strict'; | ||
var typeis = require('type-is'); | ||
var events = require('events'); | ||
@@ -56,3 +57,3 @@ var standardRequestOptions = [ | ||
var mockRequest = {}; | ||
var mockRequest = Object.create(events.EventEmitter.prototype); | ||
@@ -59,0 +60,0 @@ mockRequest.method = (options.method) ? options.method : 'GET'; |
@@ -359,2 +359,33 @@ 'use strict'; | ||
/** | ||
* Function: vary | ||
* | ||
* Adds the field/s to the Vary response header | ||
* | ||
* Examples: | ||
* | ||
* res.vary('A-B-Test'); | ||
* res.vary(['A-B-Test', 'Known-User']); | ||
*/ | ||
mockResponse.vary = function(fields) { | ||
var header = mockResponse.getHeader('Vary') || ''; | ||
var values = header.length ? header.split(', ') : []; | ||
fields = Array.isArray(fields) ? fields : [ fields ]; | ||
fields = fields.filter(function(field) { | ||
var regex = new RegExp(field, 'i'); | ||
var matches = values.filter(function(value) { | ||
return value.match(regex); | ||
}); | ||
return !matches.length; | ||
}); | ||
values = values.concat(fields); | ||
return mockResponse.setHeader('Vary', values.join(', ')); | ||
}; | ||
/** | ||
@@ -361,0 +392,0 @@ * Set header `field` to `val`, or pass |
@@ -5,3 +5,3 @@ { | ||
"description": "Mock 'http' objects for testing Express routing functions", | ||
"version": "1.5.0", | ||
"version": "1.5.1", | ||
"homepage": "https://github.com/howardabrams/node-mocks-http", | ||
@@ -8,0 +8,0 @@ "bugs": { |
@@ -25,2 +25,8 @@ 'use strict'; | ||
it('should have event emitter prototype functions', function () { | ||
expect(request.on).to.be.a('function'); | ||
expect(request.once).to.be.a('function'); | ||
expect(request.emit).to.be.a('function'); | ||
}); | ||
it('should return an object', function() { | ||
@@ -27,0 +33,0 @@ expect(request).to.be.an('object'); |
@@ -300,2 +300,35 @@ 'use strict'; | ||
describe('.vary()', function() { | ||
var response; | ||
beforeEach(function() { | ||
response = mockResponse.createResponse(); | ||
sinon.spy(response, 'setHeader'); | ||
}); | ||
afterEach(function() { | ||
response.setHeader.restore(); | ||
response = null; | ||
}); | ||
it('should set vary header, when called with a single field', function() { | ||
response.vary('value1'); | ||
expect(response.setHeader).to.have.been.calledWith('Vary', 'value1'); | ||
expect(response.get('Vary')).to.equal('value1'); | ||
}); | ||
it('should set vary header, when called with a an array of fields', function() { | ||
response.vary([ 'value1', 'value2' ]); | ||
expect(response.setHeader).to.have.been.calledWith('Vary', 'value1, value2'); | ||
expect(response.get('Vary')).to.equal('value1, value2'); | ||
}); | ||
it('should not duplicate vary header values (regardless of case)', function() { | ||
response.vary([ 'value1', 'value2' ]); | ||
response.vary([ 'Value1', 'VALUE2', 'value3' ]); | ||
expect(response.get('Vary')).to.equal('value1, value2, value3'); | ||
}); | ||
}); | ||
describe('.set()/.header()', function() { | ||
@@ -302,0 +335,0 @@ var response; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
123497
2672