-
Notifications
You must be signed in to change notification settings - Fork 1
/
HttpServer.test.js
44 lines (40 loc) · 1.28 KB
/
HttpServer.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
const Test = require('thunk-test')
const HttpServer = require('./HttpServer')
const http = require('http')
const fetch = require('node-fetch')
const assert = require('assert')
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
const test = Test('HttpServer', HttpServer)
.case((request, response) => {
response.writeHead(200, { 'Content-Type': 'text/plain' })
response.write('hello')
response.end()
}, async server => {
await new Promise(resolve => {
server.listen(3000, async () => {
const response = await fetch('http://localhost:3000')
assert.strictEqual(await response.text(), 'hello')
assert.strictEqual(response.headers.get('content-type'), 'text/plain')
server.close(resolve)
})
})
})
.case(async (request, response) => {
await sleep(25)
response.setHeader('Content-Type', 'text/plain')
response.status = 200
response.endGzip('hello')
}, async server => {
await new Promise(resolve => {
server.listen(3000, async () => {
const response = await fetch('http://localhost:3000')
assert.strictEqual(await response.text(), 'hello')
assert.strictEqual(response.headers.get('content-type'), 'text/plain')
server.close(resolve)
})
})
})
if (process.argv[1] == __filename) {
test()
}
module.exports = test