forked from request/request
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-headers.js
More file actions
61 lines (55 loc) · 1.86 KB
/
Copy pathtest-headers.js
File metadata and controls
61 lines (55 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
try {
require('tough-cookie')
} catch (e) {
console.error('tough-cookie must be installed to run this test.')
console.error('skipping this test. please install tough-cookie and run again if you need to test this feature.')
process.exit(0)
}
var server = require('./server')
, assert = require('assert')
, request = require('../index')
, Cookie = require('tough-cookie')
, Jar = Cookie.CookieJar
, s = server.createServer()
s.listen(s.port, function () {
var serverUri = 'http://localhost:' + s.port
, numTests = 0
, numOutstandingTests = 0
function createTest(requestObj, serverAssertFn) {
var testNumber = numTests;
numTests += 1;
numOutstandingTests += 1;
s.on('/' + testNumber, function (req, res) {
serverAssertFn(req, res);
res.writeHead(200);
res.end();
});
requestObj.url = serverUri + '/' + testNumber
request(requestObj, function (err, res, body) {
assert.ok(!err)
assert.equal(res.statusCode, 200)
numOutstandingTests -= 1
if (numOutstandingTests === 0) {
console.log(numTests + ' tests passed.')
s.close()
}
})
}
// Issue #125: headers.cookie shouldn't be replaced when a cookie jar isn't specified
createTest({headers: {cookie: 'foo=bar'}}, function (req, res) {
assert.ok(req.headers.cookie)
assert.equal(req.headers.cookie, 'foo=bar')
})
// Issue #125: headers.cookie + cookie jar
//using new cookie module
var jar = new Jar()
jar.setCookie('quux=baz', serverUri, function(){});
createTest({jar: jar, headers: {cookie: 'foo=bar'}}, function (req, res) {
assert.ok(req.headers.cookie)
assert.equal(req.headers.cookie, 'foo=bar; quux=baz')
})
// There should be no cookie header when neither headers.cookie nor a cookie jar is specified
createTest({}, function (req, res) {
assert.ok(!req.headers.cookie)
})
})