1
1
use anyhow:: { bail, Context , Result } ;
2
- use log:: { trace, warn } ;
2
+ use log:: trace;
3
3
use std:: { collections:: HashMap , fs, str:: FromStr } ;
4
4
5
5
use crate :: {
@@ -40,6 +40,7 @@ impl Router {
40
40
fn find_matching_route ( & self , route : & RequestRoute ) -> Result < Option < & StoredRoute > > {
41
41
let mut excluded: Vec < & StoredRoute > = vec ! [ ] ;
42
42
let request_route_parts = route. path . split ( '/' ) ;
43
+ trace ! ( "trying to match request parts: {:?}" , request_route_parts) ;
43
44
44
45
for ( idx, part) in request_route_parts. enumerate ( ) {
45
46
for match_candidate in self . routes . keys ( ) {
@@ -49,9 +50,17 @@ impl Router {
49
50
50
51
if let Some ( match_part) = match_candidate. parts . get ( idx) {
51
52
if !match_part. is_dynamic && !match_part. value . eq ( part) {
53
+ trace ! (
54
+ "excluding server route from search because part differ and not dynamic: {:?}" ,
55
+ match_candidate
56
+ ) ;
52
57
excluded. push ( match_candidate) ;
53
58
}
54
59
} else {
60
+ trace ! (
61
+ "excluding server route from search because too small: {:?}" ,
62
+ match_candidate
63
+ ) ;
55
64
excluded. push ( match_candidate) ;
56
65
} ;
57
66
}
@@ -86,6 +95,7 @@ impl Router {
86
95
. routes
87
96
. get ( matching_route)
88
97
. context ( "failed to get callback, even though route should be a valid key" ) ?;
98
+
89
99
return callback ( request, & routing_data) ;
90
100
}
91
101
@@ -307,6 +317,15 @@ mod tests {
307
317
308
318
use super :: * ;
309
319
320
+ fn catcher_get_404 (
321
+ _request : & HttpRequest ,
322
+ _routing_data : & RoutingData ,
323
+ ) -> Result < HttpResponse > {
324
+ HttpResponseBuilder :: new ( )
325
+ . set_html_body ( "404 YOU ARE LOST" )
326
+ . build ( )
327
+ }
328
+
310
329
fn get_hello_callback (
311
330
_request : & HttpRequest ,
312
331
_routing_data : & RoutingData ,
@@ -360,7 +379,7 @@ mod tests {
360
379
#[ test]
361
380
fn test_unmatched_get_catcher ( ) {
362
381
let router = Router :: new ( )
363
- . catch_all ( HttpMethod :: GET , get_hello_callback )
382
+ . catch_all ( HttpMethod :: GET , catcher_get_404 )
364
383
. unwrap ( ) ;
365
384
366
385
let request = HttpRequest :: from_raw_request ( HttpRequestRaw {
@@ -371,12 +390,16 @@ mod tests {
371
390
. unwrap ( ) ;
372
391
373
392
let response = router. handle_request ( & request) . unwrap ( ) ;
374
- assert_eq ! ( "Hello World! \r \n " . as_bytes( ) , response. body) ;
393
+ assert_eq ! ( "404 YOU ARE LOST \r \n " . as_bytes( ) , response. body) ;
375
394
}
376
395
377
396
#[ test]
378
397
fn test_get_hello_html ( ) {
379
- let router = Router :: new ( ) . get ( "/hello" , get_hello_callback) . unwrap ( ) ;
398
+ let router = Router :: new ( )
399
+ . get ( "/hello" , get_hello_callback)
400
+ . unwrap ( )
401
+ . catch_all ( HttpMethod :: GET , catcher_get_404)
402
+ . unwrap ( ) ;
380
403
381
404
let request = HttpRequest :: from_raw_request ( HttpRequestRaw {
382
405
request_line : "GET /hello HTTP/1.1" . to_owned ( ) ,
0 commit comments