Skip to content

Commit

Permalink
Refactor and expand basic auth tests
Browse files Browse the repository at this point in the history
The new tests catch the regression corrected by c40993f and the bug
corrected by edc2e17.
  • Loading branch information
nylen committed Apr 1, 2013
1 parent edc2e17 commit a375ac1
Showing 1 changed file with 77 additions and 28 deletions.
105 changes: 77 additions & 28 deletions tests/test-basic-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ var basicServer = http.createServer(function (req, res) {
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');
Expand All @@ -36,39 +47,77 @@ var basicServer = http.createServer(function (req, res) {

basicServer.listen(6767);

request({
'method': 'GET',
'uri': 'http://localhost:6767/test/',
'auth': {
'user': 'test',
'pass': 'testing2',
'sendImmediately': false
}
}, function(error, response, body) {
assert.equal(response.statusCode, 200);
assert.equal(numBasicRequests, 2);
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();
});
},

// 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, response, body) {
assert.equal(response.statusCode, 200);
assert.equal(numBasicRequests, 3);
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, response, body) {
assert.equal(response.statusCode, 200);
}, function(error, res, body) {
assert.equal(res.statusCode, 200);
assert.equal(numBasicRequests, 4);
next();
});
},

console.log('All tests passed');
basicServer.close();
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 runTest(i) {
if (i < tests.length) {
tests[i](function() {
runTest(i + 1);
});
} else {
console.log('All tests passed');
basicServer.close();
}
}

runTest(0);

0 comments on commit a375ac1

Please sign in to comment.