@wiajs/request 3.0.0 → 3.0.1
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/.github/ISSUE_TEMPLATE.md +56 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +13 -0
- package/.github/stale.yml +19 -0
- package/.swcrc +57 -0
- package/.travis.yml +21 -0
- package/CHANGELOG.md +717 -0
- package/CONTRIBUTING.md +81 -0
- package/biome.json +44 -0
- package/codecov.yml +2 -0
- package/disabled.appveyor.yml +36 -0
- package/dist/request.cjs +1476 -0
- package/dist/request.mjs +1474 -0
- package/examples/README.md +135 -0
- package/gulpfile.js +71 -0
- package/package.json +1 -2
- package/release.sh +45 -0
- package/tests/browser/karma.conf.js +57 -0
- package/tests/browser/ssl/ca.crt +14 -0
- package/tests/browser/ssl/server.crt +14 -0
- package/tests/browser/ssl/server.key +15 -0
- package/tests/browser/start.js +37 -0
- package/tests/browser/test.js +34 -0
- package/tests/fixtures/har.json +158 -0
- package/tests/googledoodle.jpg +0 -0
- package/tests/server.js +142 -0
- package/tests/squid.conf +76 -0
- package/tests/ssl/ca/README.md +8 -0
- package/tests/ssl/ca/ca.cnf +20 -0
- package/tests/ssl/ca/ca.crl +0 -0
- package/tests/ssl/ca/ca.crt +17 -0
- package/tests/ssl/ca/ca.csr +13 -0
- package/tests/ssl/ca/ca.key +18 -0
- package/tests/ssl/ca/ca.srl +1 -0
- package/tests/ssl/ca/client-enc.key +30 -0
- package/tests/ssl/ca/client.cnf +20 -0
- package/tests/ssl/ca/client.crt +20 -0
- package/tests/ssl/ca/client.csr +18 -0
- package/tests/ssl/ca/client.key +27 -0
- package/tests/ssl/ca/gen-all-certs.sh +6 -0
- package/tests/ssl/ca/gen-client.sh +25 -0
- package/tests/ssl/ca/gen-localhost.sh +22 -0
- package/tests/ssl/ca/gen-server.sh +18 -0
- package/tests/ssl/ca/localhost.cnf +20 -0
- package/tests/ssl/ca/localhost.crt +20 -0
- package/tests/ssl/ca/localhost.csr +18 -0
- package/tests/ssl/ca/localhost.js +33 -0
- package/tests/ssl/ca/localhost.key +27 -0
- package/tests/ssl/ca/server.cnf +19 -0
- package/tests/ssl/ca/server.crt +25 -0
- package/tests/ssl/ca/server.csr +29 -0
- package/tests/ssl/ca/server.js +34 -0
- package/tests/ssl/ca/server.key +51 -0
- package/tests/ssl/npm-ca.crt +16 -0
- package/tests/ssl/test.crt +15 -0
- package/tests/ssl/test.key +15 -0
- package/tests/test-agent.js +102 -0
- package/tests/test-agentOptions.js +51 -0
- package/tests/test-api.js +33 -0
- package/tests/test-aws.js +123 -0
- package/tests/test-baseUrl.js +133 -0
- package/tests/test-basic-auth.js +221 -0
- package/tests/test-bearer-auth.js +187 -0
- package/tests/test-body.js +154 -0
- package/tests/test-cookies.js +130 -0
- package/tests/test-defaults.js +340 -0
- package/tests/test-digest-auth.js +232 -0
- package/tests/test-emptyBody.js +56 -0
- package/tests/test-errors.js +108 -0
- package/tests/test-event-forwarding.js +39 -0
- package/tests/test-follow-all-303.js +45 -0
- package/tests/test-follow-all.js +57 -0
- package/tests/test-form-data-error.js +85 -0
- package/tests/test-form-data.js +133 -0
- package/tests/test-form-urlencoded.js +73 -0
- package/tests/test-form.js +101 -0
- package/tests/test-gzip.js +296 -0
- package/tests/test-har.js +175 -0
- package/tests/test-hawk.js +187 -0
- package/tests/test-headers.js +305 -0
- package/tests/test-http-signature.js +110 -0
- package/tests/test-httpModule.js +112 -0
- package/tests/test-https.js +116 -0
- package/tests/test-isUrl.js +120 -0
- package/tests/test-json-request.js +117 -0
- package/tests/test-localAddress.js +49 -0
- package/tests/test-multipart-encoding.js +147 -0
- package/tests/test-multipart.js +129 -0
- package/tests/test-node-debug.js +95 -0
- package/tests/test-oauth.js +721 -0
- package/tests/test-onelineproxy.js +61 -0
- package/tests/test-option-reuse.js +54 -0
- package/tests/test-options-convenience-method.js +52 -0
- package/tests/test-params.js +101 -0
- package/tests/test-piped-redirect.js +55 -0
- package/tests/test-pipes.js +383 -0
- package/tests/test-pool.js +148 -0
- package/tests/test-promise.js +53 -0
- package/tests/test-proxy-connect.js +80 -0
- package/tests/test-proxy.js +304 -0
- package/tests/test-qs.js +135 -0
- package/tests/test-redirect-auth.js +131 -0
- package/tests/test-redirect-complex.js +93 -0
- package/tests/test-redirect.js +449 -0
- package/tests/test-rfc3986.js +106 -0
- package/tests/test-stream.js +36 -0
- package/tests/test-timeout.js +260 -0
- package/tests/test-timing.js +147 -0
- package/tests/test-toJSON.js +45 -0
- package/tests/test-tunnel.js +466 -0
- package/tests/test-unix.js +74 -0
- package/tests/unicycle.jpg +0 -0
- package/request.js +0 -1553
- package/src/ZlibTransform.js +0 -27
- package/src/caseless.js +0 -118
- package/src/index.js +0 -122
- package/src/request.js +0 -967
- package/src/utils.js +0 -274
|
@@ -0,0 +1,296 @@
|
|
|
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
|
+
})
|
|
@@ -0,0 +1,175 @@
|
|
|
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
|
+
})
|
|
@@ -0,0 +1,187 @@
|
|
|
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
|
+
}
|