in-process-request
Advanced tools
Comparing version 0.0.1-beta.6 to 0.0.1
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const getHeadersLegacy = (res) => { | ||
// In node 6 the headers were stored in `this._headers` | ||
// In node 6 the headers are stored in `this._headers` | ||
return res._headers || {}; | ||
@@ -6,0 +6,0 @@ }; |
@@ -7,3 +7,3 @@ "use strict"; | ||
const res = httpMock_1.createMockResponse(req); | ||
res.on('finish', resolve); | ||
res.on('_response', resolve); | ||
app(req, res); | ||
@@ -10,0 +10,0 @@ }); |
/// <reference types="node" /> | ||
import { IncomingHttpHeaders, OutgoingHttpHeaders, ServerResponse, IncomingMessage } from 'http'; | ||
export interface MockRequestOptions { | ||
method: string; | ||
method?: string; | ||
path: string; | ||
@@ -6,0 +6,0 @@ body?: string | Buffer; |
@@ -15,3 +15,3 @@ "use strict"; | ||
}; | ||
const toBuffer = (param) => { | ||
const toBuffer = (param, encoding) => { | ||
if (Buffer.isBuffer(param)) { | ||
@@ -21,3 +21,3 @@ return param; | ||
else if (typeof param === 'string') { | ||
return Buffer.from(param); | ||
return Buffer.from(param, encoding); | ||
} | ||
@@ -31,3 +31,3 @@ else { | ||
const chunks = []; | ||
const addChunk = (chunk) => chunks.push(toBuffer(chunk)); | ||
const addChunk = (chunk, encoding) => chunks.push(toBuffer(chunk, encoding)); | ||
res.write = (chunk) => { | ||
@@ -37,15 +37,17 @@ addChunk(chunk); | ||
}; | ||
res.end = (chunk) => { | ||
addChunk(chunk); | ||
const responseBody = Buffer.concat(chunks); | ||
const overriddenEnd = (chunk, encoding) => { | ||
addChunk(chunk, encoding); | ||
const body = Buffer.concat(chunks); | ||
const headers = Object.assign({}, getHeaders_1.default(res)); | ||
const response = { | ||
body: responseBody, | ||
body, | ||
isUTF8: !!(headers['content-type'] || '').match(/charset=utf-8/i), | ||
statusCode: res.statusCode, | ||
headers: headers, | ||
headers, | ||
}; | ||
res.emit('prefinish'); | ||
res.emit('finish', response); | ||
res.emit('finish'); | ||
res.emit('_response', response); | ||
}; | ||
res.end = overriddenEnd; | ||
return res; | ||
@@ -57,3 +59,3 @@ }; | ||
const contentLength = Buffer.byteLength(body); | ||
req.method = opts.method.toUpperCase(); | ||
req.method = (opts.method || 'GET').toUpperCase(); | ||
req.url = opts.path; | ||
@@ -60,0 +62,0 @@ req.headers = keysToLowerCase(opts.headers || {}); |
{ | ||
"name": "in-process-request", | ||
"version": "0.0.1-beta.6", | ||
"version": "0.0.1", | ||
"license": "(MIT OR Apache-2.0)", | ||
@@ -5,0 +5,0 @@ "scripts": { |
158
README.md
@@ -8,4 +8,6 @@ # in-process-request | ||
It supports the following frameworks | ||
* Express | ||
* Connect | ||
* Express.js v3 | ||
* Express.js v4 | ||
* Connect v3 | ||
* More to come... | ||
@@ -17,1 +19,153 @@ It has been tested with the following node versions | ||
* v12 | ||
## Usage | ||
```sh | ||
$ npm install in-process-request | ||
``` | ||
```javascript | ||
const inProcessRequest = require('in-process-request'); | ||
const handler = inProcessRequest(app); | ||
handler(requestOptions) | ||
.then((response) => { | ||
console.log(response); | ||
// do something with the response | ||
}); | ||
``` | ||
`requestOptions` is an object with the following properties | ||
* `path` (**mandatory**) - The request path with optional query string, for example `'/my/resource/123?full=true'` | ||
* `method` - request method, the default is `'GET'` | ||
* `body` - request body, `string` or `Buffer` | ||
* `headers` - request headers object, for example: `{'conent-type': 'application/json'}` | ||
* `remoteAddress` - IP address of the client making the request | ||
* `remotePort` - IP port of the client connection | ||
* `ssl` - Set to `true` to pretend that SSL connection is used. Defaults to `false` | ||
`response` is an object with the following properties | ||
* `statusCode` - status code of the response | ||
* `headers` - object with response headers | ||
* `body` - `Buffer` containing the body | ||
* `isUTF8` - set to `true` if the response is a utf-8 string. In that case `response.body.toString()` can be used to extract the utf-8 string | ||
### Express.js example | ||
```javascript | ||
const inProcessRequest = require('in-process-request') | ||
const express = require('express'); | ||
const myApp = express(); | ||
myApp.get('/test', (req, res) => { | ||
res.json({ok: true, a: req.query.a}); | ||
}); | ||
cons myAppHandler = inProcessRequest(myApp); | ||
const requestOptions = { | ||
path: '/test', | ||
method: 'GET', | ||
} | ||
myAppHandler(requestOptions).then(response => { | ||
console.log('Body', response.body); | ||
console.log('Headers', response.headers); | ||
console.log('Status Code', response.statusCode); | ||
console.log('Is UTF8 body?', response.isUTF8); | ||
}) | ||
``` | ||
## Usage in integration tests | ||
### Mocha / Chai | ||
```javascript | ||
const { expect } = require('chai'); | ||
const inProcessRequest = require('in-process-request') | ||
const express = require('express'); | ||
const app = express(); | ||
app.get('/test', (req, res) => { | ||
res.set('X-Hello', 'world'); | ||
res.json({ok: true, a: req.query.a}) | ||
}); | ||
describe('handler', () => { | ||
it('responds with 200, custom header and query param in JSON body', () => { | ||
const reqOptions = { | ||
path: '/test?a=xyz', | ||
method: 'GET', | ||
} | ||
return inProcessRequest(app)(reqOptions).then(response => { | ||
const json = JSON.parse(response.body.toString()) | ||
expect(json).to.deep.equal({ok: true, a: 'xyz'}); | ||
expect(response.headers['x-hello']).to.equal('world'); | ||
expect(response.statusCode).to.equal(200); | ||
expect(response.isUTF8).to.be.true; | ||
}); | ||
}); | ||
it('responds with 200, custom header and query param in JSON body', (done) => { | ||
const reqOptions = { | ||
path: '/test?a=xyz', | ||
method: 'GET', | ||
} | ||
inProcessRequest(app)(reqOptions).then(response => { | ||
const json = JSON.parse(response.body.toString()) | ||
expect(json).to.deep.equal({ok: true, a: 'xyz'}); | ||
expect(response.headers['x-hello']).to.equal('world'); | ||
expect(response.statusCode).to.equal(200); | ||
expect(response.isUTF8).to.be.true; | ||
done(); | ||
}); | ||
}); | ||
}) | ||
``` | ||
### Jest | ||
```javascript | ||
const inProcessRequest = require('in-process-request') | ||
const express = require('express'); | ||
const app = express(); | ||
app.get('/test', (req, res) => { | ||
res.set('X-Hello', 'world'); | ||
res.json({ok: true, a: req.query.a}) | ||
}); | ||
describe('handler', () => { | ||
it('responds with 200, custom header and query param in JSON body', () => { | ||
const reqOptions = { | ||
path: '/test?a=xyz', | ||
method: 'GET', | ||
} | ||
return inProcessRequest(app)(reqOptions).then(response => { | ||
const json = JSON.parse(response.body.toString()) | ||
expect(json).toEqual({ok: true, a: 'xyz'}); | ||
expect(response.headers['x-hello']).toEequal('world'); | ||
expect(response.statusCode).toEequal(200); | ||
expect(response.isUTF8).to.be.true; | ||
}); | ||
}); | ||
it('responds with 200, custom header and query param in JSON body', (done) => { | ||
const reqOptions = { | ||
path: '/test?a=xyz', | ||
method: 'GET', | ||
} | ||
inProcessRequest(app)(reqOptions).then(response => { | ||
const json = JSON.parse(response.body.toString()) | ||
expect(json).toEqual({ok: true, a: 'xyz'}); | ||
expect(response.headers['x-hello']).toEequal('world'); | ||
expect(response.statusCode).toEequal(200); | ||
expect(response.isUTF8).toBeTrue(); | ||
done(); | ||
}); | ||
}); | ||
}) | ||
``` |
9854
138
170