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

javascript - How to send Headers ('Authorization','Bearer token') in Mocha Test cases - Stack Ov

programmeradmin2浏览0评论

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

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();
          });
      });
Share Improve this question edited Aug 27, 2019 at 5:49 Ryan 4031 gold badge7 silver badges22 bronze badges asked Aug 27, 2019 at 5:40 Leela VathiLeela Vathi 711 gold badge2 silver badges5 bronze badges 8
  • where the token is defined? – arizafar Commented Aug 27, 2019 at 6:01
  • It is available globally , Even i have tried by hard coding the token . I think there is no issue with token – Leela Vathi Commented Aug 27, 2019 at 6:03
  • Can you please show your baseUrl variable – Ryan Commented Aug 27, 2019 at 6:15
  • This is the base Url = "10.83.35.193" – Leela Vathi Commented Aug 27, 2019 at 6:19
  • @LeelaVathi it seems like your aren't specifying a port number. Try including the port number that your server is running on in the url. Also try including http:// at the beginning on the url – Ryan Commented Aug 27, 2019 at 6:24
 |  Show 3 more comments

4 Answers 4

Reset to default 14

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.

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

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

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论