@@ -70,6 +70,78 @@ Deno.test("UrlPatternRouter - wrong + correct method", () => {
7070 } ) ;
7171} ) ;
7272
73+ Deno . test ( "UrlPatternRouter - trailing slash matches route without slash" , ( ) => {
74+ const router = new UrlPatternRouter ( ) ;
75+ const A = ( ) => { } ;
76+ router . add ( "GET" , "/wissen" , [ A ] ) ;
77+
78+ const res = router . match ( "GET" , new URL ( "/wissen/" , "http://localhost" ) ) ;
79+ expect ( res ) . toEqual ( {
80+ params : Object . create ( null ) ,
81+ handlers : [ A ] ,
82+ methodMatch : true ,
83+ pattern : "/wissen" ,
84+ } ) ;
85+ } ) ;
86+
87+ Deno . test ( "UrlPatternRouter - no trailing slash matches route with slash" , ( ) => {
88+ const router = new UrlPatternRouter ( ) ;
89+ const A = ( ) => { } ;
90+ router . add ( "GET" , "/wissen/" , [ A ] ) ;
91+
92+ const res = router . match ( "GET" , new URL ( "/wissen" , "http://localhost" ) ) ;
93+ expect ( res ) . toEqual ( {
94+ params : Object . create ( null ) ,
95+ handlers : [ A ] ,
96+ methodMatch : true ,
97+ pattern : "/wissen/" ,
98+ } ) ;
99+ } ) ;
100+
101+ Deno . test ( "UrlPatternRouter - exact match takes priority over trailing slash fallback" , ( ) => {
102+ const router = new UrlPatternRouter ( ) ;
103+ const A = ( ) => { } ;
104+ const B = ( ) => { } ;
105+ router . add ( "GET" , "/wissen" , [ A ] ) ;
106+ router . add ( "GET" , "/wissen/" , [ B ] ) ;
107+
108+ const withSlash = router . match (
109+ "GET" ,
110+ new URL ( "/wissen/" , "http://localhost" ) ,
111+ ) ;
112+ expect ( withSlash ) . toEqual ( {
113+ params : Object . create ( null ) ,
114+ handlers : [ B ] ,
115+ methodMatch : true ,
116+ pattern : "/wissen/" ,
117+ } ) ;
118+
119+ const withoutSlash = router . match (
120+ "GET" ,
121+ new URL ( "/wissen" , "http://localhost" ) ,
122+ ) ;
123+ expect ( withoutSlash ) . toEqual ( {
124+ params : Object . create ( null ) ,
125+ handlers : [ A ] ,
126+ methodMatch : true ,
127+ pattern : "/wissen" ,
128+ } ) ;
129+ } ) ;
130+
131+ Deno . test ( "UrlPatternRouter - root trailing slash does not double-match" , ( ) => {
132+ const router = new UrlPatternRouter ( ) ;
133+ const A = ( ) => { } ;
134+ router . add ( "GET" , "/" , [ A ] ) ;
135+
136+ const res = router . match ( "GET" , new URL ( "/" , "http://localhost" ) ) ;
137+ expect ( res ) . toEqual ( {
138+ params : Object . create ( null ) ,
139+ handlers : [ A ] ,
140+ methodMatch : true ,
141+ pattern : "/" ,
142+ } ) ;
143+ } ) ;
144+
73145Deno . test ( "UrlPatternRouter - convert patterns automatically" , ( ) => {
74146 const router = new UrlPatternRouter ( ) ;
75147 const A = ( ) => { } ;
0 commit comments