Skip to content

Commit

Permalink
fix matched-path bug (#1033)
Browse files Browse the repository at this point in the history
* fix matched-path bug

* Format Rust code using rustfmt

* wip

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
chrislearn and github-actions[bot] authored Jan 23, 2025
1 parent 60a4edd commit 4805761
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
6 changes: 0 additions & 6 deletions crates/core/src/routing/filters/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,21 +1064,15 @@ impl PathFilter {
/// Detect is that path is match.
pub fn detect(&self, state: &mut PathState) -> bool {
let original_cursor = state.cursor;
#[cfg(feature = "matched-path")]
let original_matched_parts_len = state.matched_parts.len();
for ps in &self.path_wisps {
let row = state.cursor.0;
if ps.detect(state) {
if row == state.cursor.0 && row != state.parts.len() {
state.cursor = original_cursor;
#[cfg(feature = "matched-path")]
state.matched_parts.truncate(original_matched_parts_len);
return false;
}
} else {
state.cursor = original_cursor;
#[cfg(feature = "matched-path")]
state.matched_parts.truncate(original_matched_parts_len);
return false;
}
}
Expand Down
64 changes: 64 additions & 0 deletions crates/core/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,68 @@ mod tests {
.contains("404: Not Found"));
assert_eq!(access(&service, "localhost").await, "Hello World");
}

#[tokio::test]
async fn test_matched_path() {
#[handler]
async fn alice1(req: &mut Request) {
assert_eq!(req.matched_path(), "open/alice1");
}
#[handler]
async fn bob1(req: &mut Request) {
assert_eq!(req.matched_path(), "open/alice1/bob1");
}

#[handler]
async fn alice2(req: &mut Request) {
assert_eq!(req.matched_path(), "open/alice2");
}
#[handler]
async fn bob2(req: &mut Request) {
assert_eq!(req.matched_path(), "open/alice2/bob2");
}

#[handler]
async fn alice3(req: &mut Request) {
assert_eq!(req.matched_path(), "alice3");
}
#[handler]
async fn bob3(req: &mut Request) {
assert_eq!(req.matched_path(), "alice3/bob3");
}

let router = Router::new()
.push(
Router::with_path("open").push(
Router::with_path("alice1")
.get(alice1)
.push(Router::with_path("bob1").get(bob1)),
),
)
.push(
Router::with_path("open").push(
Router::with_path("alice2")
.get(alice2)
.push(Router::with_path("bob2").get(bob2)),
),
)
.push(
Router::with_path("alice3")
.get(alice3)
.push(Router::with_path("bob3").get(bob3)),
);
let service = Service::new(router);

async fn access(service: &Service, path: &str) {
TestClient::get(format!("http://127.0.0.1/{}", path))
.send(service)
.await;
}
access(&service, "/open/alice1").await;
access(&service, "/open/alice1/bob1").await;
access(&service, "/open/alice2").await;
access(&service, "/open/alice2/bob2").await;
access(&service, "/alice3").await;
access(&service, "/alice1/bob3").await;
}
}
6 changes: 6 additions & 0 deletions crates/core/src/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,19 @@ impl Router {
}
if !self.routers.is_empty() {
let original_cursor = path_state.cursor;
#[cfg(feature = "matched-path")]
let original_matched_parts_len = path_state.matched_parts.len();
for child in &self.routers {
if let Some(dm) = child.detect(req, path_state).await {
return Some(DetectMatched {
hoops: [&self.hoops[..], &dm.hoops[..]].concat(),
goal: dm.goal.clone(),
});
} else {
#[cfg(feature = "matched-path")]
path_state
.matched_parts
.truncate(original_matched_parts_len);
path_state.cursor = original_cursor;
}
}
Expand Down

0 comments on commit 4805761

Please sign in to comment.