diff --git a/src/request.ts b/src/request.ts index 563fde8..9bffea8 100644 --- a/src/request.ts +++ b/src/request.ts @@ -4,6 +4,7 @@ import type { IncomingMessage } from 'node:http' import { Http2ServerRequest } from 'node:http2' import { Readable } from 'node:stream' +import type { TLSSocket } from 'node:tls' export const GlobalRequest = global.Request export class Request extends GlobalRequest { @@ -117,7 +118,12 @@ export const newRequest = (incoming: IncomingMessage | Http2ServerRequest) => { const req = Object.create(requestPrototype) req[incomingKey] = incoming req[urlKey] = new URL( - `http://${incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host}${ + `${ + incoming instanceof Http2ServerRequest || + (incoming.socket && (incoming.socket as TLSSocket).encrypted) + ? 'https' + : 'http' + }://${incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host}${ incoming.url }` ).href diff --git a/test/server.test.ts b/test/server.test.ts index b3291d2..3bd61f2 100644 --- a/test/server.test.ts +++ b/test/server.test.ts @@ -14,6 +14,7 @@ import type { HttpBindings } from '../src/types' describe('Basic', () => { const app = new Hono() app.get('/', (c) => c.text('Hello! Node!')) + app.get('/url', (c) => c.text(c.req.url)) app.get('/posts', (c) => { return c.text(`Page ${c.req.query('page')}`) @@ -44,6 +45,16 @@ describe('Basic', () => { expect(res.text).toBe('Hello! Node!') }) + it('Should return 200 response - GET /url', async () => { + const res = await request(server).get('/url').trustLocalhost() + expect(res.status).toBe(200) + expect(res.headers['content-type']).toMatch('text/plain') + const url = new URL(res.text) + expect(url.pathname).toBe('/url') + expect(url.hostname).toBe('127.0.0.1') + expect(url.protocol).toBe('http:') + }) + it('Should return 200 response - GET /posts?page=2', async () => { const res = await request(server).get('/posts?page=2') expect(res.status).toBe(200) @@ -525,6 +536,7 @@ describe('Stream and non-stream response', () => { describe('SSL', () => { const app = new Hono() app.get('/', (c) => c.text('Hello! Node!')) + app.get('/url', (c) => c.text(c.req.url)) const server = createAdaptorServer({ fetch: app.fetch, @@ -541,6 +553,16 @@ describe('SSL', () => { expect(res.headers['content-type']).toMatch('text/plain') expect(res.text).toBe('Hello! Node!') }) + + it('Should return 200 response - GET /url', async () => { + const res = await request(server).get('/url').trustLocalhost() + expect(res.status).toBe(200) + expect(res.headers['content-type']).toMatch('text/plain') + const url = new URL(res.text) + expect(url.pathname).toBe('/url') + expect(url.hostname).toBe('127.0.0.1') + expect(url.protocol).toBe('https:') + }) }) describe('HTTP2', () => { @@ -580,7 +602,10 @@ describe('HTTP2', () => { const res = await request(server, { http2: true }).get('/url').trustLocalhost() expect(res.status).toBe(200) expect(res.headers['content-type']).toMatch('text/plain') - expect(new URL(res.text).hostname).toBe('127.0.0.1') + const url = new URL(res.text) + expect(url.pathname).toBe('/url') + expect(url.hostname).toBe('127.0.0.1') + expect(url.protocol).toBe('https:') }) })