Add function to add a route
All checks were successful
Cargo Build & Test / Tests (pull_request) Successful in 9s
Cargo Build & Test / Test publish (pull_request) Successful in 10s
Cargo Build & Test / check-cargo-version (pull_request) Successful in 7s

This commit is contained in:
Jan Gleytenhoover 2024-08-20 21:58:14 +02:00
parent 97d7946554
commit 55fb3a196e

@ -22,5 +22,34 @@ impl Router {
None => respond_not_found(request), 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);
}
} }