在Mocha和SuperTest中设置基本Auth

Setting Basic Auth in Mocha and SuperTest

本文关键字:Auth 设置 Mocha SuperTest      更新时间:2024-06-07

我正试图为我们设置一个测试,以验证被用户名和密码的基本身份验证阻止的路径的用户名和密码。

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,它提供身份验证方法来促进基本身份验证:

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-身份验证


PS:您可以将done直接传递给任何.expect()调用

username:password部分必须是base64编码的

你可以使用类似的东西

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