首页 > 解决方案 > 请求路径包含未转义字符,在 Mocha/chai 测试中

问题描述

给出的是我的代码来测试一个带有设置为 jwt 令牌的 Authorization 标头和一个传递给 post 路径的路径参数的 post 请求,即 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);
  });
});

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

在此处输入图像描述

我该如何解决这个错误?

标签: javascriptnode.jsunit-testingmocha.jschai

解决方案


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

截图2


推荐阅读