Skip to content

Commit

Permalink
fix(request): guess appropriate request protocol from incoming object (
Browse files Browse the repository at this point in the history
…#155)

Fixes #146
  • Loading branch information
usualoma authored Mar 29, 2024
1 parent b254208 commit 2eb0bae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
27 changes: 26 additions & 1 deletion test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')}`)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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:')
})
})

Expand Down

0 comments on commit 2eb0bae

Please sign in to comment.