diff --git a/CHANGES.md b/CHANGES.md index 882563302..494ad7a65 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,12 @@ * Add implementation of `FromRequest` for `Option` and `Result` + * Allow to handle application prefix, i.e. allow to handle `/app` path + for application with `/app` prefix. + Check [`App::prefix()`](https://actix.rs/actix-web/actix_web/struct.App.html#method.prefix) + api doc. + + ### Fixed * removed the timestamp from the default logger middleware diff --git a/src/application.rs b/src/application.rs index f36adf69e..a5cd3386f 100644 --- a/src/application.rs +++ b/src/application.rs @@ -171,7 +171,9 @@ where /// In the following example only requests with an `/app/` path /// prefix get handled. Requests with path `/app/test/` would be /// handled, while requests with the paths `/application` or - /// `/other/...` would return `NOT FOUND`. + /// `/other/...` would return `NOT FOUND`. It is also possible to + /// handle `/app` path, to do this you can register resource for + /// empty string `""` /// /// ```rust /// # extern crate actix_web; @@ -180,6 +182,8 @@ where /// fn main() { /// let app = App::new() /// .prefix("/app") + /// .resource("", |r| r.f(|_| HttpResponse::Ok())) // <- handle `/app` path + /// .resource("/", |r| r.f(|_| HttpResponse::Ok())) // <- handle `/app/` path /// .resource("/test", |r| { /// r.get().f(|_| HttpResponse::Ok()); /// r.head().f(|_| HttpResponse::MethodNotAllowed()); @@ -822,6 +826,23 @@ mod tests { assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND); } + #[test] + fn test_option_responder() { + let app = App::new() + .resource("/none", |r| r.f(|_| -> Option<&'static str> { None })) + .resource("/some", |r| r.f(|_| Some("some"))) + .finish(); + + let req = TestRequest::with_uri("/none").request(); + let resp = app.run(req); + assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND); + + let req = TestRequest::with_uri("/some").request(); + let resp = app.run(req); + assert_eq!(resp.as_msg().status(), StatusCode::OK); + assert_eq!(resp.as_msg().body(), &Body::Binary(Binary::Slice(b"some"))); + } + #[test] fn test_filter() { let mut srv = TestServer::with_factory(|| { @@ -840,19 +861,21 @@ mod tests { } #[test] - fn test_option_responder() { - let app = App::new() - .resource("/none", |r| r.f(|_| -> Option<&'static str> { None })) - .resource("/some", |r| r.f(|_| Some("some"))) - .finish(); + fn test_prefix_root() { + let mut srv = TestServer::with_factory(|| { + App::new() + .prefix("/test") + .resource("/", |r| r.f(|_| HttpResponse::Ok())) + .resource("", |r| r.f(|_| HttpResponse::Created())) + }); - let req = TestRequest::with_uri("/none").request(); - let resp = app.run(req); - assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND); + let request = srv.get().uri(srv.url("/test/")).finish().unwrap(); + let response = srv.execute(request.send()).unwrap(); + assert_eq!(response.status(), StatusCode::OK); - let req = TestRequest::with_uri("/some").request(); - let resp = app.run(req); - assert_eq!(resp.as_msg().status(), StatusCode::OK); - assert_eq!(resp.as_msg().body(), &Body::Binary(Binary::Slice(b"some"))); + let request = srv.get().uri(srv.url("/test")).finish().unwrap(); + let response = srv.execute(request.send()).unwrap(); + assert_eq!(response.status(), StatusCode::CREATED); } + } diff --git a/src/router.rs b/src/router.rs index e79dc93da..f3f657b58 100644 --- a/src/router.rs +++ b/src/router.rs @@ -463,7 +463,7 @@ impl ResourceDef { /// /// Panics if path pattern is wrong. pub fn new(path: &str) -> Self { - ResourceDef::with_prefix(path, "/", false) + ResourceDef::with_prefix(path, if path.is_empty() { "" } else { "/" }, false) } /// Parse path pattern and create new `Resource` instance.