当 url 包含单引号时,如何使用 nock 和 request-promise 测试路由?
How can I test a route with nock and request-promise when the url contains single quotes?
我正在尝试使用 nock + request-promise 测试 API 调用,但我收到错误消息,因为路由不匹配。问题似乎是 API 的 url 包含单引号,请求承诺是 url 对引号进行编码,但 Nock 不是。
Codesandbox(仅 运行 来自终端的纱线测试):
https://codesandbox.io/s/immutable-water-6pw3d
诺克匹配错误
matching https://test.com:443/%27health1%27 to GET https://test.com:443/'health2': false
如果您无法访问 codesandbox,请使用示例代码:
const nock = require("nock");
const rp = require("request-promise");
describe("#getHealth", () => {
it("should return the health", async () => {
const getHealth = async () => {
const response = await rp.get(`https://test.com/'health1'`);
return JSON.parse(response);
};
nock("https://test.com")
.get(`/'health2'`)
.reply(200, { status: "up" })
.log(console.log);
const health = await getHealth();
expect(health.status).equal("up");
});
});
关于请求 URL 编码路径的说法是正确的,而 Nock 不是。
像这样设置 Nock 时,您需要自己对其进行编码:
nock("https://test.com")
.get(escape("/'health1'"))
.reply(200, { status: "up" })
内部 request
模块使用 Node.js 本机 url.parse to parse url strings, see the source code。
所以你可以在测试中使用相同的模块:
const nock = require("nock");
const rp = require("request-promise");
const url = require("url");
describe("#getHealth", () => {
it("should return the health", async () => {
const getHealth = async () => {
const response = await rp.get(`https://example.com/'health1'`);
return JSON.parse(response);
};
const { pathname } = url.parse("https://example.com/'health1'");
nock("https://example.com")
.get(pathname)
.reply(200, { status: "up" })
.log(console.log);
const health = await getHealth();
expect(health.status).equal("up");
});
});
我正在尝试使用 nock + request-promise 测试 API 调用,但我收到错误消息,因为路由不匹配。问题似乎是 API 的 url 包含单引号,请求承诺是 url 对引号进行编码,但 Nock 不是。
Codesandbox(仅 运行 来自终端的纱线测试): https://codesandbox.io/s/immutable-water-6pw3d
诺克匹配错误
matching https://test.com:443/%27health1%27 to GET https://test.com:443/'health2': false
如果您无法访问 codesandbox,请使用示例代码:
const nock = require("nock");
const rp = require("request-promise");
describe("#getHealth", () => {
it("should return the health", async () => {
const getHealth = async () => {
const response = await rp.get(`https://test.com/'health1'`);
return JSON.parse(response);
};
nock("https://test.com")
.get(`/'health2'`)
.reply(200, { status: "up" })
.log(console.log);
const health = await getHealth();
expect(health.status).equal("up");
});
});
关于请求 URL 编码路径的说法是正确的,而 Nock 不是。
像这样设置 Nock 时,您需要自己对其进行编码:
nock("https://test.com")
.get(escape("/'health1'"))
.reply(200, { status: "up" })
内部 request
模块使用 Node.js 本机 url.parse to parse url strings, see the source code。
所以你可以在测试中使用相同的模块:
const nock = require("nock");
const rp = require("request-promise");
const url = require("url");
describe("#getHealth", () => {
it("should return the health", async () => {
const getHealth = async () => {
const response = await rp.get(`https://example.com/'health1'`);
return JSON.parse(response);
};
const { pathname } = url.parse("https://example.com/'health1'");
nock("https://example.com")
.get(pathname)
.reply(200, { status: "up" })
.log(console.log);
const health = await getHealth();
expect(health.status).equal("up");
});
});