2019-03-02 07:51:32 +01:00
|
|
|
use std::cell::RefCell;
|
2019-04-14 07:25:00 +02:00
|
|
|
use std::fmt;
|
2019-11-20 18:33:22 +01:00
|
|
|
use std::future::Future;
|
2018-04-14 01:02:01 +02:00
|
|
|
use std::rc::Rc;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2019-05-05 04:43:49 +02:00
|
|
|
use actix_http::{Error, Extensions, Response};
|
2019-12-25 17:13:52 +01:00
|
|
|
use actix_router::IntoPattern;
|
2019-11-26 06:25:50 +01:00
|
|
|
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
2019-03-02 07:51:32 +01:00
|
|
|
use actix_service::{
|
2021-01-09 04:36:58 +01:00
|
|
|
apply, apply_fn_factory, fn_service, IntoServiceFactory, Service, ServiceFactory,
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceFactoryExt, Transform,
|
2019-03-02 07:51:32 +01:00
|
|
|
};
|
2021-01-02 20:40:31 +01:00
|
|
|
use futures_core::future::LocalBoxFuture;
|
2021-01-09 04:36:58 +01:00
|
|
|
use futures_util::future::join_all;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2019-05-05 04:43:49 +02:00
|
|
|
use crate::data::Data;
|
2019-04-15 16:32:49 +02:00
|
|
|
use crate::dev::{insert_slash, AppService, HttpServiceFactory, ResourceDef};
|
2019-03-03 22:53:31 +01:00
|
|
|
use crate::extract::FromRequest;
|
2019-03-07 00:47:15 +01:00
|
|
|
use crate::guard::Guard;
|
2020-12-26 22:46:19 +01:00
|
|
|
use crate::handler::Handler;
|
2019-03-02 07:51:32 +01:00
|
|
|
use crate::responder::Responder;
|
2021-01-09 04:36:58 +01:00
|
|
|
use crate::route::{Route, RouteService};
|
2019-03-02 07:51:32 +01:00
|
|
|
use crate::service::{ServiceRequest, ServiceResponse};
|
2018-06-25 06:58:04 +02:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
|
|
|
type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
2019-03-02 08:59:44 +01:00
|
|
|
|
2019-03-17 05:35:02 +01:00
|
|
|
/// *Resource* is an entry in resources table which corresponds to requested URL.
|
2017-10-08 08:59:57 +02:00
|
|
|
///
|
2019-03-03 23:45:56 +01:00
|
|
|
/// Resource in turn has at least one route.
|
|
|
|
/// Route consists of an handlers objects and list of guards
|
|
|
|
/// (objects that implement `Guard` trait).
|
2019-06-25 19:23:36 +02:00
|
|
|
/// Resources and routes uses builder-like pattern for configuration.
|
2019-03-03 23:45:56 +01:00
|
|
|
/// During request handling, resource object iterate through all routes
|
|
|
|
/// and check guards for specific route, if request matches all
|
|
|
|
/// guards, route considered matched and route handler get called.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_web::{web, App, HttpResponse};
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/")
|
|
|
|
/// .route(web::get().to(|| HttpResponse::Ok())));
|
2019-03-03 23:45:56 +01:00
|
|
|
/// }
|
2019-03-25 00:15:34 +01:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// If no matching route could be found, *405* response code get returned.
|
2020-04-21 05:09:35 +02:00
|
|
|
/// Default behavior could be overridden with `default_resource()` method.
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct Resource<T = ResourceEndpoint> {
|
2019-03-02 07:51:32 +01:00
|
|
|
endpoint: T,
|
2019-12-25 17:13:52 +01:00
|
|
|
rdef: Vec<String>,
|
2019-03-09 23:06:24 +01:00
|
|
|
name: Option<String>,
|
2019-04-13 23:50:54 +02:00
|
|
|
routes: Vec<Route>,
|
2021-01-09 04:36:58 +01:00
|
|
|
app_data: Option<Extensions>,
|
2019-07-17 11:48:37 +02:00
|
|
|
guards: Vec<Box<dyn Guard>>,
|
2021-01-09 04:36:58 +01:00
|
|
|
default: HttpNewService,
|
2019-04-13 23:50:54 +02:00
|
|
|
factory_ref: Rc<RefCell<Option<ResourceFactory>>>,
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl Resource {
|
2019-12-25 17:13:52 +01:00
|
|
|
pub fn new<T: IntoPattern>(path: T) -> Resource {
|
2019-03-02 07:51:32 +01:00
|
|
|
let fref = Rc::new(RefCell::new(None));
|
|
|
|
|
2018-07-12 11:30:01 +02:00
|
|
|
Resource {
|
2019-03-02 07:51:32 +01:00
|
|
|
routes: Vec::new(),
|
2019-12-25 17:13:52 +01:00
|
|
|
rdef: path.patterns(),
|
2019-03-09 23:06:24 +01:00
|
|
|
name: None,
|
2019-03-02 07:51:32 +01:00
|
|
|
endpoint: ResourceEndpoint::new(fref.clone()),
|
|
|
|
factory_ref: fref,
|
2019-03-07 00:47:15 +01:00
|
|
|
guards: Vec::new(),
|
2021-01-09 04:36:58 +01:00
|
|
|
app_data: None,
|
|
|
|
default: boxed::factory(fn_service(|req: ServiceRequest| async {
|
|
|
|
Ok(req.into_response(Response::MethodNotAllowed().finish()))
|
|
|
|
})),
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl<T> Resource<T>
|
2019-03-02 07:51:32 +01:00
|
|
|
where
|
2019-11-20 18:33:22 +01:00
|
|
|
T: ServiceFactory<
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceRequest,
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 07:51:32 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
2019-03-09 23:06:24 +01:00
|
|
|
/// Set resource name.
|
|
|
|
///
|
|
|
|
/// Name is used for url generation.
|
|
|
|
pub fn name(mut self, name: &str) -> Self {
|
|
|
|
self.name = Some(name.to_string());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:47:15 +01:00
|
|
|
/// Add match guard to a resource.
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-07 23:01:52 +01:00
|
|
|
/// use actix_web::{web, guard, App, HttpResponse};
|
2019-03-07 00:47:15 +01:00
|
|
|
///
|
2019-11-21 16:34:04 +01:00
|
|
|
/// async fn index(data: web::Path<(String, String)>) -> &'static str {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new()
|
|
|
|
/// .service(
|
|
|
|
/// web::resource("/app")
|
|
|
|
/// .guard(guard::Header("content-type", "text/plain"))
|
|
|
|
/// .route(web::get().to(index))
|
|
|
|
/// )
|
|
|
|
/// .service(
|
|
|
|
/// web::resource("/app")
|
|
|
|
/// .guard(guard::Header("content-type", "text/json"))
|
|
|
|
/// .route(web::get().to(|| HttpResponse::MethodNotAllowed()))
|
|
|
|
/// );
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn guard<G: Guard + 'static>(mut self, guard: G) -> Self {
|
|
|
|
self.guards.push(Box::new(guard));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-07-17 11:48:37 +02:00
|
|
|
pub(crate) fn add_guards(mut self, guards: Vec<Box<dyn Guard>>) -> Self {
|
2019-03-07 00:47:15 +01:00
|
|
|
self.guards.extend(guards);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-03 21:09:38 +01:00
|
|
|
/// Register a new route.
|
2017-12-04 22:32:05 +01:00
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// ```rust
|
|
|
|
/// use actix_web::{web, guard, App, HttpResponse};
|
2017-12-04 22:32:05 +01:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/").route(
|
|
|
|
/// web::route()
|
2019-03-03 21:09:38 +01:00
|
|
|
/// .guard(guard::Any(guard::Get()).or(guard::Put()))
|
|
|
|
/// .guard(guard::Header("Content-Type", "text/plain"))
|
|
|
|
/// .to(|| HttpResponse::Ok()))
|
2019-03-07 00:47:15 +01:00
|
|
|
/// );
|
2017-12-04 22:32:05 +01:00
|
|
|
/// }
|
2017-12-06 17:03:08 +01:00
|
|
|
/// ```
|
2019-03-03 23:45:56 +01:00
|
|
|
///
|
2019-03-17 05:35:02 +01:00
|
|
|
/// Multiple routes could be added to a resource. Resource object uses
|
|
|
|
/// match guards for route selection.
|
2019-03-03 23:45:56 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
2019-11-20 05:05:16 +01:00
|
|
|
/// use actix_web::{web, guard, App};
|
2019-03-03 23:45:56 +01:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/container/")
|
|
|
|
/// .route(web::get().to(get_handler))
|
2019-03-03 23:45:56 +01:00
|
|
|
/// .route(web::post().to(post_handler))
|
|
|
|
/// .route(web::delete().to(delete_handler))
|
2019-03-07 00:47:15 +01:00
|
|
|
/// );
|
2019-03-03 23:45:56 +01:00
|
|
|
/// }
|
2019-11-20 05:05:16 +01:00
|
|
|
/// # async fn get_handler() -> impl actix_web::Responder { actix_web::HttpResponse::Ok() }
|
|
|
|
/// # async fn post_handler() -> impl actix_web::Responder { actix_web::HttpResponse::Ok() }
|
|
|
|
/// # async fn delete_handler() -> impl actix_web::Responder { actix_web::HttpResponse::Ok() }
|
2019-03-03 23:45:56 +01:00
|
|
|
/// ```
|
2019-04-13 23:50:54 +02:00
|
|
|
pub fn route(mut self, route: Route) -> Self {
|
2019-05-05 04:43:49 +02:00
|
|
|
self.routes.push(route);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide resource specific data. This method allows to add extractor
|
|
|
|
/// configuration or specific state available via `Data<T>` extractor.
|
|
|
|
/// Provided data is available for all routes registered for the current resource.
|
|
|
|
/// Resource data overrides data registered by `App::data()` method.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_web::{web, App, FromRequest};
|
|
|
|
///
|
|
|
|
/// /// extract text data from request
|
2019-11-21 16:34:04 +01:00
|
|
|
/// async fn index(body: String) -> String {
|
2019-05-05 04:43:49 +02:00
|
|
|
/// format!("Body {}!", body)
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/index.html")
|
|
|
|
/// // limit size of the payload
|
|
|
|
/// .data(String::configure(|cfg| {
|
|
|
|
/// cfg.limit(4096)
|
|
|
|
/// }))
|
|
|
|
/// .route(
|
|
|
|
/// web::get()
|
|
|
|
/// // register handler
|
|
|
|
/// .to(index)
|
|
|
|
/// ));
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-09-18 02:36:39 +02:00
|
|
|
pub fn data<U: 'static>(self, data: U) -> Self {
|
2019-12-20 12:13:09 +01:00
|
|
|
self.app_data(Data::new(data))
|
2019-09-18 02:36:39 +02:00
|
|
|
}
|
|
|
|
|
2020-04-29 19:20:47 +02:00
|
|
|
/// Add resource data.
|
2019-09-18 02:36:39 +02:00
|
|
|
///
|
2020-05-09 01:31:26 +02:00
|
|
|
/// Data of different types from parent contexts will still be accessible.
|
2019-12-20 12:13:09 +01:00
|
|
|
pub fn app_data<U: 'static>(mut self, data: U) -> Self {
|
2021-01-16 00:37:33 +01:00
|
|
|
self.app_data
|
|
|
|
.get_or_insert_with(Extensions::new)
|
|
|
|
.insert(data);
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
self
|
2017-12-06 17:03:08 +01:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2019-03-07 00:47:15 +01:00
|
|
|
/// Register a new route and add handler. This route matches all requests.
|
2018-03-27 08:10:31 +02:00
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// ```rust
|
2018-06-02 00:57:24 +02:00
|
|
|
/// use actix_web::*;
|
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// fn index(req: HttpRequest) -> HttpResponse {
|
|
|
|
/// unimplemented!()
|
|
|
|
/// }
|
|
|
|
///
|
2019-03-07 00:47:15 +01:00
|
|
|
/// App::new().service(web::resource("/").to(index));
|
2018-06-02 00:57:24 +02:00
|
|
|
/// ```
|
|
|
|
///
|
2018-03-27 08:10:31 +02:00
|
|
|
/// This is shortcut for:
|
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// ```rust
|
2018-06-02 00:57:07 +02:00
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # fn index(req: HttpRequest) -> HttpResponse { unimplemented!() }
|
2019-03-07 00:47:15 +01:00
|
|
|
/// App::new().service(web::resource("/").route(web::route().to(index)));
|
2018-03-27 08:10:31 +02:00
|
|
|
/// ```
|
2020-12-26 22:46:19 +01:00
|
|
|
pub fn to<F, I, R>(mut self, handler: F) -> Self
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2020-12-26 22:46:19 +01:00
|
|
|
F: Handler<I, R>,
|
2019-04-13 23:50:54 +02:00
|
|
|
I: FromRequest + 'static,
|
2020-12-26 22:46:19 +01:00
|
|
|
R: Future + 'static,
|
|
|
|
R::Output: Responder + 'static,
|
2018-05-10 01:27:31 +02:00
|
|
|
{
|
2019-11-21 16:34:04 +01:00
|
|
|
self.routes.push(Route::new().to(handler));
|
2019-03-02 07:51:32 +01:00
|
|
|
self
|
2018-05-10 01:27:31 +02:00
|
|
|
}
|
|
|
|
|
2019-03-17 05:35:02 +01:00
|
|
|
/// Register a resource middleware.
|
2018-01-10 05:00:18 +01:00
|
|
|
///
|
2019-03-17 05:35:02 +01:00
|
|
|
/// This is similar to `App's` middlewares, but middleware get invoked on resource level.
|
|
|
|
/// Resource level middlewares are not allowed to change response
|
|
|
|
/// type (i.e modify response's body).
|
2019-03-25 20:47:58 +01:00
|
|
|
///
|
|
|
|
/// **Note**: middlewares get called in opposite order of middlewares registration.
|
2019-11-20 18:33:22 +01:00
|
|
|
pub fn wrap<M>(
|
2019-03-02 07:51:32 +01:00
|
|
|
self,
|
2019-11-20 18:33:22 +01:00
|
|
|
mw: M,
|
2019-03-02 07:51:32 +01:00
|
|
|
) -> Resource<
|
2019-11-20 18:33:22 +01:00
|
|
|
impl ServiceFactory<
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceRequest,
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 07:51:32 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
2019-03-05 06:37:57 +01:00
|
|
|
M: Transform<
|
2019-03-02 07:51:32 +01:00
|
|
|
T::Service,
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceRequest,
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 07:51:32 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
|
|
|
Resource {
|
2019-11-20 18:33:22 +01:00
|
|
|
endpoint: apply(mw, self.endpoint),
|
2019-03-07 00:47:15 +01:00
|
|
|
rdef: self.rdef,
|
2019-03-09 23:06:24 +01:00
|
|
|
name: self.name,
|
2019-03-07 00:47:15 +01:00
|
|
|
guards: self.guards,
|
2019-03-02 07:51:32 +01:00
|
|
|
routes: self.routes,
|
|
|
|
default: self.default,
|
2021-01-09 04:36:58 +01:00
|
|
|
app_data: self.app_data,
|
2019-03-02 07:51:32 +01:00
|
|
|
factory_ref: self.factory_ref,
|
|
|
|
}
|
2018-01-10 05:00:18 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 20:47:58 +01:00
|
|
|
/// Register a resource middleware function.
|
|
|
|
///
|
|
|
|
/// This function accepts instance of `ServiceRequest` type and
|
|
|
|
/// mutable reference to the next middleware in chain.
|
|
|
|
///
|
|
|
|
/// This is similar to `App's` middlewares, but middleware get invoked on resource level.
|
|
|
|
/// Resource level middlewares are not allowed to change response
|
|
|
|
/// type (i.e modify response's body).
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_service::Service;
|
|
|
|
/// use actix_web::{web, App};
|
|
|
|
/// use actix_web::http::{header::CONTENT_TYPE, HeaderValue};
|
|
|
|
///
|
2019-11-21 16:34:04 +01:00
|
|
|
/// async fn index() -> &'static str {
|
2019-03-25 20:47:58 +01:00
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/index.html")
|
2019-11-20 18:33:22 +01:00
|
|
|
/// .wrap_fn(|req, srv| {
|
|
|
|
/// let fut = srv.call(req);
|
|
|
|
/// async {
|
|
|
|
/// let mut res = fut.await?;
|
2019-03-25 20:47:58 +01:00
|
|
|
/// res.headers_mut().insert(
|
|
|
|
/// CONTENT_TYPE, HeaderValue::from_static("text/plain"),
|
|
|
|
/// );
|
2019-11-20 18:33:22 +01:00
|
|
|
/// Ok(res)
|
|
|
|
/// }
|
|
|
|
/// })
|
2019-03-25 20:47:58 +01:00
|
|
|
/// .route(web::get().to(index)));
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn wrap_fn<F, R>(
|
|
|
|
self,
|
|
|
|
mw: F,
|
|
|
|
) -> Resource<
|
2019-11-20 18:33:22 +01:00
|
|
|
impl ServiceFactory<
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceRequest,
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2019-03-25 20:47:58 +01:00
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = Error,
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
2021-02-07 02:00:40 +01:00
|
|
|
F: Fn(ServiceRequest, &T::Service) -> R + Clone,
|
2019-11-20 18:33:22 +01:00
|
|
|
R: Future<Output = Result<ServiceResponse, Error>>,
|
2019-03-25 20:47:58 +01:00
|
|
|
{
|
2019-11-20 18:33:22 +01:00
|
|
|
Resource {
|
|
|
|
endpoint: apply_fn_factory(self.endpoint, mw),
|
|
|
|
rdef: self.rdef,
|
|
|
|
name: self.name,
|
|
|
|
guards: self.guards,
|
|
|
|
routes: self.routes,
|
|
|
|
default: self.default,
|
2021-01-09 04:36:58 +01:00
|
|
|
app_data: self.app_data,
|
2019-11-20 18:33:22 +01:00
|
|
|
factory_ref: self.factory_ref,
|
|
|
|
}
|
2019-03-25 20:47:58 +01:00
|
|
|
}
|
|
|
|
|
2019-04-14 07:25:00 +02:00
|
|
|
/// Default service to be used if no matching route could be found.
|
2019-03-25 00:15:34 +01:00
|
|
|
/// By default *405* response get returned. Resource does not use
|
|
|
|
/// default handler from `App` or `Scope`.
|
2019-04-14 07:25:00 +02:00
|
|
|
pub fn default_service<F, U>(mut self, f: F) -> Self
|
2019-03-02 07:51:32 +01:00
|
|
|
where
|
2021-01-04 00:47:04 +01:00
|
|
|
F: IntoServiceFactory<U, ServiceRequest>,
|
2019-11-20 18:33:22 +01:00
|
|
|
U: ServiceFactory<
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceRequest,
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2019-03-09 18:49:11 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-09 18:49:11 +01:00
|
|
|
> + 'static,
|
2019-04-14 07:25:00 +02:00
|
|
|
U::InitError: fmt::Debug,
|
2019-03-02 07:51:32 +01:00
|
|
|
{
|
|
|
|
// create and configure default resource
|
2021-02-12 00:03:17 +01:00
|
|
|
self.default = boxed::factory(
|
|
|
|
f.into_factory()
|
|
|
|
.map_init_err(|e| log::error!("Can not construct default service: {:?}", e)),
|
|
|
|
);
|
2019-03-02 07:51:32 +01:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
2019-03-07 00:47:15 +01:00
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl<T> HttpServiceFactory for Resource<T>
|
2019-03-07 00:47:15 +01:00
|
|
|
where
|
2019-11-20 18:33:22 +01:00
|
|
|
T: ServiceFactory<
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceRequest,
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2019-03-07 00:47:15 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-07 00:47:15 +01:00
|
|
|
InitError = (),
|
|
|
|
> + 'static,
|
|
|
|
{
|
2019-04-15 16:32:49 +02:00
|
|
|
fn register(mut self, config: &mut AppService) {
|
2019-03-07 00:47:15 +01:00
|
|
|
let guards = if self.guards.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2020-05-17 03:54:42 +02:00
|
|
|
Some(std::mem::take(&mut self.guards))
|
2019-03-07 00:47:15 +01:00
|
|
|
};
|
2021-01-16 00:37:33 +01:00
|
|
|
|
2019-03-09 23:06:24 +01:00
|
|
|
let mut rdef = if config.is_root() || !self.rdef.is_empty() {
|
2019-12-25 17:13:52 +01:00
|
|
|
ResourceDef::new(insert_slash(self.rdef.clone()))
|
2019-03-07 00:47:15 +01:00
|
|
|
} else {
|
2019-12-25 17:13:52 +01:00
|
|
|
ResourceDef::new(self.rdef.clone())
|
2019-03-07 00:47:15 +01:00
|
|
|
};
|
2021-01-16 00:37:33 +01:00
|
|
|
|
2019-03-09 23:06:24 +01:00
|
|
|
if let Some(ref name) = self.name {
|
|
|
|
*rdef.name_mut() = name.clone();
|
|
|
|
}
|
2020-04-29 19:20:47 +02:00
|
|
|
|
2019-03-09 16:39:34 +01:00
|
|
|
config.register_service(rdef, guards, self, None)
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 00:47:04 +01:00
|
|
|
impl<T> IntoServiceFactory<T, ServiceRequest> for Resource<T>
|
2019-03-02 07:51:32 +01:00
|
|
|
where
|
2019-11-20 18:33:22 +01:00
|
|
|
T: ServiceFactory<
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceRequest,
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 07:51:32 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
2019-11-20 18:33:22 +01:00
|
|
|
fn into_factory(self) -> T {
|
2019-03-02 07:51:32 +01:00
|
|
|
*self.factory_ref.borrow_mut() = Some(ResourceFactory {
|
|
|
|
routes: self.routes,
|
2021-01-09 04:36:58 +01:00
|
|
|
app_data: self.app_data.map(Rc::new),
|
2019-03-02 07:51:32 +01:00
|
|
|
default: self.default,
|
|
|
|
});
|
|
|
|
|
|
|
|
self.endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct ResourceFactory {
|
|
|
|
routes: Vec<Route>,
|
2021-01-09 04:36:58 +01:00
|
|
|
app_data: Option<Rc<Extensions>>,
|
|
|
|
default: HttpNewService,
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 00:47:04 +01:00
|
|
|
impl ServiceFactory<ServiceRequest> for ResourceFactory {
|
2019-03-02 07:51:32 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2021-01-04 00:47:04 +01:00
|
|
|
type Config = ();
|
2019-04-13 23:50:54 +02:00
|
|
|
type Service = ResourceService;
|
2021-01-04 00:47:04 +01:00
|
|
|
type InitError = ();
|
2021-01-09 04:36:58 +01:00
|
|
|
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
|
2019-03-02 07:51:32 +01:00
|
|
|
|
2019-12-02 16:37:13 +01:00
|
|
|
fn new_service(&self, _: ()) -> Self::Future {
|
2021-01-09 04:36:58 +01:00
|
|
|
// construct default service factory future.
|
|
|
|
let default_fut = self.default.new_service(());
|
2019-03-02 07:51:32 +01:00
|
|
|
|
2021-01-09 04:36:58 +01:00
|
|
|
// construct route service factory futures
|
2021-02-12 00:03:17 +01:00
|
|
|
let factory_fut = join_all(self.routes.iter().map(|route| route.new_service(())));
|
2019-03-02 07:51:32 +01:00
|
|
|
|
2021-01-09 04:36:58 +01:00
|
|
|
let app_data = self.app_data.clone();
|
2018-06-19 19:46:58 +02:00
|
|
|
|
2021-01-09 04:36:58 +01:00
|
|
|
Box::pin(async move {
|
|
|
|
let default = default_fut.await?;
|
|
|
|
let routes = factory_fut
|
|
|
|
.await
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
2019-03-02 07:51:32 +01:00
|
|
|
|
2021-01-09 04:36:58 +01:00
|
|
|
Ok(ResourceService {
|
|
|
|
app_data,
|
|
|
|
default,
|
2019-03-02 07:51:32 +01:00
|
|
|
routes,
|
2021-01-09 04:36:58 +01:00
|
|
|
})
|
|
|
|
})
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-15 11:12:21 +02:00
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct ResourceService {
|
|
|
|
routes: Vec<RouteService>,
|
2021-01-09 04:36:58 +01:00
|
|
|
app_data: Option<Rc<Extensions>>,
|
|
|
|
default: HttpService,
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 00:47:04 +01:00
|
|
|
impl Service<ServiceRequest> for ResourceService {
|
2019-03-02 07:51:32 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2021-01-09 04:36:58 +01:00
|
|
|
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
2018-07-15 11:12:21 +02:00
|
|
|
|
2021-01-09 04:36:58 +01:00
|
|
|
actix_service::always_ready!();
|
2018-07-15 11:12:21 +02:00
|
|
|
|
2021-02-07 02:00:40 +01:00
|
|
|
fn call(&self, mut req: ServiceRequest) -> Self::Future {
|
|
|
|
for route in self.routes.iter() {
|
2019-03-02 07:51:32 +01:00
|
|
|
if route.check(&mut req) {
|
2021-01-09 04:36:58 +01:00
|
|
|
if let Some(ref app_data) = self.app_data {
|
|
|
|
req.add_data_container(app_data.clone());
|
2019-05-05 04:43:49 +02:00
|
|
|
}
|
2021-01-16 00:37:33 +01:00
|
|
|
|
2021-01-02 20:40:31 +01:00
|
|
|
return route.call(req);
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-16 00:37:33 +01:00
|
|
|
|
2021-01-09 04:36:58 +01:00
|
|
|
if let Some(ref app_data) = self.app_data {
|
|
|
|
req.add_data_container(app_data.clone());
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
2021-01-16 00:37:33 +01:00
|
|
|
|
2021-01-09 04:36:58 +01:00
|
|
|
self.default.call(req)
|
2018-07-15 11:12:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
#[doc(hidden)]
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct ResourceEndpoint {
|
|
|
|
factory: Rc<RefCell<Option<ResourceFactory>>>,
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl ResourceEndpoint {
|
|
|
|
fn new(factory: Rc<RefCell<Option<ResourceFactory>>>) -> Self {
|
2019-03-02 07:51:32 +01:00
|
|
|
ResourceEndpoint { factory }
|
2018-07-15 11:12:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 00:47:04 +01:00
|
|
|
impl ServiceFactory<ServiceRequest> for ResourceEndpoint {
|
2019-03-02 07:51:32 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2021-01-09 04:36:58 +01:00
|
|
|
type Config = ();
|
2019-04-13 23:50:54 +02:00
|
|
|
type Service = ResourceService;
|
2021-01-09 04:36:58 +01:00
|
|
|
type InitError = ();
|
|
|
|
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
|
2019-03-02 07:51:32 +01:00
|
|
|
|
2019-12-02 16:37:13 +01:00
|
|
|
fn new_service(&self, _: ()) -> Self::Future {
|
2021-01-09 04:36:58 +01:00
|
|
|
self.factory.borrow().as_ref().unwrap().new_service(())
|
2018-07-15 11:12:21 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-25 00:15:34 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-03-25 22:33:34 +01:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2021-01-04 00:47:04 +01:00
|
|
|
use actix_rt::time::sleep;
|
2019-03-25 20:47:58 +01:00
|
|
|
use actix_service::Service;
|
2020-05-18 04:47:20 +02:00
|
|
|
use futures_util::future::ok;
|
2019-03-25 20:47:58 +01:00
|
|
|
|
|
|
|
use crate::http::{header, HeaderValue, Method, StatusCode};
|
2019-11-20 18:33:22 +01:00
|
|
|
use crate::middleware::DefaultHeaders;
|
2019-11-26 06:25:50 +01:00
|
|
|
use crate::service::ServiceRequest;
|
2020-05-09 01:31:26 +02:00
|
|
|
use crate::test::{call_service, init_service, TestRequest};
|
|
|
|
use crate::{guard, web, App, Error, HttpResponse};
|
2019-03-25 20:47:58 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_middleware() {
|
2021-02-12 00:03:17 +01:00
|
|
|
let srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::resource("/test")
|
|
|
|
.name("test")
|
|
|
|
.wrap(
|
|
|
|
DefaultHeaders::new()
|
|
|
|
.header(header::CONTENT_TYPE, HeaderValue::from_static("0001")),
|
|
|
|
)
|
|
|
|
.route(web::get().to(HttpResponse::Ok)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("0001")
|
|
|
|
);
|
2019-03-25 20:47:58 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_middleware_fn() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new().service(
|
|
|
|
web::resource("/test")
|
|
|
|
.wrap_fn(|req, srv| {
|
|
|
|
let fut = srv.call(req);
|
|
|
|
async {
|
|
|
|
fut.await.map(|mut res| {
|
|
|
|
res.headers_mut().insert(
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
HeaderValue::from_static("0001"),
|
|
|
|
);
|
|
|
|
res
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2020-07-22 01:28:33 +02:00
|
|
|
.route(web::get().to(HttpResponse::Ok)),
|
2019-11-26 06:25:50 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("0001")
|
|
|
|
);
|
2019-03-25 20:47:58 +01:00
|
|
|
}
|
2019-03-25 00:15:34 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_to() {
|
2021-02-12 00:03:17 +01:00
|
|
|
let srv = init_service(App::new().service(web::resource("/test").to(|| async {
|
|
|
|
sleep(Duration::from_millis(100)).await;
|
|
|
|
Ok::<_, Error>(HttpResponse::Ok())
|
|
|
|
})))
|
|
|
|
.await;
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-03-25 22:33:34 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
2019-12-25 17:13:52 +01:00
|
|
|
async fn test_pattern() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-12-25 17:17:22 +01:00
|
|
|
App::new().service(
|
|
|
|
web::resource(["/test", "/test2"])
|
|
|
|
.to(|| async { Ok::<_, Error>(HttpResponse::Ok()) }),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2019-12-25 17:13:52 +01:00
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-12-25 17:13:52 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
let req = TestRequest::with_uri("/test2").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-12-25 17:13:52 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2019-11-26 06:25:50 +01:00
|
|
|
async fn test_default_resource() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new()
|
2020-07-22 01:28:33 +02:00
|
|
|
.service(web::resource("/test").route(web::get().to(HttpResponse::Ok)))
|
2019-11-26 06:25:50 +01:00
|
|
|
.default_service(|r: ServiceRequest| {
|
|
|
|
ok(r.into_response(HttpResponse::BadRequest()))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
|
|
|
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new().service(
|
|
|
|
web::resource("/test")
|
2020-07-22 01:28:33 +02:00
|
|
|
.route(web::get().to(HttpResponse::Ok))
|
2019-04-14 07:25:00 +02:00
|
|
|
.default_service(|r: ServiceRequest| {
|
2019-11-20 18:33:22 +01:00
|
|
|
ok(r.into_response(HttpResponse::BadRequest()))
|
2019-04-14 07:25:00 +02:00
|
|
|
}),
|
2019-11-26 06:25:50 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
2019-03-25 00:15:34 +01:00
|
|
|
}
|
2019-05-15 19:31:40 +02:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_resource_guards() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new()
|
|
|
|
.service(
|
|
|
|
web::resource("/test/{p}")
|
|
|
|
.guard(guard::Get())
|
2020-07-22 01:28:33 +02:00
|
|
|
.to(HttpResponse::Ok),
|
2019-11-26 06:25:50 +01:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/test/{p}")
|
|
|
|
.guard(guard::Put())
|
2020-07-22 01:28:33 +02:00
|
|
|
.to(HttpResponse::Created),
|
2019-11-26 06:25:50 +01:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/test/{p}")
|
|
|
|
.guard(guard::Delete())
|
2020-07-22 01:28:33 +02:00
|
|
|
.to(HttpResponse::NoContent),
|
2019-11-26 06:25:50 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test/it")
|
|
|
|
.method(Method::GET)
|
|
|
|
.to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test/it")
|
|
|
|
.method(Method::PUT)
|
|
|
|
.to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test/it")
|
|
|
|
.method(Method::DELETE)
|
|
|
|
.to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
|
2019-05-15 19:31:40 +02:00
|
|
|
}
|
2019-09-18 02:36:39 +02:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_data() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new()
|
|
|
|
.data(1.0f64)
|
|
|
|
.data(1usize)
|
2019-12-20 12:13:09 +01:00
|
|
|
.app_data(web::Data::new('-'))
|
2019-11-26 06:25:50 +01:00
|
|
|
.service(
|
|
|
|
web::resource("/test")
|
|
|
|
.data(10usize)
|
2019-12-20 12:13:09 +01:00
|
|
|
.app_data(web::Data::new('*'))
|
2019-11-26 06:25:50 +01:00
|
|
|
.guard(guard::Get())
|
|
|
|
.to(
|
|
|
|
|data1: web::Data<usize>,
|
|
|
|
data2: web::Data<char>,
|
|
|
|
data3: web::Data<f64>| {
|
2019-12-20 12:45:35 +01:00
|
|
|
assert_eq!(**data1, 10);
|
|
|
|
assert_eq!(**data2, '*');
|
2020-07-22 01:28:33 +02:00
|
|
|
let error = std::f64::EPSILON;
|
|
|
|
assert!((**data3 - 1.0).abs() < error);
|
2019-11-26 06:25:50 +01:00
|
|
|
HttpResponse::Ok()
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2019-11-20 18:33:22 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::get().uri("/test").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-09-18 02:36:39 +02:00
|
|
|
}
|
2020-04-14 03:33:19 +02:00
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_data_default_service() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2020-04-14 03:33:19 +02:00
|
|
|
App::new().data(1usize).service(
|
|
|
|
web::resource("/test")
|
|
|
|
.data(10usize)
|
|
|
|
.default_service(web::to(|data: web::Data<usize>| {
|
|
|
|
assert_eq!(**data, 10);
|
|
|
|
HttpResponse::Ok()
|
|
|
|
})),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::get().uri("/test").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2020-04-14 03:33:19 +02:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
2019-03-25 00:15:34 +01:00
|
|
|
}
|