-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
test.js
55 lines (48 loc) · 1.59 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
49
50
51
52
53
54
55
const { spawn } = require('child_process')
const { request } = require('http')
const PORT = process.env.PORT || 5006
describe('getting started guide', () => {
let app
beforeEach(async () => {
app = spawn('node', ['index.js'])
app.stdout.on('data', (data) => console.log(data.toString()))
// give the server a short time to start up
return new Promise(resolve => setTimeout(resolve, 500))
})
afterEach(() => {
if (app) {
app.stdout.removeAllListeners()
app.kill('SIGTERM')
}
})
it('should bind to IPv4 and respond to GET /', async () => {
const response = await get(`http://127.0.0.1:${PORT}`)
expect(response.statusCode).toBe(200)
expect(response.body).toMatch("<title>Node.js Getting Started on Heroku</title>")
expect(response.body).toMatch("Getting Started on Heroku with Node.js")
})
it('should bind to IPv6 and respond to GET /', async () => {
const response = await get(`http://[::1]:${PORT}`)
expect(response.statusCode).toBe(200)
expect(response.body).toMatch("<title>Node.js Getting Started on Heroku</title>")
expect(response.body).toMatch("Getting Started on Heroku with Node.js")
})
})
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({
statusCode: res.statusCode,
body: body
})
})
res.on('error', reject)
})
req.on('error', reject)
req.end()
})
}