@podium/test-utils
Version:
Misc common test utils for Podium
107 lines (70 loc) • 2.4 kB
Markdown
A dummy https server. The server is signed with a temporary, in memory, self signed certificate.
When requesting a server signed with a self signed certificate from node.js do set `rejectUnauthorized` to `false` when doing the request to ignore the self signed certificate warning.
```bash
$ npm install @podium/test-utils --save-dev
```
Start a https server at any random available http port
```js
const { HttpsServer } = require('@podium/test-utils');
const server = new HttpsServer();
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.
await server.close();
```
Create a new https server.
```js
const server = new HttpsServer();
```
The https server instance has the following API:
Starts the https server listening on a random available port. Returns a
`promise` which will resolve with the address of the running instance.
```js
const server = new HttpsServer();
const service = await server.listen();
console.log(service) // addresses to server etc.
```
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.
Helpe method to request any path on the server instance. Returns a `promise`
which will resolve with the data of the request.
Example:
```js
const server = new HttpsServer();
server.request = (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end(JSON.stringify({ data: 'hello world' }));
};
await server.listen();
const data = await server.get({ pathname: '/foo' });
console.log(data);
await server.close();
```
The method takes the following options:
* `pathname` - `String` - A pathname on the server to request. Defaults to `/`.
A `setter` for a function to be run on each request. Function will be called
with `request` and `response` object.
```js
const server = new HttpsServer();
server.request = (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ status: 'ok' }));
}
```