在摩卡和SuperTest中设置基本身份validation

我试图设置一个testing,以validation用户名和密码的基本身份validation阻止的path的用户名和密码。

it('should receive a status code of 200 with login', function(done) { request(url) .get("/staging") .expect(200) .set('Authorization', 'Basic username:password') .end(function(err, res) { if (err) { throw err; } done(); }); }); 

使用auth方法

SuperTest基于SuperAgent ,它提供了基于身份validation的auth方法:

 it('should receive a status code of 200 with login', function(done) { request(url) .get('/staging') .auth('the-username', 'the-password') .expect(200, done); }); 

来源: http : //visionmedia.github.io/superagent/#basic-authentication


PS:你可以直接传递任何.expect()调用

“用户名:密码”部分必须是base64编码的

你可以使用类似的东西

 .set("Authorization", "basic " + new Buffer("username:password").toString("base64"))