From 05165c6f0d2d2dbeade503268ff988df74268d55 Mon Sep 17 00:00:00 2001 From: Taku Amano Date: Fri, 14 Jun 2024 07:15:03 +0900 Subject: [PATCH] fix: avoid error when using TRACE method (#175) --- src/request.ts | 11 +++++++++++ test/server.test.ts | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/request.ts b/src/request.ts index 5a87712..28408ba 100644 --- a/src/request.ts +++ b/src/request.ts @@ -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 diff --git a/test/server.test.ts b/test/server.test.ts index c7fad1c..52bbcb4 100644 --- a/test/server.test.ts +++ b/test/server.test.ts @@ -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 () => { @@ -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', () => {