1use super::Route;
2use crate::core::into_response::IntoResponse;
3use crate::extractor::{FromRequest, handler_from_extractor, handler_from_extractor_with_request};
4use crate::handler::HandlerFn;
5use crate::{Handler, HandlerWrapper, Method, Request, Response, Result};
6use async_trait::async_trait;
7use std::collections::HashMap;
8use std::future::Future;
9use std::sync::Arc;
10
11pub trait HandlerGetter {
12 fn get_handler_mut(&mut self) -> &mut HashMap<Method, Arc<dyn Handler>>;
13 fn insert_handler(self, method: Method, handler: Arc<dyn Handler>) -> Self;
14 fn handler(self, method: Method, handler: Arc<dyn Handler>) -> Self;
15}
16
17pub trait HandlerAppend<F, T, Fut>: HandlerGetter
18where
19 Fut: Future<Output = Result<T>> + Send + 'static,
20 F: Fn(Request) -> Fut + Send + Sync + 'static,
21 T: IntoResponse,
22{
23 fn get(self, handler: F) -> Self;
24 fn post(self, handler: F) -> Self;
25 fn put(self, handler: F) -> Self;
26 fn delete(self, handler: F) -> Self;
27 fn patch(self, handler: F) -> Self;
28 fn options(self, handler: F) -> Self;
29 fn handler_append(&mut self, method: Method, handler: F) {
30 let handler = Arc::new(HandlerWrapper::new(handler));
31 let handler_map = self.get_handler_mut();
32 handler_map.insert(method, handler);
33 }
34}
35
36impl HandlerGetter for Route {
37 fn get_handler_mut(&mut self) -> &mut HashMap<Method, Arc<dyn Handler>> {
38 if self.path == self.create_path {
39 &mut self.handler
40 } else {
41 let mut iter = self.create_path.splitn(2, '/');
42 let _local_url = iter.next().unwrap_or("");
43 let last_url = iter.next().unwrap_or("");
44 let route = self
45 .children
46 .iter_mut()
47 .find(|c| c.create_path == last_url)
48 .unwrap();
49 <Route as HandlerGetter>::get_handler_mut(route)
50 }
51 }
52 fn insert_handler(mut self, method: Method, handler: Arc<dyn Handler>) -> Self {
53 self.handler.insert(method, handler);
54 self
55 }
56
57 fn handler(mut self, method: Method, handler: Arc<dyn Handler>) -> Self {
58 self.get_handler_mut().insert(method, handler);
59 self
60 }
61}
62
63impl<F, T, Fut> HandlerAppend<F, T, Fut> for Route
64where
65 Fut: Future<Output = Result<T>> + Send + 'static,
66 F: Fn(Request) -> Fut + Send + Sync + 'static,
67 T: IntoResponse,
68{
69 fn get(mut self, handler: F) -> Self {
70 self.handler_append(Method::GET, handler);
71 self
72 }
73
74 fn post(mut self, handler: F) -> Self {
75 self.handler_append(Method::POST, handler);
76 self
77 }
78
79 fn put(mut self, handler: F) -> Self {
80 self.handler_append(Method::PUT, handler);
81 self
82 }
83
84 fn delete(mut self, handler: F) -> Self {
85 self.handler_append(Method::DELETE, handler);
86 self
87 }
88
89 fn patch(mut self, handler: F) -> Self {
90 self.handler_append(Method::PATCH, handler);
91 self
92 }
93
94 fn options(mut self, handler: F) -> Self {
95 self.handler_append(Method::OPTIONS, handler);
96 self
97 }
98}
99
100pub trait IntoRouteHandler<Args> {
102 fn into_handler(self) -> std::sync::Arc<dyn Handler>;
103}
104
105trait RouteDispatch: Sized {
106 fn into_arc_handler<F, Fut>(handler: F) -> std::sync::Arc<dyn Handler>
107 where
108 F: Fn(Request) -> Fut + Send + Sync + 'static,
109 Fut: Future<Output = Self> + Send + 'static;
110}
111
112impl RouteDispatch for Response {
113 fn into_arc_handler<F, Fut>(handler: F) -> std::sync::Arc<dyn Handler>
114 where
115 F: Fn(Request) -> Fut + Send + Sync + 'static,
116 Fut: Future<Output = Self> + Send + 'static,
117 {
118 HandlerFn::new(handler).arc()
119 }
120}
121
122impl<T> RouteDispatch for crate::error::SilentResult<T>
123where
124 T: IntoResponse + Send + 'static,
125{
126 fn into_arc_handler<F, Fut>(handler: F) -> std::sync::Arc<dyn Handler>
127 where
128 F: Fn(Request) -> Fut + Send + Sync + 'static,
129 Fut: Future<Output = Self> + Send + 'static,
130 {
131 std::sync::Arc::new(HandlerWrapper::new(handler))
132 }
133}
134
135#[allow(dead_code)]
138pub(crate) struct IntoResponseResultHandler<F> {
139 pub(crate) handler: F,
140}
141
142#[async_trait]
143impl<F, T, E, Fut> Handler for IntoResponseResultHandler<F>
144where
145 T: IntoResponse + Send + 'static,
146 E: IntoResponse + Send + 'static,
147 Fut: Future<Output = std::result::Result<T, E>> + Send + 'static,
148 F: Fn(Request) -> Fut + Send + Sync + 'static,
149{
150 async fn call(&self, req: Request) -> Result<Response> {
151 match (self.handler)(req).await {
152 Ok(v) => Ok(v.into_response()),
153 Err(e) => Ok(e.into_response()),
154 }
155 }
156}
157
158impl<F, Fut> IntoRouteHandler<crate::Request> for F
159where
160 F: Fn(Request) -> Fut + Send + Sync + 'static,
161 Fut: Future + Send + 'static,
162 Fut::Output: RouteDispatch,
163{
164 fn into_handler(self) -> std::sync::Arc<dyn Handler> {
165 <Fut::Output as RouteDispatch>::into_arc_handler(self)
166 }
167}
168
169impl<Args, F, Fut, T> IntoRouteHandler<Args> for F
170where
171 Args: FromRequest + Send + 'static,
172 <Args as FromRequest>::Rejection: Into<Response> + Send + 'static,
173 F: Fn(Args) -> Fut + Send + Sync + 'static,
174 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
175 T: IntoResponse + Send + 'static,
176{
177 fn into_handler(self) -> std::sync::Arc<dyn Handler> {
178 let adapted = handler_from_extractor::<Args, F, Fut, T>(self);
179 std::sync::Arc::new(HandlerWrapper::new(adapted))
180 }
181}
182
183impl<Args, F, Fut, T> IntoRouteHandler<(Request, Args)> for F
184where
185 Args: FromRequest + Send + 'static,
186 <Args as FromRequest>::Rejection: Into<Response> + Send + 'static,
187 F: Fn(Request, Args) -> Fut + Send + Sync + 'static,
188 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
189 T: IntoResponse + Send + 'static,
190{
191 fn into_handler(self) -> std::sync::Arc<dyn Handler> {
192 let adapted = handler_from_extractor_with_request::<Args, F, Fut, T>(self);
193 std::sync::Arc::new(HandlerWrapper::new(adapted))
194 }
195}
196
197impl Route {
198 pub fn get<H, Args>(self, handler: H) -> Self
199 where
200 H: IntoRouteHandler<Args>,
201 {
202 let handler = handler.into_handler();
203 <Route as HandlerGetter>::handler(self, Method::GET, handler)
204 }
205
206 pub fn post<H, Args>(self, handler: H) -> Self
207 where
208 H: IntoRouteHandler<Args>,
209 {
210 let handler = handler.into_handler();
211 <Route as HandlerGetter>::handler(self, Method::POST, handler)
212 }
213
214 pub fn put<H, Args>(self, handler: H) -> Self
215 where
216 H: IntoRouteHandler<Args>,
217 {
218 let handler = handler.into_handler();
219 <Route as HandlerGetter>::handler(self, Method::PUT, handler)
220 }
221
222 pub fn delete<H, Args>(self, handler: H) -> Self
223 where
224 H: IntoRouteHandler<Args>,
225 {
226 let handler = handler.into_handler();
227 <Route as HandlerGetter>::handler(self, Method::DELETE, handler)
228 }
229
230 pub fn patch<H, Args>(self, handler: H) -> Self
231 where
232 H: IntoRouteHandler<Args>,
233 {
234 let handler = handler.into_handler();
235 <Route as HandlerGetter>::handler(self, Method::PATCH, handler)
236 }
237
238 pub fn options<H, Args>(self, handler: H) -> Self
239 where
240 H: IntoRouteHandler<Args>,
241 {
242 let handler = handler.into_handler();
243 <Route as HandlerGetter>::handler(self, Method::OPTIONS, handler)
244 }
245}
246
247impl Route {
249 pub fn get_ex<Args, F, Fut, T>(mut self, f: F) -> Self
250 where
251 Args: crate::extractor::FromRequest + Send + 'static,
252 <Args as crate::extractor::FromRequest>::Rejection: Into<Response> + Send + 'static,
253 F: Fn(Args) -> Fut + Send + Sync + 'static,
254 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
255 T: IntoResponse + Send + 'static,
256 {
257 let adapted = handler_from_extractor::<Args, F, Fut, T>(f);
258 self.handler_append(Method::GET, adapted);
259 self
260 }
261
262 pub fn post_ex<Args, F, Fut, T>(mut self, f: F) -> Self
263 where
264 Args: crate::extractor::FromRequest + Send + 'static,
265 <Args as crate::extractor::FromRequest>::Rejection: Into<Response> + Send + 'static,
266 F: Fn(Args) -> Fut + Send + Sync + 'static,
267 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
268 T: IntoResponse + Send + 'static,
269 {
270 let adapted = handler_from_extractor::<Args, F, Fut, T>(f);
271 self.handler_append(Method::POST, adapted);
272 self
273 }
274
275 pub fn put_ex<Args, F, Fut, T>(mut self, f: F) -> Self
276 where
277 Args: crate::extractor::FromRequest + Send + 'static,
278 <Args as crate::extractor::FromRequest>::Rejection: Into<Response> + Send + 'static,
279 F: Fn(Args) -> Fut + Send + Sync + 'static,
280 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
281 T: IntoResponse + Send + 'static,
282 {
283 let adapted = handler_from_extractor::<Args, _, _, T>(f);
284 self.handler_append(Method::PUT, adapted);
285 self
286 }
287
288 pub fn delete_ex<Args, F, Fut, T>(mut self, f: F) -> Self
289 where
290 Args: crate::extractor::FromRequest + Send + 'static,
291 <Args as crate::extractor::FromRequest>::Rejection: Into<Response> + Send + 'static,
292 F: Fn(Args) -> Fut + Send + Sync + 'static,
293 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
294 T: IntoResponse + Send + 'static,
295 {
296 let adapted = handler_from_extractor::<Args, _, _, T>(f);
297 self.handler_append(Method::DELETE, adapted);
298 self
299 }
300
301 pub fn patch_ex<Args, F, Fut, T>(mut self, f: F) -> Self
302 where
303 Args: crate::extractor::FromRequest + Send + 'static,
304 <Args as crate::extractor::FromRequest>::Rejection: Into<Response> + Send + 'static,
305 F: Fn(Args) -> Fut + Send + Sync + 'static,
306 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
307 T: IntoResponse + Send + 'static,
308 {
309 let adapted = handler_from_extractor::<Args, _, _, T>(f);
310 self.handler_append(Method::PATCH, adapted);
311 self
312 }
313
314 pub fn options_ex<Args, F, Fut, T>(mut self, f: F) -> Self
315 where
316 Args: crate::extractor::FromRequest + Send + 'static,
317 <Args as crate::extractor::FromRequest>::Rejection: Into<Response> + Send + 'static,
318 F: Fn(Args) -> Fut + Send + Sync + 'static,
319 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
320 T: IntoResponse + Send + 'static,
321 {
322 let adapted = handler_from_extractor::<Args, _, _, T>(f);
323 self.handler_append(Method::OPTIONS, adapted);
324 self
325 }
326}
327
328#[cfg(test)]
329mod tests {
330 use super::*;
331 use crate::Response;
332 use crate::error::SilentResult;
333 use std::sync::Arc;
334
335 #[test]
338 fn test_get_handler_mut_current_route() {
339 let mut route = Route::new("test");
340 route.create_path = "test".to_string();
341
342 let handler_map = route.get_handler_mut();
343 assert!(handler_map.is_empty());
344 }
345
346 #[test]
347 fn test_get_handler_mut_child_route() {
348 let mut route = Route::new("api");
349 route.create_path = "api/test".to_string();
350
351 let child_route = Route::new("test");
353 route.children.push(child_route);
354
355 let handler_map = route.get_handler_mut();
357 assert!(handler_map.is_empty());
358 }
359
360 #[test]
361 fn test_insert_handler() {
362 let route = Route::new("test");
363 let handler = Arc::new(HandlerWrapper::new(|_req: Request| async {
364 Ok(Response::text("test"))
365 }));
366
367 let route = route.insert_handler(Method::GET, handler);
368 assert!(route.handler.contains_key(&Method::GET));
369 }
370
371 #[test]
372 fn test_handler_method() {
373 let route = Route::new("test");
374 let handler = Arc::new(HandlerWrapper::new(|_req: Request| async {
375 Ok(Response::text("test"))
376 }));
377
378 let route = route.handler(Method::POST, handler);
379 assert!(route.handler.contains_key(&Method::POST));
380 }
381
382 #[test]
385 fn test_get_method() {
386 let route = Route::new("test").get(|_req: Request| async { Ok("test") });
387
388 assert!(route.handler.contains_key(&Method::GET));
389 }
390
391 #[test]
392 fn test_post_method() {
393 let route = Route::new("test").post(|_req: Request| async { Ok("test") });
394
395 assert!(route.handler.contains_key(&Method::POST));
396 }
397
398 #[test]
399 fn test_put_method() {
400 let route = Route::new("test").put(|_req: Request| async { Ok("test") });
401
402 assert!(route.handler.contains_key(&Method::PUT));
403 }
404
405 #[test]
406 fn test_delete_method() {
407 let route = Route::new("test").delete(|_req: Request| async { Ok("test") });
408
409 assert!(route.handler.contains_key(&Method::DELETE));
410 }
411
412 #[test]
413 fn test_patch_method() {
414 let route = Route::new("test").patch(|_req: Request| async { Ok("test") });
415
416 assert!(route.handler.contains_key(&Method::PATCH));
417 }
418
419 #[test]
420 fn test_options_method() {
421 let route = Route::new("test").options(|_req: Request| async { Ok("test") });
422
423 assert!(route.handler.contains_key(&Method::OPTIONS));
424 }
425
426 #[test]
427 fn test_multiple_methods() {
428 let route = Route::new("test")
429 .get(|_req: Request| async { Ok("get") })
430 .post(|_req: Request| async { Ok("post") })
431 .put(|_req: Request| async { Ok("put") });
432
433 assert!(route.handler.contains_key(&Method::GET));
434 assert!(route.handler.contains_key(&Method::POST));
435 assert!(route.handler.contains_key(&Method::PUT));
436 }
437
438 #[test]
439 fn test_handler_append_method() {
440 let mut route = Route::new("test");
441
442 route.handler_append(Method::GET, |_req: Request| async { Ok("test") });
443
444 assert!(route.handler.contains_key(&Method::GET));
445 }
446
447 #[test]
450 fn test_response_into_arc_handler() {
451 let handler = |_req: Request| async { Response::text("test") };
452 let arc_handler = Response::into_arc_handler(handler);
453
454 let _ = Arc::into_raw(arc_handler);
456 }
457
458 #[test]
459 fn test_silent_result_into_arc_handler() {
460 let handler = |_req: Request| async { Ok(Response::text("test")) };
461 let arc_handler = <SilentResult<Response>>::into_arc_handler(handler);
462
463 let _ = Arc::into_raw(arc_handler);
465 }
466
467 #[test]
470 fn test_into_handler_with_request() {
471 let handler: fn(Request) -> _ = |_req: Request| async { Ok(Response::text("test")) };
472 let arc_handler = handler.into_handler();
473
474 let _ = Arc::into_raw(arc_handler);
476 }
477
478 #[test]
479 fn test_into_handler_with_response_output() {
480 let handler: fn(Request) -> _ = |_req: Request| async { Response::text("test") };
481 let arc_handler = handler.into_handler();
482
483 let _ = Arc::into_raw(arc_handler);
485 }
486
487 #[test]
490 fn test_route_get_with_into_handler() {
491 let route = Route::new("test").get(|_req: Request| async { Ok(Response::text("get")) });
492
493 assert!(route.handler.contains_key(&Method::GET));
494 }
495
496 #[test]
497 fn test_route_post_with_into_handler() {
498 let route = Route::new("test").post(|_req: Request| async { Ok(Response::text("post")) });
499
500 assert!(route.handler.contains_key(&Method::POST));
501 }
502
503 #[test]
504 fn test_route_put_with_into_handler() {
505 let route = Route::new("test").put(|_req: Request| async { Ok(Response::text("put")) });
506
507 assert!(route.handler.contains_key(&Method::PUT));
508 }
509
510 #[test]
511 fn test_route_delete_with_into_handler() {
512 let route =
513 Route::new("test").delete(|_req: Request| async { Ok(Response::text("delete")) });
514
515 assert!(route.handler.contains_key(&Method::DELETE));
516 }
517
518 #[test]
519 fn test_route_patch_with_into_handler() {
520 let route = Route::new("test").patch(|_req: Request| async { Ok(Response::text("patch")) });
521
522 assert!(route.handler.contains_key(&Method::PATCH));
523 }
524
525 #[test]
526 fn test_route_options_with_into_handler() {
527 let route =
528 Route::new("test").options(|_req: Request| async { Ok(Response::text("options")) });
529
530 assert!(route.handler.contains_key(&Method::OPTIONS));
531 }
532
533 #[test]
534 fn test_route_with_response_output() {
535 let route =
536 Route::new("test").get(|_req: Request| async { Response::text("direct response") });
537
538 assert!(route.handler.contains_key(&Method::GET));
539 }
540
541 #[test]
550 fn test_route_trait_get_with_fn_pointer() {
551 async fn handler(_req: Request) -> Result<Response> {
553 Ok(Response::text("test"))
554 }
555
556 let route = Route::new("test").get(handler);
557 assert!(route.handler.contains_key(&Method::GET));
558 }
559
560 #[test]
561 fn test_into_route_handler_trait_request() {
562 let handler: fn(Request) -> _ = |_req: Request| async { Ok(Response::text("test")) };
564 let _arc_handler = handler.into_handler();
565
566 }
568
569 #[test]
570 fn test_handler_wrapper_arc_conversion() {
571 let wrapper = HandlerWrapper::new(|_req: Request| async { Ok("test") });
573 let _arc: std::sync::Arc<dyn Handler> = std::sync::Arc::new(wrapper);
574 }
575
576 #[test]
577 fn test_handler_fn_arc_conversion() {
578 let handler_fn = HandlerFn::new(|_req: Request| async { Response::text("test") });
580 let _arc = handler_fn.arc();
581 }
582
583 #[test]
584 fn test_multiple_arcs_from_same_handler() {
585 let route = Route::new("test")
587 .get(|_req: Request| async { Ok("get") })
588 .post(|_req: Request| async { Ok("post") });
589
590 if let Some(get_handler) = route.handler.get(&Method::GET) {
592 let _clone1 = get_handler.clone();
593 }
594
595 if let Some(post_handler) = route.handler.get(&Method::POST) {
596 let _clone2 = post_handler.clone();
597 }
598
599 assert_eq!(route.handler.len(), 2);
600 }
601
602 #[test]
603 fn test_handler_storage_type() {
604 let route = Route::new("test").get(|_req: Request| async { Ok("test") });
606
607 if let Some(handler) = route.handler.get(&Method::GET) {
608 let _arc_handler: std::sync::Arc<dyn Handler> = handler.clone();
610 }
611 }
612
613 #[test]
614 fn test_route_trait_post_with_closure() {
615 let route = Route::new("test").post(|_req: Request| async { Ok("post") });
617 assert!(route.handler.contains_key(&Method::POST));
618 }
619
620 #[test]
621 fn test_route_trait_put() {
622 let route = Route::new("test").put(|_req: Request| async { Ok("put") });
624 assert!(route.handler.contains_key(&Method::PUT));
625 }
626
627 #[test]
628 fn test_route_trait_delete() {
629 let route = Route::new("test").delete(|_req: Request| async { Ok("delete") });
631 assert!(route.handler.contains_key(&Method::DELETE));
632 }
633
634 #[test]
635 fn test_route_trait_patch() {
636 let route = Route::new("test").patch(|_req: Request| async { Ok("patch") });
638 assert!(route.handler.contains_key(&Method::PATCH));
639 }
640
641 #[test]
642 fn test_route_trait_options() {
643 let route = Route::new("test").options(|_req: Request| async { Ok("options") });
645 assert!(route.handler.contains_key(&Method::OPTIONS));
646 }
647
648 #[test]
649 fn test_route_trait_methods_chain() {
650 let route = Route::new("test")
652 .get(|_req: Request| async { Ok("get") })
653 .post(|_req: Request| async { Ok("post") })
654 .put(|_req: Request| async { Ok("put") })
655 .delete(|_req: Request| async { Ok("delete") })
656 .patch(|_req: Request| async { Ok("patch") })
657 .options(|_req: Request| async { Ok("options") });
658
659 assert_eq!(route.handler.len(), 6);
660 }
661
662 #[test]
665 fn test_mixed_handler_append_and_route_trait() {
666 let route = Route::new("test")
668 .get(|_req: Request| async { Ok("handler append get") })
670 .post(|_req: Request| async { Ok("handler append post") })
671 .put(|_req: Request| async { Ok("route trait put") })
673 .delete(|_req: Request| async { Ok("route trait delete") })
674 .patch(|_req: Request| async { Ok("patch") })
676 .options(|_req: Request| async { Ok("options") });
677
678 assert_eq!(route.handler.len(), 6);
679 }
680
681 #[test]
684 fn test_handler_with_different_response_types() {
685 let route1 =
688 Route::new("test1").get(|_req: Request| async { Ok(Response::text("ok response")) });
689
690 let route2 =
691 Route::new("test2").post(|_req: Request| async { Response::text("direct response") });
692
693 let route3 = Route::new("test3").put(|_req: Request| async { Ok("string") });
694
695 assert!(route1.handler.contains_key(&Method::GET));
696 assert!(route2.handler.contains_key(&Method::POST));
697 assert!(route3.handler.contains_key(&Method::PUT));
698 }
699
700 #[test]
701 fn test_handler_clone_behavior() {
702 let route = Route::new("test")
704 .get(|_req: Request| async { Ok("test") })
705 .post(|_req: Request| async { Ok("test") });
706
707 if let Some(get_handler) = route.handler.get(&Method::GET) {
709 let _cloned = get_handler.clone();
710 }
711
712 if let Some(post_handler) = route.handler.get(&Method::POST) {
713 let _cloned = post_handler.clone();
714 }
715
716 assert_eq!(route.handler.len(), 2);
717 }
718
719 #[test]
722 fn test_get_ex_with_extractor() {
723 use crate::extractor::Path;
724 let route = Route::new("test")
725 .get_ex(|Path(id): Path<u64>| async move { Ok(format!("user {}", id)) });
726 assert!(route.handler.contains_key(&Method::GET));
727 }
728
729 #[test]
730 fn test_post_ex_with_extractor() {
731 use crate::extractor::Path;
732 let route = Route::new("test")
733 .post_ex(|Path(id): Path<u64>| async move { Ok(format!("created {}", id)) });
734 assert!(route.handler.contains_key(&Method::POST));
735 }
736
737 #[test]
738 fn test_put_ex_with_extractor() {
739 use crate::extractor::Path;
740 let route = Route::new("test")
741 .put_ex(|Path(id): Path<u64>| async move { Ok(format!("updated {}", id)) });
742 assert!(route.handler.contains_key(&Method::PUT));
743 }
744
745 #[test]
746 fn test_delete_ex_with_extractor() {
747 use crate::extractor::Path;
748 let route = Route::new("test")
749 .delete_ex(|Path(id): Path<u64>| async move { Ok(format!("deleted {}", id)) });
750 assert!(route.handler.contains_key(&Method::DELETE));
751 }
752
753 #[test]
754 fn test_patch_ex_with_extractor() {
755 use crate::extractor::Path;
756 let route = Route::new("test")
757 .patch_ex(|Path(id): Path<u64>| async move { Ok(format!("patched {}", id)) });
758 assert!(route.handler.contains_key(&Method::PATCH));
759 }
760
761 #[test]
762 fn test_options_ex_with_extractor() {
763 use crate::extractor::Path;
764 let route = Route::new("test")
765 .options_ex(|Path(id): Path<u64>| async move { Ok(format!("options {}", id)) });
766 assert!(route.handler.contains_key(&Method::OPTIONS));
767 }
768
769 #[test]
770 fn test_all_ex_methods_chain() {
771 use crate::extractor::Path;
772 let route = Route::new("test")
773 .get_ex(|Path(_): Path<u64>| async { Ok("get") })
774 .post_ex(|Path(_): Path<u64>| async { Ok("post") })
775 .put_ex(|Path(_): Path<u64>| async { Ok("put") })
776 .delete_ex(|Path(_): Path<u64>| async { Ok("delete") })
777 .patch_ex(|Path(_): Path<u64>| async { Ok("patch") })
778 .options_ex(|Path(_): Path<u64>| async { Ok("options") });
779 assert_eq!(route.handler.len(), 6);
780 }
781
782 #[test]
785 fn test_get_handler_mut_deep_nested() {
786 let mut route = Route::new("api");
787 let child = Route::new("v1");
788 let grandchild = Route::new("users");
789 let mut child_with_grandchild = child;
790 child_with_grandchild.children.push(grandchild);
791 route.children.push(child_with_grandchild);
792 route.create_path = "api/v1".to_string();
793
794 let handler_map = route.get_handler_mut();
796 assert!(handler_map.is_empty());
797 }
798
799 #[test]
800 fn test_handler_getter_handler_on_child_route() {
801 let mut route = Route::new("api");
803 let child = Route::new("users");
804 route.children.push(child);
805 route.create_path = "api/users".to_string();
806
807 let handler = Arc::new(HandlerWrapper::new(|_req: Request| async {
808 Ok(Response::text("ok"))
809 }));
810 let route = <Route as HandlerGetter>::handler(route, Method::GET, handler);
811
812 let child_route = &route.children[0];
814 assert!(child_route.handler.contains_key(&Method::GET));
815 }
816
817 #[test]
820 fn test_handler_overwrite() {
821 let route = Route::new("test")
822 .get(|_req: Request| async { Ok("first") })
823 .get(|_req: Request| async { Ok("second") });
824
825 assert!(route.handler.contains_key(&Method::GET));
827 assert_eq!(route.handler.len(), 1);
828 }
829
830 #[test]
831 fn test_empty_route_handler() {
832 let route = Route::new("test");
833 assert!(route.handler.is_empty());
834 }
835
836 #[test]
837 fn test_chain_methods() {
838 let route = Route::new("test")
839 .get(|_req: Request| async { Ok("get") })
840 .post(|_req: Request| async { Ok("post") })
841 .put(|_req: Request| async { Ok("put") })
842 .delete(|_req: Request| async { Ok("delete") })
843 .patch(|_req: Request| async { Ok("patch") })
844 .options(|_req: Request| async { Ok("options") });
845
846 assert_eq!(route.handler.len(), 6);
847 assert!(route.handler.contains_key(&Method::GET));
848 assert!(route.handler.contains_key(&Method::POST));
849 assert!(route.handler.contains_key(&Method::PUT));
850 assert!(route.handler.contains_key(&Method::DELETE));
851 assert!(route.handler.contains_key(&Method::PATCH));
852 assert!(route.handler.contains_key(&Method::OPTIONS));
853 }
854
855 #[test]
856 fn test_handler_append_custom_method() {
857 let mut route = Route::new("test");
858
859 route.handler_append(Method::GET, |_req: Request| async { Ok("custom get") });
860
861 assert!(route.handler.contains_key(&Method::GET));
862 }
863
864 #[test]
867 fn test_handler_return_types() {
868 let route1 =
870 Route::new("test1").get(|_req: Request| async { Ok(Response::text("string")) });
871
872 let route2 =
873 Route::new("test2").post(|_req: Request| async { Response::text("direct response") });
874
875 let route3 = Route::new("test3").put(|_req: Request| async { Ok("text value") });
876
877 assert!(route1.handler.contains_key(&Method::GET));
878 assert!(route2.handler.contains_key(&Method::POST));
879 assert!(route3.handler.contains_key(&Method::PUT));
880 }
881
882 #[test]
883 fn test_handler_arc_storage() {
884 let route = Route::new("test").get(|_req: Request| async { Ok(Response::text("test")) });
885
886 if let Some(handler) = route.handler.get(&Method::GET) {
888 let _ = handler.clone();
890 } else {
891 panic!("Handler not found");
892 }
893 }
894}