1 var server = require('./server')
2 , assert = require('assert')
3 , request = require('../main.js')
4 , Cookie = require('../vendor/cookie')
5 , Jar = require('../vendor/cookie/jar')
7 var s = server.createServer()
9 s.listen(s.port, function () {
10 var server = 'http://localhost:' + s.port;
18 function bouncer(code, label) {
19 var landing = label+'_landing';
21 s.on('/'+label, function (req, res) {
24 'location':server + '/'+landing,
25 'set-cookie': 'ham=eggs'
30 s.on('/'+landing, function (req, res) {
31 if (req.method !== 'GET') { // We should only accept GET redirects
32 console.error("Got a non-GET request to the redirect destination URL");
37 // Make sure the cookie doesn't get included twice, see #139:
38 // Make sure cookies are set properly after redirect
39 assert.equal(req.headers.cookie, 'foo=bar; quux=baz; ham=eggs');
48 jar.add(new Cookie('quux=baz'))
49 request({uri: server+'/perm', jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
51 if (res.statusCode !== 200) throw new Error('Status is not 200: '+res.statusCode)
52 assert.ok(hits.perm, 'Original request is to /perm')
53 assert.ok(hits.perm_landing, 'Forward to permanent landing URL')
54 assert.equal(body, 'perm_landing', 'Got permanent landing content')
60 request({uri: server+'/temp', jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
62 if (res.statusCode !== 200) throw new Error('Status is not 200: '+res.statusCode)
63 assert.ok(hits.temp, 'Original request is to /temp')
64 assert.ok(hits.temp_landing, 'Forward to temporary landing URL')
65 assert.equal(body, 'temp_landing', 'Got temporary landing content')
71 request({uri:server+'/nope', jar: jar, headers: {cookie: 'foo=bar'}, followRedirect:false}, function (er, res, body) {
73 if (res.statusCode !== 302) throw new Error('Status is not 302: '+res.statusCode)
74 assert.ok(hits.nope, 'Original request to /nope')
75 assert.ok(!hits.nope_landing, 'No chasing the redirect')
76 assert.equal(res.statusCode, 302, 'Response is the bounce itself')
81 // Should not follow post redirects by default
82 request.post(server+'/temp', { jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
84 if (res.statusCode !== 301) throw new Error('Status is not 301: '+res.statusCode)
85 assert.ok(hits.temp, 'Original request is to /temp')
86 assert.ok(!hits.temp_landing, 'No chasing the redirect when post')
87 assert.equal(res.statusCode, 301, 'Response is the bounce itself')
92 // Should follow post redirects when followAllRedirects true
93 request.post({uri:server+'/temp', followAllRedirects:true, jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
95 if (res.statusCode !== 200) throw new Error('Status is not 200: '+res.statusCode)
96 assert.ok(hits.temp, 'Original request is to /temp')
97 assert.ok(hits.temp_landing, 'Forward to temporary landing URL')
98 assert.equal(body, 'temp_landing', 'Got temporary landing content')
103 request.post({uri:server+'/temp', followAllRedirects:false, jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
105 if (res.statusCode !== 301) throw new Error('Status is not 301: '+res.statusCode)
106 assert.ok(hits.temp, 'Original request is to /temp')
107 assert.ok(!hits.temp_landing, 'No chasing the redirect')
108 assert.equal(res.statusCode, 301, 'Response is the bounce itself')
113 // Should not follow delete redirects by default
114 request.del(server+'/temp', { jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
116 if (res.statusCode < 301) throw new Error('Status is not a redirect.')
117 assert.ok(hits.temp, 'Original request is to /temp')
118 assert.ok(!hits.temp_landing, 'No chasing the redirect when delete')
119 assert.equal(res.statusCode, 301, 'Response is the bounce itself')
124 // Should not follow delete redirects even if followRedirect is set to true
125 request.del(server+'/temp', { followRedirect: true, jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
127 if (res.statusCode !== 301) throw new Error('Status is not 301: '+res.statusCode)
128 assert.ok(hits.temp, 'Original request is to /temp')
129 assert.ok(!hits.temp_landing, 'No chasing the redirect when delete')
130 assert.equal(res.statusCode, 301, 'Response is the bounce itself')
135 // Should follow delete redirects when followAllRedirects true
136 request.del(server+'/temp', {followAllRedirects:true, jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
138 if (res.statusCode !== 200) throw new Error('Status is not 200: '+res.statusCode)
139 assert.ok(hits.temp, 'Original request is to /temp')
140 assert.ok(hits.temp_landing, 'Forward to temporary landing URL')
141 assert.equal(body, 'temp_landing', 'Got temporary landing content')
150 console.log(passed + ' tests passed.')