【问题标题】:How to send Headers ('Authorization','Bearer token') in Mocha Test cases如何在 Mocha 测试用例中发送 Headers ('Authorization','Bearer token')
【发布时间】:2019-12-31 06:09:04
【问题描述】:

我正在编写一个测试用例来测试我的 API。当我尝试测试任何开放 API 时,它工作正常。但是当我尝试将授权令牌与我的 API 一起发送时,它不起作用。代码如下:

我发送标头的方式是:

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

发送方式正确吗?

我已尝试在 Auth 中发送授权令牌。但不能得到相同的。但是当我尝试在 Postman 中使用相同的内容时,它工作正常。

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

【问题讨论】:

  • 令牌在哪里定义?
  • 它在全球范围内可用,即使我尝试过硬编码令牌。我认为令牌没有问题
  • 请出示您的baseUrl变量
  • 这是基本 Url = "10.83.35.193"
  • @LeelaVathi 似乎您没有指定端口号。尝试在 url 中包含您的服务器正在运行的端口号。还可以尝试在网址开头包含http://

标签: javascript node.js unit-testing mocha.js chai


【解决方案1】:

我喜欢按以下方式设置我的测试:

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

首先,我将在测试的最顶部、use() 部分之后实例化我的变量 baseUrltoken

接下来是测试的设置。

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

现在,.set() 不一定要像我一样,也适用于你的情况。

【讨论】:

  • 只有这个对我有用!
【解决方案2】:

您可以使用auth 函数来设置授权标头。

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

【讨论】:

    【解决方案3】:

    chai-http 有auth 函数来发送授权承载令牌。

    根据Github上的chai-http代码,可以使用以下方式传递令牌:

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

    代码如下:

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

    【讨论】:

      【解决方案4】:

      尝试调用.get()你调用.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();
           });
      });
      

      【讨论】:

      • @LeelaVathi 你传递给 chai.request() 的那个对象是什么?它应该是服务器对象。比如:chai.request(app) 或者它应该是 localhost URL。你能把它包含在你的代码中吗
      • 请找到我传递的网址。它在没有任何授权的情况下工作,意味着如果我传递一个像“jsonplaceholder.typicode.com/users”这样的网址,那么它工作正常。但是,如果我尝试测试需要发送授权和承载令牌的 API,我将面临上述问题
      • 以上会引发错误。当它是 get 然后 set 作为标题时,它对我来说工作得很好。
      • 它应该总是在请求方法之后,即set应该在get之后调用
      猜你喜欢
      • 2019-01-01
      • 2021-04-28
      • 2020-08-15
      • 2016-12-18
      • 1970-01-01
      • 2019-04-29
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多