Skip to content

Commit

Permalink
fix: status code 204 responses #3
Browse files Browse the repository at this point in the history
  • Loading branch information
bogeychan committed Jan 7, 2024
1 parent 9543564 commit a8a0b15
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/env/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ globalThis.Request = class Request extends globalThis.Request {
};

globalThis.Response = class Response extends globalThis.Response {
constructor(body?: Bun.BodyInit | null, init?: Bun.ResponseInit) {
super(init?.status === 204 ? null : body, init);
}

// @ts-expect-error
get headers() {
return new globalThis.Headers(
Expand Down
13 changes: 11 additions & 2 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ const TEST_BLOCKS: { [moduleName: string]: TestBlock } = {}; // ignore this :D

const req = (path: string = '/') => new Request(`http://localhost${path}`);

useCustom('elysia', () => {
desc('elysia', () => {
it('return raw response', async () => {
const app = new Elysia().get('/', () => new Response('foo'));

const res = await app.handle(req());
assert.equal(await res.text(), 'foo');
});

it('behave like bun with status code 204 responses', async () => {
try {
const res = new Response('', { status: 204 });
assert.equal(res.status, 204);
} catch (err) {
assert.isTrue(false, err);
}
});
});

use(
Expand Down Expand Up @@ -479,7 +488,7 @@ export async function runTests(env: 'node' | 'deno') {
}
}

function useCustom(moduleName: string, callback: () => void | Promise<void>) {
function desc(moduleName: string, callback: () => void | Promise<void>) {
TEST_BLOCKS[moduleName] = {
moduleName,
callback: async () => {
Expand Down

0 comments on commit a8a0b15

Please sign in to comment.