forked from request/request
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-basic-auth.js
More file actions
143 lines (129 loc) · 3.41 KB
/
Copy pathtest-basic-auth.js
File metadata and controls
143 lines (129 loc) · 3.41 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var assert = require('assert')
, http = require('http')
, request = require('../index')
;
var numBasicRequests = 0;
var basicServer = http.createServer(function (req, res) {
console.error('Basic auth server: ', req.method, req.url);
numBasicRequests++;
var ok;
if (req.headers.authorization) {
if (req.headers.authorization == 'Basic ' + new Buffer('test:testing2').toString('base64')) {
ok = true;
} else if ( req.headers.authorization == 'Basic ' + new Buffer(':apassword').toString('base64')) {
ok = true;
} else {
// Bad auth header, don't send back WWW-Authenticate header
ok = false;
}
} else {
// No auth header, send back WWW-Authenticate header
ok = false;
res.setHeader('www-authenticate', 'Basic realm="Private"');
}
if (req.url == '/post/') {
var expectedContent = 'data_key=data_value';
req.on('data', function(data) {
assert.equal(data, expectedContent);
console.log('received request data: ' + data);
});
assert.equal(req.method, 'POST');
assert.equal(req.headers['content-length'], '' + expectedContent.length);
assert.equal(req.headers['content-type'], 'application/x-www-form-urlencoded; charset=utf-8');
}
if (ok) {
console.log('request ok');
res.end('ok');
} else {
console.log('status=401');
res.statusCode = 401;
res.end('401');
}
});
basicServer.listen(6767);
var tests = [
function(next) {
request({
'method': 'GET',
'uri': 'http://localhost:6767/test/',
'auth': {
'user': 'test',
'pass': 'testing2',
'sendImmediately': false
}
}, function(error, res, body) {
assert.equal(res.statusCode, 200);
assert.equal(numBasicRequests, 2);
next();
});
},
function(next) {
// If we don't set sendImmediately = false, request will send basic auth
request({
'method': 'GET',
'uri': 'http://localhost:6767/test2/',
'auth': {
'user': 'test',
'pass': 'testing2'
}
}, function(error, res, body) {
assert.equal(res.statusCode, 200);
assert.equal(numBasicRequests, 3);
next();
});
},
function(next) {
request({
'method': 'GET',
'uri': 'http://test:testing2@localhost:6767/test2/'
}, function(error, res, body) {
assert.equal(res.statusCode, 200);
assert.equal(numBasicRequests, 4);
next();
});
},
function(next) {
request({
'method': 'POST',
'form': { 'data_key': 'data_value' },
'uri': 'http://localhost:6767/post/',
'auth': {
'user': 'test',
'pass': 'testing2',
'sendImmediately': false
}
}, function(error, res, body) {
assert.equal(res.statusCode, 200);
assert.equal(numBasicRequests, 6);
next();
});
},
function(next) {
assert.doesNotThrow( function() {
request({
'method': 'GET',
'uri': 'http://localhost:6767/allow_empty_user/',
'auth': {
'user': '',
'pass': 'apassword',
'sendImmediately': false
}
}, function(error, res, body ) {
assert.equal(res.statusCode, 200);
assert.equal(numBasicRequests, 8);
next();
});
})
}
];
function runTest(i) {
if (i < tests.length) {
tests[i](function() {
runTest(i + 1);
});
} else {
console.log('All tests passed');
basicServer.close();
}
}
runTest(0);