Skip to content

Commit

Permalink
Add a test after parsing a route to ensure it doesn't contain duplica…
Browse files Browse the repository at this point in the history
…te parameter names
  • Loading branch information
Gameldar committed Aug 16, 2019
1 parent 61e3b07 commit 32c73d2
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use nfa::NFA;
use std::cmp::Ordering;
use std::collections::btree_map;
use std::collections::BTreeMap;
use std::collections::HashSet;
use std::ops::Index;

pub mod nfa;
Expand Down Expand Up @@ -173,6 +174,12 @@ impl<T> Router<T> {
metadata.statics += 1;
}
}
let mut hashes = HashSet::new();
for name in metadata.param_names.iter() {
if !hashes.insert(name.to_string()) {
panic!("Duplicate name '{}' in route {}", name.to_string(), &route);
}
}

nfa.acceptance(state);
nfa.metadata(state, metadata);
Expand Down Expand Up @@ -362,6 +369,41 @@ fn unnamed_parameters() {
assert_eq!(m.params, params("bar", "test"));
}

#[test]
#[should_panic]
fn duplicate_named_parameter() {
let mut router = Router::new();
router.add("/foo/:bar/:bar", "test".to_string());
}

#[test]
#[should_panic]
fn duplicate_star_parameter() {
let mut router = Router::new();
router.add("/foo/*bar/*bar", "test".to_string());
}

#[test]
#[should_panic]
fn duplicate_mixed_parameter() {
let mut router = Router::new();
router.add("/foo/*bar/:bar", "test".to_string());
}

#[test]
#[should_panic]
fn duplicate_mixed_reversed_parameter() {
let mut router = Router::new();
router.add("/foo/:bar/*bar", "test".to_string());
}

#[test]
#[should_panic]
fn duplicate_separated_parameter() {
let mut router = Router::new();
router.add("/foo/:bar/bleg/:bar", "test".to_string());
}

#[allow(dead_code)]
fn params(key: &str, val: &str) -> Params {
let mut map = Params::new();
Expand Down

0 comments on commit 32c73d2

Please sign in to comment.