node-mocks-http
Advanced tools
Comparing version 1.6.7 to 1.7.0
@@ -0,7 +1,33 @@ | ||
v 1.7.0 | ||
------- | ||
- Add support for Buffer payload [#154][154] | ||
- Send request body/payload to trigger relevant events [#164][164] | ||
[154]: https://github.com/howardabrams/node-mocks-http/pull/154 | ||
[164]: https://github.com/howardabrams/node-mocks-http/pull/164 | ||
v 1.6.8 | ||
------- | ||
- Better typings, including the following (see [PR #158][158] for details): | ||
- request object for a controller fn which is typed as extension of `express.Request` | ||
- same for `Response` | ||
- custom properties appended to request object | ||
- fixed missing `_getRenderView` method on `Response` | ||
**Note:** As of this release, we are officially supporting: | ||
- 6.13 | ||
- 8.9 | ||
- 9.6 | ||
[158]: https://github.com/howardabrams/node-mocks-http/pull/158 | ||
v 1.6.7 | ||
------- | ||
- Set an expiration date to a cookie when deleting it [#155][115] | ||
- Set an expiration date to a cookie when deleting it [#155][155] | ||
- No `finish` event, `end` event called when it shouldn't be. [#112][112] | ||
- Add support for [append][] on MockResponse [#143][143] | ||
- Add [locals][] object to response [#135][135] | ||
@@ -12,5 +38,7 @@ Special shoutout to [Eugene Fidelin](https://github.com/eugef) for | ||
[112]: https://github.com/howardabrams/node-mocks-http/issues/112 | ||
[135]: https://github.com/howardabrams/node-mocks-http/issues/135 | ||
[143]: https://github.com/howardabrams/node-mocks-http/issues/143 | ||
[155]: https://github.com/howardabrams/node-mocks-http/issues/155 | ||
[append]: http://expressjs.com/en/api.html#res.append | ||
[locals]: https://expressjs.com/en/api.html#res.locals | ||
@@ -17,0 +45,0 @@ v 1.6.6 |
@@ -1,2 +0,2 @@ | ||
import { Request, Response } from 'express'; | ||
import { Request, Response, CookieOptions } from 'express'; | ||
@@ -50,5 +50,8 @@ declare module 'node-mocks-http' { | ||
files?: Files; | ||
// Support custom properties appended on Request objects. | ||
[key: string]: any; | ||
} | ||
export interface MockRequest extends Request { | ||
export type MockRequest<T extends Request> = T & { | ||
_setParameter: (key: string, value: string) => void; | ||
@@ -65,2 +68,5 @@ _setSessionVariable: (variable: string, value: string) => void; | ||
_addBody: (key: string, value: any) => void; | ||
// Support custom properties appended on Request objects. | ||
[key: string]: any; | ||
} | ||
@@ -74,3 +80,8 @@ | ||
export interface MockResponse extends Response { | ||
export type ResponseCookie = { | ||
value: any; | ||
options: CookieOptions; | ||
} | ||
export type MockResponse<T extends Response> = T & { | ||
_isEndCalled: () => boolean; | ||
@@ -86,14 +97,17 @@ _getHeaders: () => Headers; | ||
_getRenderData: () => any; | ||
_getRenderView: () => string; | ||
cookies: {[name: string]: ResponseCookie}; | ||
} | ||
export function createRequest(options?: RequestOptions): MockRequest; | ||
export function createRequest<T extends Request = Request>(options?: RequestOptions): MockRequest<T>; | ||
export function createResponse(options?: ResponseOptions): MockResponse; | ||
export function createResponse<T extends Response = Response>(options?: ResponseOptions): MockResponse<T>; | ||
export interface Mocks { | ||
req: MockRequest; | ||
res: MockResponse; | ||
export interface Mocks<T1 extends Request, T2 extends Response> { | ||
req: MockRequest<T1>; | ||
res: MockResponse<T2>; | ||
} | ||
export function createMocks(reqOptions?: RequestOptions, resOptions?: ResponseOptions): Mocks; | ||
export function createMocks<T1 extends Request = Request, T2 extends Response = Response>(reqOptions?: RequestOptions, resOptions?: ResponseOptions): Mocks<T1, T2>; | ||
} |
@@ -399,2 +399,24 @@ 'use strict'; | ||
/** | ||
* Function: send | ||
* | ||
* Write data to the request stream which will trigger request's 'data', and 'end' event | ||
* | ||
* Parameters: | ||
* | ||
* data - string, array, object, number, buffer | ||
*/ | ||
mockRequest.send = function(data) { | ||
if(Buffer.isBuffer(data)){ | ||
this.emit('data', data); | ||
}else if(typeof data === 'object' || typeof data === 'number'){ | ||
this.emit('data', Buffer.from(JSON.stringify(data))); | ||
}else if(typeof data === 'string'){ | ||
this.emit('data', Buffer.from(data)); | ||
} | ||
this.emit('end'); | ||
}; | ||
return mockRequest; | ||
@@ -401,0 +423,0 @@ } |
@@ -41,2 +41,5 @@ 'use strict'; | ||
var _data = ''; | ||
var _buffer = new Buffer(0); | ||
var _chunks = []; | ||
var _size = 0; | ||
var _encoding = options.encoding; | ||
@@ -365,3 +368,8 @@ | ||
_data += data; | ||
if (data instanceof Buffer) { | ||
_chunks.push(data); | ||
_size += data.length; | ||
} else { | ||
_data += data; | ||
} | ||
@@ -398,5 +406,26 @@ if (encoding) { | ||
if (data) { | ||
_data += data; | ||
if (data instanceof Buffer) { | ||
_chunks.push(data); | ||
_size += data.length; | ||
} else { | ||
_data += data; | ||
} | ||
} | ||
if (_chunks.length) { | ||
switch (_chunks.length) { | ||
case 1: | ||
_buffer = _chunks[0]; | ||
break; | ||
default: | ||
_buffer = new Buffer(_size); | ||
for (var i = 0, pos = 0, l = _chunks.length; i < l; i++) { | ||
var chunk = _chunks[i]; | ||
chunk.copy(_buffer, pos); | ||
pos += chunk.length; | ||
} | ||
break; | ||
} | ||
} | ||
if (encoding) { | ||
@@ -550,2 +579,6 @@ _encoding = encoding; | ||
mockResponse.getEncoding = function() { | ||
return _encoding; | ||
}; | ||
/** | ||
@@ -685,2 +718,23 @@ * Function: redirect | ||
/** | ||
* Function: _getBuffer | ||
* | ||
* The buffer containing data to be sent to the user. | ||
* Non-empty if Buffers were given in calls to write() and end() | ||
*/ | ||
mockResponse._getBuffer = function() { | ||
return _buffer; | ||
}; | ||
/** | ||
* Function: _getChunks | ||
* | ||
* The buffer containing data to be sent to the user. | ||
* Non-empty if Buffers were given in calls to write() and end() | ||
*/ | ||
mockResponse._getChunks = function() { | ||
return _chunks; | ||
}; | ||
/** | ||
* Function: _getStatusCode | ||
@@ -687,0 +741,0 @@ * |
@@ -5,3 +5,3 @@ { | ||
"description": "Mock 'http' objects for testing Express routing functions", | ||
"version": "1.6.7", | ||
"version": "1.7.0", | ||
"homepage": "https://github.com/howardabrams/node-mocks-http", | ||
@@ -8,0 +8,0 @@ "bugs": { |
@@ -139,2 +139,38 @@ [![node-mocks-http logo][nmh-logo]][nmh-url] | ||
> This is an example to send request body and trigger it's 'data' and 'end' events: | ||
```js | ||
var httpMocks = require('node-mocks-http'); | ||
var req = httpMocks.createRequest(); | ||
var res = httpMocks.createResponse({ | ||
eventEmitter: require('events').EventEmitter | ||
}); | ||
// ... | ||
it('should do something', function(done) { | ||
res.on('end', function() { | ||
expect(response._getData()).to.equal('data sent in request'); | ||
done(); | ||
}); | ||
route(req,res); | ||
req.send('data sent in request'); | ||
}); | ||
function route(req,res){ | ||
var data= []; | ||
req.on("data", chunk => { | ||
data.push(chunk) | ||
}); | ||
req.on("end", () => { | ||
data = Buffer.concat(data) | ||
res.write(data); | ||
res.end(); | ||
}); | ||
} | ||
// ... | ||
``` | ||
### .createMocks() | ||
@@ -141,0 +177,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
68687
1664
236
3