1 回答

TA貢獻1772條經驗 獲得超6個贊
好吧,我想似乎沒有人有任何值得作為答案的建議,所以我會嘗試完全意識到這不會完全回答你的問題。
當我需要一個可以返回各種晦澀的 HTTP 代碼以進行測試的 API 時,我會使用此網站: https: //httpstat.us/。這就像附加所需的代碼并進行調用(https://httpstat.us/400
、https://httpstat.us/404
等https://httpstat.us/200
)一樣簡單
顯然,它不能完成您需要的所有“狀態代碼、標頭、響應正文等”,但它肯定會提供測試各種 HTTP 響應代碼的能力。

TA貢獻1831條經驗 獲得超9個贊
我發現使用頁面 URL 查詢參數是模擬錯誤狀態的一種非常好的方法:
export const handlers = [
? rest.get(`/your/api/endpoint`, (req, res, ctx) => {
? ? // Check if the '?error=true' query param has been included in the page url.
? ? const pageParams = new URLSearchParams(window.location.search);
? ? const isError = pageParams.get('error') === 'true';
? ? // Example: http://localhost:3000/your_page_url?error=true
? ? if (isError) {
? ? ? // Query param was found, so return an error response.
? ? ? return res(
? ? ? ? ctx.status(404),
? ? ? ? ctx.json({ message: 'Oops! Something went terribly wrong.' })
? ? ? );
? ? }
? ? // Otherwise - return a 200 OK response.
? ? return res(ctx.status(200), ctx.json({ data: 'Some cool data' }));
? })
];
添加回答
舉報