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

14

我正在尝试设置一个测试,以验证路径的用户名和密码是否被基本认证所阻止。

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();
        });
});
2个回答

38

1

username:password部分必须进行base64编码。

你可以使用以下方式:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接