【问题标题】:Request path contains unescaped characters, in Mocha/chai testing请求路径包含未转义字符,在 Mocha/chai 测试中
【发布时间】:2020-06-17 07:14:53
【问题描述】:

给定的是我的代码来测试一个带有设置为 jwt 令牌的 Authorization 标头和 传递到帖子路径的路径参数的发布请求,即 id:5ee9b12ab08b6c3c58375a6d

有没有更好的方法来做到这一点?

const expect = require("expect");
const request = require("request");
const chai = require("chai");
let chaiHttp = require("chai-http");
let server = require("../app");
let should = chai.should();
chai.use(chaiHttp);

describe("Admin Priveleges", () => {
  describe("/Update Status", () => {
    it("Update membership and registration status", (done) => {
      chai
        .request(server)
        .post("/api​/v2​/user​/update-status​/5ee9b12ab08b6c3c58375a6d")
        .set('Authorization', "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZWU5YjEzN2IwOGI2YzNjNTgzNzVhNmUiLCJyb2xlIjoiYWRtaW4iLCJleHAiOjE1OTI0NjEyMjIyMDgsIm5hbWUiOiJVdGthcnNoIFNocml2YXN0YXZhIiwiaWF0IjoxNTkyMzc0ODIyfQ.M53gRzIppbhhLSCf9bD6xcdXfITiD1jUOzTlDqHK3is")
        .send({
          membership_status: "active",
          registration_status: "pending_approval",
          status_comment: "Good going"
        })
        .end((err, res) => {
          if (err) throw err;
          if (should) console.log("****Status Updated Successfully****");
          res.should.have.status(200);
          done();
        });
    }).timeout(30000);
  });
});

所以这是我的测试代码,每次运行它进行测试时,我都会收到以下错误:

我该如何解决这个错误?

【问题讨论】:

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


    【解决方案1】:

    当我在 web 控制台中复制粘贴你的“url”时,它显示了 actual 字符串,否则为“不可见”.

    Test:

    context('Should not show "Request path contains unescaped characters, in Mocha/chai testing"', function() {
      it('POST /api/v2/user/update-status/5ee9b12ab08b6c3c58375a6d', function(done) {
        chai
          .request(server)
          .post('/api/v2/user/update-status/5ee9b12ab08b6c3c58375a6d')
          // .post('/api​/v2​/user​/update-status​/5ee9b12ab08b6c3c58375a6d') // with the 'hidden dots'
          .set('Authorization', "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZWU5YjEzN2IwOGI2YzNjNTgzNzVhNmUiLCJyb2xlIjoiYWRtaW4iLCJleHAiOjE1OTI0NjEyMjIyMDgsIm5hbWUiOiJVdGthcnNoIFNocml2YXN0YXZhIiwiaWF0IjoxNTkyMzc0ODIyfQ.M53gRzIppbhhLSCf9bD6xcdXfITiD1jUOzTlDqHK3is")
          .send({
            membership_status: "active",
            registration_status: "pending_approval",
            status_comment: "Good going"
          })
          .end((err, res) => {
            // if (err) throw err;
            // if (should) console.log("****Status Updated Successfully****");
            // res.should.have.status(200);
            expect(res).to.have.status(404);
            
            done();
          });
      });
    })
    

    【讨论】:

      猜你喜欢
      • 2013-08-03
      • 2021-04-21
      • 2019-05-30
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多