Leela Vathi
Leela Vathi

Reputation: 71

How to send Headers ('Authorization','Bearer token') in Mocha Test cases

I am writing a test case to test my API . When I try to test for any open API, it is working fine. But When I try to send Authorization Token along with my API, it is not working. Here is the code:

The way i am sending headers is:

.set("Authorization", "Bearer " + token)

Is it the correct way of sending?

I have tried to send the Authorization token in Auth. But not able to get the same. But when I tried to consume same in Postman, it is working fine.

    it("Get some random Info", function(done) {
        chai
          .request(baseUrl)
          .get("/someRandomApi")
          .set("Authorization", "Bearer " + token)
          .end(function(err, res) {
            expect(res).to.have.status(200);
            done();
          });
      });

Upvotes: 4

Views: 15407

Answers (4)

danielr
danielr

Reputation: 31

You can use the auth function to set the Authorization header.

it("Get some random Info", function(done) {
    chai
      .request(baseUrl)
      .get("/someRandomApi")
      .auth(token, { type: 'bearer' })
      .end(function(err, res) {
        expect(res).to.have.status(200);
        done();
      });
  });

Upvotes: 2

Keyul
Keyul

Reputation: 759

chai-http has auth function to send the Authorization Bearer token.

Accroding to chai-http code on Github, token can be pass using:

.auth(accessToken, { type: 'bearer' })

The code would be like:

it("Get some random Info", function(done) {
   chai.request(baseUrl)
     .get('/someRandomApi')
     .set(token,{ type: 'bearer' }) //token is actual token data
     .then((res) => {
        expect(res).to.have.status(200)
       done();
     }).catch((err) => done(err))
});

Upvotes: 1

buck80
buck80

Reputation: 161

I like to set up my tests in the following way:

let baseUrl = 'http://localhost:9090'
let token = 'some_authorization_token'

First I would instantiate my variables baseUrl and token at the very top of the test, right after use() part.

Next to come is the setup of the test.

    it("Get some random Info", function(done) {
       chai.request(baseUrl)
         .get('/someRandomApi')
         .set({ "Authorization": `Bearer ${token}` })
         .then((res) => {
            expect(res).to.have.status(200)
            const body = res.body
            // console.log(body) - not really needed, but I include them as a comment
           done();
         }).catch((err) => done(err))
    });

Now, .set() doesn't necessarily have to be like mine, works in your case as well.

Upvotes: 14

Ryan
Ryan

Reputation: 403

Try calling .get() after you call .set():

it("Get some random Info", function(done) {
   chai
     .request(baseUrl)
     .set("Authorization", "Bearer " + token) //set the header first
     .get("/someRandomApi") //then get the data
     .end(function(err, res) {
       expect(res).to.have.status(200);
       done();
     });
});

Upvotes: 0

Related Questions