in-process-request
Advanced tools
Comparing version 0.0.10 to 0.1.0
/// <reference types="node" /> | ||
import { RequestListener } from 'http'; | ||
import { MockResponse, MockRequestOptions } from './httpMock'; | ||
import { HapiListener } from './hapiListener'; | ||
declare namespace handler { | ||
@@ -8,3 +9,6 @@ type InProcessRequestOptions = MockRequestOptions; | ||
} | ||
declare const handler: (app: RequestListener) => (reqOptions: MockRequestOptions) => Promise<MockResponse>; | ||
declare const handler: { | ||
(app: RequestListener): (reqOptions: MockRequestOptions) => Promise<MockResponse>; | ||
HapiListener: typeof HapiListener; | ||
}; | ||
export = handler; |
"use strict"; | ||
const httpMock_1 = require("./httpMock"); | ||
const hapiListener_1 = require("./hapiListener"); | ||
const handler = (app) => (reqOptions) => { | ||
@@ -11,2 +12,3 @@ return new Promise((resolve) => { | ||
}; | ||
handler.HapiListener = hapiListener_1.HapiListener; | ||
module.exports = handler; |
@@ -100,2 +100,4 @@ "use strict"; | ||
encrypted: opts.ssl ? true : false, | ||
end: () => { }, | ||
destroy: () => { }, | ||
}; | ||
@@ -102,0 +104,0 @@ const body = toBuffer(opts.body); |
{ | ||
"name": "in-process-request", | ||
"version": "0.0.10", | ||
"version": "0.1.0", | ||
"license": "(MIT OR Apache-2.0)", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -11,2 +11,4 @@ # in-process-request | ||
* Express.js v5 | ||
* Apollo Server v2 | ||
* Hapi v19 | ||
* Connect v3 | ||
@@ -19,3 +21,3 @@ * Koa v2 | ||
* v12 | ||
* v13 | ||
* v14 | ||
@@ -82,3 +84,90 @@ ## Usage | ||
### Koa example | ||
```javascript | ||
const inProcessRequest = require('in-process-request') | ||
const Koa = require('koa'); | ||
const serve = require('koa-static'); | ||
const mount = require('koa-mount'); | ||
const Router = require('koa-router'); | ||
const koa = new Koa(); | ||
const router = new Router(); | ||
router.get('/test', ctx => { | ||
ctx.body = "Hello" | ||
}); | ||
koa.use(router.routes()); | ||
// koa.callback() returns the requestListener functions | ||
const myApp = koa.callback(); | ||
const 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('Status Message', response.statusMessage); | ||
console.log('Is UTF8 body?', response.isUTF8); | ||
}) | ||
``` | ||
### Hapi example | ||
In order to get access to the `requestListener` function in Hapi we need to use custom `listener` object. The `in-process-request` module comes with a helper class `HapiListener` that makes it easy. An instance of this class is used as the listener and the `handler` property of that instance provides the request listener function. | ||
```javascript | ||
const inProcessRequest = require('in-process-request') | ||
const Hapi = require('@hapi/hapi'); | ||
// create custom listener for Hapi | ||
const myListener = new inProcessRequest.HapiListener() | ||
// Pass the custom listener to Hapi.server | ||
const server = Hapi.server({ | ||
listener: myListener | ||
}); | ||
server.route({ | ||
method: 'GET', | ||
path: '/', | ||
handler: (_request: any, _h: any) => { | ||
return 'Hello World!'; | ||
} | ||
}); | ||
const waitForServer = async () => { | ||
//wait for the server to initialize | ||
await server.start() | ||
// return the request listener function | ||
return myListener.handler | ||
} | ||
waitForServer().then((myApp) => { | ||
// The server is initialized and we have access to the request handler - myApp | ||
const 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('Status Message', response.statusMessage); | ||
console.log('Is UTF8 body?', response.isUTF8); | ||
}) | ||
}) | ||
``` | ||
## Usage in integration tests | ||
@@ -85,0 +174,0 @@ |
17068
11
232
262