rum_framework/
lib_test.rs

1#[cfg(test)]
2use crate::{method::MethodType, handler::Handler, context::RumContext};
3
4#[test]
5fn check_basic_route() {
6
7    let mut handler = Handler::new();
8    handler.add_route(MethodType::GET, "/app/v1/test", |_:&mut RumContext|{ });
9    let target_route = "/app/v1/test";
10    let route_segs: Vec<&str> = target_route.trim_end_matches('/').split('/').collect();
11    let result = handler.router.get_info_and_controller(MethodType::GET, &route_segs);
12    assert_eq!(result.is_some(), true);
13}
14
15#[test]
16fn check_route_with_params() {
17    let mut handler = Handler::new();
18    handler.add_route(MethodType::GET, "/app/v1/test/:param1/page", |_:&mut RumContext|{ });
19    let target_route = "/app/v1/test/aaaaa/page";
20    let route_segs: Vec<&str> = target_route.trim_end_matches('/').split('/').collect();
21    let result = handler.router.get_info_and_controller(MethodType::GET, &route_segs);
22    assert_eq!(result.is_some(), true);
23}
24
25#[test]
26fn check_different_route() {
27    let mut handler = Handler::new();
28    handler.add_route(MethodType::GET, "/app/v1/test", |_:&mut RumContext|{ });
29    let target_route = "/app/v1/test2";
30    let route_segs: Vec<&str> = target_route.trim_end_matches('/').split('/').collect();
31    let result = handler.router.get_info_and_controller(MethodType::GET, &route_segs);
32    assert_eq!(result.is_some(), false);
33}
34
35#[test]
36fn check_same_route_but_diffent_method() {
37    let mut handler = Handler::new();
38    handler.add_route(MethodType::POST, "/app/v1/test", |_:&mut RumContext|{ });
39    let target_route = "/app/v1/test";
40    let route_segs: Vec<&str> = target_route.trim_end_matches('/').split('/').collect();
41    let result = handler.router.get_info_and_controller(MethodType::GET, &route_segs);
42    assert_eq!(result.is_some(), false);
43}
44
45#[test]
46#[should_panic]
47fn check_set_same_controller_crash() {
48    let mut handler = Handler::new();
49    handler.add_route(MethodType::POST, "/app/v1/test", |_:&mut RumContext|{ });
50    handler.add_route(MethodType::POST, "/app/v1/test", |_:&mut RumContext|{ });
51}
52
53#[test]
54#[should_panic]
55fn check_set_same_position_multiple_param_crash() {
56    let mut handler = Handler::new();
57    handler.add_route(MethodType::POST, "/app/v1/test/:param1/page", |_:&mut RumContext|{ });
58    handler.add_route(MethodType::POST, "/app/v1/test/:param2/page", |_:&mut RumContext|{ });
59}