mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
do not use default resource from app, return 405 if no matching route found
This commit is contained in:
parent
9932a342ef
commit
ffb3324129
@ -39,6 +39,10 @@ type HttpNewService<P> =
|
|||||||
/// web::resource("/")
|
/// web::resource("/")
|
||||||
/// .route(web::get().to(|| HttpResponse::Ok())));
|
/// .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<P, T = ResourceEndpoint<P>> {
|
pub struct Resource<P, T = ResourceEndpoint<P>> {
|
||||||
endpoint: T,
|
endpoint: T,
|
||||||
rdef: String,
|
rdef: String,
|
||||||
@ -261,6 +265,8 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Default resource to be used if no matching route could be found.
|
/// 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<F, R, U>(mut self, f: F) -> Self
|
pub fn default_resource<F, R, U>(mut self, f: F) -> Self
|
||||||
where
|
where
|
||||||
F: FnOnce(Resource<P>) -> R,
|
F: FnOnce(Resource<P>) -> R,
|
||||||
@ -291,9 +297,6 @@ where
|
|||||||
> + 'static,
|
> + 'static,
|
||||||
{
|
{
|
||||||
fn register(mut self, config: &mut ServiceConfig<P>) {
|
fn register(mut self, config: &mut ServiceConfig<P>) {
|
||||||
if self.default.borrow().is_none() {
|
|
||||||
*self.default.borrow_mut() = Some(config.default_service());
|
|
||||||
}
|
|
||||||
let guards = if self.guards.is_empty() {
|
let guards = if self.guards.is_empty() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
@ -454,7 +457,7 @@ impl<P> Service for ResourceService<P> {
|
|||||||
let req = req.into_request();
|
let req = req.into_request();
|
||||||
Either::B(Either::B(ok(ServiceResponse::new(
|
Either::B(Either::B(ok(ServiceResponse::new(
|
||||||
req,
|
req,
|
||||||
Response::NotFound().finish(),
|
Response::MethodNotAllowed().finish(),
|
||||||
))))
|
))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -483,3 +486,48 @@ impl<P: 'static> NewService for ResourceEndpoint<P> {
|
|||||||
self.factory.borrow_mut().as_mut().unwrap().new_service(&())
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user