From ffb3324129fa84595e37fb11a5162116498e88c0 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 24 Mar 2019 16:15:34 -0700 Subject: [PATCH] do not use default resource from app, return 405 if no matching route found --- src/resource.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) 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(mut self, f: F) -> Self where F: FnOnce(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 NewService for ResourceEndpoint

{ 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); + } +}