Skip to content

Commit 1f70ef1

Browse files
authored
Fix match_pattern() returning None for scope with resource of empty path (#1798)
* fix match_pattern function not returning pattern where scope has resource of path "" * remove print in test * make comparison on existing else if block * add fix to changelog
1 parent 7981e00 commit 1f70ef1

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
### Fixed
55
* Ensure `actix-http` dependency uses same `serde_urlencoded`.
66
* Removed an occasional `unwrap` on `None` panic in `NormalizePathNormalization`.
7+
* Fix match_pattern() returning None for scope with resource of empty path. [#1798]
8+
9+
[#1798]: https://github.com/actix/actix-web/pull/1798
710

811

912
## 3.3.0 - 2020-11-25

src/request.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,4 +675,40 @@ mod tests {
675675
let res = call_service(&mut srv, req).await;
676676
assert_eq!(res.status(), StatusCode::OK);
677677
}
678+
679+
#[actix_rt::test]
680+
async fn extract_path_pattern_complex() {
681+
let mut srv = init_service(
682+
App::new()
683+
.service(web::scope("/user").service(web::scope("/{id}").service(
684+
web::resource("").to(move |req: HttpRequest| {
685+
assert_eq!(req.match_pattern(), Some("/user/{id}".to_owned()));
686+
687+
HttpResponse::Ok().finish()
688+
}),
689+
)))
690+
.service(web::resource("/").to(move |req: HttpRequest| {
691+
assert_eq!(req.match_pattern(), Some("/".to_owned()));
692+
693+
HttpResponse::Ok().finish()
694+
}))
695+
.default_service(web::to(move |req: HttpRequest| {
696+
assert!(req.match_pattern().is_none());
697+
HttpResponse::Ok().finish()
698+
})),
699+
)
700+
.await;
701+
702+
let req = TestRequest::get().uri("/user/test").to_request();
703+
let res = call_service(&mut srv, req).await;
704+
assert_eq!(res.status(), StatusCode::OK);
705+
706+
let req = TestRequest::get().uri("/").to_request();
707+
let res = call_service(&mut srv, req).await;
708+
assert_eq!(res.status(), StatusCode::OK);
709+
710+
let req = TestRequest::get().uri("/not-exist").to_request();
711+
let res = call_service(&mut srv, req).await;
712+
assert_eq!(res.status(), StatusCode::OK);
713+
}
678714
}

src/rmap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl ResourceMap {
8686
if let Some(plen) = pattern.is_prefix_match(path) {
8787
return rmap.has_resource(&path[plen..]);
8888
}
89-
} else if pattern.is_match(path) {
89+
} else if pattern.is_match(path) || pattern.pattern() == "" && path == "/" {
9090
return true;
9191
}
9292
}

0 commit comments

Comments
 (0)