7cf1d6d82066a5882e649bfe01c874d286dbd81d
[platform/upstream/nodejs.git] / deps / npm / node_modules / request / tests / test-basic-auth.js
1 var assert = require('assert')
2   , http = require('http')
3   , request = require('../index')
4   ;
5
6 var numBasicRequests = 0;
7
8 var basicServer = http.createServer(function (req, res) {
9   console.error('Basic auth server: ', req.method, req.url);
10   numBasicRequests++;
11
12   var ok;
13
14   if (req.headers.authorization) {
15     if (req.headers.authorization == 'Basic ' + new Buffer('test:testing2').toString('base64')) {
16       ok = true;
17     } else if ( req.headers.authorization == 'Basic ' + new Buffer(':apassword').toString('base64')) {
18       ok = true;
19     } else if ( req.headers.authorization == 'Basic ' + new Buffer('justauser').toString('base64')) {
20       ok = true;
21     } else {
22       // Bad auth header, don't send back WWW-Authenticate header
23       ok = false;
24     }
25   } else {
26     // No auth header, send back WWW-Authenticate header
27     ok = false;
28     res.setHeader('www-authenticate', 'Basic realm="Private"');
29   }
30
31   if (req.url == '/post/') {
32     var expectedContent = 'data_key=data_value';
33     req.on('data', function(data) {
34       assert.equal(data, expectedContent);
35       console.log('received request data: ' + data);
36     });
37     assert.equal(req.method, 'POST');
38     assert.equal(req.headers['content-length'], '' + expectedContent.length);
39     assert.equal(req.headers['content-type'], 'application/x-www-form-urlencoded; charset=utf-8');
40   }
41
42   if (ok) {
43     console.log('request ok');
44     res.end('ok');
45   } else {
46     console.log('status=401');
47     res.statusCode = 401;
48     res.end('401');
49   }
50 });
51
52 basicServer.listen(6767);
53
54 var tests = [
55   function(next) {
56     request({
57       'method': 'GET',
58       'uri': 'http://localhost:6767/test/',
59       'auth': {
60         'user': 'test',
61         'pass': 'testing2',
62         'sendImmediately': false
63       }
64     }, function(error, res, body) {
65       assert.equal(res.statusCode, 200);
66       assert.equal(numBasicRequests, 2);
67       next();
68     });
69   },
70
71   function(next) {
72     // If we don't set sendImmediately = false, request will send basic auth
73     request({
74       'method': 'GET',
75       'uri': 'http://localhost:6767/test2/',
76       'auth': {
77         'user': 'test',
78         'pass': 'testing2'
79       }
80     }, function(error, res, body) {
81       assert.equal(res.statusCode, 200);
82       assert.equal(numBasicRequests, 3);
83       next();
84     });
85   },
86
87   function(next) {
88     request({
89       'method': 'GET',
90       'uri': 'http://test:testing2@localhost:6767/test2/'
91     }, function(error, res, body) {
92       assert.equal(res.statusCode, 200);
93       assert.equal(numBasicRequests, 4);
94       next();
95     });
96   },
97
98   function(next) {
99     request({
100       'method': 'POST',
101       'form': { 'data_key': 'data_value' },
102       'uri': 'http://localhost:6767/post/',
103       'auth': {
104         'user': 'test',
105         'pass': 'testing2',
106         'sendImmediately': false
107       }
108     }, function(error, res, body) {
109       assert.equal(res.statusCode, 200);
110       assert.equal(numBasicRequests, 6);
111       next();
112     });
113   },
114
115   function(next) {
116     assert.doesNotThrow( function() {
117       request({
118         'method': 'GET',
119         'uri': 'http://localhost:6767/allow_empty_user/',
120         'auth': {
121           'user': '',
122           'pass': 'apassword',
123           'sendImmediately': false
124         }
125       }, function(error, res, body ) {
126         assert.equal(res.statusCode, 200);
127         assert.equal(numBasicRequests, 8);
128         next();
129       });
130     })
131   },
132
133   function(next) {
134     assert.doesNotThrow( function() {
135       request({
136         'method': 'GET',
137         'uri': 'http://localhost:6767/allow_undefined_password/',
138         'auth': {
139           'user': 'justauser',
140           'pass': undefined,
141           'sendImmediately': false
142         }
143       }, function(error, res, body ) {
144         assert.equal(res.statusCode, 200);
145         assert.equal(numBasicRequests, 10);
146         next();
147       });
148     })
149   }
150 ];
151
152 function runTest(i) {
153   if (i < tests.length) {
154     tests[i](function() {
155       runTest(i + 1);
156     });
157   } else {
158     console.log('All tests passed');
159     basicServer.close();
160   }
161 }
162
163 runTest(0);