Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
fix: respect implicit status code (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
boywithkeyboard authored Aug 26, 2023
1 parent 0ff1066 commit 5eaba5e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
16 changes: 10 additions & 6 deletions cheetah.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,14 +664,18 @@ export class cheetah extends base<cheetah>() {
}

case 'Object': {
$.b = JSON.stringify($.b)
try {
if ((($.b as unknown) as { code: number }).code) {
$.c = (($.b as unknown) as { code: number }).code
}

if (!$.h.has('content-type')) {
$.h.set('content-type', 'application/json; charset=utf-8')
}
$.b = JSON.stringify($.b)

if ((($.b as unknown) as { code: number }).code) {
$.c = (($.b as unknown) as { code: number }).code
if (!$.h.has('content-type')) {
$.h.set('content-type', 'application/json; charset=utf-8')
}
} catch (err) {
console.log(err)
}

break
Expand Down
14 changes: 12 additions & 2 deletions test/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ Deno.test('Response', async (t) => {
const app = new cheetah()

await t.step('res.code', async () => {
app.get('/code', (c) => {
app.get('/code-explicit', (c) => {
c.res.code = 101
})
assertEquals(
(await app.fetch(new Request('http://localhost/code'))).status,
(await app.fetch(new Request('http://localhost/code-explicit'))).status,
101,
)

app.get('/code-implicit', () => {
return {
code: 403,
}
})
assertEquals(
(await app.fetch(new Request('http://localhost/code-implicit'))).status,
403,
)
})

await t.step('res.bodySize', async () => {
Expand Down

0 comments on commit 5eaba5e

Please sign in to comment.