From 55fb3a196e3e89cb5b0261a990ea551dcb1ce1f4 Mon Sep 17 00:00:00 2001 From: Milkman337 Date: Tue, 20 Aug 2024 21:58:14 +0200 Subject: [PATCH] Add function to add a route --- src/router.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/router.rs b/src/router.rs index 941c9dd..9999ab5 100644 --- a/src/router.rs +++ b/src/router.rs @@ -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); + } }