Skip to content

Commit

Permalink
Fix match_pattern() returning None for scope with resource of empty p…
Browse files Browse the repository at this point in the history
…ath (#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
  • Loading branch information
jdeepee authored Dec 1, 2020
1 parent 7981e00 commit 1f70ef1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
### Fixed
* Ensure `actix-http` dependency uses same `serde_urlencoded`.
* Removed an occasional `unwrap` on `None` panic in `NormalizePathNormalization`.
* Fix match_pattern() returning None for scope with resource of empty path. [#1798]

[#1798]: https://github.com/actix/actix-web/pull/1798


## 3.3.0 - 2020-11-25
Expand Down
36 changes: 36 additions & 0 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,4 +675,40 @@ mod tests {
let res = call_service(&mut srv, req).await;
assert_eq!(res.status(), StatusCode::OK);
}

#[actix_rt::test]
async fn extract_path_pattern_complex() {
let mut srv = init_service(
App::new()
.service(web::scope("/user").service(web::scope("/{id}").service(
web::resource("").to(move |req: HttpRequest| {
assert_eq!(req.match_pattern(), Some("/user/{id}".to_owned()));

HttpResponse::Ok().finish()
}),
)))
.service(web::resource("/").to(move |req: HttpRequest| {
assert_eq!(req.match_pattern(), Some("/".to_owned()));

HttpResponse::Ok().finish()
}))
.default_service(web::to(move |req: HttpRequest| {
assert!(req.match_pattern().is_none());
HttpResponse::Ok().finish()
})),
)
.await;

let req = TestRequest::get().uri("/user/test").to_request();
let res = call_service(&mut srv, req).await;
assert_eq!(res.status(), StatusCode::OK);

let req = TestRequest::get().uri("/").to_request();
let res = call_service(&mut srv, req).await;
assert_eq!(res.status(), StatusCode::OK);

let req = TestRequest::get().uri("/not-exist").to_request();
let res = call_service(&mut srv, req).await;
assert_eq!(res.status(), StatusCode::OK);
}
}
2 changes: 1 addition & 1 deletion src/rmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl ResourceMap {
if let Some(plen) = pattern.is_prefix_match(path) {
return rmap.has_resource(&path[plen..]);
}
} else if pattern.is_match(path) {
} else if pattern.is_match(path) || pattern.pattern() == "" && path == "/" {
return true;
}
}
Expand Down

0 comments on commit 1f70ef1

Please sign in to comment.