index.test.ts
This file
Path
src/middleware/trailing-slash/index.test.ts
381 lines 路 13.0 KiB 路 TypeScript
Last changed by
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382import { appendTrailingSlash, trimTrailingSlash } from '.'import { Hono } from '../../hono'
describe('Resolve trailing slash', () => { describe('trimTrailingSlash middleware', () => { const app = new Hono({ strict: true }) app.use('*', trimTrailingSlash())
app.get('/', async (c) => { return c.text('ok') }) app.get('/the/example/endpoint/without/trailing/slash', async (c) => { return c.text('ok') })
it('should handle GET request for root path correctly', async () => { const 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')
expect(resp).not.toBeNull() expect(resp.status).toBe(200) })
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')!)
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' })
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('trimTrailingSlash middleware with alwaysRedirect option', () => { const app = new Hono() app.use('*', trimTrailingSlash({ alwaysRedirect: true }))
app.get('/', async (c) => { return c.text('ok') }) app.get('/my-path/*', async (c) => { return c.text('wildcard') }) app.get('/exact-path', async (c) => { return c.text('exact') })
it('should handle GET request for root path correctly', async () => { const resp = await app.request('/')
expect(resp).not.toBeNull() expect(resp.status).toBe(200) })
it('should redirect wildcard route with trailing slash', async () => { const resp = await app.request('/my-path/something/else/') const loc = new URL(resp.headers.get('location')!)
expect(resp).not.toBeNull() expect(resp.status).toBe(301) expect(loc.pathname).toBe('/my-path/something/else') })
it('should not redirect wildcard route without trailing slash', async () => { const resp = await app.request('/my-path/something/else')
expect(resp).not.toBeNull() expect(resp.status).toBe(200) expect(await resp.text()).toBe('wildcard') })
it('should redirect exact route with trailing slash', async () => { const resp = await app.request('/exact-path/') const loc = new URL(resp.headers.get('location')!)
expect(resp).not.toBeNull() expect(resp.status).toBe(301) expect(loc.pathname).toBe('/exact-path') })
it('should preserve query parameters when redirecting', async () => { const resp = await app.request('/my-path/something/?param=1') const loc = new URL(resp.headers.get('location')!)
expect(resp).not.toBeNull() expect(resp.status).toBe(301) expect(loc.pathname).toBe('/my-path/something') expect(loc.searchParams.get('param')).toBe('1') })
it('should handle HEAD request for wildcard route with trailing slash', async () => { const resp = await app.request('/my-path/something/', { method: 'HEAD' }) const loc = new URL(resp.headers.get('location')!)
expect(resp).not.toBeNull() expect(resp.status).toBe(301) expect(loc.pathname).toBe('/my-path/something') }) })
describe('appendTrailingSlash middleware', () => { const app = new Hono({ strict: true }) app.use('*', appendTrailingSlash())
app.get('/', async (c) => { return c.text('ok') }) app.get('/the/example/endpoint/with/trailing/slash/', async (c) => { return c.text('ok') }) app.get('/the/example/simulate/a.file', async (c) => { return c.text('ok') })
it('should handle GET request for root path correctly', async () => { const resp = await app.request('/')
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')
expect(resp).not.toBeNull() expect(resp.status).toBe(200) })
it('should handle GET request for path with trailing slash correctly', async () => { const resp = await app.request('/the/example/endpoint/with/trailing/slash/')
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') }) })
describe('appendTrailingSlash middleware with skip option', () => { const hasExtension = (path: string): boolean => /\.\w+$/.test(path.split('/').at(-1) ?? '') const app = new Hono({ strict: true }) app.use('*', appendTrailingSlash({ skip: hasExtension }))
app.get('/page/', (c) => c.text('ok'))
it('should not redirect paths with file extensions', async () => { const resp = await app.request('/foo.html')
expect(resp.status).toBe(404) })
it('should redirect paths without file extensions', async () => { const resp = await app.request('/page')
expect(resp.status).toBe(301) const loc = new URL(resp.headers.get('location')!) expect(loc.pathname).toBe('/page/') }) })
describe('appendTrailingSlash middleware with alwaysRedirect and skip options', () => { const hasExtension = (path: string): boolean => /\.\w+$/.test(path.split('/').at(-1) ?? '') const app = new Hono() app.use('*', appendTrailingSlash({ alwaysRedirect: true, skip: hasExtension }))
app.get('/', (c) => c.text('ok')) app.get('/my-path/*', (c) => c.text('wildcard'))
it('should redirect paths without extensions', async () => { const resp = await app.request('/my-path/something') const loc = new URL(resp.headers.get('location')!)
expect(resp.status).toBe(301) expect(loc.pathname).toBe('/my-path/something/') })
it('should not redirect paths with file extensions', async () => { const resp = await app.request('/my-path/style.css')
expect(resp.status).toBe(200) })
it('should not redirect paths with extensions like .html', async () => { const resp = await app.request('/my-path/page.html')
expect(resp.status).toBe(200) })
it('should handle HEAD request for paths with extensions', async () => { const resp = await app.request('/my-path/script.js', { method: 'HEAD' })
expect(resp.status).toBe(200) }) })
describe('appendTrailingSlash middleware with alwaysRedirect option', () => { const app = new Hono() app.use('*', appendTrailingSlash({ alwaysRedirect: true }))
app.get('/', async (c) => { return c.text('ok') }) app.get('/my-path/*', async (c) => { return c.text('wildcard') }) app.get('/exact-path/', async (c) => { return c.text('exact') })
it('should handle GET request for root path correctly', async () => { const resp = await app.request('/')
expect(resp).not.toBeNull() expect(resp.status).toBe(200) })
it('should redirect wildcard route without trailing slash', async () => { const resp = await app.request('/my-path/something/else') const loc = new URL(resp.headers.get('location')!)
expect(resp).not.toBeNull() expect(resp.status).toBe(301) expect(loc.pathname).toBe('/my-path/something/else/') })
it('should not redirect wildcard route with trailing slash', async () => { const resp = await app.request('/my-path/something/else/')
expect(resp).not.toBeNull() expect(resp.status).toBe(200) expect(await resp.text()).toBe('wildcard') })
it('should redirect exact route without trailing slash', async () => { const resp = await app.request('/exact-path') const loc = new URL(resp.headers.get('location')!)
expect(resp).not.toBeNull() expect(resp.status).toBe(301) expect(loc.pathname).toBe('/exact-path/') })
it('should preserve query parameters when redirecting', async () => { const resp = await app.request('/my-path/something?param=1') const loc = new URL(resp.headers.get('location')!)
expect(resp).not.toBeNull() expect(resp.status).toBe(301) expect(loc.pathname).toBe('/my-path/something/') expect(loc.searchParams.get('param')).toBe('1') })
it('should handle HEAD request for wildcard route without trailing slash', async () => { const resp = await app.request('/my-path/something', { method: 'HEAD' }) const loc = new URL(resp.headers.get('location')!)
expect(resp).not.toBeNull() expect(resp.status).toBe(301) expect(loc.pathname).toBe('/my-path/something/') }) })})