1
+ <<<<<<< HEAD
1
2
use std:: {
2
3
cmp:: Ordering ,
3
4
collections:: { btree_map , BTreeMap } ,
4
5
ops:: Index ,
5
6
} ;
6
7
7
8
use crate :: nfa:: { CharacterClass , NFA } ;
9
+ =======
10
+ use nfa:: CharacterClass ;
11
+ use nfa:: NFA ;
12
+ use std:: cmp:: Ordering ;
13
+ use std:: collections:: btree_map ;
14
+ use std:: collections:: BTreeMap ;
15
+ use std:: collections:: HashSet ;
16
+ use std:: ops:: Index ;
17
+ >>>>>>> Add a test after parsing a route to ensure it doesn' t contain duplicate parameter names
8
18
9
19
pub mod nfa;
10
20
@@ -174,6 +184,12 @@ impl<T> Router<T> {
174
184
metadata. statics += 1 ;
175
185
}
176
186
}
187
+ let mut hashes = HashSet :: new ( ) ;
188
+ for name in metadata. param_names . iter ( ) {
189
+ if !hashes. insert ( name. to_string ( ) ) {
190
+ panic ! ( "Duplicate name '{}' in route {}" , name. to_string( ) , & route) ;
191
+ }
192
+ }
177
193
178
194
nfa. acceptance ( state) ;
179
195
nfa. metadata ( state, metadata) ;
@@ -360,6 +376,7 @@ mod tests {
360
376
router. add ( "/a/*b/c" , "abc" . to_string ( ) ) ;
361
377
router. add ( "/a/*b/c/:d" , "abcd" . to_string ( ) ) ;
362
378
379
+ <<<<<<< HEAD
363
380
let m = router. recognize( "/a/foo" ) . unwrap ( ) ;
364
381
assert_eq ! ( * m. handler, "ab" . to_string( ) ) ;
365
382
assert_eq ! ( m. params, params( "b" , "foo" ) ) ;
@@ -379,6 +396,49 @@ mod tests {
379
396
let m = router. recognize( "/a/foo/c/baz" ) . unwrap( ) ;
380
397
assert_eq ! ( * m. handler, "abcd" . to_string( ) ) ;
381
398
assert_eq ! ( m. params, two_params( "b" , "foo" , "d" , "baz" ) ) ;
399
+ =======
400
+ #[ test]
401
+ #[ should_panic]
402
+ fn duplicate_named_parameter( ) {
403
+ let mut router = Router :: new ( ) ;
404
+ router. add( "/foo/:bar/:bar" , "test" . to_string( ) ) ;
405
+ }
406
+
407
+ #[ test]
408
+ #[ should_panic]
409
+ fn duplicate_star_parameter( ) {
410
+ let mut router = Router :: new ( ) ;
411
+ router. add( "/foo/*bar/*bar" , "test" . to_string( ) ) ;
412
+ }
413
+
414
+ #[ test]
415
+ #[ should_panic]
416
+ fn duplicate_mixed_parameter( ) {
417
+ let mut router = Router :: new ( ) ;
418
+ router. add( "/foo/*bar/:bar" , "test" . to_string( ) ) ;
419
+ }
420
+
421
+ #[ test]
422
+ #[ should_panic]
423
+ fn duplicate_mixed_reversed_parameter( ) {
424
+ let mut router = Router :: new ( ) ;
425
+ router. add( "/foo/:bar/*bar" , "test" . to_string( ) ) ;
426
+ }
427
+
428
+ #[ test]
429
+ #[ should_panic]
430
+ fn duplicate_separated_parameter( ) {
431
+ let mut router = Router :: new ( ) ;
432
+ router. add( "/foo/:bar/bleg/:bar" , "test" . to_string( ) ) ;
433
+ }
434
+
435
+ #[ allow( dead_code) ]
436
+ fn params ( key : & str , val : & str ) -> Params {
437
+ let mut map = Params :: new( ) ;
438
+ map. insert( key. to_string( ) , val. to_string( ) ) ;
439
+ map
440
+ }
441
+ >>>>>>> Add a test after parsing a route to ensure it doesn' t contain duplicate parameter names
382
442
383
443
let m = router. recognize( "/a/foo/bar/c/baz" ) . unwrap( ) ;
384
444
assert_eq ! ( * m. handler, "abcd" . to_string( ) ) ;
0 commit comments