javascript - 请求路径包含未转义字符,在 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);
});
});
所以这是我的测试代码,每次我运行它进行测试时,我都会收到以下错误:
我该如何解决这个错误?
解决方案
当我在 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();
});
});
})
推荐阅读
- java - 有一个错误:java.lang.ClassNotFoundException
- android - 切换到主活动时启动屏幕正在调整大小
- python - CPython 如何确定用户是否提供了可选参数?
- azure-functions - Cosmos 客户端 - 绑定还是 DI?
- google-cloud-platform - 设置云运行权限以访问 POST 方法
- kubernetes - 如何在 GKE 集群上启用准入控制器?
- matlab - 按钮推送状态属性 GUI MATLAB
- css - 在 VSCode 中运行 SASS
- java - 触摸监听器上的文本视图
- javascript - 如何使用模块模式或任何其他方式改进我的代码?