Skip to content

Commit 861ab21

Browse files
committed
fix: add more logs + update test
1 parent 03e2da2 commit 861ab21

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/router.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::{bail, Context, Result};
2-
use log::{trace, warn};
2+
use log::trace;
33
use std::{collections::HashMap, fs, str::FromStr};
44

55
use crate::{
@@ -40,6 +40,7 @@ impl Router {
4040
fn find_matching_route(&self, route: &RequestRoute) -> Result<Option<&StoredRoute>> {
4141
let mut excluded: Vec<&StoredRoute> = vec![];
4242
let request_route_parts = route.path.split('/');
43+
trace!("trying to match request parts: {:?}", request_route_parts);
4344

4445
for (idx, part) in request_route_parts.enumerate() {
4546
for match_candidate in self.routes.keys() {
@@ -49,9 +50,17 @@ impl Router {
4950

5051
if let Some(match_part) = match_candidate.parts.get(idx) {
5152
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+
);
5257
excluded.push(match_candidate);
5358
}
5459
} else {
60+
trace!(
61+
"excluding server route from search because too small: {:?}",
62+
match_candidate
63+
);
5564
excluded.push(match_candidate);
5665
};
5766
}
@@ -86,6 +95,7 @@ impl Router {
8695
.routes
8796
.get(matching_route)
8897
.context("failed to get callback, even though route should be a valid key")?;
98+
8999
return callback(request, &routing_data);
90100
}
91101

@@ -307,6 +317,15 @@ mod tests {
307317

308318
use super::*;
309319

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+
310329
fn get_hello_callback(
311330
_request: &HttpRequest,
312331
_routing_data: &RoutingData,
@@ -360,7 +379,7 @@ mod tests {
360379
#[test]
361380
fn test_unmatched_get_catcher() {
362381
let router = Router::new()
363-
.catch_all(HttpMethod::GET, get_hello_callback)
382+
.catch_all(HttpMethod::GET, catcher_get_404)
364383
.unwrap();
365384

366385
let request = HttpRequest::from_raw_request(HttpRequestRaw {
@@ -371,12 +390,16 @@ mod tests {
371390
.unwrap();
372391

373392
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);
375394
}
376395

377396
#[test]
378397
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();
380403

381404
let request = HttpRequest::from_raw_request(HttpRequestRaw {
382405
request_line: "GET /hello HTTP/1.1".to_owned(),

0 commit comments

Comments
 (0)