14

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();
        });
});
Yves M.
  • 29,855
  • 23
  • 108
  • 144
Peter Chappy
  • 1,165
  • 4
  • 22
  • 41

2 Answers2

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

Community
  • 1
  • 1
Yves M.
  • 29,855
  • 23
  • 108
  • 144
1

The username:password part must be base64 encoded

You can use something like

.set("Authorization", "basic " + new Buffer("username:password").toString("base64"))
Yves M.
  • 29,855
  • 23
  • 108
  • 144
pretorh
  • 132
  • 3
  • 4