Skip to content

Commit

Permalink
fix: avoid error when using TRACE method (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
usualoma authored Jun 13, 2024
1 parent e3b1f82 commit 05165c6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ const newRequestFromIncoming = (
signal: abortController.signal,
} as RequestInit

if (method === 'TRACE') {
init.method = 'GET'
const req = new Request(url, init)
Object.defineProperty(req, 'method', {
get() {
return 'TRACE'
},
})
return req
}

if (!(method === 'GET' || method === 'HEAD')) {
// lazy-consume request body
init.body = Readable.toWeb(incoming) as ReadableStream<Uint8Array>
Expand Down
10 changes: 10 additions & 0 deletions test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ describe('Basic', () => {
return new PonyfillResponse('Pony')
})

app.on('trace', '/', (c) => {
const headers = c.req.raw.headers // build new request object
return c.text(`headers: ${JSON.stringify(headers)}`)
})

const server = createAdaptorServer(app)

it('Should return 200 response - GET /', async () => {
Expand Down Expand Up @@ -94,6 +99,11 @@ describe('Basic', () => {
expect(res.headers['content-type']).toMatch('text/plain')
expect(res.text).toBe('Pony')
})

it('Should not raise error for TRACE method', async () => {
const res = await request(server).trace('/')
expect(res.text).toBe('headers: {}')
})
})

describe('via internal body', () => {
Expand Down

0 comments on commit 05165c6

Please sign in to comment.