node-mocks-http
Advanced tools
Comparing version 1.4.4 to 1.5.0
@@ -0,3 +1,23 @@ | ||
v 1.5.0 | ||
------- | ||
Documentation changes, a new feature, and better behaviors, including: | ||
* Added `jsonp` method that takes a status code and a payload, see [PR #79][79] | ||
* Now able to attach non-standard properties to the mock request object. [PR #74][74] | ||
* param now takes a default value, see [PR #76][76] | ||
* Emit `end` when redirecting, see [PR #77][77] | ||
* Documentation changes, see [PR #64][64], [PR #75][75], [PR #78][78] | ||
[64]: https://github.com/howardabrams/node-mocks-http/issues/64 | ||
[74]: https://github.com/howardabrams/node-mocks-http/issues/74 | ||
[75]: https://github.com/howardabrams/node-mocks-http/issues/75 | ||
[76]: https://github.com/howardabrams/node-mocks-http/issues/76 | ||
[77]: https://github.com/howardabrams/node-mocks-http/issues/77 | ||
[78]: https://github.com/howardabrams/node-mocks-http/issues/78 | ||
[79]: https://github.com/howardabrams/node-mocks-http/issues/79 | ||
v 1.4.4 | ||
--- | ||
------- | ||
@@ -10,6 +30,6 @@ Bug fix release, including the following: | ||
[67]: https://github.com/howardabrams/node-mocks-http/issues/67 | ||
[68]: https://github.com/howardabrams/node-mocks-http/issues/68 | ||
[70]: https://github.com/howardabrams/node-mocks-http/issues/70 | ||
[73]: https://github.com/howardabrams/node-mocks-http/issues/73 | ||
[67]: https://github.com/howardabrams/node-mocks-http/issues/67 | ||
[68]: https://github.com/howardabrams/node-mocks-http/issues/68 | ||
[70]: https://github.com/howardabrams/node-mocks-http/issues/70 | ||
[73]: https://github.com/howardabrams/node-mocks-http/issues/73 | ||
@@ -16,0 +36,0 @@ v 1.2.0 |
@@ -35,2 +35,6 @@ 'use strict'; | ||
var standardRequestOptions = [ | ||
'method', 'url', 'originalUrl', 'path', 'params', 'session', 'cookies', 'headers', 'body', 'query', 'files' | ||
]; | ||
function convertKeysToLowerCase(map) { | ||
@@ -77,2 +81,9 @@ var newMap = {}; | ||
// attach any other provided objects into the request for more advanced testing | ||
for (var n in options) { | ||
if (standardRequestOptions.indexOf(n) === -1) { | ||
mockRequest[n] = options[n]; | ||
} | ||
} | ||
/** | ||
@@ -155,3 +166,3 @@ * Return request header. | ||
*/ | ||
mockRequest.param = function(parameterName) { | ||
mockRequest.param = function(parameterName, defaultValue) { | ||
if (mockRequest.params[parameterName]) { | ||
@@ -164,3 +175,3 @@ return mockRequest.params[parameterName]; | ||
} | ||
return null; | ||
return defaultValue; | ||
}; | ||
@@ -167,0 +178,0 @@ |
@@ -252,2 +252,40 @@ 'use strict'; | ||
/** | ||
* Function: jsonp | ||
* | ||
* The 'jsonp' function from node's HTTP API that returns JSON | ||
* data to the client. | ||
* | ||
* Parameters: | ||
* | ||
* a - Either a statusCode or string containing JSON payload | ||
* b - Either a statusCode or string containing JSON payload | ||
* | ||
* If not specified, the statusCode defaults to 200. | ||
* Second parameter is optional. | ||
*/ | ||
mockResponse.jsonp = function(a, b) { | ||
mockResponse.setHeader('Content-Type', 'text/javascript'); | ||
if (a) { | ||
if (typeof a === 'number') { | ||
mockResponse.statusCode = a; | ||
} else { | ||
_data += JSON.stringify(a); | ||
} | ||
} | ||
if (b) { | ||
if (typeof b === 'number') { | ||
mockResponse.statusCode = b; | ||
} else { | ||
_data += JSON.stringify(b); | ||
} | ||
} | ||
mockResponse.emit('send'); | ||
mockResponse.emit('end'); | ||
}; | ||
/** | ||
* Set "Content-Type" response header with `type` through `mime.lookup()` | ||
@@ -427,2 +465,3 @@ * when it does not contain "/", or set the Content-Type to `type` otherwise. | ||
mockResponse.emit('end'); | ||
}; | ||
@@ -429,0 +468,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "Mock 'http' objects for testing Express routing functions", | ||
"version": "1.4.4", | ||
"version": "1.5.0", | ||
"homepage": "https://github.com/howardabrams/node-mocks-http", | ||
@@ -8,0 +8,0 @@ "bugs": { |
@@ -123,7 +123,7 @@ [![node-mocks-http logo][nmh-logo]][nmh-url] | ||
var res = httpMocks.createResponse({ | ||
EventEmitter: require('events').EventEmitter | ||
eventEmitter: require('events').EventEmitter | ||
}); | ||
// ... | ||
it('should do something', funciton(done) { | ||
it('should do something', function(done) { | ||
res.on('end', function() { | ||
@@ -130,0 +130,0 @@ assert.equal(...); |
@@ -241,2 +241,11 @@ 'use strict'; | ||
it('should accept and set non-standard options passed to it', function() { | ||
var options = { | ||
mySampleProp: 'la LA LA' | ||
}; | ||
request = mockRequest.createRequest(options); | ||
expect(request.mySampleProp).to.equal('la LA LA'); | ||
}); | ||
}); | ||
@@ -387,2 +396,14 @@ | ||
it('should return defaultValue, when not found in params/body/query', function() { | ||
request = mockRequest.createRequest(); | ||
expect(request.get('key')).to.not.exist; | ||
expect(request.param('key', 'defaultValue')).to.equal('defaultValue'); | ||
}); | ||
it('should return undefined, when not found in params/body/query', function() { | ||
request = mockRequest.createRequest(); | ||
expect(request.get('key')).to.not.exist; | ||
expect(request.param('key')).to.be.undefined; | ||
}); | ||
}); | ||
@@ -389,0 +410,0 @@ |
@@ -47,2 +47,5 @@ 'use strict'; | ||
expect(response).to.have.property('jsonp'); | ||
expect(response.jsonp).to.be.a('function'); | ||
expect(response).to.have.property('contentType'); | ||
@@ -492,2 +495,27 @@ expect(response.contentType).to.be.a('function'); | ||
// TODO: fix in 2.0; method should mimic Express Response.jsonp() | ||
describe('.jsonp()', function() { | ||
var response; | ||
beforeEach(function() { | ||
response = mockResponse.createResponse(); | ||
sinon.spy(response, 'emit'); | ||
}); | ||
afterEach(function() { | ||
response.emit.restore(); | ||
response = null; | ||
}); | ||
it('method should mimic Express Response.jsonp()'); | ||
it('should emit send and end events', function() { | ||
response.jsonp({}); | ||
expect(response.emit).to.have.been.calledTwice; | ||
expect(response.emit).to.have.been.calledWith('send'); | ||
expect(response.emit).to.have.been.calledWith('end'); | ||
}); | ||
}); | ||
// TODO: fix in 2.0; method should mimic Express Response.redirect() | ||
@@ -494,0 +522,0 @@ describe('.redirect()', function() { |
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
121174
2616