mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-23 16:21:06 +01:00
allow to handle empty path for application with prefix
This commit is contained in:
parent
b79a9aaec7
commit
d6abd2fe22
@ -6,6 +6,12 @@
|
||||
|
||||
* Add implementation of `FromRequest<S>` for `Option<T>` and `Result<T, Error>`
|
||||
|
||||
* 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
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user