Wiremock standalone JSON response.ok 等于 false 从未达到
Wiremock standalone JSON response.ok equals false never reached
我正在尝试使用具有 JSON 映射的独立 Wiremock 测试 JS 应用程序的不同获取失败场景。
try {
const response = await fetch('/api', options); // 1
if (response.ok) {
// do something with a successful response
} else {
throw new Error('response not ok'); // 2
}
} catch (error) {
// log the error // 3
}
当我根据 Wiremock 文档 simulate bad response failures 时,它在获取调用 (1) 时失败并触发捕获 (3)。
{
"request": {
"urlPath": "/api",
"method": "ANY"
},
"response": {
"fault": "RANDOM_DATA_THEN_CLOSE"
"headers": {
"Content-Type": "application/json; charset=utf-8",
"Access-Control-Allow-Origin": "http://localhost:8080",
"Access-Control-Allow-Methods": "PUT",
"Access-Control-Allow-Headers": "x-csrf-token, content-type, pragma, cache-control",
"Access-Control-Allow-Credentials": "true",
"X-Content-Type-Options": "nosniff",
"x-frame-options": "DENY",
"x-xss-protection": "1; mode=block"
}
}
}
根据 MDN docs response.ok 是只读布尔值 2XX,但如果我将状态设置为 408 response.ok 仍然为真。
{
"request": {
"urlPath": "/api",
"method": "ANY"
},
"response": {
"status": 408,
"jsonBody": {},
"headers": {
"Content-Type": "application/json; charset=utf-8",
...
}
}
}
我还没有找到 status codes/faults 与 wiremock 的组合来实际触发 !response.ok 错误 (2)。我们正在从我们的 UI 获取日志到我们的生产服务器,它告诉我们 !response.ok 分支正在被命中。
来自fetch MDN:
A fetch() promise only rejects when a network error is encountered
(which is usually when there’s a permissions issue or similar). A
fetch() promise does not reject on HTTP errors (404, etc.). Instead, a
then() handler must check the Response.ok and/or Response.status
properties.
因此,为了达到 response.ok === false
,您可以做的最简单的测试是针对未知 URL
调用它以强制服务器 return 404
.
例如,让我们使用 SO
和这个问题来测试这个功能。您可以像下面这样获取它:
async function fetchMe() {
let resp = await fetch("");
if (resp.ok) {
console.log("Question exists!");
} else {
console.log("Question does not exist!");
}
};
fetchMe();
当我们在 URL
末尾添加一些文本时,SO
应该 return 404
:
async function fetchUnknown() {
let resp = await fetch("UNKNOWN");
if (resp.ok) {
console.log("Question exists!");
} else {
console.log("Question does not exist!");
}
};
fetchUnknown();
我正在尝试使用具有 JSON 映射的独立 Wiremock 测试 JS 应用程序的不同获取失败场景。
try {
const response = await fetch('/api', options); // 1
if (response.ok) {
// do something with a successful response
} else {
throw new Error('response not ok'); // 2
}
} catch (error) {
// log the error // 3
}
当我根据 Wiremock 文档 simulate bad response failures 时,它在获取调用 (1) 时失败并触发捕获 (3)。
{
"request": {
"urlPath": "/api",
"method": "ANY"
},
"response": {
"fault": "RANDOM_DATA_THEN_CLOSE"
"headers": {
"Content-Type": "application/json; charset=utf-8",
"Access-Control-Allow-Origin": "http://localhost:8080",
"Access-Control-Allow-Methods": "PUT",
"Access-Control-Allow-Headers": "x-csrf-token, content-type, pragma, cache-control",
"Access-Control-Allow-Credentials": "true",
"X-Content-Type-Options": "nosniff",
"x-frame-options": "DENY",
"x-xss-protection": "1; mode=block"
}
}
}
根据 MDN docs response.ok 是只读布尔值 2XX,但如果我将状态设置为 408 response.ok 仍然为真。
{
"request": {
"urlPath": "/api",
"method": "ANY"
},
"response": {
"status": 408,
"jsonBody": {},
"headers": {
"Content-Type": "application/json; charset=utf-8",
...
}
}
}
我还没有找到 status codes/faults 与 wiremock 的组合来实际触发 !response.ok 错误 (2)。我们正在从我们的 UI 获取日志到我们的生产服务器,它告诉我们 !response.ok 分支正在被命中。
来自fetch MDN:
A fetch() promise only rejects when a network error is encountered (which is usually when there’s a permissions issue or similar). A fetch() promise does not reject on HTTP errors (404, etc.). Instead, a then() handler must check the Response.ok and/or Response.status properties.
因此,为了达到 response.ok === false
,您可以做的最简单的测试是针对未知 URL
调用它以强制服务器 return 404
.
例如,让我们使用 SO
和这个问题来测试这个功能。您可以像下面这样获取它:
async function fetchMe() {
let resp = await fetch("");
if (resp.ok) {
console.log("Question exists!");
} else {
console.log("Question does not exist!");
}
};
fetchMe();
当我们在 URL
末尾添加一些文本时,SO
应该 return 404
:
async function fetchUnknown() {
let resp = await fetch("UNKNOWN");
if (resp.ok) {
console.log("Question exists!");
} else {
console.log("Question does not exist!");
}
};
fetchUnknown();