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
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 1why, one step back1import { appendTrailingSlash, trimTrailingSlash } from '.'chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
line 2why, one step back2import { Hono } from '../../hono'Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 3–381why, one step back34describe('Resolve trailing slash', () => {5 describe('trimTrailingSlash middleware', () => {6 const app = new Hono({ strict: true })7 app.use('*', trimTrailingSlash())89 app.get('/', async (c) => {10 return c.text('ok')11 })12 app.get('/the/example/endpoint/without/trailing/slash', async (c) => {13 return c.text('ok')14 })1516 it('should handle GET request for root path correctly', async () => {17 const resp = await app.request('/')1819 expect(resp).not.toBeNull()20 expect(resp.status).toBe(200)21 })2223 it('should handle GET request for path without trailing slash correctly', async () => {24 const resp = await app.request('/the/example/endpoint/without/trailing/slash')2526 expect(resp).not.toBeNull()27 expect(resp.status).toBe(200)28 })2930 it('should handle GET request for path with trailing slash correctly', async () => {31 const resp = await app.request('/the/example/endpoint/without/trailing/slash/')32 const loc = new URL(resp.headers.get('location')!)3334 expect(resp).not.toBeNull()35 expect(resp.status).toBe(301)36 expect(loc.pathname).toBe('/the/example/endpoint/without/trailing/slash')37 })3839 it('should preserve query parameters when redirecting', async () => {40 const resp = await app.request('/the/example/endpoint/without/trailing/slash/?exampleParam=1')41 const loc = new URL(resp.headers.get('location')!)4243 expect(resp).not.toBeNull()44 expect(resp.status).toBe(301)45 expect(loc.pathname).toBe('/the/example/endpoint/without/trailing/slash')46 expect(loc.searchParams.get('exampleParam')).toBe('1')47 })4849 it('should handle HEAD request for root path correctly', async () => {50 const resp = await app.request('/', { method: 'HEAD' })5152 expect(resp).not.toBeNull()53 expect(resp.status).toBe(200)54 })5556 it('should handle HEAD request for path without trailing slash correctly', async () => {57 const resp = await app.request('/the/example/endpoint/without/trailing/slash', {58 method: 'HEAD',59 })6061 expect(resp).not.toBeNull()62 expect(resp.status).toBe(200)63 })6465 it('should handle HEAD request for path with trailing slash correctly', async () => {66 const resp = await app.request('/the/example/endpoint/without/trailing/slash/', {67 method: 'HEAD',68 })69 const loc = new URL(resp.headers.get('location')!)7071 expect(resp).not.toBeNull()72 expect(resp.status).toBe(301)73 expect(loc.pathname).toBe('/the/example/endpoint/without/trailing/slash')74 })7576 it('should preserve query parameters when redirecting HEAD requests', async () => {77 const resp = await app.request(78 '/the/example/endpoint/without/trailing/slash/?exampleParam=1',79 { method: 'HEAD' }80 )81 const loc = new URL(resp.headers.get('location')!)8283 expect(resp).not.toBeNull()84 expect(resp.status).toBe(301)85 expect(loc.pathname).toBe('/the/example/endpoint/without/trailing/slash')86 expect(loc.searchParams.get('exampleParam')).toBe('1')87 })88 })8990 describe('trimTrailingSlash middleware with alwaysRedirect option', () => {91 const app = new Hono()92 app.use('*', trimTrailingSlash({ alwaysRedirect: true }))9394 app.get('/', async (c) => {95 return c.text('ok')96 })97 app.get('/my-path/*', async (c) => {98 return c.text('wildcard')99 })100 app.get('/exact-path', async (c) => {101 return c.text('exact')102 })103104 it('should handle GET request for root path correctly', async () => {105 const resp = await app.request('/')106107 expect(resp).not.toBeNull()108 expect(resp.status).toBe(200)109 })110111 it('should redirect wildcard route with trailing slash', async () => {112 const resp = await app.request('/my-path/something/else/')113 const loc = new URL(resp.headers.get('location')!)114115 expect(resp).not.toBeNull()116 expect(resp.status).toBe(301)117 expect(loc.pathname).toBe('/my-path/something/else')118 })119120 it('should not redirect wildcard route without trailing slash', async () => {121 const resp = await app.request('/my-path/something/else')122123 expect(resp).not.toBeNull()124 expect(resp.status).toBe(200)125 expect(await resp.text()).toBe('wildcard')126 })127128 it('should redirect exact route with trailing slash', async () => {129 const resp = await app.request('/exact-path/')130 const loc = new URL(resp.headers.get('location')!)131132 expect(resp).not.toBeNull()133 expect(resp.status).toBe(301)134 expect(loc.pathname).toBe('/exact-path')135 })136137 it('should preserve query parameters when redirecting', async () => {138 const resp = await app.request('/my-path/something/?param=1')139 const loc = new URL(resp.headers.get('location')!)140141 expect(resp).not.toBeNull()142 expect(resp.status).toBe(301)143 expect(loc.pathname).toBe('/my-path/something')144 expect(loc.searchParams.get('param')).toBe('1')145 })146147 it('should handle HEAD request for wildcard route with trailing slash', async () => {148 const resp = await app.request('/my-path/something/', { method: 'HEAD' })149 const loc = new URL(resp.headers.get('location')!)150151 expect(resp).not.toBeNull()152 expect(resp.status).toBe(301)153 expect(loc.pathname).toBe('/my-path/something')154 })155 })156157 describe('appendTrailingSlash middleware', () => {158 const app = new Hono({ strict: true })159 app.use('*', appendTrailingSlash())160161 app.get('/', async (c) => {162 return c.text('ok')163 })164 app.get('/the/example/endpoint/with/trailing/slash/', async (c) => {165 return c.text('ok')166 })167 app.get('/the/example/simulate/a.file', async (c) => {168 return c.text('ok')169 })170171 it('should handle GET request for root path correctly', async () => {172 const resp = await app.request('/')173174 expect(resp).not.toBeNull()175 expect(resp.status).toBe(200)176 })177178 it('should handle GET request for file-like paths correctly', async () => {179 const resp = await app.request('/the/example/simulate/a.file')180181 expect(resp).not.toBeNull()182 expect(resp.status).toBe(200)183 })184185 it('should handle GET request for path with trailing slash correctly', async () => {186 const resp = await app.request('/the/example/endpoint/with/trailing/slash/')187188 expect(resp).not.toBeNull()189 expect(resp.status).toBe(200)190 })191192 it('should redirect path without trailing slash to one with it', async () => {193 const resp = await app.request('/the/example/endpoint/with/trailing/slash')194 const loc = new URL(resp.headers.get('location')!)195196 expect(resp).not.toBeNull()197 expect(resp.status).toBe(301)198 expect(loc.pathname).toBe('/the/example/endpoint/with/trailing/slash/')199 })200201 it('should preserve query parameters when redirecting', async () => {202 const resp = await app.request('/the/example/endpoint/with/trailing/slash?exampleParam=1')203 const loc = new URL(resp.headers.get('location')!)204205 expect(resp).not.toBeNull()206 expect(resp.status).toBe(301)207 expect(loc.pathname).toBe('/the/example/endpoint/with/trailing/slash/')208 expect(loc.searchParams.get('exampleParam')).toBe('1')209 })210211 it('should handle HEAD request for root path correctly', async () => {212 const resp = await app.request('/', { method: 'HEAD' })213214 expect(resp).not.toBeNull()215 expect(resp.status).toBe(200)216 })217218 it('should handle HEAD request for file-like paths correctly', async () => {219 const resp = await app.request('/the/example/simulate/a.file', { method: 'HEAD' })220221 expect(resp).not.toBeNull()222 expect(resp.status).toBe(200)223 })224225 it('should handle HEAD request for path with trailing slash correctly', async () => {226 const resp = await app.request('/the/example/endpoint/with/trailing/slash/', {227 method: 'HEAD',228 })229230 expect(resp).not.toBeNull()231 expect(resp.status).toBe(200)232 })233234 it('should redirect HEAD request for path without trailing slash to one with it', async () => {235 const resp = await app.request('/the/example/endpoint/with/trailing/slash', {236 method: 'HEAD',237 })238 const loc = new URL(resp.headers.get('location')!)239240 expect(resp).not.toBeNull()241 expect(resp.status).toBe(301)242 expect(loc.pathname).toBe('/the/example/endpoint/with/trailing/slash/')243 })244245 it('should preserve query parameters when redirecting HEAD requests', async () => {246 const resp = await app.request('/the/example/endpoint/with/trailing/slash?exampleParam=1', {247 method: 'HEAD',248 })249 const loc = new URL(resp.headers.get('location')!)250251 expect(resp).not.toBeNull()252 expect(resp.status).toBe(301)253 expect(loc.pathname).toBe('/the/example/endpoint/with/trailing/slash/')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 }))318319 app.get('/', async (c) => {320 return c.text('ok')321 })322 app.get('/my-path/*', async (c) => {323 return c.text('wildcard')324 })325 app.get('/exact-path/', async (c) => {326 return c.text('exact')327 })328329 it('should handle GET request for root path correctly', async () => {330 const resp = await app.request('/')331332 expect(resp).not.toBeNull()333 expect(resp.status).toBe(200)334 })335336 it('should redirect wildcard route without trailing slash', async () => {337 const resp = await app.request('/my-path/something/else')338 const loc = new URL(resp.headers.get('location')!)339340 expect(resp).not.toBeNull()341 expect(resp.status).toBe(301)342 expect(loc.pathname).toBe('/my-path/something/else/')343 })344345 it('should not redirect wildcard route with trailing slash', async () => {346 const resp = await app.request('/my-path/something/else/')347348 expect(resp).not.toBeNull()349 expect(resp.status).toBe(200)350 expect(await resp.text()).toBe('wildcard')351 })352353 it('should redirect exact route without trailing slash', async () => {354 const resp = await app.request('/exact-path')355 const loc = new URL(resp.headers.get('location')!)356357 expect(resp).not.toBeNull()358 expect(resp.status).toBe(301)359 expect(loc.pathname).toBe('/exact-path/')360 })361362 it('should preserve query parameters when redirecting', async () => {363 const resp = await app.request('/my-path/something?param=1')364 const loc = new URL(resp.headers.get('location')!)365366 expect(resp).not.toBeNull()367 expect(resp.status).toBe(301)368 expect(loc.pathname).toBe('/my-path/something/')369 expect(loc.searchParams.get('param')).toBe('1')370 })371372 it('should handle HEAD request for wildcard route without trailing slash', async () => {373 const resp = await app.request('/my-path/something', { method: 'HEAD' })374 const loc = new URL(resp.headers.get('location')!)375376 expect(resp).not.toBeNull()377 expect(resp.status).toBe(301)378 expect(loc.pathname).toBe('/my-path/something/')379 })380 })381})