Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(request): guess request protocol from incoming object #155

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading