Skip to content

fix(trailing-slash): handle HEAD request in trailing slash middleware #4049

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

Merged
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
199 changes: 152 additions & 47 deletions src/middleware/trailing-slash/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import { Hono } from '../../hono'
import { appendTrailingSlash, trimTrailingSlash } from '.'

describe('Resolve trailing slash', () => {
let app: Hono

it('Trim', async () => {
app = new Hono({ strict: true })

describe('trimTrailingSlash middleware', () => {
const app = new Hono({ strict: true })
app.use('*', trimTrailingSlash())

app.get('/', async (c) => {
Expand All @@ -16,33 +13,82 @@ describe('Resolve trailing slash', () => {
return c.text('ok')
})

let resp: Response, loc: URL
it('should handle GET request for root path correctly', async () => {
const resp = await app.request('/')

expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
})

resp = await app.request('/')
expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
it('should handle GET request for path without trailing slash correctly', async () => {
const resp = await app.request('/the/example/endpoint/without/trailing/slash')

resp = await app.request('/the/example/endpoint/without/trailing/slash')
expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
})

resp = await app.request('/the/example/endpoint/without/trailing/slash/')
loc = new URL(resp.headers.get('location')!)
expect(resp).not.toBeNull()
expect(resp.status).toBe(301)
expect(loc.pathname).toBe('/the/example/endpoint/without/trailing/slash')
it('should handle GET request for path with trailing slash correctly', async () => {
const resp = await app.request('/the/example/endpoint/without/trailing/slash/')
const loc = new URL(resp.headers.get('location')!)

resp = await app.request('/the/example/endpoint/without/trailing/slash/?exampleParam=1')
loc = new URL(resp.headers.get('location')!)
expect(resp).not.toBeNull()
expect(resp.status).toBe(301)
expect(loc.pathname).toBe('/the/example/endpoint/without/trailing/slash')
expect(loc.searchParams.get('exampleParam')).toBe('1')
})
expect(resp).not.toBeNull()
expect(resp.status).toBe(301)
expect(loc.pathname).toBe('/the/example/endpoint/without/trailing/slash')
})

it('should preserve query parameters when redirecting', async () => {
const resp = await app.request('/the/example/endpoint/without/trailing/slash/?exampleParam=1')
const loc = new URL(resp.headers.get('location')!)

expect(resp).not.toBeNull()
expect(resp.status).toBe(301)
expect(loc.pathname).toBe('/the/example/endpoint/without/trailing/slash')
expect(loc.searchParams.get('exampleParam')).toBe('1')
})

it('should handle HEAD request for root path correctly', async () => {
const resp = await app.request('/', { method: 'HEAD' })

it('Append', async () => {
app = new Hono({ strict: true })
expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
})

it('should handle HEAD request for path without trailing slash correctly', async () => {
const resp = await app.request('/the/example/endpoint/without/trailing/slash', {
method: 'HEAD',
})

expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
})

it('should handle HEAD request for path with trailing slash correctly', async () => {
const resp = await app.request('/the/example/endpoint/without/trailing/slash/', {
method: 'HEAD',
})
const loc = new URL(resp.headers.get('location')!)

expect(resp).not.toBeNull()
expect(resp.status).toBe(301)
expect(loc.pathname).toBe('/the/example/endpoint/without/trailing/slash')
})

it('should preserve query parameters when redirecting HEAD requests', async () => {
const resp = await app.request(
'/the/example/endpoint/without/trailing/slash/?exampleParam=1',
{ method: 'HEAD' }
)
const loc = new URL(resp.headers.get('location')!)

expect(resp).not.toBeNull()
expect(resp.status).toBe(301)
expect(loc.pathname).toBe('/the/example/endpoint/without/trailing/slash')
expect(loc.searchParams.get('exampleParam')).toBe('1')
})
})

describe('appendTrailingSlash middleware', () => {
const app = new Hono({ strict: true })
app.use('*', appendTrailingSlash())

app.get('/', async (c) => {
Expand All @@ -55,31 +101,90 @@ describe('Resolve trailing slash', () => {
return c.text('ok')
})

let resp: Response, loc: URL
it('should handle GET request for root path correctly', async () => {
const resp = await app.request('/')

resp = await app.request('/')
expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
})

resp = await app.request('/the/example/simulate/a.file')
expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
it('should handle GET request for file-like paths correctly', async () => {
const resp = await app.request('/the/example/simulate/a.file')

resp = await app.request('/the/example/endpoint/with/trailing/slash/')
expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
})

resp = await app.request('/the/example/endpoint/with/trailing/slash')
loc = new URL(resp.headers.get('location')!)
expect(resp).not.toBeNull()
expect(resp.status).toBe(301)
expect(loc.pathname).toBe('/the/example/endpoint/with/trailing/slash/')
it('should handle GET request for path with trailing slash correctly', async () => {
const resp = await app.request('/the/example/endpoint/with/trailing/slash/')

resp = await app.request('/the/example/endpoint/with/trailing/slash?exampleParam=1')
loc = new URL(resp.headers.get('location')!)
expect(resp).not.toBeNull()
expect(resp.status).toBe(301)
expect(loc.pathname).toBe('/the/example/endpoint/with/trailing/slash/')
expect(loc.searchParams.get('exampleParam')).toBe('1')
expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
})

it('should redirect path without trailing slash to one with it', async () => {
const resp = await app.request('/the/example/endpoint/with/trailing/slash')
const loc = new URL(resp.headers.get('location')!)

expect(resp).not.toBeNull()
expect(resp.status).toBe(301)
expect(loc.pathname).toBe('/the/example/endpoint/with/trailing/slash/')
})

it('should preserve query parameters when redirecting', async () => {
const resp = await app.request('/the/example/endpoint/with/trailing/slash?exampleParam=1')
const loc = new URL(resp.headers.get('location')!)

expect(resp).not.toBeNull()
expect(resp.status).toBe(301)
expect(loc.pathname).toBe('/the/example/endpoint/with/trailing/slash/')
expect(loc.searchParams.get('exampleParam')).toBe('1')
})

it('should handle HEAD request for root path correctly', async () => {
const resp = await app.request('/', { method: 'HEAD' })

expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
})

it('should handle HEAD request for file-like paths correctly', async () => {
const resp = await app.request('/the/example/simulate/a.file', { method: 'HEAD' })

expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
})

it('should handle HEAD request for path with trailing slash correctly', async () => {
const resp = await app.request('/the/example/endpoint/with/trailing/slash/', {
method: 'HEAD',
})

expect(resp).not.toBeNull()
expect(resp.status).toBe(200)
})

it('should redirect HEAD request for path without trailing slash to one with it', async () => {
const resp = await app.request('/the/example/endpoint/with/trailing/slash', {
method: 'HEAD',
})
const loc = new URL(resp.headers.get('location')!)

expect(resp).not.toBeNull()
expect(resp.status).toBe(301)
expect(loc.pathname).toBe('/the/example/endpoint/with/trailing/slash/')
})

it('should preserve query parameters when redirecting HEAD requests', async () => {
const resp = await app.request('/the/example/endpoint/with/trailing/slash?exampleParam=1', {
method: 'HEAD',
})
const loc = new URL(resp.headers.get('location')!)

expect(resp).not.toBeNull()
expect(resp.status).toBe(301)
expect(loc.pathname).toBe('/the/example/endpoint/with/trailing/slash/')
expect(loc.searchParams.get('exampleParam')).toBe('1')
})
})
})
8 changes: 6 additions & 2 deletions src/middleware/trailing-slash/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const trimTrailingSlash = (): MiddlewareHandler => {

if (
c.res.status === 404 &&
c.req.method === 'GET' &&
(c.req.method === 'GET' || c.req.method === 'HEAD') &&
c.req.path !== '/' &&
c.req.path.at(-1) === '/'
) {
Expand Down Expand Up @@ -57,7 +57,11 @@ export const appendTrailingSlash = (): MiddlewareHandler => {
return async function appendTrailingSlash(c, next) {
await next()

if (c.res.status === 404 && c.req.method === 'GET' && c.req.path.at(-1) !== '/') {
if (
c.res.status === 404 &&
(c.req.method === 'GET' || c.req.method === 'HEAD') &&
c.req.path.at(-1) !== '/'
) {
const url = new URL(c.req.url)
url.pathname += '/'

Expand Down
Loading