Add function to add a route
This commit is contained in:
parent
97d7946554
commit
55fb3a196e
@ -22,5 +22,34 @@ impl Router {
|
||||
None => respond_not_found(request),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_route(&mut self, route: &str, route_function: fn(Request)) {
|
||||
self.routes.insert(route.to_string(), route_function);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::HashMap;
|
||||
|
||||
use tiny_http::Request;
|
||||
|
||||
use super::Router;
|
||||
|
||||
#[test]
|
||||
fn test_add_route() {
|
||||
let mut router = Router::new();
|
||||
|
||||
fn test_route(_request: Request) {
|
||||
|
||||
}
|
||||
|
||||
router.add_route("/test", test_route as fn(Request));
|
||||
|
||||
let mut expected = HashMap::new();
|
||||
expected.insert("/test".to_string(), test_route as fn(Request));
|
||||
|
||||
assert_eq!(router.routes, expected);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user