Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow sending responses with status 0 #856

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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