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 { |
---|
20 | // Bad auth header, don't send back WWW-Authenticate header |
---|
21 | ok = false; |
---|
22 | } |
---|
23 | } else { |
---|
24 | // No auth header, send back WWW-Authenticate header |
---|
25 | ok = false; |
---|
26 | res.setHeader('www-authenticate', 'Basic realm="Private"'); |
---|
27 | } |
---|
28 | |
---|
29 | if (req.url == '/post/') { |
---|
30 | var expectedContent = 'data_key=data_value'; |
---|
31 | req.on('data', function(data) { |
---|
32 | assert.equal(data, expectedContent); |
---|
33 | console.log('received request data: ' + data); |
---|
34 | }); |
---|
35 | assert.equal(req.method, 'POST'); |
---|
36 | assert.equal(req.headers['content-length'], '' + expectedContent.length); |
---|
37 | assert.equal(req.headers['content-type'], 'application/x-www-form-urlencoded; charset=utf-8'); |
---|
38 | } |
---|
39 | |
---|
40 | if (ok) { |
---|
41 | console.log('request ok'); |
---|
42 | res.end('ok'); |
---|
43 | } else { |
---|
44 | console.log('status=401'); |
---|
45 | res.statusCode = 401; |
---|
46 | res.end('401'); |
---|
47 | } |
---|
48 | }); |
---|
49 | |
---|
50 | basicServer.listen(6767); |
---|
51 | |
---|
52 | var tests = [ |
---|
53 | function(next) { |
---|
54 | request({ |
---|
55 | 'method': 'GET', |
---|
56 | 'uri': 'http://localhost:6767/test/', |
---|
57 | 'auth': { |
---|
58 | 'user': 'test', |
---|
59 | 'pass': 'testing2', |
---|
60 | 'sendImmediately': false |
---|
61 | } |
---|
62 | }, function(error, res, body) { |
---|
63 | assert.equal(res.statusCode, 200); |
---|
64 | assert.equal(numBasicRequests, 2); |
---|
65 | next(); |
---|
66 | }); |
---|
67 | }, |
---|
68 | |
---|
69 | function(next) { |
---|
70 | // If we don't set sendImmediately = false, request will send basic auth |
---|
71 | request({ |
---|
72 | 'method': 'GET', |
---|
73 | 'uri': 'http://localhost:6767/test2/', |
---|
74 | 'auth': { |
---|
75 | 'user': 'test', |
---|
76 | 'pass': 'testing2' |
---|
77 | } |
---|
78 | }, function(error, res, body) { |
---|
79 | assert.equal(res.statusCode, 200); |
---|
80 | assert.equal(numBasicRequests, 3); |
---|
81 | next(); |
---|
82 | }); |
---|
83 | }, |
---|
84 | |
---|
85 | function(next) { |
---|
86 | request({ |
---|
87 | 'method': 'GET', |
---|
88 | 'uri': 'http://test:testing2@localhost:6767/test2/' |
---|
89 | }, function(error, res, body) { |
---|
90 | assert.equal(res.statusCode, 200); |
---|
91 | assert.equal(numBasicRequests, 4); |
---|
92 | next(); |
---|
93 | }); |
---|
94 | }, |
---|
95 | |
---|
96 | function(next) { |
---|
97 | request({ |
---|
98 | 'method': 'POST', |
---|
99 | 'form': { 'data_key': 'data_value' }, |
---|
100 | 'uri': 'http://localhost:6767/post/', |
---|
101 | 'auth': { |
---|
102 | 'user': 'test', |
---|
103 | 'pass': 'testing2', |
---|
104 | 'sendImmediately': false |
---|
105 | } |
---|
106 | }, function(error, res, body) { |
---|
107 | assert.equal(res.statusCode, 200); |
---|
108 | assert.equal(numBasicRequests, 6); |
---|
109 | next(); |
---|
110 | }); |
---|
111 | }, |
---|
112 | |
---|
113 | function(next) { |
---|
114 | assert.doesNotThrow( function() { |
---|
115 | request({ |
---|
116 | 'method': 'GET', |
---|
117 | 'uri': 'http://localhost:6767/allow_empty_user/', |
---|
118 | 'auth': { |
---|
119 | 'user': '', |
---|
120 | 'pass': 'apassword', |
---|
121 | 'sendImmediately': false |
---|
122 | } |
---|
123 | }, function(error, res, body ) { |
---|
124 | assert.equal(res.statusCode, 200); |
---|
125 | assert.equal(numBasicRequests, 8); |
---|
126 | next(); |
---|
127 | }); |
---|
128 | }) |
---|
129 | } |
---|
130 | ]; |
---|
131 | |
---|
132 | function runTest(i) { |
---|
133 | if (i < tests.length) { |
---|
134 | tests[i](function() { |
---|
135 | runTest(i + 1); |
---|
136 | }); |
---|
137 | } else { |
---|
138 | console.log('All tests passed'); |
---|
139 | basicServer.close(); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | runTest(0); |
---|