最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting Basic Auth in Mocha and SuperTest - Stack Overflow

programmeradmin1浏览0评论

I'm trying to set us a test to verify the username and password of a path blocked by the basic auth of a username and password.

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();
        });
});

I'm trying to set us a test to verify the username and password of a path blocked by the basic auth of a username and password.

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();
        });
});
Share Improve this question edited May 4, 2015 at 10:51 Yves M. 31k24 gold badges109 silver badges149 bronze badges asked Feb 27, 2015 at 2:31 Peter ChappyPeter Chappy 1,1795 gold badges23 silver badges42 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 38

Using the auth method

SuperTest is based on SuperAgent which provides the auth method to facilitate Basic Authentication:

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

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


PS: You can pass done straight to any of the .expect() calls

The username:password part must be base64 encoded

You can use something like

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

评论列表(0)

  1. 暂无评论