-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
test.js
48 lines (44 loc) · 1.3 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const { spawn } = require('child_process');
const { request } = require('http')
const { URL } = require('url')
const test = require('tape');
// Start the app
const env = Object.assign({}, process.env, {PORT: 5001});
const child = spawn('node', ['index.js'], {env});
test('responds to requests', (t) => {
t.plan(4);
// Wait until the server is ready
child.stdout.on('data', _ => {
// Make a request to our app
(async () => {
const response = await get('http://127.0.0.1:5001')
// stop the server
child.kill();
// No error
t.false(response.error);
// Successful response
t.equal(response.statusCode, 200);
// Assert content checks
t.notEqual(response.body.indexOf("<title>Node.js Getting Started on Heroku</title>"), -1);
t.notEqual(response.body.indexOf("Getting Started on Heroku with Node.js"), -1);
})();
});
});
async function get(url) {
return new Promise((resolve, reject) => {
const req = request(url, {method: 'GET'}, (res) => {
let body = ''
res.setEncoding('utf-8')
res.on('data', (data) => body += data)
res.on('end', () => {
resolve({
error: false,
statusCode: res.statusCode,
body: body
})
})
})
req.on('error', reject)
req.end()
})
}