|
4 | 4 |
|
5 | 5 | [](https://travis-ci.org/fastify/light-my-request) [](http://standardjs.com/) |
6 | 6 |
|
7 | | -Injects a fake HTTP request/response into a node HTTP server for simulating server logic, writing tests, or debugging. |
8 | | -Does not use a socket connection so can be run against an inactive server (server not in listen mode). |
| 7 | +Injects a fake HTTP request/response into a node HTTP server for simulating server logic, writing tests, or debugging. |
| 8 | +Does not use a socket connection so can be run against an inactive server (server not in listen mode). |
9 | 9 |
|
10 | 10 | ## Example |
11 | 11 |
|
@@ -131,6 +131,26 @@ The declaration file exports types for the following parts of the API: |
131 | 131 | - `Request` - custom light-my-request `request` object interface. Extends Node.js `stream.Readable` type |
132 | 132 | - `Response` - custom light-my-request `response` object interface. Extends Node.js `http.ServerResponse` type |
133 | 133 |
|
| 134 | +### Example with Express |
| 135 | + |
| 136 | +```javascript |
| 137 | +const http = require('http') |
| 138 | +const inject = require('light-my-request') |
| 139 | + |
| 140 | +const dispatch = function (req, res) { |
| 141 | + const reply = 'Hello World' |
| 142 | + res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': reply.length }) |
| 143 | + res.end(reply) |
| 144 | +} |
| 145 | + |
| 146 | +const server = http.createServer(dispatch) |
| 147 | + |
| 148 | +inject(dispatch, { method: 'get', url: '/', express: true }, (err, res) => { |
| 149 | + console.log(res.payload) |
| 150 | +}) |
| 151 | +``` |
| 152 | +Note the `express: true` in the above example. This flag makes sure that we still get the correct behaviour with express framework. |
| 153 | + |
134 | 154 | ## API |
135 | 155 |
|
136 | 156 | #### `inject(dispatchFunc[, options, callback])` |
@@ -159,6 +179,7 @@ Injects a fake request into an HTTP server. |
159 | 179 | - `server` - Optional http server. It is used for binding the `dispatchFunc`. |
160 | 180 | - `autoStart` - Automatically start the request as soon as the method |
161 | 181 | is called. It is only valid when not passing a callback. Defaults to `true`. |
| 182 | + - `express` - A boolean flag to enable working with express framework. By default express doesn't support request injection, this enforces. |
162 | 183 | - `callback` - the callback function using the signature `function (err, res)` where: |
163 | 184 | - `err` - error object |
164 | 185 | - `res` - a response object where: |
@@ -225,8 +246,8 @@ inject(dispatch) |
225 | 246 | Note: The application would not respond multiple times. If you try to invoking any method after the application has responded, the application would throw an error. |
226 | 247 |
|
227 | 248 | ## Acknowledgements |
228 | | -This project has been forked from [`hapi/shot`](https://github.com/hapijs/shot) because we wanted to support *Node ≥ v4* and not only *Node ≥ v8*. |
229 | | -All the credits before the commit [00a2a82](https://github.com/fastify/light-my-request/commit/00a2a82eb773b765003b6085788cc3564cd08326) goes to the `hapi/shot` project [contributors](https://github.com/hapijs/shot/graphs/contributors). |
| 249 | +This project has been forked from [`hapi/shot`](https://github.com/hapijs/shot) because we wanted to support *Node ≥ v4* and not only *Node ≥ v8*. |
| 250 | +All the credits before the commit [00a2a82](https://github.com/fastify/light-my-request/commit/00a2a82eb773b765003b6085788cc3564cd08326) goes to the `hapi/shot` project [contributors](https://github.com/hapijs/shot/graphs/contributors). |
230 | 251 | Since the commit [db8bced](https://github.com/fastify/light-my-request/commit/db8bced10b4367731688c8738621d42f39680efc) the project will be maintained by the Fastify team. |
231 | 252 |
|
232 | 253 | ## License |
|
0 commit comments