diff --git a/src/router.rs b/src/router.rs index 602326f8a..81a78e577 100644 --- a/src/router.rs +++ b/src/router.rs @@ -313,11 +313,11 @@ impl Resource { } PatternType::Prefix(ref s) => if path == s { Some(s.len()) - } else if path.starts_with(s) && (s.ends_with('/') || - path.split_at(s.len()).1.starts_with('/')) + } else if path.starts_with(s) + && (s.ends_with('/') || path.split_at(s.len()).1.starts_with('/')) { if s.ends_with('/') { - Some(s.len()-1) + Some(s.len() - 1) } else { Some(s.len()) } diff --git a/src/scope.rs b/src/scope.rs index 839c4746c..dba490b2f 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -792,6 +792,40 @@ mod tests { assert_eq!(resp.as_msg().status(), StatusCode::CREATED); } + #[test] + fn test_scope_root2() { + let mut app = App::new() + .scope("/app/", |scope| { + scope.resource("", |r| r.f(|_| HttpResponse::Ok())) + }) + .finish(); + + let req = TestRequest::with_uri("/app").finish(); + let resp = app.run(req); + assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND); + + let req = TestRequest::with_uri("/app/").finish(); + let resp = app.run(req); + assert_eq!(resp.as_msg().status(), StatusCode::OK); + } + + #[test] + fn test_scope_root3() { + let mut app = App::new() + .scope("/app/", |scope| { + scope.resource("/", |r| r.f(|_| HttpResponse::Ok())) + }) + .finish(); + + let req = TestRequest::with_uri("/app").finish(); + let resp = app.run(req); + assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND); + + let req = TestRequest::with_uri("/app/").finish(); + let resp = app.run(req); + assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND); + } + #[test] fn test_scope_route() { let mut app = App::new() @@ -916,6 +950,48 @@ mod tests { assert_eq!(resp.as_msg().status(), StatusCode::CREATED); } + #[test] + fn test_scope_with_state_root2() { + struct State; + + let mut app = App::new() + .scope("/app", |scope| { + scope.with_state("/t1/", State, |scope| { + scope.resource("", |r| r.f(|_| HttpResponse::Ok())) + }) + }) + .finish(); + + let req = TestRequest::with_uri("/app/t1").finish(); + let resp = app.run(req); + assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND); + + let req = TestRequest::with_uri("/app/t1/").finish(); + let resp = app.run(req); + assert_eq!(resp.as_msg().status(), StatusCode::OK); + } + + #[test] + fn test_scope_with_state_root3() { + struct State; + + let mut app = App::new() + .scope("/app", |scope| { + scope.with_state("/t1/", State, |scope| { + scope.resource("/", |r| r.f(|_| HttpResponse::Ok())) + }) + }) + .finish(); + + let req = TestRequest::with_uri("/app/t1").finish(); + let resp = app.run(req); + assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND); + + let req = TestRequest::with_uri("/app/t1/").finish(); + let resp = app.run(req); + assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND); + } + #[test] fn test_scope_with_state_filter() { struct State;