Tizen 2.1 base
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / testswarm / node_modules / request / tests / test-redirect.js
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')
6
7 var s = server.createServer()
8
9 s.listen(s.port, function () {
10   var server = 'http://localhost:' + s.port;
11   var hits = {}
12   var passed = 0;
13
14   bouncer(301, 'temp')
15   bouncer(302, 'perm')
16   bouncer(302, 'nope')
17
18   function bouncer(code, label) {
19     var landing = label+'_landing';
20
21     s.on('/'+label, function (req, res) {
22       hits[label] = true;
23       res.writeHead(code, {
24         'location':server + '/'+landing,
25         'set-cookie': 'ham=eggs'
26       })
27       res.end()
28     })
29
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");
33         res.writeHead(400);
34         res.end();
35         return;
36       }
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');
40       hits[landing] = true;
41       res.writeHead(200)
42       res.end(landing)
43     })
44   }
45
46   // Permanent bounce
47   var jar = new Jar()
48   jar.add(new Cookie('quux=baz'))
49   request({uri: server+'/perm', jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
50     if (er) throw er
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')
55     passed += 1
56     done()
57   })
58   
59   // Temporary bounce
60   request({uri: server+'/temp', jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
61     if (er) throw er
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')
66     passed += 1
67     done()
68   })
69   
70   // Prevent bouncing.
71   request({uri:server+'/nope', jar: jar, headers: {cookie: 'foo=bar'}, followRedirect:false}, function (er, res, body) {
72     if (er) throw er
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')
77     passed += 1
78     done()
79   })
80   
81   // Should not follow post redirects by default
82   request.post(server+'/temp', { jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
83     if (er) throw er
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')
88     passed += 1
89     done()
90   })
91   
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) {
94     if (er) throw er
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')
99     passed += 1
100     done()
101   })
102   
103   request.post({uri:server+'/temp', followAllRedirects:false, jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
104     if (er) throw er
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')
109     passed += 1
110     done()
111   })
112
113   // Should not follow delete redirects by default
114   request.del(server+'/temp', { jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
115     if (er) throw er
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')
120     passed += 1
121     done()
122   })
123   
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) {
126     if (er) throw er
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')
131     passed += 1
132     done()
133   })
134   
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) {
137     if (er) throw er
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')
142     passed += 1
143     done()
144   })
145
146   var reqs_done = 0;
147   function done() {
148     reqs_done += 1;
149     if(reqs_done == 9) {
150       console.log(passed + ' tests passed.')
151       s.close()
152     }
153   }
154 })