在Mocha和SuperTest中设置基本身份验证

Pet*_*ppy 12 javascript mocha.js basic-authentication node.js supertest

我正在尝试为我们设置测试,以验证用户名和密码的基本身份验证阻止的路径的用户名和密码.

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();
        });
});
Run Code Online (Sandbox Code Playgroud)

Yve*_* M. 26

使用auth方法

SuperTest基于SuperAgent,它提供了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);
});
Run Code Online (Sandbox Code Playgroud)

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


PS:您可以done直接转到任何一个.expect()电话