-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
110 lines (91 loc) · 2.45 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const test = require('tape')
const fetch = require('./lib/pull-fetch.js')
const { pull, collect, values, map, onEnd } = require('pull-stream')
const { createServer } = require('http')
const P = process.env.TEST_PORT || 8000
test('GET request', t => {
t.plan(4)
const server = createServer((request, response) => {
t.is(request.method, 'GET', 'method')
t.is(request.url, '/foobar', 'url')
t.true(request.headers['x-foobar'], 'request header')
response.setHeader('X-Bazqux', 'Hi')
response.write('Ping')
setTimeout(() => response.end('ping'), 100)
})
server.listen(P, async () => {
const response = await fetch({
host: 'localhost',
method: 'GET',
path: '/foobar',
port: P,
headers: {
'X-Foobar': 'Hello world'
}
})()
pull(
response,
map(x => x + ''), // coerce to strings
collect((err, data) => {
if (err) t.error(err)
else t.same(data, ['Ping', 'ping'], 'response data')
server.close()
})
)
})
})
test('POST request', t => {
t.plan(5)
const server = createServer((request, response) => {
t.is(request.method, 'POST', 'method')
t.is(request.url, '/foobar', 'url')
t.true(request.headers['x-foobar'], 'header')
const data = []
request.on('data', chunk => {
data.push(chunk + '')
})
request.on('end', () => {
t.same(data, ['Ping', 'ping'], 'request data')
response.write('Pong')
setTimeout(() => response.end('pong'), 100)
})
})
server.listen(P, async () => {
const response = await fetch({
host: 'localhost',
method: 'POST',
path: '/foobar',
port: P,
headers: {
'X-Foobar': 'Hello world'
}
})(
values(['Ping', 'ping'])
)
pull(
response,
map(x => x + ''), // coerce to strings
collect((err, data) => {
if (err) t.error(err)
else t.same(data, ['Pong', 'pong'], 'response data')
server.close()
})
)
})
})
test('shorthand', t => {
t.plan(3)
const server = createServer((request, response) => {
t.is(request.method, 'GET', 'method')
t.is(request.url, '/foobar', 'url')
response.write('Ping')
setTimeout(() => response.end('ping'), 100)
})
server.listen(P, async () => {
const response = await fetch(`http://localhost:${P}/foobar`)()
onEnd(err => {
t.error(err)
server.close()
})(response.source)
})
})