@podium/test-utils
Version:
Misc common test utils for Podium
86 lines (56 loc) • 1.7 kB
Markdown
A dummy http server.
```bash
$ npm install @podium/test-utils --save-dev
```
Start a http server at any random available http port
```js
const { HttpServer } = require('@podium/test-utils');
const server = new HttpServer();
server.request = (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('hello world');
};
const service = await server.listen();
console.log(service) // addresses to server etc.
await server.close();
```
Create a new http server.
```js
const server = new HttpServer();
```
The http server instance has the following API:
Starts the http server listening on a random available port. Returns a
`promise` which will resolve with the address of the running instance.
```js
const server = new HttpServer();
const service = await server.listen();
console.log(service) // addresses to server etc.
```
Takes a `host` to start the instance listening on a given hostname and port.
```js
const server = new HttpServer();
const service = await server.listen('http://localhost:8080');
```
Closes the running instance. Returns a `promise` which will resolve when all
open connections to the instance is closed and the server is properly closed.
A `setter` for a function to be run on each request. Function will be called
with `request` and `response` object.
```js
const server = new HttpServer();
server.request = (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ status: 'ok' }));
}
```