-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathhttp.spec.js
189 lines (154 loc) · 5.67 KB
/
http.spec.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'use strict'
/* eslint-env mocha */
const { expect } = require('aegir/utils/chai')
const HTTP = require('../src/http')
const toStream = require('it-to-stream')
const delay = require('delay')
const AbortController = require('../src/abort-controller')
const drain = require('it-drain')
const all = require('it-all')
const { isBrowser, isWebWorker } = require('../src/env')
const { Buffer } = require('buffer')
const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayEquals = require('uint8arrays/equals')
describe('http', function () {
it('makes a GET request', async function () {
const req = await HTTP.get(`${process.env.ECHO_SERVER}/echo/query?test=one`)
const rsp = await req.json()
expect(rsp).to.be.deep.eq({ test: 'one' })
})
it('makes a GET request with redirect', async function () {
const req = await HTTP.get(`${process.env.ECHO_SERVER}/redirect?to=${encodeURI(`${process.env.ECHO_SERVER}/echo/query?test=one`)}`)
const rsp = await req.json()
expect(rsp).to.be.deep.eq({ test: 'one' })
})
it('makes a GET request with a really short timeout', function () {
return expect(HTTP.get(`${process.env.ECHO_SERVER}/redirect?to=${encodeURI(`${process.env.ECHO_SERVER}/echo/query?test=one`)}`, {
timeout: 1
})).to.eventually.be.rejectedWith().instanceOf(HTTP.TimeoutError)
})
it('respects headers', async function () {
const req = await HTTP.post(`${process.env.ECHO_SERVER}/echo/headers`, {
headers: {
foo: 'bar'
}
})
const rsp = await req.json()
expect(rsp).to.have.property('foo', 'bar')
})
it('respects constructor headers', async function () {
const http = new HTTP({
headers: {
bar: 'baz'
}
})
const req = await http.post(`${process.env.ECHO_SERVER}/echo/headers`)
const rsp = await req.json()
expect(rsp).to.have.property('bar', 'baz')
})
it('makes a JSON request', async () => {
const req = await HTTP.post(`${process.env.ECHO_SERVER}/echo`, {
json: {
test: 2
}
})
const out = await req.text()
expect(out).to.be.eq('{"test":2}')
})
it('makes a DELETE request', async () => {
const req = await HTTP.delete(`${process.env.ECHO_SERVER}/echo`, {
json: {
test: 2
}
})
const out = await req.text()
expect(out).to.be.eq('{"test":2}')
})
it('allow async aborting', async function () {
const controller = new AbortController()
const res = HTTP.get(process.env.ECHO_SERVER, {
signal: controller.signal
})
controller.abort()
await expect(res).to.eventually.be.rejectedWith(/aborted/)
})
it('parses the response as ndjson', async function () {
const res = await HTTP.post(`${process.env.ECHO_SERVER}/echo`, {
body: '{}\n{}'
})
const entities = await all(res.ndjson())
expect(entities).to.deep.equal([{}, {}])
})
it('parses the response as an async iterable', async function () {
const res = await HTTP.post('echo', {
base: process.env.ECHO_SERVER,
body: 'hello world'
})
const entities = await all(res.iterator())
expect(entities).to.deep.equal([Buffer.from('hello world')])
})
it.skip('should handle errors in streaming bodies', async function () {
if (isBrowser || isWebWorker) {
// streaming bodies not supported by browsers
return this.skip()
}
const err = new Error('Should be caught')
const body = (async function * () {
yield Buffer.from('{}\n')
await delay(100)
throw err
}())
const res = await HTTP.post(process.env.ECHO_SERVER, {
body: toStream.readable(body)
})
await expect(drain(HTTP.ndjson(res.body))).to.eventually.be.rejectedWith(/aborted/)
})
it.skip('should handle errors in streaming bodies when a signal is passed', async function () {
if (isBrowser || isWebWorker) {
// streaming bodies not supported by browsers
return this.skip()
}
const controller = new AbortController()
const err = new Error('Should be caught')
const body = (async function * () {
yield Buffer.from('{}\n')
await delay(100)
throw err
}())
const res = await HTTP.post(process.env.ECHO_SERVER, {
body: toStream.readable(body),
signal: controller.signal
})
await expect(drain(HTTP.ndjson(res.body))).to.eventually.be.rejectedWith(/aborted/)
})
it('progress events', async () => {
let upload = 0
let download = 0
const body = new Uint8Array(1000000 / 2)
const request = await HTTP.post(`${process.env.ECHO_SERVER}/echo`, {
body,
onUploadProgress: (progress) => {
expect(progress).to.have.property('lengthComputable').to.be.a('boolean')
expect(progress).to.have.property('total', body.byteLength)
expect(progress).to.have.property('loaded').to.be.a('number')
upload += 1
},
onDownloadProgress: (progress) => {
expect(progress).to.have.property('lengthComputable').to.be.a('boolean')
expect(progress).to.have.property('total').to.be.a('number')
expect(progress).to.have.property('loaded').to.be.a('number')
download += 1
}
})
await all(request.iterator())
expect(upload).to.be.greaterThan(0)
expect(download).to.be.greaterThan(0)
})
it('makes a GET request with unprintable characters', async function () {
const buf = uint8ArrayFromString('a163666f6f6c6461672d63626f722d626172', 'base16')
const params = Array.from(buf).map(val => `data=${val.toString()}`).join('&')
const req = await HTTP.get(`${process.env.ECHO_SERVER}/download?${params}`)
const rsp = await req.arrayBuffer()
expect(uint8ArrayEquals(new Uint8Array(rsp), buf)).to.be.true()
})
})