@@ -87,6 +87,73 @@ describe('Resolve trailing slash', () => {
8787 } )
8888 } )
8989
90+ describe ( 'trimTrailingSlash middleware with alwaysRedirect option' , ( ) => {
91+ const app = new Hono ( )
92+ app . use ( '*' , trimTrailingSlash ( { alwaysRedirect : true } ) )
93+
94+ 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+ } )
103+
104+ it ( 'should handle GET request for root path correctly' , async ( ) => {
105+ const resp = await app . request ( '/' )
106+
107+ expect ( resp ) . not . toBeNull ( )
108+ expect ( resp . status ) . toBe ( 200 )
109+ } )
110+
111+ 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' ) ! )
114+
115+ expect ( resp ) . not . toBeNull ( )
116+ expect ( resp . status ) . toBe ( 301 )
117+ expect ( loc . pathname ) . toBe ( '/my-path/something/else' )
118+ } )
119+
120+ it ( 'should not redirect wildcard route without trailing slash' , async ( ) => {
121+ const resp = await app . request ( '/my-path/something/else' )
122+
123+ expect ( resp ) . not . toBeNull ( )
124+ expect ( resp . status ) . toBe ( 200 )
125+ expect ( await resp . text ( ) ) . toBe ( 'wildcard' )
126+ } )
127+
128+ 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' ) ! )
131+
132+ expect ( resp ) . not . toBeNull ( )
133+ expect ( resp . status ) . toBe ( 301 )
134+ expect ( loc . pathname ) . toBe ( '/exact-path' )
135+ } )
136+
137+ 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' ) ! )
140+
141+ 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+ } )
146+
147+ 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' ) ! )
150+
151+ expect ( resp ) . not . toBeNull ( )
152+ expect ( resp . status ) . toBe ( 301 )
153+ expect ( loc . pathname ) . toBe ( '/my-path/something' )
154+ } )
155+ } )
156+
90157 describe ( 'appendTrailingSlash middleware' , ( ) => {
91158 const app = new Hono ( { strict : true } )
92159 app . use ( '*' , appendTrailingSlash ( ) )
@@ -187,4 +254,71 @@ describe('Resolve trailing slash', () => {
187254 expect ( loc . searchParams . get ( 'exampleParam' ) ) . toBe ( '1' )
188255 } )
189256 } )
257+
258+ describe ( 'appendTrailingSlash middleware with alwaysRedirect option' , ( ) => {
259+ const app = new Hono ( )
260+ app . use ( '*' , appendTrailingSlash ( { alwaysRedirect : true } ) )
261+
262+ app . get ( '/' , async ( c ) => {
263+ return c . text ( 'ok' )
264+ } )
265+ app . get ( '/my-path/*' , async ( c ) => {
266+ return c . text ( 'wildcard' )
267+ } )
268+ app . get ( '/exact-path/' , async ( c ) => {
269+ return c . text ( 'exact' )
270+ } )
271+
272+ it ( 'should handle GET request for root path correctly' , async ( ) => {
273+ const resp = await app . request ( '/' )
274+
275+ expect ( resp ) . not . toBeNull ( )
276+ expect ( resp . status ) . toBe ( 200 )
277+ } )
278+
279+ it ( 'should redirect wildcard route without trailing slash' , async ( ) => {
280+ const resp = await app . request ( '/my-path/something/else' )
281+ const loc = new URL ( resp . headers . get ( 'location' ) ! )
282+
283+ expect ( resp ) . not . toBeNull ( )
284+ expect ( resp . status ) . toBe ( 301 )
285+ expect ( loc . pathname ) . toBe ( '/my-path/something/else/' )
286+ } )
287+
288+ it ( 'should not redirect wildcard route with trailing slash' , async ( ) => {
289+ const resp = await app . request ( '/my-path/something/else/' )
290+
291+ expect ( resp ) . not . toBeNull ( )
292+ expect ( resp . status ) . toBe ( 200 )
293+ expect ( await resp . text ( ) ) . toBe ( 'wildcard' )
294+ } )
295+
296+ it ( 'should redirect exact route without trailing slash' , async ( ) => {
297+ const resp = await app . request ( '/exact-path' )
298+ const loc = new URL ( resp . headers . get ( 'location' ) ! )
299+
300+ expect ( resp ) . not . toBeNull ( )
301+ expect ( resp . status ) . toBe ( 301 )
302+ expect ( loc . pathname ) . toBe ( '/exact-path/' )
303+ } )
304+
305+ it ( 'should preserve query parameters when redirecting' , async ( ) => {
306+ const resp = await app . request ( '/my-path/something?param=1' )
307+ const loc = new URL ( resp . headers . get ( 'location' ) ! )
308+
309+ expect ( resp ) . not . toBeNull ( )
310+ expect ( resp . status ) . toBe ( 301 )
311+ expect ( loc . pathname ) . toBe ( '/my-path/something/' )
312+ expect ( loc . searchParams . get ( 'param' ) ) . toBe ( '1' )
313+ } )
314+
315+ it ( 'should handle HEAD request for wildcard route without trailing slash' , async ( ) => {
316+ const resp = await app . request ( '/my-path/something' , { method : 'HEAD' } )
317+ const loc = new URL ( resp . headers . get ( 'location' ) ! )
318+
319+ expect ( resp ) . not . toBeNull ( )
320+ expect ( resp . status ) . toBe ( 301 )
321+ expect ( loc . pathname ) . toBe ( '/my-path/something/' )
322+ } )
323+ } )
190324} )
0 commit comments