@wiajs/request 3.0.1 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/.github/ISSUE_TEMPLATE.md +0 -56
- package/.github/PULL_REQUEST_TEMPLATE.md +0 -13
- package/.github/stale.yml +0 -19
- package/.swcrc +0 -57
- package/.travis.yml +0 -21
- package/CONTRIBUTING.md +0 -81
- package/biome.json +0 -44
- package/codecov.yml +0 -2
- package/disabled.appveyor.yml +0 -36
- package/examples/README.md +0 -135
- package/gulpfile.js +0 -71
- package/release.sh +0 -45
- package/tests/browser/karma.conf.js +0 -57
- package/tests/browser/ssl/ca.crt +0 -14
- package/tests/browser/ssl/server.crt +0 -14
- package/tests/browser/ssl/server.key +0 -15
- package/tests/browser/start.js +0 -37
- package/tests/browser/test.js +0 -34
- package/tests/fixtures/har.json +0 -158
- package/tests/googledoodle.jpg +0 -0
- package/tests/server.js +0 -142
- package/tests/squid.conf +0 -76
- package/tests/ssl/ca/README.md +0 -8
- package/tests/ssl/ca/ca.cnf +0 -20
- package/tests/ssl/ca/ca.crl +0 -0
- package/tests/ssl/ca/ca.crt +0 -17
- package/tests/ssl/ca/ca.csr +0 -13
- package/tests/ssl/ca/ca.key +0 -18
- package/tests/ssl/ca/ca.srl +0 -1
- package/tests/ssl/ca/client-enc.key +0 -30
- package/tests/ssl/ca/client.cnf +0 -20
- package/tests/ssl/ca/client.crt +0 -20
- package/tests/ssl/ca/client.csr +0 -18
- package/tests/ssl/ca/client.key +0 -27
- package/tests/ssl/ca/gen-all-certs.sh +0 -6
- package/tests/ssl/ca/gen-client.sh +0 -25
- package/tests/ssl/ca/gen-localhost.sh +0 -22
- package/tests/ssl/ca/gen-server.sh +0 -18
- package/tests/ssl/ca/localhost.cnf +0 -20
- package/tests/ssl/ca/localhost.crt +0 -20
- package/tests/ssl/ca/localhost.csr +0 -18
- package/tests/ssl/ca/localhost.js +0 -33
- package/tests/ssl/ca/localhost.key +0 -27
- package/tests/ssl/ca/server.cnf +0 -19
- package/tests/ssl/ca/server.crt +0 -25
- package/tests/ssl/ca/server.csr +0 -29
- package/tests/ssl/ca/server.js +0 -34
- package/tests/ssl/ca/server.key +0 -51
- package/tests/ssl/npm-ca.crt +0 -16
- package/tests/ssl/test.crt +0 -15
- package/tests/ssl/test.key +0 -15
- package/tests/test-agent.js +0 -102
- package/tests/test-agentOptions.js +0 -51
- package/tests/test-api.js +0 -33
- package/tests/test-aws.js +0 -123
- package/tests/test-baseUrl.js +0 -133
- package/tests/test-basic-auth.js +0 -221
- package/tests/test-bearer-auth.js +0 -187
- package/tests/test-body.js +0 -154
- package/tests/test-cookies.js +0 -130
- package/tests/test-defaults.js +0 -340
- package/tests/test-digest-auth.js +0 -232
- package/tests/test-emptyBody.js +0 -56
- package/tests/test-errors.js +0 -108
- package/tests/test-event-forwarding.js +0 -39
- package/tests/test-follow-all-303.js +0 -45
- package/tests/test-follow-all.js +0 -57
- package/tests/test-form-data-error.js +0 -85
- package/tests/test-form-data.js +0 -133
- package/tests/test-form-urlencoded.js +0 -73
- package/tests/test-form.js +0 -101
- package/tests/test-gzip.js +0 -296
- package/tests/test-har.js +0 -175
- package/tests/test-hawk.js +0 -187
- package/tests/test-headers.js +0 -305
- package/tests/test-http-signature.js +0 -110
- package/tests/test-httpModule.js +0 -112
- package/tests/test-https.js +0 -116
- package/tests/test-isUrl.js +0 -120
- package/tests/test-json-request.js +0 -117
- package/tests/test-localAddress.js +0 -49
- package/tests/test-multipart-encoding.js +0 -147
- package/tests/test-multipart.js +0 -129
- package/tests/test-node-debug.js +0 -95
- package/tests/test-oauth.js +0 -721
- package/tests/test-onelineproxy.js +0 -61
- package/tests/test-option-reuse.js +0 -54
- package/tests/test-options-convenience-method.js +0 -52
- package/tests/test-params.js +0 -101
- package/tests/test-piped-redirect.js +0 -55
- package/tests/test-pipes.js +0 -383
- package/tests/test-pool.js +0 -148
- package/tests/test-promise.js +0 -53
- package/tests/test-proxy-connect.js +0 -80
- package/tests/test-proxy.js +0 -304
- package/tests/test-qs.js +0 -135
- package/tests/test-redirect-auth.js +0 -131
- package/tests/test-redirect-complex.js +0 -93
- package/tests/test-redirect.js +0 -449
- package/tests/test-rfc3986.js +0 -106
- package/tests/test-stream.js +0 -36
- package/tests/test-timeout.js +0 -260
- package/tests/test-timing.js +0 -147
- package/tests/test-toJSON.js +0 -45
- package/tests/test-tunnel.js +0 -466
- package/tests/test-unix.js +0 -74
- package/tests/unicycle.jpg +0 -0
package/tests/test-form-data.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var http = require('http')
|
|
4
|
-
var path = require('path')
|
|
5
|
-
var mime = require('mime-types')
|
|
6
|
-
var request = require('../index')
|
|
7
|
-
var fs = require('fs')
|
|
8
|
-
var tape = require('tape')
|
|
9
|
-
|
|
10
|
-
function runTest (t, options) {
|
|
11
|
-
var remoteFile = path.join(__dirname, 'googledoodle.jpg')
|
|
12
|
-
var localFile = path.join(__dirname, 'unicycle.jpg')
|
|
13
|
-
var multipartFormData = {}
|
|
14
|
-
|
|
15
|
-
var server = http.createServer(function (req, res) {
|
|
16
|
-
if (req.url === '/file') {
|
|
17
|
-
res.writeHead(200, {'content-type': 'image/jpg', 'content-length': 7187})
|
|
18
|
-
res.end(fs.readFileSync(remoteFile), 'binary')
|
|
19
|
-
return
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (options.auth) {
|
|
23
|
-
if (!req.headers.authorization) {
|
|
24
|
-
res.writeHead(401, {'www-authenticate': 'Basic realm="Private"'})
|
|
25
|
-
res.end()
|
|
26
|
-
return
|
|
27
|
-
} else {
|
|
28
|
-
t.ok(req.headers.authorization === 'Basic ' + Buffer.from('user:pass').toString('base64'))
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
t.ok(/multipart\/form-data; boundary=--------------------------\d+/
|
|
33
|
-
.test(req.headers['content-type']))
|
|
34
|
-
|
|
35
|
-
// temp workaround
|
|
36
|
-
var data = ''
|
|
37
|
-
req.setEncoding('utf8')
|
|
38
|
-
|
|
39
|
-
req.on('data', function (d) {
|
|
40
|
-
data += d
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
req.on('end', function () {
|
|
44
|
-
// check for the fields' traces
|
|
45
|
-
|
|
46
|
-
// 1st field : my_field
|
|
47
|
-
t.ok(data.indexOf('form-data; name="my_field"') !== -1)
|
|
48
|
-
t.ok(data.indexOf(multipartFormData.my_field) !== -1)
|
|
49
|
-
|
|
50
|
-
// 2nd field : my_buffer
|
|
51
|
-
t.ok(data.indexOf('form-data; name="my_buffer"') !== -1)
|
|
52
|
-
t.ok(data.indexOf(multipartFormData.my_buffer) !== -1)
|
|
53
|
-
|
|
54
|
-
// 3rd field : my_file
|
|
55
|
-
t.ok(data.indexOf('form-data; name="my_file"') !== -1)
|
|
56
|
-
t.ok(data.indexOf('; filename="' + path.basename(multipartFormData.my_file.path) + '"') !== -1)
|
|
57
|
-
// check for unicycle.jpg traces
|
|
58
|
-
t.ok(data.indexOf('2005:06:21 01:44:12') !== -1)
|
|
59
|
-
t.ok(data.indexOf('Content-Type: ' + mime.lookup(multipartFormData.my_file.path)) !== -1)
|
|
60
|
-
|
|
61
|
-
// 4th field : remote_file
|
|
62
|
-
t.ok(data.indexOf('form-data; name="remote_file"') !== -1)
|
|
63
|
-
t.ok(data.indexOf('; filename="' + path.basename(multipartFormData.remote_file.path) + '"') !== -1)
|
|
64
|
-
|
|
65
|
-
// 5th field : file with metadata
|
|
66
|
-
t.ok(data.indexOf('form-data; name="secret_file"') !== -1)
|
|
67
|
-
t.ok(data.indexOf('Content-Disposition: form-data; name="secret_file"; filename="topsecret.jpg"') !== -1)
|
|
68
|
-
t.ok(data.indexOf('Content-Type: image/custom') !== -1)
|
|
69
|
-
|
|
70
|
-
// 6th field : batch of files
|
|
71
|
-
t.ok(data.indexOf('form-data; name="batch"') !== -1)
|
|
72
|
-
t.ok(data.match(/form-data; name="batch"/g).length === 2)
|
|
73
|
-
|
|
74
|
-
// check for http://localhost:nnnn/file traces
|
|
75
|
-
t.ok(data.indexOf('Photoshop ICC') !== -1)
|
|
76
|
-
t.ok(data.indexOf('Content-Type: ' + mime.lookup(remoteFile)) !== -1)
|
|
77
|
-
|
|
78
|
-
res.writeHead(200)
|
|
79
|
-
res.end(options.json ? JSON.stringify({status: 'done'}) : 'done')
|
|
80
|
-
})
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
server.listen(0, function () {
|
|
84
|
-
var url = 'http://localhost:' + this.address().port
|
|
85
|
-
// @NOTE: multipartFormData properties must be set here so that my_file read stream does not leak in node v0.8
|
|
86
|
-
multipartFormData.my_field = 'my_value'
|
|
87
|
-
multipartFormData.my_buffer = Buffer.from([1, 2, 3])
|
|
88
|
-
multipartFormData.my_file = fs.createReadStream(localFile)
|
|
89
|
-
multipartFormData.remote_file = request(url + '/file')
|
|
90
|
-
multipartFormData.secret_file = {
|
|
91
|
-
value: fs.createReadStream(localFile),
|
|
92
|
-
options: {
|
|
93
|
-
filename: 'topsecret.jpg',
|
|
94
|
-
contentType: 'image/custom'
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
multipartFormData.batch = [
|
|
98
|
-
fs.createReadStream(localFile),
|
|
99
|
-
fs.createReadStream(localFile)
|
|
100
|
-
]
|
|
101
|
-
|
|
102
|
-
var reqOptions = {
|
|
103
|
-
url: url + '/upload',
|
|
104
|
-
formData: multipartFormData
|
|
105
|
-
}
|
|
106
|
-
if (options.json) {
|
|
107
|
-
reqOptions.json = true
|
|
108
|
-
}
|
|
109
|
-
if (options.auth) {
|
|
110
|
-
reqOptions.auth = {user: 'user', pass: 'pass', sendImmediately: false}
|
|
111
|
-
}
|
|
112
|
-
request.post(reqOptions, function (err, res, body) {
|
|
113
|
-
t.equal(err, null)
|
|
114
|
-
t.equal(res.statusCode, 200)
|
|
115
|
-
t.deepEqual(body, options.json ? {status: 'done'} : 'done')
|
|
116
|
-
server.close(function () {
|
|
117
|
-
t.end()
|
|
118
|
-
})
|
|
119
|
-
})
|
|
120
|
-
})
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
tape('multipart formData', function (t) {
|
|
124
|
-
runTest(t, {json: false})
|
|
125
|
-
})
|
|
126
|
-
|
|
127
|
-
tape('multipart formData + JSON', function (t) {
|
|
128
|
-
runTest(t, {json: true})
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
tape('multipart formData + basic auth', function (t) {
|
|
132
|
-
runTest(t, {json: false, auth: true})
|
|
133
|
-
})
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var http = require('http')
|
|
4
|
-
var request = require('../index')
|
|
5
|
-
var tape = require('tape')
|
|
6
|
-
|
|
7
|
-
function runTest (t, options, index) {
|
|
8
|
-
var server = http.createServer(function (req, res) {
|
|
9
|
-
if (index === 0 || index === 3) {
|
|
10
|
-
t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded')
|
|
11
|
-
} else {
|
|
12
|
-
t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded; charset=UTF-8')
|
|
13
|
-
}
|
|
14
|
-
t.equal(req.headers['content-length'], '21')
|
|
15
|
-
t.equal(req.headers.accept, 'application/json')
|
|
16
|
-
|
|
17
|
-
var data = ''
|
|
18
|
-
req.setEncoding('utf8')
|
|
19
|
-
|
|
20
|
-
req.on('data', function (d) {
|
|
21
|
-
data += d
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
req.on('end', function () {
|
|
25
|
-
t.equal(data, 'some=url&encoded=data')
|
|
26
|
-
|
|
27
|
-
res.writeHead(200)
|
|
28
|
-
res.end('done')
|
|
29
|
-
})
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
server.listen(0, function () {
|
|
33
|
-
var url = 'http://localhost:' + this.address().port
|
|
34
|
-
var r = request.post(url, options, function (err, res, body) {
|
|
35
|
-
t.equal(err, null)
|
|
36
|
-
t.equal(res.statusCode, 200)
|
|
37
|
-
t.equal(body, 'done')
|
|
38
|
-
server.close(function () {
|
|
39
|
-
t.end()
|
|
40
|
-
})
|
|
41
|
-
})
|
|
42
|
-
if (!options.form && !options.body) {
|
|
43
|
-
r.form({some: 'url', encoded: 'data'})
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
var cases = [
|
|
49
|
-
{
|
|
50
|
-
form: {some: 'url', encoded: 'data'},
|
|
51
|
-
json: true
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
headers: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'},
|
|
55
|
-
form: {some: 'url', encoded: 'data'},
|
|
56
|
-
json: true
|
|
57
|
-
},
|
|
58
|
-
{
|
|
59
|
-
headers: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'},
|
|
60
|
-
body: 'some=url&encoded=data',
|
|
61
|
-
json: true
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
// body set via .form() method
|
|
65
|
-
json: true
|
|
66
|
-
}
|
|
67
|
-
]
|
|
68
|
-
|
|
69
|
-
cases.forEach(function (options, index) {
|
|
70
|
-
tape('application/x-www-form-urlencoded ' + index, function (t) {
|
|
71
|
-
runTest(t, options, index)
|
|
72
|
-
})
|
|
73
|
-
})
|
package/tests/test-form.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var http = require('http')
|
|
4
|
-
var path = require('path')
|
|
5
|
-
var mime = require('mime-types')
|
|
6
|
-
var request = require('../index')
|
|
7
|
-
var fs = require('fs')
|
|
8
|
-
var tape = require('tape')
|
|
9
|
-
|
|
10
|
-
tape('multipart form append', function (t) {
|
|
11
|
-
var remoteFile = path.join(__dirname, 'googledoodle.jpg')
|
|
12
|
-
var localFile = path.join(__dirname, 'unicycle.jpg')
|
|
13
|
-
var totalLength = null
|
|
14
|
-
var FIELDS = []
|
|
15
|
-
|
|
16
|
-
var server = http.createServer(function (req, res) {
|
|
17
|
-
if (req.url === '/file') {
|
|
18
|
-
res.writeHead(200, {'content-type': 'image/jpg', 'content-length': 7187})
|
|
19
|
-
res.end(fs.readFileSync(remoteFile), 'binary')
|
|
20
|
-
return
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
t.ok(/multipart\/form-data; boundary=--------------------------\d+/
|
|
24
|
-
.test(req.headers['content-type']))
|
|
25
|
-
|
|
26
|
-
// temp workaround
|
|
27
|
-
var data = ''
|
|
28
|
-
req.setEncoding('utf8')
|
|
29
|
-
|
|
30
|
-
req.on('data', function (d) {
|
|
31
|
-
data += d
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
req.on('end', function () {
|
|
35
|
-
var field
|
|
36
|
-
// check for the fields' traces
|
|
37
|
-
|
|
38
|
-
// 1st field : my_field
|
|
39
|
-
field = FIELDS.shift()
|
|
40
|
-
t.ok(data.indexOf('form-data; name="' + field.name + '"') !== -1)
|
|
41
|
-
t.ok(data.indexOf(field.value) !== -1)
|
|
42
|
-
|
|
43
|
-
// 2nd field : my_buffer
|
|
44
|
-
field = FIELDS.shift()
|
|
45
|
-
t.ok(data.indexOf('form-data; name="' + field.name + '"') !== -1)
|
|
46
|
-
t.ok(data.indexOf(field.value) !== -1)
|
|
47
|
-
|
|
48
|
-
// 3rd field : my_file
|
|
49
|
-
field = FIELDS.shift()
|
|
50
|
-
t.ok(data.indexOf('form-data; name="' + field.name + '"') !== -1)
|
|
51
|
-
t.ok(data.indexOf('; filename="' + path.basename(field.value.path) + '"') !== -1)
|
|
52
|
-
// check for unicycle.jpg traces
|
|
53
|
-
t.ok(data.indexOf('2005:06:21 01:44:12') !== -1)
|
|
54
|
-
t.ok(data.indexOf('Content-Type: ' + mime.lookup(field.value.path)) !== -1)
|
|
55
|
-
|
|
56
|
-
// 4th field : remote_file
|
|
57
|
-
field = FIELDS.shift()
|
|
58
|
-
t.ok(data.indexOf('form-data; name="' + field.name + '"') !== -1)
|
|
59
|
-
t.ok(data.indexOf('; filename="' + path.basename(field.value.path) + '"') !== -1)
|
|
60
|
-
// check for http://localhost:nnnn/file traces
|
|
61
|
-
t.ok(data.indexOf('Photoshop ICC') !== -1)
|
|
62
|
-
t.ok(data.indexOf('Content-Type: ' + mime.lookup(remoteFile)) !== -1)
|
|
63
|
-
|
|
64
|
-
t.ok(+req.headers['content-length'] === totalLength)
|
|
65
|
-
|
|
66
|
-
res.writeHead(200)
|
|
67
|
-
res.end('done')
|
|
68
|
-
|
|
69
|
-
t.equal(FIELDS.length, 0)
|
|
70
|
-
})
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
server.listen(0, function () {
|
|
74
|
-
var url = 'http://localhost:' + this.address().port
|
|
75
|
-
FIELDS = [
|
|
76
|
-
{ name: 'my_field', value: 'my_value' },
|
|
77
|
-
{ name: 'my_buffer', value: Buffer.from([1, 2, 3]) },
|
|
78
|
-
{ name: 'my_file', value: fs.createReadStream(localFile) },
|
|
79
|
-
{ name: 'remote_file', value: request(url + '/file') }
|
|
80
|
-
]
|
|
81
|
-
|
|
82
|
-
var req = request.post(url + '/upload', function (err, res, body) {
|
|
83
|
-
t.equal(err, null)
|
|
84
|
-
t.equal(res.statusCode, 200)
|
|
85
|
-
t.equal(body, 'done')
|
|
86
|
-
server.close(function () {
|
|
87
|
-
t.end()
|
|
88
|
-
})
|
|
89
|
-
})
|
|
90
|
-
var form = req.form()
|
|
91
|
-
|
|
92
|
-
FIELDS.forEach(function (field) {
|
|
93
|
-
form.append(field.name, field.value)
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
form.getLength(function (err, length) {
|
|
97
|
-
t.equal(err, null)
|
|
98
|
-
totalLength = length
|
|
99
|
-
})
|
|
100
|
-
})
|
|
101
|
-
})
|
package/tests/test-gzip.js
DELETED
|
@@ -1,296 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var request = require('../index')
|
|
4
|
-
var http = require('http')
|
|
5
|
-
var zlib = require('zlib')
|
|
6
|
-
var assert = require('assert')
|
|
7
|
-
var bufferEqual = require('buffer-equal')
|
|
8
|
-
var tape = require('tape')
|
|
9
|
-
|
|
10
|
-
var testContent = 'Compressible response content.\n'
|
|
11
|
-
var testContentBig
|
|
12
|
-
var testContentBigGzip
|
|
13
|
-
var testContentGzip
|
|
14
|
-
|
|
15
|
-
var server = http.createServer(function (req, res) {
|
|
16
|
-
res.statusCode = 200
|
|
17
|
-
res.setHeader('Content-Type', 'text/plain')
|
|
18
|
-
|
|
19
|
-
if (req.method === 'HEAD') {
|
|
20
|
-
res.setHeader('Content-Encoding', 'gzip')
|
|
21
|
-
res.end()
|
|
22
|
-
return
|
|
23
|
-
}
|
|
24
|
-
if (req.headers.code) {
|
|
25
|
-
res.writeHead(req.headers.code, {
|
|
26
|
-
'Content-Encoding': 'gzip',
|
|
27
|
-
code: req.headers.code
|
|
28
|
-
})
|
|
29
|
-
res.end()
|
|
30
|
-
return
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (/\bgzip\b/i.test(req.headers['accept-encoding'])) {
|
|
34
|
-
res.setHeader('Content-Encoding', 'gzip')
|
|
35
|
-
if (req.url === '/error') {
|
|
36
|
-
// send plaintext instead of gzip (should cause an error for the client)
|
|
37
|
-
res.end(testContent)
|
|
38
|
-
} else if (req.url === '/chunks') {
|
|
39
|
-
res.writeHead(200)
|
|
40
|
-
res.write(testContentBigGzip.slice(0, 4096))
|
|
41
|
-
setTimeout(function () { res.end(testContentBigGzip.slice(4096)) }, 10)
|
|
42
|
-
} else if (req.url === '/just-slightly-truncated') {
|
|
43
|
-
zlib.gzip(testContent, function (err, data) {
|
|
44
|
-
assert.equal(err, null)
|
|
45
|
-
// truncate the CRC checksum and size check at the end of the stream
|
|
46
|
-
res.end(data.slice(0, data.length - 8))
|
|
47
|
-
})
|
|
48
|
-
} else {
|
|
49
|
-
zlib.gzip(testContent, function (err, data) {
|
|
50
|
-
assert.equal(err, null)
|
|
51
|
-
res.end(data)
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
} else if (/\bdeflate\b/i.test(req.headers['accept-encoding'])) {
|
|
55
|
-
res.setHeader('Content-Encoding', 'deflate')
|
|
56
|
-
zlib.deflate(testContent, function (err, data) {
|
|
57
|
-
assert.equal(err, null)
|
|
58
|
-
res.end(data)
|
|
59
|
-
})
|
|
60
|
-
} else {
|
|
61
|
-
res.end(testContent)
|
|
62
|
-
}
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
tape('setup', function (t) {
|
|
66
|
-
// Need big compressed content to be large enough to chunk into gzip blocks.
|
|
67
|
-
// Want it to be deterministic to ensure test is reliable.
|
|
68
|
-
// Generate pseudo-random printable ASCII characters using MINSTD
|
|
69
|
-
var a = 48271
|
|
70
|
-
var m = 0x7FFFFFFF
|
|
71
|
-
var x = 1
|
|
72
|
-
testContentBig = Buffer.alloc(10240)
|
|
73
|
-
for (var i = 0; i < testContentBig.length; ++i) {
|
|
74
|
-
x = (a * x) & m
|
|
75
|
-
// Printable ASCII range from 32-126, inclusive
|
|
76
|
-
testContentBig[i] = (x % 95) + 32
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
zlib.gzip(testContent, function (err, data) {
|
|
80
|
-
t.equal(err, null)
|
|
81
|
-
testContentGzip = data
|
|
82
|
-
|
|
83
|
-
zlib.gzip(testContentBig, function (err, data2) {
|
|
84
|
-
t.equal(err, null)
|
|
85
|
-
testContentBigGzip = data2
|
|
86
|
-
|
|
87
|
-
server.listen(0, function () {
|
|
88
|
-
server.url = 'http://localhost:' + this.address().port
|
|
89
|
-
t.end()
|
|
90
|
-
})
|
|
91
|
-
})
|
|
92
|
-
})
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
tape('transparently supports gzip decoding to callbacks', function (t) {
|
|
96
|
-
var options = { url: server.url + '/foo', gzip: true }
|
|
97
|
-
request.get(options, function (err, res, body) {
|
|
98
|
-
t.equal(err, null)
|
|
99
|
-
t.equal(res.headers['content-encoding'], 'gzip')
|
|
100
|
-
t.equal(body, testContent)
|
|
101
|
-
t.end()
|
|
102
|
-
})
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
tape('supports slightly invalid gzip content', function (t) {
|
|
106
|
-
var options = { url: server.url + '/just-slightly-truncated', gzip: true }
|
|
107
|
-
request.get(options, function (err, res, body) {
|
|
108
|
-
t.equal(err, null)
|
|
109
|
-
t.equal(res.headers['content-encoding'], 'gzip')
|
|
110
|
-
t.equal(body, testContent)
|
|
111
|
-
t.end()
|
|
112
|
-
})
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
tape('transparently supports gzip decoding to pipes', function (t) {
|
|
116
|
-
var options = { url: server.url + '/foo', gzip: true }
|
|
117
|
-
var chunks = []
|
|
118
|
-
request.get(options)
|
|
119
|
-
.on('data', function (chunk) {
|
|
120
|
-
chunks.push(chunk)
|
|
121
|
-
})
|
|
122
|
-
.on('end', function () {
|
|
123
|
-
t.equal(Buffer.concat(chunks).toString(), testContent)
|
|
124
|
-
t.end()
|
|
125
|
-
})
|
|
126
|
-
.on('error', function (err) {
|
|
127
|
-
t.fail(err)
|
|
128
|
-
})
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
tape('does not request gzip if user specifies Accepted-Encodings', function (t) {
|
|
132
|
-
var headers = { 'Accept-Encoding': null }
|
|
133
|
-
var options = {
|
|
134
|
-
url: server.url + '/foo',
|
|
135
|
-
headers: headers,
|
|
136
|
-
gzip: true
|
|
137
|
-
}
|
|
138
|
-
request.get(options, function (err, res, body) {
|
|
139
|
-
t.equal(err, null)
|
|
140
|
-
t.equal(res.headers['content-encoding'], undefined)
|
|
141
|
-
t.equal(body, testContent)
|
|
142
|
-
t.end()
|
|
143
|
-
})
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
tape('does not decode user-requested encoding by default', function (t) {
|
|
147
|
-
var headers = { 'Accept-Encoding': 'gzip' }
|
|
148
|
-
var options = { url: server.url + '/foo', headers: headers }
|
|
149
|
-
request.get(options, function (err, res, body) {
|
|
150
|
-
t.equal(err, null)
|
|
151
|
-
t.equal(res.headers['content-encoding'], 'gzip')
|
|
152
|
-
t.equal(body, testContentGzip.toString())
|
|
153
|
-
t.end()
|
|
154
|
-
})
|
|
155
|
-
})
|
|
156
|
-
|
|
157
|
-
tape('supports character encoding with gzip encoding', function (t) {
|
|
158
|
-
var headers = { 'Accept-Encoding': 'gzip' }
|
|
159
|
-
var options = {
|
|
160
|
-
url: server.url + '/foo',
|
|
161
|
-
headers: headers,
|
|
162
|
-
gzip: true,
|
|
163
|
-
encoding: 'utf8'
|
|
164
|
-
}
|
|
165
|
-
var strings = []
|
|
166
|
-
request.get(options)
|
|
167
|
-
.on('data', function (string) {
|
|
168
|
-
t.equal(typeof string, 'string')
|
|
169
|
-
strings.push(string)
|
|
170
|
-
})
|
|
171
|
-
.on('end', function () {
|
|
172
|
-
t.equal(strings.join(''), testContent)
|
|
173
|
-
t.end()
|
|
174
|
-
})
|
|
175
|
-
.on('error', function (err) {
|
|
176
|
-
t.fail(err)
|
|
177
|
-
})
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
tape('transparently supports gzip error to callbacks', function (t) {
|
|
181
|
-
var options = { url: server.url + '/error', gzip: true }
|
|
182
|
-
request.get(options, function (err, res, body) {
|
|
183
|
-
t.equal(err.code, 'Z_DATA_ERROR')
|
|
184
|
-
t.equal(res, undefined)
|
|
185
|
-
t.equal(body, undefined)
|
|
186
|
-
t.end()
|
|
187
|
-
})
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
tape('transparently supports gzip error to pipes', function (t) {
|
|
191
|
-
var options = { url: server.url + '/error', gzip: true }
|
|
192
|
-
request.get(options)
|
|
193
|
-
.on('data', function (chunk) {
|
|
194
|
-
t.fail('Should not receive data event')
|
|
195
|
-
})
|
|
196
|
-
.on('end', function () {
|
|
197
|
-
t.fail('Should not receive end event')
|
|
198
|
-
})
|
|
199
|
-
.on('error', function (err) {
|
|
200
|
-
t.equal(err.code, 'Z_DATA_ERROR')
|
|
201
|
-
t.end()
|
|
202
|
-
})
|
|
203
|
-
})
|
|
204
|
-
|
|
205
|
-
tape('pause when streaming from a gzip request object', function (t) {
|
|
206
|
-
var chunks = []
|
|
207
|
-
var paused = false
|
|
208
|
-
var options = { url: server.url + '/chunks', gzip: true }
|
|
209
|
-
request.get(options)
|
|
210
|
-
.on('data', function (chunk) {
|
|
211
|
-
var self = this
|
|
212
|
-
|
|
213
|
-
t.notOk(paused, 'Only receive data when not paused')
|
|
214
|
-
|
|
215
|
-
chunks.push(chunk)
|
|
216
|
-
if (chunks.length === 1) {
|
|
217
|
-
self.pause()
|
|
218
|
-
paused = true
|
|
219
|
-
setTimeout(function () {
|
|
220
|
-
paused = false
|
|
221
|
-
self.resume()
|
|
222
|
-
}, 100)
|
|
223
|
-
}
|
|
224
|
-
})
|
|
225
|
-
.on('end', function () {
|
|
226
|
-
t.ok(chunks.length > 1, 'Received multiple chunks')
|
|
227
|
-
t.ok(bufferEqual(Buffer.concat(chunks), testContentBig), 'Expected content')
|
|
228
|
-
t.end()
|
|
229
|
-
})
|
|
230
|
-
})
|
|
231
|
-
|
|
232
|
-
tape('pause before streaming from a gzip request object', function (t) {
|
|
233
|
-
var paused = true
|
|
234
|
-
var options = { url: server.url + '/foo', gzip: true }
|
|
235
|
-
var r = request.get(options)
|
|
236
|
-
r.pause()
|
|
237
|
-
r.on('data', function (data) {
|
|
238
|
-
t.notOk(paused, 'Only receive data when not paused')
|
|
239
|
-
t.equal(data.toString(), testContent)
|
|
240
|
-
})
|
|
241
|
-
r.on('end', t.end.bind(t))
|
|
242
|
-
|
|
243
|
-
setTimeout(function () {
|
|
244
|
-
paused = false
|
|
245
|
-
r.resume()
|
|
246
|
-
}, 100)
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
tape('transparently supports deflate decoding to callbacks', function (t) {
|
|
250
|
-
var options = { url: server.url + '/foo', gzip: true, headers: { 'Accept-Encoding': 'deflate' } }
|
|
251
|
-
|
|
252
|
-
request.get(options, function (err, res, body) {
|
|
253
|
-
t.equal(err, null)
|
|
254
|
-
t.equal(res.headers['content-encoding'], 'deflate')
|
|
255
|
-
t.equal(body, testContent)
|
|
256
|
-
t.end()
|
|
257
|
-
})
|
|
258
|
-
})
|
|
259
|
-
|
|
260
|
-
tape('do not try to pipe HEAD request responses', function (t) {
|
|
261
|
-
var options = { method: 'HEAD', url: server.url + '/foo', gzip: true }
|
|
262
|
-
|
|
263
|
-
request(options, function (err, res, body) {
|
|
264
|
-
t.equal(err, null)
|
|
265
|
-
t.equal(body, '')
|
|
266
|
-
t.end()
|
|
267
|
-
})
|
|
268
|
-
})
|
|
269
|
-
|
|
270
|
-
tape('do not try to pipe responses with no body', function (t) {
|
|
271
|
-
var options = { url: server.url + '/foo', gzip: true }
|
|
272
|
-
|
|
273
|
-
// skip 105 on Node >= v10
|
|
274
|
-
var statusCodes = process.version.split('.')[0].slice(1) >= 10
|
|
275
|
-
? [204, 304] : [105, 204, 304]
|
|
276
|
-
|
|
277
|
-
;(function next (index) {
|
|
278
|
-
if (index === statusCodes.length) {
|
|
279
|
-
t.end()
|
|
280
|
-
return
|
|
281
|
-
}
|
|
282
|
-
options.headers = {code: statusCodes[index]}
|
|
283
|
-
request.post(options, function (err, res, body) {
|
|
284
|
-
t.equal(err, null)
|
|
285
|
-
t.equal(res.headers.code, statusCodes[index].toString())
|
|
286
|
-
t.equal(body, '')
|
|
287
|
-
next(++index)
|
|
288
|
-
})
|
|
289
|
-
})(0)
|
|
290
|
-
})
|
|
291
|
-
|
|
292
|
-
tape('cleanup', function (t) {
|
|
293
|
-
server.close(function () {
|
|
294
|
-
t.end()
|
|
295
|
-
})
|
|
296
|
-
})
|