Reputation: 3803
I want to test a Basic Authentication on my webpage with mocha
:
describe('Test GET /messages', function(){
describe('Test HTTP basic authentication', function(){
it('should return 200 because right credentials', function(done){
request.get(createCustomRequest(true, "/messages", "test", "test"), function(err, res, body){
if (err) {
console.error(err)
} else {
// Whatever, it will always pass
assert.equal(2400, res.statusCode);
}
}, done());
});
});
function createCustomRequest(withAuth, service, username, password){
if(!withAuth){
return {
url: URL+service,
followRedirect: false
};
} else {
return {
url: URL+service,
followRedirect: false,
headers: {
'Authorization': createCredentials(username, password)
}
};
}
}
From the code ahead, I do not understand why the test always pass. I think it is related to the done()
callback.
What did I do wrong here?
Upvotes: 1
Views: 1353
Reputation: 203514
done()
should be called within the callback to request.get()
:
it('should return 200 because right credentials', function(done) {
request.get(createCustomRequest(true, "/messages", "test", "test"), function(err, res, body){
if (err) {
done(err);
return;
}
assert.equal(2400, res.statusCode);
done();
});
});
Upvotes: 3