Skip to content

Commit 92c06e9

Browse files
committed
fix: allow sending responses with status 0
1 parent db26289 commit 92c06e9

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

packages/fetch-mock/src/Route.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ function isBodyInit(body: BodyInit | object): body is BodyInit {
8989
}
9090

9191
function sanitizeStatus(status?: number): number {
92+
if (status === 0) {
93+
// we do this here for now because we can't construct a Response with status 0
94+
// we overwrite to 0 later in the proxy wrapper around teh response.
95+
return 200;
96+
}
97+
9298
if (!status) {
9399
return 200;
94100
}

packages/fetch-mock/src/Router.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function shouldSendAsObject(responseInput: RouteResponseData): boolean {
7070
// TODO improve this... make it less hacky and magic
7171
if (
7272
responseConfigProps.some(
73-
(prop) => (responseInput as RouteResponseConfig)[prop],
73+
(prop) => prop in (responseInput as RouteResponseConfig),
7474
)
7575
) {
7676
if (
@@ -268,6 +268,11 @@ export default class Router {
268268
return false;
269269
}
270270
}
271+
272+
if (responseInput.status === 0) {
273+
if (name === 'status') return 0;
274+
if (name === 'statusText') return '';
275+
}
271276
// TODO fix these types properly
272277
//@ts-expect-error TODO probably make use of generics here
273278
if (typeof response[name] === 'function') {

packages/fetch-mock/src/__tests__/FetchMock/response-construction.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ describe('response construction', () => {
2727
);
2828
}
2929
});
30+
31+
it('should be able to send status 0', async () => {
32+
fm.route('*', { status: 0 });
33+
const res = await fm.fetchHandler('http://a.com/');
34+
expect(res.status).toEqual(0);
35+
expect(res.statusText).toEqual('');
36+
});
3037
});
3138

3239
describe('string', () => {

0 commit comments

Comments
 (0)