Skip to content
Myr

feat(trailing-slash): add `skip` option (#4862)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by GitHub · parent 1aa32fb · Browse files · 2 files+80 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+80 added−2 removed

  • tests:+57 added−0 removed
  • other:+23 added−2 removed

2 files+80 added−2 removed

modified:src/middleware/trailing-slash/index.test.ts+57 added
253 unmodified lines
254 expect(loc.searchParams.get('exampleParam')).toBe('1')255 })256 })257258 describe('appendTrailingSlash middleware with skip option', () => {259 const hasExtension = (path: string): boolean => /\.\w+$/.test(path.split('/').at(-1) ?? '')260 const app = new Hono({ strict: true })261 app.use('*', appendTrailingSlash({ skip: hasExtension }))262263 app.get('/page/', (c) => c.text('ok'))264265 it('should not redirect paths with file extensions', async () => {266 const resp = await app.request('/foo.html')267268 expect(resp.status).toBe(404)269 })270271 it('should redirect paths without file extensions', async () => {272 const resp = await app.request('/page')273274 expect(resp.status).toBe(301)275 const loc = new URL(resp.headers.get('location')!)276 expect(loc.pathname).toBe('/page/')277 })278 })279280 describe('appendTrailingSlash middleware with alwaysRedirect and skip options', () => {281 const hasExtension = (path: string): boolean => /\.\w+$/.test(path.split('/').at(-1) ?? '')282 const app = new Hono()283 app.use('*', appendTrailingSlash({ alwaysRedirect: true, skip: hasExtension }))284285 app.get('/', (c) => c.text('ok'))286 app.get('/my-path/*', (c) => c.text('wildcard'))287288 it('should redirect paths without extensions', async () => {289 const resp = await app.request('/my-path/something')290 const loc = new URL(resp.headers.get('location')!)291292 expect(resp.status).toBe(301)293 expect(loc.pathname).toBe('/my-path/something/')294 })295296 it('should not redirect paths with file extensions', async () => {297 const resp = await app.request('/my-path/style.css')298299 expect(resp.status).toBe(200)300 })301302 it('should not redirect paths with extensions like .html', async () => {303 const resp = await app.request('/my-path/page.html')304305 expect(resp.status).toBe(200)306 })307308 it('should handle HEAD request for paths with extensions', async () => {309 const resp = await app.request('/my-path/script.js', { method: 'HEAD' })310311 expect(resp.status).toBe(200)312 })313 })314315 describe('appendTrailingSlash middleware with alwaysRedirect option', () => {316 const app = new Hono()317 app.use('*', appendTrailingSlash({ alwaysRedirect: true }))318
63 unmodified lines
modified:src/middleware/trailing-slash/index.ts+23 added-2 removed