create-test-server
Advanced tools
Comparing version 0.2.1 to 1.0.0
{ | ||
"name": "create-test-server", | ||
"version": "0.2.1", | ||
"version": "1.0.0", | ||
"description": "Creates a minimal express server for testing", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
160
README.md
@@ -9,4 +9,8 @@ # create-test-server | ||
Inspired by the `createServer()` helper function in the [Got tests](https://github.com/sindresorhus/got/blob/1f1b6ffb6da13f483ef7f6bd92dd33f022e7de47/test/helpers/server.js) | ||
Inspired by the `createServer()` helper function in the [Got tests](https://github.com/sindresorhus/got/blob/1f1b6ffb6da13f483ef7f6bd92dd33f022e7de47/test/helpers/server.js). | ||
A simple interface for creating a preconfigured express instance listening for both HTTP and HTTPS traffic. | ||
Ports are chosen at random for HTTP/HTTPS. A self signed certificate is automatically generated, along with an associated CA certificate for you to validate against. | ||
## Install | ||
@@ -18,4 +22,158 @@ | ||
## Usage | ||
`createTestServer()` has a Promise based API that pairs well with a modern asynchronous test runner such as [AVA](https://github.com/avajs/ava). | ||
You can create a separate server per test: | ||
```js | ||
import test from 'ava'; | ||
import got from 'got'; | ||
import createTestServer from 'create-test-server'; | ||
test(async t => { | ||
const server = await createTestServer(); | ||
console.log(server.url); | ||
// http://localhost:5486 | ||
console.log(server.sslUrl); | ||
// https://localhost:5487 | ||
// This is just an express route | ||
// You could use any express middleware too | ||
server.get('/foo', (req, res) => res.send('bar')); | ||
const response = await got(server.url + '/foo'); | ||
t.is(response.body, 'bar'); | ||
}); | ||
``` | ||
Or share a server across multiple tests: | ||
```js | ||
let server; | ||
test.before(async () => { | ||
server = await createTestServer(); | ||
server.get('/foo', (req, res) => res.send('bar')); | ||
}); | ||
test(async t => { | ||
const response = await got(server.url + '/foo'); | ||
t.is(response.body, 'bar'); | ||
}); | ||
test(async t => { | ||
const response = await got(server.url + '/foo'); | ||
t.is(response.statusCode, 200); | ||
}); | ||
``` | ||
You can also make properly authenticated SSL requests by setting a common name for the server certificate and validating against the provided CA certificate: | ||
```js | ||
test(async t => { | ||
const server = await createTestServer({ certificate: 'foobar.com' }); | ||
server.get('/foo', (req, res) => res.send('bar')); | ||
const response = await got(server.sslUrl + '/foo', { | ||
ca: server.caCert, | ||
headers: { host: 'foobar.com' } | ||
}); | ||
t.is(response.body, 'bar'); | ||
}); | ||
``` | ||
You can still make an SSL connection without messing about with certificates if your client supports unauthorised SSL requests: | ||
```js | ||
test(async t => { | ||
const server = await createTestServer(); | ||
server.get('/foo', (req, res) => res.send('bar')); | ||
const response = await got(server.sslUrl + '/foo', { | ||
rejectUnauthorized: false | ||
}); | ||
t.is(response.body, 'bar'); | ||
}); | ||
``` | ||
You can also easily stop/restart the server: | ||
```js | ||
const server = await createTestServer(); | ||
// server.url and server.sslUrl are listening | ||
await server.close(); | ||
// server.url and server.sslUrl are closed | ||
await server.listen(); | ||
// server.url and server.sslUrl are listening | ||
``` | ||
## API | ||
### createTestServer([options]) | ||
Returns a Promise which resolves to an express instance. | ||
#### options | ||
Type: `object` | ||
##### options.certificate | ||
Type: `string`, `object`<br> | ||
Default: `undefined` | ||
SSL certificate options to be passed to [`createCert()`](https://github.com/lukechilds/create-cert). | ||
### server | ||
express instance resolved from `createTestServer()` | ||
This is just a normal express instance with a few extra properties. | ||
#### server.url | ||
Type: `string` | ||
The url you can reach the HTTP server on. | ||
e.g: `'http://localhost:5486'` | ||
#### server.sslUrl | ||
Type: `string` | ||
The url you can reach the HTTPS server on. | ||
e.g: `'https://localhost:5487'` | ||
#### server.caCert | ||
Type: `string` | ||
The CA certificate to validate the server certificate against. | ||
#### server.listen() | ||
Type: `function` | ||
Returns a Promise that resolves when both the HTTP and HTTPS servers are listening. | ||
Please note, this function doesn't take a port argument, it uses the predetermined port from `server.url` and `server.sslUrl`. Also, you don't need to manually call this after creating a server, it will listen automatically. | ||
#### server.close() | ||
Type: `function` | ||
Returns a Promise that resolves when both the HTTP and HTTPS servers have stopped listening. | ||
## Related | ||
- [`create-cert`](https://github.com/lukechilds/create-cert) - Super simple self signed certificates | ||
## License | ||
MIT © Luke Childs |
@@ -16,16 +16,17 @@ 'use strict'; | ||
.then(results => { | ||
const port = results[0]; | ||
const sslPort = results[1]; | ||
const cert = results[2]; | ||
const app = express(); | ||
app.port = results[0]; | ||
app.sslPort = results[1]; | ||
app.sslCert = results[2]; | ||
app.host = 'localhost'; | ||
app.url = `http://${app.host}:${app.port}`; | ||
app.sslUrl = `https://${app.host}:${app.sslPort}`; | ||
app.caCert = cert.caKeys.cert; | ||
app.url = `http://localhost:${port}`; | ||
app.sslUrl = `https://localhost:${sslPort}`; | ||
const server = http.createServer(app); | ||
const sslServer = https.createServer(app.sslCert.keys, app); | ||
const sslServer = https.createServer(cert.keys, app); | ||
app.listen = () => Promise.all([ | ||
pify(server.listen.bind(server))(app.port), | ||
pify(sslServer.listen.bind(sslServer))(app.sslPort) | ||
pify(server.listen.bind(server))(port), | ||
pify(sslServer.listen.bind(sslServer))(sslPort) | ||
]); | ||
@@ -32,0 +33,0 @@ app.close = () => Promise.all([ |
@@ -16,8 +16,5 @@ import test from 'ava'; | ||
t.is(server.host, 'localhost'); | ||
t.true(typeof server.port === 'number'); | ||
t.true(typeof server.sslPort === 'number'); | ||
t.is(server.url, `http://${server.host}:${server.port}`); | ||
t.is(server.sslUrl, `https://${server.host}:${server.sslPort}`); | ||
t.true(typeof server.sslCert === 'object'); | ||
t.true(typeof server.url === 'string'); | ||
t.true(typeof server.sslUrl === 'string'); | ||
t.true(typeof server.caCert === 'string'); | ||
t.true(typeof server.listen === 'function'); | ||
@@ -34,4 +31,4 @@ t.true(typeof server.close === 'function'); | ||
const response = await got(server.url + '/foo'); | ||
t.is(response.body, 'bar'); | ||
const { body } = await got(server.url + '/foo'); | ||
t.is(body, 'bar'); | ||
}); | ||
@@ -42,7 +39,10 @@ | ||
server.get('/foo', (req, res) => { | ||
res.send('bar'); | ||
}); | ||
t.plan(3); | ||
await got(server.url + '/foo').catch(err => { | ||
t.is(err.statusCode, 404); | ||
}); | ||
const { body } = await got(server.url + '/foo'); | ||
t.is(body, 'bar'); | ||
@@ -57,5 +57,4 @@ await server.close(); | ||
await got(server.url + '/foo').catch(err => { | ||
t.is(err.statusCode, 404); | ||
}); | ||
const { body: bodyRestarted } = await got(server.url + '/foo'); | ||
t.is(bodyRestarted, 'bar'); | ||
}); | ||
@@ -70,4 +69,4 @@ | ||
const response = await got(server.sslUrl + '/foo', { rejectUnauthorized: false }); | ||
t.is(response.body, 'bar'); | ||
const { body } = await got(server.sslUrl + '/foo', { rejectUnauthorized: false }); | ||
t.is(body, 'bar'); | ||
}); | ||
@@ -82,8 +81,8 @@ | ||
const response = await got(server.sslUrl + '/foo', { | ||
const { body } = await got(server.sslUrl + '/foo', { | ||
strictSSL: true, | ||
ca: server.sslCert.caKeys.cert, | ||
ca: server.caCert, | ||
headers: { host: 'foo.bar' } | ||
}); | ||
t.is(response.body, 'bar'); | ||
t.is(body, 'bar'); | ||
}); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11023
0
178
94