2

I am currently writing a test suite for an API that I am building however I cannot work out how to set the Basic Auth Header to test certain routes.

I can test normal routes like the following:

describe('Get /', function() {
    it('returns statusCode 200', function(done) {
        request.get(base_url, function(error, response){
            expect(response.statusCode).toBe(SuccessCode)
            done()
        })

However many of mu routes require data from the basic auth header and I cannot work out how to set this. Any help would be greatly appreciated

Jack Tidbury
  • 183
  • 1
  • 1
  • 12

1 Answers1

3

It looks like you're using the request module, so something like this should work:

describe('Get /secure/test', function() {
    it('returns statusCode 200', function(done) {

        var username = 'username';
        var password = 'password';
        var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');

        var options = {
            url: 'https://api.com/secure/test',
            headers: {
                'Authorization': auth
            }
        };

        request.get(options, function(error, response){
            expect(response.statusCode).toBe(SuccessCode)
            done()
        })

    })
})
dan
  • 1,944
  • 1
  • 15
  • 22