Skip to content

Commit

Permalink
fix: allow sending responses with status 0
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Oct 28, 2024
1 parent db26289 commit 92c06e9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/fetch-mock/src/Route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ function isBodyInit(body: BodyInit | object): body is BodyInit {
}

function sanitizeStatus(status?: number): number {
if (status === 0) {
// we do this here for now because we can't construct a Response with status 0
// we overwrite to 0 later in the proxy wrapper around teh response.
return 200;
}

if (!status) {
return 200;
}
Expand Down
7 changes: 6 additions & 1 deletion packages/fetch-mock/src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function shouldSendAsObject(responseInput: RouteResponseData): boolean {
// TODO improve this... make it less hacky and magic
if (
responseConfigProps.some(
(prop) => (responseInput as RouteResponseConfig)[prop],
(prop) => prop in (responseInput as RouteResponseConfig),
)
) {
if (
Expand Down Expand Up @@ -268,6 +268,11 @@ export default class Router {
return false;
}
}

if (responseInput.status === 0) {
if (name === 'status') return 0;
if (name === 'statusText') return '';
}
// TODO fix these types properly
//@ts-expect-error TODO probably make use of generics here
if (typeof response[name] === 'function') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ describe('response construction', () => {
);
}
});

it('should be able to send status 0', async () => {
fm.route('*', { status: 0 });
const res = await fm.fetchHandler('http://a.com/');
expect(res.status).toEqual(0);
expect(res.statusText).toEqual('');
});
});

describe('string', () => {
Expand Down

0 comments on commit 92c06e9

Please sign in to comment.