forked from request/request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-gzip.js
296 lines (266 loc) Β· 7.99 KB
/
test-gzip.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
'use strict'
var request = require('../index')
var http = require('http')
var zlib = require('zlib')
var assert = require('assert')
var bufferEqual = require('buffer-equal')
var tape = require('tape')
var testContent = 'Compressible response content.\n'
var testContentBig
var testContentBigGzip
var testContentGzip
var server = http.createServer(function (req, res) {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
if (req.method === 'HEAD') {
res.setHeader('Content-Encoding', 'gzip')
res.end()
return
}
if (req.headers.code) {
res.writeHead(req.headers.code, {
'Content-Encoding': 'gzip',
code: req.headers.code
})
res.end()
return
}
if (/\bgzip\b/i.test(req.headers['accept-encoding'])) {
res.setHeader('Content-Encoding', 'gzip')
if (req.url === '/error') {
// send plaintext instead of gzip (should cause an error for the client)
res.end(testContent)
} else if (req.url === '/chunks') {
res.writeHead(200)
res.write(testContentBigGzip.slice(0, 4096))
setTimeout(function () { res.end(testContentBigGzip.slice(4096)) }, 10)
} else if (req.url === '/just-slightly-truncated') {
zlib.gzip(testContent, function (err, data) {
assert.equal(err, null)
// truncate the CRC checksum and size check at the end of the stream
res.end(data.slice(0, data.length - 8))
})
} else {
zlib.gzip(testContent, function (err, data) {
assert.equal(err, null)
res.end(data)
})
}
} else if (/\bdeflate\b/i.test(req.headers['accept-encoding'])) {
res.setHeader('Content-Encoding', 'deflate')
zlib.deflate(testContent, function (err, data) {
assert.equal(err, null)
res.end(data)
})
} else {
res.end(testContent)
}
})
tape('setup', function (t) {
// Need big compressed content to be large enough to chunk into gzip blocks.
// Want it to be deterministic to ensure test is reliable.
// Generate pseudo-random printable ASCII characters using MINSTD
var a = 48271
var m = 0x7FFFFFFF
var x = 1
testContentBig = Buffer.alloc(10240)
for (var i = 0; i < testContentBig.length; ++i) {
x = (a * x) & m
// Printable ASCII range from 32-126, inclusive
testContentBig[i] = (x % 95) + 32
}
zlib.gzip(testContent, function (err, data) {
t.equal(err, null)
testContentGzip = data
zlib.gzip(testContentBig, function (err, data2) {
t.equal(err, null)
testContentBigGzip = data2
server.listen(0, function () {
server.url = 'http://localhost:' + this.address().port
t.end()
})
})
})
})
tape('transparently supports gzip decoding to callbacks', function (t) {
var options = { url: server.url + '/foo', gzip: true }
request.get(options, function (err, res, body) {
t.equal(err, null)
t.equal(res.headers['content-encoding'], 'gzip')
t.equal(body, testContent)
t.end()
})
})
tape('supports slightly invalid gzip content', function (t) {
var options = { url: server.url + '/just-slightly-truncated', gzip: true }
request.get(options, function (err, res, body) {
t.equal(err, null)
t.equal(res.headers['content-encoding'], 'gzip')
t.equal(body, testContent)
t.end()
})
})
tape('transparently supports gzip decoding to pipes', function (t) {
var options = { url: server.url + '/foo', gzip: true }
var chunks = []
request.get(options)
.on('data', function (chunk) {
chunks.push(chunk)
})
.on('end', function () {
t.equal(Buffer.concat(chunks).toString(), testContent)
t.end()
})
.on('error', function (err) {
t.fail(err)
})
})
tape('does not request gzip if user specifies Accepted-Encodings', function (t) {
var headers = { 'Accept-Encoding': null }
var options = {
url: server.url + '/foo',
headers: headers,
gzip: true
}
request.get(options, function (err, res, body) {
t.equal(err, null)
t.equal(res.headers['content-encoding'], undefined)
t.equal(body, testContent)
t.end()
})
})
tape('does not decode user-requested encoding by default', function (t) {
var headers = { 'Accept-Encoding': 'gzip' }
var options = { url: server.url + '/foo', headers: headers }
request.get(options, function (err, res, body) {
t.equal(err, null)
t.equal(res.headers['content-encoding'], 'gzip')
t.equal(body, testContentGzip.toString())
t.end()
})
})
tape('supports character encoding with gzip encoding', function (t) {
var headers = { 'Accept-Encoding': 'gzip' }
var options = {
url: server.url + '/foo',
headers: headers,
gzip: true,
encoding: 'utf8'
}
var strings = []
request.get(options)
.on('data', function (string) {
t.equal(typeof string, 'string')
strings.push(string)
})
.on('end', function () {
t.equal(strings.join(''), testContent)
t.end()
})
.on('error', function (err) {
t.fail(err)
})
})
tape('transparently supports gzip error to callbacks', function (t) {
var options = { url: server.url + '/error', gzip: true }
request.get(options, function (err, res, body) {
t.equal(err.code, 'Z_DATA_ERROR')
t.equal(res, undefined)
t.equal(body, undefined)
t.end()
})
})
tape('transparently supports gzip error to pipes', function (t) {
var options = { url: server.url + '/error', gzip: true }
request.get(options)
.on('data', function (chunk) {
t.fail('Should not receive data event')
})
.on('end', function () {
t.fail('Should not receive end event')
})
.on('error', function (err) {
t.equal(err.code, 'Z_DATA_ERROR')
t.end()
})
})
tape('pause when streaming from a gzip request object', function (t) {
var chunks = []
var paused = false
var options = { url: server.url + '/chunks', gzip: true }
request.get(options)
.on('data', function (chunk) {
var self = this
t.notOk(paused, 'Only receive data when not paused')
chunks.push(chunk)
if (chunks.length === 1) {
self.pause()
paused = true
setTimeout(function () {
paused = false
self.resume()
}, 100)
}
})
.on('end', function () {
t.ok(chunks.length > 1, 'Received multiple chunks')
t.ok(bufferEqual(Buffer.concat(chunks), testContentBig), 'Expected content')
t.end()
})
})
tape('pause before streaming from a gzip request object', function (t) {
var paused = true
var options = { url: server.url + '/foo', gzip: true }
var r = request.get(options)
r.pause()
r.on('data', function (data) {
t.notOk(paused, 'Only receive data when not paused')
t.equal(data.toString(), testContent)
})
r.on('end', t.end.bind(t))
setTimeout(function () {
paused = false
r.resume()
}, 100)
})
tape('transparently supports deflate decoding to callbacks', function (t) {
var options = { url: server.url + '/foo', gzip: true, headers: { 'Accept-Encoding': 'deflate' } }
request.get(options, function (err, res, body) {
t.equal(err, null)
t.equal(res.headers['content-encoding'], 'deflate')
t.equal(body, testContent)
t.end()
})
})
tape('do not try to pipe HEAD request responses', function (t) {
var options = { method: 'HEAD', url: server.url + '/foo', gzip: true }
request(options, function (err, res, body) {
t.equal(err, null)
t.equal(body, '')
t.end()
})
})
tape('do not try to pipe responses with no body', function (t) {
var options = { url: server.url + '/foo', gzip: true }
// skip 105 on Node >= v10
var statusCodes = process.version.split('.')[0].slice(1) >= 10
? [204, 304] : [105, 204, 304]
;(function next (index) {
if (index === statusCodes.length) {
t.end()
return
}
options.headers = {code: statusCodes[index]}
request.post(options, function (err, res, body) {
t.equal(err, null)
t.equal(res.headers.code, statusCodes[index].toString())
t.equal(body, '')
next(++index)
})
})(0)
})
tape('cleanup', function (t) {
server.close(function () {
t.end()
})
})