node-mocks-http
Advanced tools
Comparing version 1.5.1 to 1.5.2
@@ -166,7 +166,7 @@ 'use strict'; | ||
mockRequest.param = function(parameterName, defaultValue) { | ||
if (mockRequest.params[parameterName]) { | ||
if (mockRequest.params.hasOwnProperty(parameterName)) { | ||
return mockRequest.params[parameterName]; | ||
} else if (mockRequest.body[parameterName]) { | ||
} else if (mockRequest.body.hasOwnProperty(parameterName)) { | ||
return mockRequest.body[parameterName]; | ||
} else if (mockRequest.query[parameterName]) { | ||
} else if (mockRequest.query.hasOwnProperty(parameterName)) { | ||
return mockRequest.query[parameterName]; | ||
@@ -173,0 +173,0 @@ } |
@@ -32,2 +32,3 @@ 'use strict'; | ||
var http = require('./node/http'); | ||
var _assign = require('lodash.assign'); | ||
@@ -64,2 +65,3 @@ function createResponse(options) { | ||
mockResponse.statusCode = 200; | ||
mockResponse.statusMessage = 'OK'; | ||
mockResponse.cookies = {}; | ||
@@ -96,3 +98,3 @@ | ||
*/ | ||
mockResponse.writeHead = function(statusCode, phrase, headers) { | ||
mockResponse.writeHead = function(statusCode, statusMessage, headers) { | ||
@@ -105,8 +107,17 @@ if (_endCalled) { | ||
// Note: Not sure if the headers given in this function | ||
// overwrite any headers specified earlier. | ||
// resolve statusMessage and headers as optional | ||
if (Object.prototype.toString.call(statusMessage) === '[object Object]') { | ||
headers = statusMessage; | ||
statusMessage = null; | ||
} | ||
if (statusMessage) { | ||
mockResponse.statusMessage = statusMessage; | ||
} | ||
// The headers specified earlier (been set with `mockResponse.setHeader`) | ||
// should not be overwritten but be merged with the headers | ||
// passed into `mockResponse.writeHead`. | ||
if (headers) { | ||
mockResponse._headers = headers; | ||
} else { | ||
mockResponse._headers = phrase; | ||
mockResponse._headers = _assign(mockResponse._headers, headers); | ||
} | ||
@@ -434,3 +445,5 @@ | ||
mockResponse.get = mockResponse.getHeader = function(name) { | ||
return mockResponse._headers[name]; | ||
return mockResponse._headers[name] || | ||
mockResponse._headers[name.toLowerCase()] || | ||
mockResponse._headers[name.toUpperCase()]; | ||
}; | ||
@@ -620,2 +633,11 @@ | ||
/** | ||
* Function: _getStatusMessage | ||
* | ||
* The status message that was sent to the user. | ||
*/ | ||
mockResponse._getStatusMessage = function() { | ||
return mockResponse.statusMessage; | ||
}; | ||
/** | ||
* Function: _isJSON | ||
@@ -627,3 +649,3 @@ * | ||
mockResponse._isJSON = function() { | ||
return (mockResponse._headers['Content-Type'] === 'application/json'); | ||
return (mockResponse.getHeader('Content-Type') === 'application/json'); | ||
}; | ||
@@ -665,4 +687,4 @@ | ||
if (mockResponse._headers['Content-Length']) { | ||
return (mockResponse._headers['Content-Length'].toString() === _data.length.toString()); | ||
if (mockResponse.getHeader('Content-Length')) { | ||
return (mockResponse.getHeader('Content-Length').toString() === _data.length.toString()); | ||
} | ||
@@ -669,0 +691,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "Mock 'http' objects for testing Express routing functions", | ||
"version": "1.5.1", | ||
"version": "1.5.2", | ||
"homepage": "https://github.com/howardabrams/node-mocks-http", | ||
@@ -44,2 +44,3 @@ "bugs": { | ||
"dependencies": { | ||
"lodash.assign": "^4.0.6", | ||
"mime": "^1.3.4", | ||
@@ -46,0 +47,0 @@ "type-is": "^1.6.4" |
@@ -373,2 +373,13 @@ 'use strict'; | ||
it('should return falsy param, when found in params', function() { | ||
var options = { | ||
params: { | ||
key: 0 | ||
} | ||
}; | ||
request = mockRequest.createRequest(options); | ||
expect(request.param('key')).to.equal(0); | ||
}); | ||
it('should return param, when found in body', function() { | ||
@@ -385,2 +396,13 @@ var options = { | ||
it('should return falsy param, when found in body', function() { | ||
var options = { | ||
body: { | ||
key: 0 | ||
} | ||
}; | ||
request = mockRequest.createRequest(options); | ||
expect(request.param('key')).to.equal(0); | ||
}); | ||
it('should return param, when found in query', function() { | ||
@@ -397,2 +419,13 @@ var options = { | ||
it('should return falsy param, when found in query', function() { | ||
var options = { | ||
query: { | ||
key: 0 | ||
} | ||
}; | ||
request = mockRequest.createRequest(options); | ||
expect(request.param('key')).to.equal(0); | ||
}); | ||
it('should not return param, when not found in params/body/query', function() { | ||
@@ -399,0 +432,0 @@ request = mockRequest.createRequest(); |
@@ -99,3 +99,2 @@ 'use strict'; | ||
it('should expose Node WritableStream methods', function() { | ||
expect(response).to.have.property('destroy'); | ||
@@ -566,5 +565,72 @@ expect(response.destroy).to.be.a('function'); | ||
describe('.writeHead()', function() { | ||
var response; | ||
beforeEach(function() { | ||
response = mockResponse.createResponse(); | ||
expect(response.statusCode).to.equal(200); | ||
expect(response.statusMessage).to.equal('OK'); | ||
expect(response._getHeaders()).to.be.empty; | ||
}); | ||
afterEach(function() { | ||
response = null; | ||
}); | ||
it('should inherit from ServerResponse.writeHead()'); | ||
it('writes the statusCode of the response', function() { | ||
response.writeHead(400); | ||
expect(response.statusCode).to.equal(400); | ||
}); | ||
it('writes the statusMessage of the response', function() { | ||
response.writeHead(400, 'NotOK'); | ||
expect(response.statusMessage).to.equal('NotOK'); | ||
}); | ||
it('writes the headers of the response', function() { | ||
var headers = { 'x-header': 'test llama' }; | ||
response.writeHead(400, headers); | ||
expect(response._getHeaders()).to.deep.equal(headers); | ||
}); | ||
it('works with statusMessage and headers as optional', function() { | ||
response.writeHead(400); | ||
expect(response.statusCode).to.equal(400); | ||
expect(response.statusMessage).to.equal('OK'); | ||
expect(response._getHeaders()).to.be.empty; | ||
}); | ||
it('works with statusMessage as optional', function() { | ||
var headers = { 'x-header': 'test llama' }; | ||
response.writeHead(400, headers); | ||
expect(response.statusMessage).to.equal('OK'); | ||
expect(response._getHeaders()).to.deep.equal(headers); | ||
}); | ||
it('works with headers as optional', function() { | ||
response.writeHead(400, 'NotOK'); | ||
expect(response.statusMessage).to.equal('NotOK'); | ||
expect(response._getHeaders()).to.be.empty; | ||
}); | ||
it('works with statusCode, statusMessage, and headers defined', function() { | ||
var headers = { 'x-header': 'test llama' }; | ||
response.writeHead(400, 'NotOK', headers); | ||
expect(response.statusMessage).to.equal('NotOK'); | ||
expect(response._getHeaders()).to.deep.equal(headers); | ||
}); | ||
it('throws if end has already been called', function() { | ||
response.end(); | ||
expect(response.writeHead.bind(response, 200)).to.throw('The end() method has already been called'); | ||
}); | ||
it('merges the given headers with the ones specified earlier (set with `setHeader`)', function() { | ||
response.setHeader('Access-Control-Allow-Origin', '*'); | ||
response.writeHead(200, {'Access-Control-Max-Age': '86400'}); | ||
expect(response._getHeaders()).to.contain.all.keys({'Access-Control-Allow-Origin': '*', 'Access-Control-Max-Age': '86400'}); | ||
}); | ||
}); | ||
@@ -619,2 +685,10 @@ | ||
it('should get header regardless of case, when called existing header', function() { | ||
response.set('NAME1', 'value1'); | ||
expect(response.getHeader('name1')).to.equal('value1'); | ||
response.header('name2', 'value2'); | ||
expect(response.getHeader('NAME2')).to.equal('value2'); | ||
}); | ||
it('should throw and error, when called without arguments', function() { | ||
@@ -813,2 +887,22 @@ expect(response.getHeader).to.throw; | ||
describe('._getStatusMessage()', function() { | ||
it('will be deprecated in 2.0'); | ||
it('should return the default status message, when not set', function() { | ||
expect(response._getStatusMessage()).to.equal('OK'); | ||
}); | ||
it('should return set status message', function() { | ||
response.statusMessage = 'NotOK'; | ||
expect(response._getStatusMessage()).to.equal('NotOK'); | ||
}); | ||
it('should return status message set by .writeHead()', function() { | ||
response.writeHead(400, 'NotOK'); | ||
expect(response._getStatusMessage()).to.equal('NotOK'); | ||
}); | ||
}); | ||
describe('._isJSON()', function() { | ||
@@ -815,0 +909,0 @@ |
128647
2793
3
+ Addedlodash.assign@^4.0.6
+ Addedlodash.assign@4.2.0(transitive)