diff --git a/src/resource.rs b/src/resource.rs index 46b3e2a8f..29ff07857 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -39,6 +39,10 @@ type HttpNewService
= /// web::resource("/") /// .route(web::get().to(|| HttpResponse::Ok()))); /// } +/// ``` +/// +/// If no matching route could be found, *405* response code get returned. +/// Default behavior could be overriden with `default_resource()` method. pub struct Resource
> {
endpoint: T,
rdef: String,
@@ -261,6 +265,8 @@ where
}
/// Default resource to be used if no matching route could be found.
+ /// By default *405* response get returned. Resource does not use
+ /// default handler from `App` or `Scope`.
pub fn default_resource ) -> R,
@@ -291,9 +297,6 @@ where
> + 'static,
{
fn register(mut self, config: &mut ServiceConfig ) {
- if self.default.borrow().is_none() {
- *self.default.borrow_mut() = Some(config.default_service());
- }
let guards = if self.guards.is_empty() {
None
} else {
@@ -454,7 +457,7 @@ impl Service for ResourceService {
let req = req.into_request();
Either::B(Either::B(ok(ServiceResponse::new(
req,
- Response::NotFound().finish(),
+ Response::MethodNotAllowed().finish(),
))))
}
}
@@ -483,3 +486,48 @@ impl {
self.factory.borrow_mut().as_mut().unwrap().new_service(&())
}
}
+
+#[cfg(test)]
+mod tests {
+ use crate::http::{Method, StatusCode};
+ use crate::test::{call_success, init_service, TestRequest};
+ use crate::{web, App, HttpResponse};
+
+ #[test]
+ fn test_default_resource() {
+ let mut srv = init_service(
+ App::new()
+ .service(
+ web::resource("/test").route(web::get().to(|| HttpResponse::Ok())),
+ )
+ .default_resource(|r| r.to(|| HttpResponse::BadRequest())),
+ );
+ let req = TestRequest::with_uri("/test").to_request();
+ let resp = call_success(&mut srv, req);
+ assert_eq!(resp.status(), StatusCode::OK);
+
+ let req = TestRequest::with_uri("/test")
+ .method(Method::POST)
+ .to_request();
+ let resp = call_success(&mut srv, req);
+ assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
+
+ let mut srv = init_service(
+ App::new().service(
+ web::resource("/test")
+ .route(web::get().to(|| HttpResponse::Ok()))
+ .default_resource(|r| r.to(|| HttpResponse::BadRequest())),
+ ),
+ );
+
+ let req = TestRequest::with_uri("/test").to_request();
+ let resp = call_success(&mut srv, req);
+ assert_eq!(resp.status(), StatusCode::OK);
+
+ let req = TestRequest::with_uri("/test")
+ .method(Method::POST)
+ .to_request();
+ let resp = call_success(&mut srv, req);
+ assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
+ }
+}