-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHttp.test.js
86 lines (85 loc) · 3.2 KB
/
Http.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const assert = require('assert')
const Test = require('thunk-test')
const HttpServer = require('./HttpServer')
const Http = require('./Http')
module.exports = Test('Http', Http)
.before(function () {
this.httpServer = HttpServer((request, response) => {
response.writeHead(200, { 'Content-Type': 'application/json' })
switch (request.method) {
case 'GET': case 'OPTIONS': case 'TRACE': case 'HEAD': {
response.write(JSON.stringify({ greeting: 'Hello World' }))
response.end()
break
} case 'POST': {
request.pipe(response)
break
} case 'PUT': {
request.pipe(response)
break
} case 'PATCH': {
request.pipe(response)
break
} case 'DELETE': {
request.pipe(response)
break
} default: {
throw new Error(`unknown request method ${request.method}`)
}
}
}).listen(3000)
})
.case('http://localhost:3000/', async helloHttp => {
const response = await helloHttp.get('/')
assert(response.headers.get('content-type') == 'application/json')
assert.deepEqual(await response.json(), { greeting: 'Hello World' })
})
.case('http://localhost:3000/', async helloHttp => {
const response = await helloHttp.head('/')
assert.strictEqual(response.status, 200)
})
.case('http://localhost:3000/', async helloHttp => {
const response = await helloHttp.post('/', {
body: JSON.stringify({ a: 1 }),
headers: { 'Content-Type': 'application/json' },
})
assert(response.headers.get('content-type') == 'application/json')
assert.deepEqual(await response.json(), { a: 1 })
})
.case('http://localhost:3000/', async helloHttp => {
const response = await helloHttp.put('/', {
body: JSON.stringify({ a: 1 }),
headers: { 'Content-Type': 'application/json' },
})
assert(response.headers.get('content-type') == 'application/json')
assert.deepEqual(await response.json(), { a: 1 })
})
.case('http://localhost:3000/', async helloHttp => {
const response = await helloHttp.delete('/', {
body: JSON.stringify({ a: 1 }),
headers: { 'Content-Type': 'application/json' },
})
assert(response.headers.get('content-type') == 'application/json')
assert.deepEqual(await response.json(), { a: 1 })
})
.case('http://localhost:3000/', async helloHttp => {
const response = await helloHttp.options('/')
assert(response.headers.get('content-type') == 'application/json')
assert.deepEqual(await response.json(), { greeting: 'Hello World' })
})
.case('http://localhost:3000/', async helloHttp => {
const response = await helloHttp.trace('/')
assert(response.headers.get('content-type') == 'application/json')
assert.deepEqual(await response.json(), { greeting: 'Hello World' })
})
.case('http://localhost:3000/', async helloHttp => {
const response = await helloHttp.patch('/', {
body: JSON.stringify({ a: 1 }),
headers: { 'Content-Type': 'application/json' },
})
assert(response.headers.get('content-type') == 'application/json')
assert.deepEqual(await response.json(), { a: 1 })
})
.after(function () {
this.httpServer.close()
})