@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-har.js
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var path = require('path')
|
|
4
|
-
var request = require('..')
|
|
5
|
-
var tape = require('tape')
|
|
6
|
-
var fixture = require('./fixtures/har.json')
|
|
7
|
-
var server = require('./server')
|
|
8
|
-
|
|
9
|
-
var s = server.createEchoServer()
|
|
10
|
-
|
|
11
|
-
tape('setup', function (t) {
|
|
12
|
-
s.listen(0, function () {
|
|
13
|
-
t.end()
|
|
14
|
-
})
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
tape('application-form-encoded', function (t) {
|
|
18
|
-
var options = {
|
|
19
|
-
url: s.url,
|
|
20
|
-
har: fixture['application-form-encoded']
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
request(options, function (err, res, body) {
|
|
24
|
-
var json = JSON.parse(body)
|
|
25
|
-
|
|
26
|
-
t.equal(err, null)
|
|
27
|
-
t.equal(json.body, 'foo=bar&hello=world')
|
|
28
|
-
t.end()
|
|
29
|
-
})
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
tape('application-json', function (t) {
|
|
33
|
-
var options = {
|
|
34
|
-
url: s.url,
|
|
35
|
-
har: fixture['application-json']
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
request(options, function (err, res, body) {
|
|
39
|
-
t.equal(err, null)
|
|
40
|
-
t.equal(body.body, fixture['application-json'].postData.text)
|
|
41
|
-
t.end()
|
|
42
|
-
})
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
tape('cookies', function (t) {
|
|
46
|
-
var options = {
|
|
47
|
-
url: s.url,
|
|
48
|
-
har: fixture.cookies
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
request(options, function (err, res, body) {
|
|
52
|
-
var json = JSON.parse(body)
|
|
53
|
-
|
|
54
|
-
t.equal(err, null)
|
|
55
|
-
t.equal(json.headers.cookie, 'foo=bar; bar=baz')
|
|
56
|
-
t.end()
|
|
57
|
-
})
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
tape('custom-method', function (t) {
|
|
61
|
-
var options = {
|
|
62
|
-
url: s.url,
|
|
63
|
-
har: fixture['custom-method']
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
request(options, function (err, res, body) {
|
|
67
|
-
var json = JSON.parse(body)
|
|
68
|
-
|
|
69
|
-
t.equal(err, null)
|
|
70
|
-
t.equal(json.method, fixture['custom-method'].method)
|
|
71
|
-
t.end()
|
|
72
|
-
})
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
tape('headers', function (t) {
|
|
76
|
-
var options = {
|
|
77
|
-
url: s.url,
|
|
78
|
-
har: fixture.headers
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
request(options, function (err, res, body) {
|
|
82
|
-
var json = JSON.parse(body)
|
|
83
|
-
|
|
84
|
-
t.equal(err, null)
|
|
85
|
-
t.equal(json.headers['x-foo'], 'Bar')
|
|
86
|
-
t.end()
|
|
87
|
-
})
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
tape('multipart-data', function (t) {
|
|
91
|
-
var options = {
|
|
92
|
-
url: s.url,
|
|
93
|
-
har: fixture['multipart-data']
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
request(options, function (err, res, body) {
|
|
97
|
-
var json = JSON.parse(body)
|
|
98
|
-
|
|
99
|
-
t.equal(err, null)
|
|
100
|
-
t.ok(~json.headers['content-type'].indexOf('multipart/form-data'))
|
|
101
|
-
t.ok(~json.body.indexOf('Content-Disposition: form-data; name="foo"; filename="hello.txt"\r\nContent-Type: text/plain\r\n\r\nHello World'))
|
|
102
|
-
t.end()
|
|
103
|
-
})
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
tape('multipart-file', function (t) {
|
|
107
|
-
var options = {
|
|
108
|
-
url: s.url,
|
|
109
|
-
har: fixture['multipart-file']
|
|
110
|
-
}
|
|
111
|
-
var absolutePath = path.resolve(__dirname, options.har.postData.params[0].fileName)
|
|
112
|
-
options.har.postData.params[0].fileName = absolutePath
|
|
113
|
-
|
|
114
|
-
request(options, function (err, res, body) {
|
|
115
|
-
var json = JSON.parse(body)
|
|
116
|
-
|
|
117
|
-
t.equal(err, null)
|
|
118
|
-
t.ok(~json.headers['content-type'].indexOf('multipart/form-data'))
|
|
119
|
-
t.ok(~json.body.indexOf('Content-Disposition: form-data; name="foo"; filename="unicycle.jpg"\r\nContent-Type: image/jpeg'))
|
|
120
|
-
t.end()
|
|
121
|
-
})
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
tape('multipart-form-data', function (t) {
|
|
125
|
-
var options = {
|
|
126
|
-
url: s.url,
|
|
127
|
-
har: fixture['multipart-form-data']
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
request(options, function (err, res, body) {
|
|
131
|
-
var json = JSON.parse(body)
|
|
132
|
-
|
|
133
|
-
t.equal(err, null)
|
|
134
|
-
t.ok(~json.headers['content-type'].indexOf('multipart/form-data'))
|
|
135
|
-
t.ok(~json.body.indexOf('Content-Disposition: form-data; name="foo"'))
|
|
136
|
-
t.end()
|
|
137
|
-
})
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
tape('query', function (t) {
|
|
141
|
-
var options = {
|
|
142
|
-
url: s.url + '/?fff=sss',
|
|
143
|
-
har: fixture.query
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
request(options, function (err, res, body) {
|
|
147
|
-
var json = JSON.parse(body)
|
|
148
|
-
|
|
149
|
-
t.equal(err, null)
|
|
150
|
-
t.equal(json.url, '/?fff=sss&foo%5B0%5D=bar&foo%5B1%5D=baz&baz=abc')
|
|
151
|
-
t.end()
|
|
152
|
-
})
|
|
153
|
-
})
|
|
154
|
-
|
|
155
|
-
tape('text/plain', function (t) {
|
|
156
|
-
var options = {
|
|
157
|
-
url: s.url,
|
|
158
|
-
har: fixture['text-plain']
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
request(options, function (err, res, body) {
|
|
162
|
-
var json = JSON.parse(body)
|
|
163
|
-
|
|
164
|
-
t.equal(err, null)
|
|
165
|
-
t.equal(json.headers['content-type'], 'text/plain')
|
|
166
|
-
t.equal(json.body, 'Hello World')
|
|
167
|
-
t.end()
|
|
168
|
-
})
|
|
169
|
-
})
|
|
170
|
-
|
|
171
|
-
tape('cleanup', function (t) {
|
|
172
|
-
s.close(function () {
|
|
173
|
-
t.end()
|
|
174
|
-
})
|
|
175
|
-
})
|
package/tests/test-hawk.js
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var http = require('http')
|
|
4
|
-
var request = require('../index')
|
|
5
|
-
var hawk = require('../lib/hawk')
|
|
6
|
-
var tape = require('tape')
|
|
7
|
-
var assert = require('assert')
|
|
8
|
-
|
|
9
|
-
var server = http.createServer(function (req, res) {
|
|
10
|
-
res.writeHead(200, {
|
|
11
|
-
'Content-Type': 'text/plain'
|
|
12
|
-
})
|
|
13
|
-
res.end(authenticate(req))
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
tape('setup', function (t) {
|
|
17
|
-
server.listen(0, function () {
|
|
18
|
-
server.url = 'http://localhost:' + this.address().port
|
|
19
|
-
t.end()
|
|
20
|
-
})
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
var creds = {
|
|
24
|
-
key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
|
|
25
|
-
algorithm: 'sha256',
|
|
26
|
-
id: 'dh37fgj492je'
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
tape('hawk-get', function (t) {
|
|
30
|
-
request(server.url, {
|
|
31
|
-
hawk: { credentials: creds }
|
|
32
|
-
}, function (err, res, body) {
|
|
33
|
-
t.equal(err, null)
|
|
34
|
-
t.equal(res.statusCode, 200)
|
|
35
|
-
t.equal(body, 'OK')
|
|
36
|
-
t.end()
|
|
37
|
-
})
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
tape('hawk-post', function (t) {
|
|
41
|
-
request.post({ url: server.url, body: 'hello', hawk: { credentials: creds, payload: 'hello' } }, function (err, res, body) {
|
|
42
|
-
t.equal(err, null)
|
|
43
|
-
t.equal(res.statusCode, 200)
|
|
44
|
-
t.equal(body, 'OK')
|
|
45
|
-
t.end()
|
|
46
|
-
})
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
tape('hawk-ext', function (t) {
|
|
50
|
-
request(server.url, {
|
|
51
|
-
hawk: { credentials: creds, ext: 'test' }
|
|
52
|
-
}, function (err, res, body) {
|
|
53
|
-
t.equal(err, null)
|
|
54
|
-
t.equal(res.statusCode, 200)
|
|
55
|
-
t.equal(body, 'OK')
|
|
56
|
-
t.end()
|
|
57
|
-
})
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
tape('hawk-app', function (t) {
|
|
61
|
-
request(server.url, {
|
|
62
|
-
hawk: { credentials: creds, app: 'test' }
|
|
63
|
-
}, function (err, res, body) {
|
|
64
|
-
t.equal(err, null)
|
|
65
|
-
t.equal(res.statusCode, 200)
|
|
66
|
-
t.equal(body, 'OK')
|
|
67
|
-
t.end()
|
|
68
|
-
})
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
tape('hawk-app+dlg', function (t) {
|
|
72
|
-
request(server.url, {
|
|
73
|
-
hawk: { credentials: creds, app: 'test', dlg: 'asd' }
|
|
74
|
-
}, function (err, res, body) {
|
|
75
|
-
t.equal(err, null)
|
|
76
|
-
t.equal(res.statusCode, 200)
|
|
77
|
-
t.equal(body, 'OK')
|
|
78
|
-
t.end()
|
|
79
|
-
})
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
tape('hawk-missing-creds', function (t) {
|
|
83
|
-
request(server.url, {
|
|
84
|
-
hawk: {}
|
|
85
|
-
}, function (err, res, body) {
|
|
86
|
-
t.equal(err, null)
|
|
87
|
-
t.equal(res.statusCode, 200)
|
|
88
|
-
t.equal(body, 'FAIL')
|
|
89
|
-
t.end()
|
|
90
|
-
})
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
tape('hawk-missing-creds-id', function (t) {
|
|
94
|
-
request(server.url, {
|
|
95
|
-
hawk: {
|
|
96
|
-
credentials: {}
|
|
97
|
-
}
|
|
98
|
-
}, function (err, res, body) {
|
|
99
|
-
t.equal(err, null)
|
|
100
|
-
t.equal(res.statusCode, 200)
|
|
101
|
-
t.equal(body, 'FAIL')
|
|
102
|
-
t.end()
|
|
103
|
-
})
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
tape('hawk-missing-creds-key', function (t) {
|
|
107
|
-
request(server.url, {
|
|
108
|
-
hawk: {
|
|
109
|
-
credentials: { id: 'asd' }
|
|
110
|
-
}
|
|
111
|
-
}, function (err, res, body) {
|
|
112
|
-
t.equal(err, null)
|
|
113
|
-
t.equal(res.statusCode, 200)
|
|
114
|
-
t.equal(body, 'FAIL')
|
|
115
|
-
t.end()
|
|
116
|
-
})
|
|
117
|
-
})
|
|
118
|
-
|
|
119
|
-
tape('hawk-missing-creds-algo', function (t) {
|
|
120
|
-
request(server.url, {
|
|
121
|
-
hawk: {
|
|
122
|
-
credentials: { key: '123', id: '123' }
|
|
123
|
-
}
|
|
124
|
-
}, function (err, res, body) {
|
|
125
|
-
t.equal(err, null)
|
|
126
|
-
t.equal(res.statusCode, 200)
|
|
127
|
-
t.equal(body, 'FAIL')
|
|
128
|
-
t.end()
|
|
129
|
-
})
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
tape('hawk-invalid-creds-algo', function (t) {
|
|
133
|
-
request(server.url, {
|
|
134
|
-
hawk: {
|
|
135
|
-
credentials: { key: '123', id: '123', algorithm: 'xx' }
|
|
136
|
-
}
|
|
137
|
-
}, function (err, res, body) {
|
|
138
|
-
t.equal(err, null)
|
|
139
|
-
t.equal(res.statusCode, 200)
|
|
140
|
-
t.equal(body, 'FAIL')
|
|
141
|
-
t.end()
|
|
142
|
-
})
|
|
143
|
-
})
|
|
144
|
-
|
|
145
|
-
tape('cleanup', function (t) {
|
|
146
|
-
server.close(function () {
|
|
147
|
-
t.end()
|
|
148
|
-
})
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
function authenticate (req) {
|
|
152
|
-
if (!req.headers.authorization) {
|
|
153
|
-
return 'FAIL'
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
var headerParts = req.headers.authorization.match(/^(\w+)(?:\s+(.*))?$/)
|
|
157
|
-
assert.equal(headerParts[1], 'Hawk')
|
|
158
|
-
var attributes = {}
|
|
159
|
-
headerParts[2].replace(/(\w+)="([^"\\]*)"\s*(?:,\s*|$)/g, function ($0, $1, $2) { attributes[$1] = $2 })
|
|
160
|
-
var hostParts = req.headers.host.split(':')
|
|
161
|
-
|
|
162
|
-
const artifacts = {
|
|
163
|
-
method: req.method,
|
|
164
|
-
host: hostParts[0],
|
|
165
|
-
port: (hostParts[1] ? hostParts[1] : (req.connection && req.connection.encrypted ? 443 : 80)),
|
|
166
|
-
resource: req.url,
|
|
167
|
-
ts: attributes.ts,
|
|
168
|
-
nonce: attributes.nonce,
|
|
169
|
-
hash: attributes.hash,
|
|
170
|
-
ext: attributes.ext,
|
|
171
|
-
app: attributes.app,
|
|
172
|
-
dlg: attributes.dlg,
|
|
173
|
-
mac: attributes.mac,
|
|
174
|
-
id: attributes.id
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
assert.equal(attributes.id, 'dh37fgj492je')
|
|
178
|
-
var credentials = {
|
|
179
|
-
key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
|
|
180
|
-
algorithm: 'sha256',
|
|
181
|
-
user: 'Steve'
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
const mac = hawk.calculateMac(credentials, artifacts)
|
|
185
|
-
assert.equal(mac, attributes.mac)
|
|
186
|
-
return 'OK'
|
|
187
|
-
}
|
package/tests/test-headers.js
DELETED
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var server = require('./server')
|
|
4
|
-
var request = require('../index')
|
|
5
|
-
var util = require('util')
|
|
6
|
-
var tape = require('tape')
|
|
7
|
-
var url = require('url')
|
|
8
|
-
var os = require('os')
|
|
9
|
-
|
|
10
|
-
var interfaces = os.networkInterfaces()
|
|
11
|
-
var loopbackKeyTest = os.platform() === 'win32' ? /Loopback Pseudo-Interface/ : /lo/
|
|
12
|
-
var hasIPv6interface = Object.keys(interfaces).some(function (name) {
|
|
13
|
-
return loopbackKeyTest.test(name) && interfaces[name].some(function (info) {
|
|
14
|
-
return info.family === 'IPv6'
|
|
15
|
-
})
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
var s = server.createServer()
|
|
19
|
-
|
|
20
|
-
s.on('/redirect/from', function (req, res) {
|
|
21
|
-
res.writeHead(301, {
|
|
22
|
-
location: '/redirect/to'
|
|
23
|
-
})
|
|
24
|
-
res.end()
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
s.on('/redirect/to', function (req, res) {
|
|
28
|
-
res.end('ok')
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
s.on('/headers.json', function (req, res) {
|
|
32
|
-
res.writeHead(200, {
|
|
33
|
-
'Content-Type': 'application/json'
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
res.end(JSON.stringify(req.headers))
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
function runTest (name, path, requestObj, serverAssertFn) {
|
|
40
|
-
tape(name, function (t) {
|
|
41
|
-
s.on('/' + path, function (req, res) {
|
|
42
|
-
serverAssertFn(t, req, res)
|
|
43
|
-
res.writeHead(200)
|
|
44
|
-
res.end()
|
|
45
|
-
})
|
|
46
|
-
requestObj.url = s.url + '/' + path
|
|
47
|
-
request(requestObj, function (err, res, body) {
|
|
48
|
-
t.equal(err, null)
|
|
49
|
-
t.equal(res.statusCode, 200)
|
|
50
|
-
t.end()
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function addTests () {
|
|
56
|
-
runTest(
|
|
57
|
-
'#125: headers.cookie with no cookie jar',
|
|
58
|
-
'no-jar',
|
|
59
|
-
{headers: {cookie: 'foo=bar'}},
|
|
60
|
-
function (t, req, res) {
|
|
61
|
-
t.equal(req.headers.cookie, 'foo=bar')
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
var jar = request.jar()
|
|
65
|
-
jar.setCookie('quux=baz', s.url)
|
|
66
|
-
runTest(
|
|
67
|
-
'#125: headers.cookie + cookie jar',
|
|
68
|
-
'header-and-jar',
|
|
69
|
-
{jar: jar, headers: {cookie: 'foo=bar'}},
|
|
70
|
-
function (t, req, res) {
|
|
71
|
-
t.equal(req.headers.cookie, 'foo=bar; quux=baz')
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
var jar2 = request.jar()
|
|
75
|
-
jar2.setCookie('quux=baz; Domain=foo.bar.com', s.url, {ignoreError: true})
|
|
76
|
-
runTest(
|
|
77
|
-
'#794: ignore cookie parsing and domain errors',
|
|
78
|
-
'ignore-errors',
|
|
79
|
-
{jar: jar2, headers: {cookie: 'foo=bar'}},
|
|
80
|
-
function (t, req, res) {
|
|
81
|
-
t.equal(req.headers.cookie, 'foo=bar')
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
runTest(
|
|
85
|
-
'#784: override content-type when json is used',
|
|
86
|
-
'json',
|
|
87
|
-
{
|
|
88
|
-
json: true,
|
|
89
|
-
method: 'POST',
|
|
90
|
-
headers: { 'content-type': 'application/json; charset=UTF-8' },
|
|
91
|
-
body: { hello: 'my friend' }},
|
|
92
|
-
function (t, req, res) {
|
|
93
|
-
t.equal(req.headers['content-type'], 'application/json; charset=UTF-8')
|
|
94
|
-
}
|
|
95
|
-
)
|
|
96
|
-
|
|
97
|
-
runTest(
|
|
98
|
-
'neither headers.cookie nor a cookie jar is specified',
|
|
99
|
-
'no-cookie',
|
|
100
|
-
{},
|
|
101
|
-
function (t, req, res) {
|
|
102
|
-
t.equal(req.headers.cookie, undefined)
|
|
103
|
-
})
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
tape('setup', function (t) {
|
|
107
|
-
s.listen(0, function () {
|
|
108
|
-
addTests()
|
|
109
|
-
tape('cleanup', function (t) {
|
|
110
|
-
s.close(function () {
|
|
111
|
-
t.end()
|
|
112
|
-
})
|
|
113
|
-
})
|
|
114
|
-
t.end()
|
|
115
|
-
})
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
tape('upper-case Host header and redirect', function (t) {
|
|
119
|
-
// Horrible hack to observe the raw data coming to the server (before Node
|
|
120
|
-
// core lower-cases the headers)
|
|
121
|
-
var rawData = ''
|
|
122
|
-
|
|
123
|
-
s.on('connection', function (socket) {
|
|
124
|
-
if (socket.ondata) {
|
|
125
|
-
var ondata = socket.ondata
|
|
126
|
-
}
|
|
127
|
-
function handledata (d, start, end) {
|
|
128
|
-
if (ondata) {
|
|
129
|
-
rawData += d.slice(start, end).toString()
|
|
130
|
-
return ondata.apply(this, arguments)
|
|
131
|
-
} else {
|
|
132
|
-
rawData += d
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
socket.on('data', handledata)
|
|
136
|
-
socket.ondata = handledata
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
function checkHostHeader (host) {
|
|
140
|
-
t.ok(
|
|
141
|
-
new RegExp('^Host: ' + host + '$', 'm').test(rawData),
|
|
142
|
-
util.format(
|
|
143
|
-
'Expected "Host: %s" in data "%s"',
|
|
144
|
-
host, rawData.trim().replace(/\r?\n/g, '\\n')))
|
|
145
|
-
rawData = ''
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
var redirects = 0
|
|
149
|
-
request({
|
|
150
|
-
url: s.url + '/redirect/from',
|
|
151
|
-
headers: { Host: '127.0.0.1' }
|
|
152
|
-
}, function (err, res, body) {
|
|
153
|
-
t.equal(err, null)
|
|
154
|
-
t.equal(res.statusCode, 200)
|
|
155
|
-
t.equal(body, 'ok')
|
|
156
|
-
t.equal(redirects, 1)
|
|
157
|
-
// XXX should the host header change like this after a redirect?
|
|
158
|
-
checkHostHeader('localhost:' + s.port)
|
|
159
|
-
t.end()
|
|
160
|
-
}).on('redirect', function () {
|
|
161
|
-
redirects++
|
|
162
|
-
t.equal(this.uri.href, s.url + '/redirect/to')
|
|
163
|
-
checkHostHeader('127.0.0.1')
|
|
164
|
-
})
|
|
165
|
-
})
|
|
166
|
-
|
|
167
|
-
tape('undefined headers', function (t) {
|
|
168
|
-
request({
|
|
169
|
-
url: s.url + '/headers.json',
|
|
170
|
-
headers: {
|
|
171
|
-
'X-TEST-1': 'test1',
|
|
172
|
-
'X-TEST-2': undefined
|
|
173
|
-
},
|
|
174
|
-
json: true
|
|
175
|
-
}, function (err, res, body) {
|
|
176
|
-
t.equal(err, null)
|
|
177
|
-
t.equal(body['x-test-1'], 'test1')
|
|
178
|
-
t.equal(typeof body['x-test-2'], 'undefined')
|
|
179
|
-
t.end()
|
|
180
|
-
})
|
|
181
|
-
})
|
|
182
|
-
|
|
183
|
-
tape('preserve port in host header if non-standard port', function (t) {
|
|
184
|
-
var r = request({
|
|
185
|
-
url: s.url + '/headers.json'
|
|
186
|
-
}, function (err, res, body) {
|
|
187
|
-
t.equal(err, null)
|
|
188
|
-
t.equal(r.originalHost, 'localhost:' + s.port)
|
|
189
|
-
t.end()
|
|
190
|
-
})
|
|
191
|
-
})
|
|
192
|
-
|
|
193
|
-
tape('strip port in host header if explicit standard port (:80) & protocol (HTTP)', function (t) {
|
|
194
|
-
var r = request({
|
|
195
|
-
url: 'http://localhost:80/headers.json'
|
|
196
|
-
}, function (_err, res, body) {
|
|
197
|
-
t.equal(r.req.socket._host, 'localhost')
|
|
198
|
-
t.end()
|
|
199
|
-
})
|
|
200
|
-
})
|
|
201
|
-
|
|
202
|
-
tape('strip port in host header if explicit standard port (:443) & protocol (HTTPS)', function (t) {
|
|
203
|
-
var r = request({
|
|
204
|
-
url: 'https://localhost:443/headers.json'
|
|
205
|
-
}, function (_err, res, body) {
|
|
206
|
-
t.equal(r.req.socket._host, 'localhost')
|
|
207
|
-
t.end()
|
|
208
|
-
})
|
|
209
|
-
})
|
|
210
|
-
|
|
211
|
-
tape('strip port in host header if implicit standard port & protocol (HTTP)', function (t) {
|
|
212
|
-
var r = request({
|
|
213
|
-
url: 'http://localhost/headers.json'
|
|
214
|
-
}, function (_err, res, body) {
|
|
215
|
-
t.equal(r.req.socket._host, 'localhost')
|
|
216
|
-
t.end()
|
|
217
|
-
})
|
|
218
|
-
})
|
|
219
|
-
|
|
220
|
-
tape('strip port in host header if implicit standard port & protocol (HTTPS)', function (t) {
|
|
221
|
-
var r = request({
|
|
222
|
-
url: 'https://localhost/headers.json'
|
|
223
|
-
}, function (_err, res, body) {
|
|
224
|
-
t.equal(r.req.socket._host, 'localhost')
|
|
225
|
-
t.end()
|
|
226
|
-
})
|
|
227
|
-
})
|
|
228
|
-
|
|
229
|
-
var isExpectedHeaderCharacterError = function (headerName, err) {
|
|
230
|
-
return err.message === 'The header content contains invalid characters' ||
|
|
231
|
-
err.message === ('Invalid character in header content ["' + headerName + '"]')
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
tape('catch invalid characters error - GET', function (t) {
|
|
235
|
-
request({
|
|
236
|
-
url: s.url + '/headers.json',
|
|
237
|
-
headers: {
|
|
238
|
-
'test': 'אבגד'
|
|
239
|
-
}
|
|
240
|
-
}, function (err, res, body) {
|
|
241
|
-
t.true(isExpectedHeaderCharacterError('test', err))
|
|
242
|
-
})
|
|
243
|
-
.on('error', function (err) {
|
|
244
|
-
t.true(isExpectedHeaderCharacterError('test', err))
|
|
245
|
-
t.end()
|
|
246
|
-
})
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
tape('catch invalid characters error - POST', function (t) {
|
|
250
|
-
request({
|
|
251
|
-
method: 'POST',
|
|
252
|
-
url: s.url + '/headers.json',
|
|
253
|
-
headers: {
|
|
254
|
-
'test': 'אבגד'
|
|
255
|
-
},
|
|
256
|
-
body: 'beep'
|
|
257
|
-
}, function (err, res, body) {
|
|
258
|
-
t.true(isExpectedHeaderCharacterError('test', err))
|
|
259
|
-
})
|
|
260
|
-
.on('error', function (err) {
|
|
261
|
-
t.true(isExpectedHeaderCharacterError('test', err))
|
|
262
|
-
t.end()
|
|
263
|
-
})
|
|
264
|
-
})
|
|
265
|
-
|
|
266
|
-
if (hasIPv6interface) {
|
|
267
|
-
tape('IPv6 Host header', function (t) {
|
|
268
|
-
// Horrible hack to observe the raw data coming to the server
|
|
269
|
-
var rawData = ''
|
|
270
|
-
|
|
271
|
-
s.on('connection', function (socket) {
|
|
272
|
-
if (socket.ondata) {
|
|
273
|
-
var ondata = socket.ondata
|
|
274
|
-
}
|
|
275
|
-
function handledata (d, start, end) {
|
|
276
|
-
if (ondata) {
|
|
277
|
-
rawData += d.slice(start, end).toString()
|
|
278
|
-
return ondata.apply(this, arguments)
|
|
279
|
-
} else {
|
|
280
|
-
rawData += d
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
socket.on('data', handledata)
|
|
284
|
-
socket.ondata = handledata
|
|
285
|
-
})
|
|
286
|
-
|
|
287
|
-
function checkHostHeader (host) {
|
|
288
|
-
t.ok(
|
|
289
|
-
new RegExp('^Host: ' + host + '$', 'im').test(rawData),
|
|
290
|
-
util.format(
|
|
291
|
-
'Expected "Host: %s" in data "%s"',
|
|
292
|
-
host, rawData.trim().replace(/\r?\n/g, '\\n')))
|
|
293
|
-
rawData = ''
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
request({
|
|
297
|
-
url: s.url.replace(url.parse(s.url).hostname, '[::1]') + '/headers.json'
|
|
298
|
-
}, function (err, res, body) {
|
|
299
|
-
t.equal(err, null)
|
|
300
|
-
t.equal(res.statusCode, 200)
|
|
301
|
-
checkHostHeader('\\[::1\\]:' + s.port)
|
|
302
|
-
t.end()
|
|
303
|
-
})
|
|
304
|
-
})
|
|
305
|
-
}
|