@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-rfc3986.js
DELETED
|
@@ -1,106 +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) {
|
|
8
|
-
var server = http.createServer(function (req, res) {
|
|
9
|
-
var data = ''
|
|
10
|
-
req.setEncoding('utf8')
|
|
11
|
-
|
|
12
|
-
req.on('data', function (d) {
|
|
13
|
-
data += d
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
req.on('end', function () {
|
|
17
|
-
if (options.qs) {
|
|
18
|
-
t.equal(req.url, '/?rfc3986=%21%2A%28%29%27')
|
|
19
|
-
}
|
|
20
|
-
t.equal(data, options._expectBody)
|
|
21
|
-
|
|
22
|
-
res.writeHead(200)
|
|
23
|
-
res.end('done')
|
|
24
|
-
})
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
server.listen(0, function () {
|
|
28
|
-
var port = this.address().port
|
|
29
|
-
request.post('http://localhost:' + port, options, function (err, res, body) {
|
|
30
|
-
t.equal(err, null)
|
|
31
|
-
server.close(function () {
|
|
32
|
-
t.end()
|
|
33
|
-
})
|
|
34
|
-
})
|
|
35
|
-
})
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
var bodyEscaped = 'rfc3986=%21%2A%28%29%27'
|
|
39
|
-
var bodyJson = '{"rfc3986":"!*()\'"}'
|
|
40
|
-
|
|
41
|
-
var cases = [
|
|
42
|
-
{
|
|
43
|
-
_name: 'qs',
|
|
44
|
-
qs: {rfc3986: "!*()'"},
|
|
45
|
-
_expectBody: ''
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
_name: 'qs + json',
|
|
49
|
-
qs: {rfc3986: "!*()'"},
|
|
50
|
-
json: true,
|
|
51
|
-
_expectBody: ''
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
_name: 'form',
|
|
55
|
-
form: {rfc3986: "!*()'"},
|
|
56
|
-
_expectBody: bodyEscaped
|
|
57
|
-
},
|
|
58
|
-
{
|
|
59
|
-
_name: 'form + json',
|
|
60
|
-
form: {rfc3986: "!*()'"},
|
|
61
|
-
json: true,
|
|
62
|
-
_expectBody: bodyEscaped
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
_name: 'qs + form',
|
|
66
|
-
qs: {rfc3986: "!*()'"},
|
|
67
|
-
form: {rfc3986: "!*()'"},
|
|
68
|
-
_expectBody: bodyEscaped
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
_name: 'qs + form + json',
|
|
72
|
-
qs: {rfc3986: "!*()'"},
|
|
73
|
-
form: {rfc3986: "!*()'"},
|
|
74
|
-
json: true,
|
|
75
|
-
_expectBody: bodyEscaped
|
|
76
|
-
},
|
|
77
|
-
{
|
|
78
|
-
_name: 'body + header + json',
|
|
79
|
-
headers: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'},
|
|
80
|
-
body: "rfc3986=!*()'",
|
|
81
|
-
json: true,
|
|
82
|
-
_expectBody: bodyEscaped
|
|
83
|
-
},
|
|
84
|
-
{
|
|
85
|
-
_name: 'body + json',
|
|
86
|
-
body: {rfc3986: "!*()'"},
|
|
87
|
-
json: true,
|
|
88
|
-
_expectBody: bodyJson
|
|
89
|
-
},
|
|
90
|
-
{
|
|
91
|
-
_name: 'json object',
|
|
92
|
-
json: {rfc3986: "!*()'"},
|
|
93
|
-
_expectBody: bodyJson
|
|
94
|
-
}
|
|
95
|
-
]
|
|
96
|
-
|
|
97
|
-
var libs = ['qs', 'querystring']
|
|
98
|
-
|
|
99
|
-
libs.forEach(function (lib) {
|
|
100
|
-
cases.forEach(function (options) {
|
|
101
|
-
options.useQuerystring = (lib === 'querystring')
|
|
102
|
-
tape(lib + ' rfc3986 ' + options._name, function (t) {
|
|
103
|
-
runTest(t, options)
|
|
104
|
-
})
|
|
105
|
-
})
|
|
106
|
-
})
|
package/tests/test-stream.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
var fs = require('fs')
|
|
2
|
-
var path = require('path')
|
|
3
|
-
var http = require('http')
|
|
4
|
-
var tape = require('tape')
|
|
5
|
-
var request = require('../')
|
|
6
|
-
var server
|
|
7
|
-
|
|
8
|
-
tape('before', function (t) {
|
|
9
|
-
server = http.createServer()
|
|
10
|
-
server.on('request', function (req, res) {
|
|
11
|
-
req.pipe(res)
|
|
12
|
-
})
|
|
13
|
-
server.listen(0, function () {
|
|
14
|
-
server.url = 'http://localhost:' + this.address().port
|
|
15
|
-
t.end()
|
|
16
|
-
})
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
tape('request body stream', function (t) {
|
|
20
|
-
var fpath = path.join(__dirname, 'unicycle.jpg')
|
|
21
|
-
var input = fs.createReadStream(fpath, {highWaterMark: 1000})
|
|
22
|
-
request({
|
|
23
|
-
uri: server.url,
|
|
24
|
-
method: 'POST',
|
|
25
|
-
body: input,
|
|
26
|
-
encoding: null
|
|
27
|
-
}, function (err, res, body) {
|
|
28
|
-
t.error(err)
|
|
29
|
-
t.equal(body.length, fs.statSync(fpath).size)
|
|
30
|
-
t.end()
|
|
31
|
-
})
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
tape('after', function (t) {
|
|
35
|
-
server.close(t.end)
|
|
36
|
-
})
|
package/tests/test-timeout.js
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
function checkErrCode (t, err) {
|
|
4
|
-
t.notEqual(err, null)
|
|
5
|
-
t.ok(err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT',
|
|
6
|
-
'Error ETIMEDOUT or ESOCKETTIMEDOUT')
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function checkEventHandlers (t, socket) {
|
|
10
|
-
var connectListeners = socket.listeners('connect')
|
|
11
|
-
var found = false
|
|
12
|
-
for (var i = 0; i < connectListeners.length; ++i) {
|
|
13
|
-
var fn = connectListeners[i]
|
|
14
|
-
if (typeof fn === 'function' && fn.name === 'onReqSockConnect') {
|
|
15
|
-
found = true
|
|
16
|
-
break
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
t.ok(!found, 'Connect listener should not exist')
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
var server = require('./server')
|
|
23
|
-
var request = require('../index')
|
|
24
|
-
var tape = require('tape')
|
|
25
|
-
|
|
26
|
-
var s = server.createServer()
|
|
27
|
-
|
|
28
|
-
// Request that waits for 200ms
|
|
29
|
-
s.on('/timeout', function (req, res) {
|
|
30
|
-
setTimeout(function () {
|
|
31
|
-
res.writeHead(200, {'content-type': 'text/plain'})
|
|
32
|
-
res.write('waited')
|
|
33
|
-
res.end()
|
|
34
|
-
}, 200)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
tape('setup', function (t) {
|
|
38
|
-
s.listen(0, function () {
|
|
39
|
-
t.end()
|
|
40
|
-
})
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
tape('should timeout', function (t) {
|
|
44
|
-
var shouldTimeout = {
|
|
45
|
-
url: s.url + '/timeout',
|
|
46
|
-
timeout: 100
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
request(shouldTimeout, function (err, res, body) {
|
|
50
|
-
checkErrCode(t, err)
|
|
51
|
-
t.end()
|
|
52
|
-
})
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
tape('should set connect to false', function (t) {
|
|
56
|
-
var shouldTimeout = {
|
|
57
|
-
url: s.url + '/timeout',
|
|
58
|
-
timeout: 100
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
request(shouldTimeout, function (err, res, body) {
|
|
62
|
-
checkErrCode(t, err)
|
|
63
|
-
t.ok(err.connect === false, 'Read Timeout Error should set \'connect\' property to false')
|
|
64
|
-
t.end()
|
|
65
|
-
})
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
tape('should timeout with events', function (t) {
|
|
69
|
-
t.plan(3)
|
|
70
|
-
|
|
71
|
-
var shouldTimeoutWithEvents = {
|
|
72
|
-
url: s.url + '/timeout',
|
|
73
|
-
timeout: 100
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
var eventsEmitted = 0
|
|
77
|
-
request(shouldTimeoutWithEvents)
|
|
78
|
-
.on('error', function (err) {
|
|
79
|
-
eventsEmitted++
|
|
80
|
-
t.equal(1, eventsEmitted)
|
|
81
|
-
checkErrCode(t, err)
|
|
82
|
-
})
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
tape('should not timeout', function (t) {
|
|
86
|
-
var shouldntTimeout = {
|
|
87
|
-
url: s.url + '/timeout',
|
|
88
|
-
timeout: 1200
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
var socket
|
|
92
|
-
request(shouldntTimeout, function (err, res, body) {
|
|
93
|
-
t.equal(err, null)
|
|
94
|
-
t.equal(body, 'waited')
|
|
95
|
-
checkEventHandlers(t, socket)
|
|
96
|
-
t.end()
|
|
97
|
-
}).on('socket', function (socket_) {
|
|
98
|
-
socket = socket_
|
|
99
|
-
})
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
tape('no timeout', function (t) {
|
|
103
|
-
var noTimeout = {
|
|
104
|
-
url: s.url + '/timeout'
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
request(noTimeout, function (err, res, body) {
|
|
108
|
-
t.equal(err, null)
|
|
109
|
-
t.equal(body, 'waited')
|
|
110
|
-
t.end()
|
|
111
|
-
})
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
tape('negative timeout', function (t) { // should be treated a zero or the minimum delay
|
|
115
|
-
var negativeTimeout = {
|
|
116
|
-
url: s.url + '/timeout',
|
|
117
|
-
timeout: -1000
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
request(negativeTimeout, function (err, res, body) {
|
|
121
|
-
// Only verify error if it is set, since using a timeout value of 0 can lead
|
|
122
|
-
// to inconsistent results, depending on a variety of factors
|
|
123
|
-
if (err) {
|
|
124
|
-
checkErrCode(t, err)
|
|
125
|
-
}
|
|
126
|
-
t.end()
|
|
127
|
-
})
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
tape('float timeout', function (t) { // should be rounded by setTimeout anyway
|
|
131
|
-
var floatTimeout = {
|
|
132
|
-
url: s.url + '/timeout',
|
|
133
|
-
timeout: 100.76
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
request(floatTimeout, function (err, res, body) {
|
|
137
|
-
checkErrCode(t, err)
|
|
138
|
-
t.end()
|
|
139
|
-
})
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
// We need a destination that will not immediately return a TCP Reset
|
|
143
|
-
// packet. StackOverflow suggests these hosts:
|
|
144
|
-
// (https://stackoverflow.com/a/904609/329700)
|
|
145
|
-
var nonRoutable = [
|
|
146
|
-
'10.255.255.1',
|
|
147
|
-
'10.0.0.0',
|
|
148
|
-
'192.168.0.0',
|
|
149
|
-
'192.168.255.255',
|
|
150
|
-
'172.16.0.0',
|
|
151
|
-
'172.31.255.255'
|
|
152
|
-
]
|
|
153
|
-
var nrIndex = 0
|
|
154
|
-
function getNonRoutable () {
|
|
155
|
-
var ip = nonRoutable[nrIndex]
|
|
156
|
-
if (!ip) {
|
|
157
|
-
throw new Error('No more non-routable addresses')
|
|
158
|
-
}
|
|
159
|
-
++nrIndex
|
|
160
|
-
return ip
|
|
161
|
-
}
|
|
162
|
-
tape('connect timeout', function tryConnect (t) {
|
|
163
|
-
var tarpitHost = 'http://' + getNonRoutable()
|
|
164
|
-
var shouldConnectTimeout = {
|
|
165
|
-
url: tarpitHost + '/timeout',
|
|
166
|
-
timeout: 100
|
|
167
|
-
}
|
|
168
|
-
var socket
|
|
169
|
-
request(shouldConnectTimeout, function (err) {
|
|
170
|
-
t.notEqual(err, null)
|
|
171
|
-
if (err.code === 'ENETUNREACH' && nrIndex < nonRoutable.length) {
|
|
172
|
-
// With some network configurations, some addresses will be reported as
|
|
173
|
-
// unreachable immediately (before the timeout occurs). In those cases,
|
|
174
|
-
// try other non-routable addresses before giving up.
|
|
175
|
-
return tryConnect(t)
|
|
176
|
-
}
|
|
177
|
-
checkErrCode(t, err)
|
|
178
|
-
t.ok(err.connect === true, 'Connect Timeout Error should set \'connect\' property to true')
|
|
179
|
-
checkEventHandlers(t, socket)
|
|
180
|
-
nrIndex = 0
|
|
181
|
-
t.end()
|
|
182
|
-
}).on('socket', function (socket_) {
|
|
183
|
-
socket = socket_
|
|
184
|
-
})
|
|
185
|
-
})
|
|
186
|
-
|
|
187
|
-
tape('connect timeout with non-timeout error', function tryConnect (t) {
|
|
188
|
-
var tarpitHost = 'http://' + getNonRoutable()
|
|
189
|
-
var shouldConnectTimeout = {
|
|
190
|
-
url: tarpitHost + '/timeout',
|
|
191
|
-
timeout: 1000
|
|
192
|
-
}
|
|
193
|
-
var socket
|
|
194
|
-
request(shouldConnectTimeout, function (err) {
|
|
195
|
-
t.notEqual(err, null)
|
|
196
|
-
if (err.code === 'ENETUNREACH' && nrIndex < nonRoutable.length) {
|
|
197
|
-
// With some network configurations, some addresses will be reported as
|
|
198
|
-
// unreachable immediately (before the timeout occurs). In those cases,
|
|
199
|
-
// try other non-routable addresses before giving up.
|
|
200
|
-
return tryConnect(t)
|
|
201
|
-
}
|
|
202
|
-
// Delay the check since the 'connect' handler is removed in a separate
|
|
203
|
-
// 'error' handler which gets triggered after this callback
|
|
204
|
-
setImmediate(function () {
|
|
205
|
-
checkEventHandlers(t, socket)
|
|
206
|
-
nrIndex = 0
|
|
207
|
-
t.end()
|
|
208
|
-
})
|
|
209
|
-
}).on('socket', function (socket_) {
|
|
210
|
-
socket = socket_
|
|
211
|
-
setImmediate(function () {
|
|
212
|
-
socket.emit('error', new Error('Fake Error'))
|
|
213
|
-
})
|
|
214
|
-
})
|
|
215
|
-
})
|
|
216
|
-
|
|
217
|
-
tape('request timeout with keep-alive connection', function (t) {
|
|
218
|
-
var Agent = require('http').Agent
|
|
219
|
-
var agent = new Agent({ keepAlive: true })
|
|
220
|
-
var firstReq = {
|
|
221
|
-
url: s.url + '/timeout',
|
|
222
|
-
agent: agent
|
|
223
|
-
}
|
|
224
|
-
request(firstReq, function (err) {
|
|
225
|
-
// We should now still have a socket open. For the second request we should
|
|
226
|
-
// see a request timeout on the active socket ...
|
|
227
|
-
t.equal(err, null)
|
|
228
|
-
var shouldReqTimeout = {
|
|
229
|
-
url: s.url + '/timeout',
|
|
230
|
-
timeout: 100,
|
|
231
|
-
agent: agent
|
|
232
|
-
}
|
|
233
|
-
request(shouldReqTimeout, function (err) {
|
|
234
|
-
checkErrCode(t, err)
|
|
235
|
-
t.ok(err.connect === false, 'Error should have been a request timeout error')
|
|
236
|
-
t.end()
|
|
237
|
-
}).on('socket', function (socket) {
|
|
238
|
-
var isConnecting = socket._connecting || socket.connecting
|
|
239
|
-
t.ok(isConnecting !== true, 'Socket should already be connected')
|
|
240
|
-
})
|
|
241
|
-
}).on('socket', function (socket) {
|
|
242
|
-
var isConnecting = socket._connecting || socket.connecting
|
|
243
|
-
t.ok(isConnecting === true, 'Socket should be new')
|
|
244
|
-
})
|
|
245
|
-
})
|
|
246
|
-
|
|
247
|
-
tape('calling abort clears the timeout', function (t) {
|
|
248
|
-
const req = request({ url: s.url + '/timeout', timeout: 2500 })
|
|
249
|
-
setTimeout(function () {
|
|
250
|
-
req.abort()
|
|
251
|
-
t.equal(req.timeoutTimer, null)
|
|
252
|
-
t.end()
|
|
253
|
-
}, 5)
|
|
254
|
-
})
|
|
255
|
-
|
|
256
|
-
tape('cleanup', function (t) {
|
|
257
|
-
s.close(function () {
|
|
258
|
-
t.end()
|
|
259
|
-
})
|
|
260
|
-
})
|
package/tests/test-timing.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var server = require('./server')
|
|
4
|
-
var request = require('../index')
|
|
5
|
-
var tape = require('tape')
|
|
6
|
-
var http = require('http')
|
|
7
|
-
|
|
8
|
-
var plainServer = server.createServer()
|
|
9
|
-
var redirectMockTime = 10
|
|
10
|
-
|
|
11
|
-
tape('setup', function (t) {
|
|
12
|
-
plainServer.listen(0, function () {
|
|
13
|
-
plainServer.on('/', function (req, res) {
|
|
14
|
-
res.writeHead(200)
|
|
15
|
-
res.end('plain')
|
|
16
|
-
})
|
|
17
|
-
plainServer.on('/redir', function (req, res) {
|
|
18
|
-
// fake redirect delay to ensure strong signal for rollup check
|
|
19
|
-
setTimeout(function () {
|
|
20
|
-
res.writeHead(301, { 'location': 'http://localhost:' + plainServer.port + '/' })
|
|
21
|
-
res.end()
|
|
22
|
-
}, redirectMockTime)
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
t.end()
|
|
26
|
-
})
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
tape('non-redirected request is timed', function (t) {
|
|
30
|
-
var options = {time: true}
|
|
31
|
-
|
|
32
|
-
var start = new Date().getTime()
|
|
33
|
-
var r = request('http://localhost:' + plainServer.port + '/', options, function (err, res, body) {
|
|
34
|
-
var end = new Date().getTime()
|
|
35
|
-
|
|
36
|
-
t.equal(err, null)
|
|
37
|
-
t.equal(typeof res.elapsedTime, 'number')
|
|
38
|
-
t.equal(typeof res.responseStartTime, 'number')
|
|
39
|
-
t.equal(typeof res.timingStart, 'number')
|
|
40
|
-
t.equal((res.timingStart >= start), true)
|
|
41
|
-
t.equal(typeof res.timings, 'object')
|
|
42
|
-
t.equal((res.elapsedTime > 0), true)
|
|
43
|
-
t.equal((res.elapsedTime <= (end - start)), true)
|
|
44
|
-
t.equal((res.responseStartTime > r.startTime), true)
|
|
45
|
-
t.equal((res.timings.socket >= 0), true)
|
|
46
|
-
t.equal((res.timings.lookup >= res.timings.socket), true)
|
|
47
|
-
t.equal((res.timings.connect >= res.timings.lookup), true)
|
|
48
|
-
t.equal((res.timings.response >= res.timings.connect), true)
|
|
49
|
-
t.equal((res.timings.end >= res.timings.response), true)
|
|
50
|
-
t.equal(typeof res.timingPhases, 'object')
|
|
51
|
-
t.equal((res.timingPhases.wait >= 0), true)
|
|
52
|
-
t.equal((res.timingPhases.dns >= 0), true)
|
|
53
|
-
t.equal((res.timingPhases.tcp >= 0), true)
|
|
54
|
-
t.equal((res.timingPhases.firstByte > 0), true)
|
|
55
|
-
t.equal((res.timingPhases.download > 0), true)
|
|
56
|
-
t.equal((res.timingPhases.total > 0), true)
|
|
57
|
-
t.equal((res.timingPhases.total <= (end - start)), true)
|
|
58
|
-
|
|
59
|
-
// validate there are no unexpected properties
|
|
60
|
-
var propNames = []
|
|
61
|
-
for (var propName in res.timings) {
|
|
62
|
-
if (res.timings.hasOwnProperty(propName)) {
|
|
63
|
-
propNames.push(propName)
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
t.deepEqual(propNames, ['socket', 'lookup', 'connect', 'response', 'end'])
|
|
67
|
-
|
|
68
|
-
propNames = []
|
|
69
|
-
for (propName in res.timingPhases) {
|
|
70
|
-
if (res.timingPhases.hasOwnProperty(propName)) {
|
|
71
|
-
propNames.push(propName)
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
t.deepEqual(propNames, ['wait', 'dns', 'tcp', 'firstByte', 'download', 'total'])
|
|
75
|
-
|
|
76
|
-
t.end()
|
|
77
|
-
})
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
tape('redirected request is timed with rollup', function (t) {
|
|
81
|
-
var options = {time: true}
|
|
82
|
-
var r = request('http://localhost:' + plainServer.port + '/redir', options, function (err, res, body) {
|
|
83
|
-
t.equal(err, null)
|
|
84
|
-
t.equal(typeof res.elapsedTime, 'number')
|
|
85
|
-
t.equal(typeof res.responseStartTime, 'number')
|
|
86
|
-
t.equal((res.elapsedTime > 0), true)
|
|
87
|
-
t.equal((res.responseStartTime > 0), true)
|
|
88
|
-
t.equal((res.elapsedTime > redirectMockTime), true)
|
|
89
|
-
t.equal((res.responseStartTime > r.startTime), true)
|
|
90
|
-
t.end()
|
|
91
|
-
})
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
tape('keepAlive is timed', function (t) {
|
|
95
|
-
var agent = new http.Agent({ keepAlive: true })
|
|
96
|
-
var options = { time: true, agent: agent }
|
|
97
|
-
var start1 = new Date().getTime()
|
|
98
|
-
|
|
99
|
-
request('http://localhost:' + plainServer.port + '/', options, function (err1, res1, body1) {
|
|
100
|
-
var end1 = new Date().getTime()
|
|
101
|
-
|
|
102
|
-
// ensure the first request's timestamps look ok
|
|
103
|
-
t.equal((res1.timingStart >= start1), true)
|
|
104
|
-
t.equal((start1 <= end1), true)
|
|
105
|
-
|
|
106
|
-
t.equal((res1.timings.socket >= 0), true)
|
|
107
|
-
t.equal((res1.timings.lookup >= res1.timings.socket), true)
|
|
108
|
-
t.equal((res1.timings.connect >= res1.timings.lookup), true)
|
|
109
|
-
t.equal((res1.timings.response >= res1.timings.connect), true)
|
|
110
|
-
|
|
111
|
-
// open a second request with the same agent so we re-use the same connection
|
|
112
|
-
var start2 = new Date().getTime()
|
|
113
|
-
request('http://localhost:' + plainServer.port + '/', options, function (err2, res2, body2) {
|
|
114
|
-
var end2 = new Date().getTime()
|
|
115
|
-
|
|
116
|
-
// ensure the second request's timestamps look ok
|
|
117
|
-
t.equal((res2.timingStart >= start2), true)
|
|
118
|
-
t.equal((start2 <= end2), true)
|
|
119
|
-
|
|
120
|
-
// ensure socket==lookup==connect for the second request
|
|
121
|
-
t.equal((res2.timings.socket >= 0), true)
|
|
122
|
-
t.equal((res2.timings.lookup === res2.timings.socket), true)
|
|
123
|
-
t.equal((res2.timings.connect === res2.timings.lookup), true)
|
|
124
|
-
t.equal((res2.timings.response >= res2.timings.connect), true)
|
|
125
|
-
|
|
126
|
-
// explicitly shut down the agent
|
|
127
|
-
if (typeof agent.destroy === 'function') {
|
|
128
|
-
agent.destroy()
|
|
129
|
-
} else {
|
|
130
|
-
// node < 0.12
|
|
131
|
-
Object.keys(agent.sockets).forEach(function (name) {
|
|
132
|
-
agent.sockets[name].forEach(function (socket) {
|
|
133
|
-
socket.end()
|
|
134
|
-
})
|
|
135
|
-
})
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
t.end()
|
|
139
|
-
})
|
|
140
|
-
})
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
tape('cleanup', function (t) {
|
|
144
|
-
plainServer.close(function () {
|
|
145
|
-
t.end()
|
|
146
|
-
})
|
|
147
|
-
})
|
package/tests/test-toJSON.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var request = require('../index')
|
|
4
|
-
var http = require('http')
|
|
5
|
-
var tape = require('tape')
|
|
6
|
-
|
|
7
|
-
var s = http.createServer(function (req, resp) {
|
|
8
|
-
resp.statusCode = 200
|
|
9
|
-
resp.end('asdf')
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
tape('setup', function (t) {
|
|
13
|
-
s.listen(0, function () {
|
|
14
|
-
s.url = 'http://localhost:' + this.address().port
|
|
15
|
-
t.end()
|
|
16
|
-
})
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
tape('request().toJSON()', function (t) {
|
|
20
|
-
var r = request({
|
|
21
|
-
url: s.url,
|
|
22
|
-
headers: { foo: 'bar' }
|
|
23
|
-
}, function (err, res) {
|
|
24
|
-
var jsonR = JSON.parse(JSON.stringify(r))
|
|
25
|
-
var jsonRes = JSON.parse(JSON.stringify(res))
|
|
26
|
-
|
|
27
|
-
t.equal(err, null)
|
|
28
|
-
|
|
29
|
-
t.equal(jsonR.uri.href, r.uri.href)
|
|
30
|
-
t.equal(jsonR.method, r.method)
|
|
31
|
-
t.equal(jsonR.headers.foo, r.headers.foo)
|
|
32
|
-
|
|
33
|
-
t.equal(jsonRes.statusCode, res.statusCode)
|
|
34
|
-
t.equal(jsonRes.body, res.body)
|
|
35
|
-
t.equal(jsonRes.headers.date, res.headers.date)
|
|
36
|
-
|
|
37
|
-
t.end()
|
|
38
|
-
})
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
tape('cleanup', function (t) {
|
|
42
|
-
s.close(function () {
|
|
43
|
-
t.end()
|
|
44
|
-
})
|
|
45
|
-
})
|