Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix matched-path bug #1033

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading