2019-03-02 07:51:32 +01:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2019-03-03 01:24:14 +01:00
|
|
|
use actix_http::body::{Body, MessageBody};
|
2019-03-28 20:32:59 +01:00
|
|
|
#[cfg(any(feature = "brotli", feature = "flate2-zlib", feature = "flate2-rust"))]
|
|
|
|
use actix_http::encoding::{Decoder, Encoder};
|
2019-03-09 18:49:11 +01:00
|
|
|
use actix_server_config::ServerConfig;
|
|
|
|
use actix_service::boxed::{self, BoxedNewService};
|
2019-03-02 07:51:32 +01:00
|
|
|
use actix_service::{
|
2019-03-30 15:56:09 +01:00
|
|
|
apply_transform, IntoNewService, IntoTransform, NewService, Transform,
|
2019-03-02 07:51:32 +01:00
|
|
|
};
|
2019-03-28 22:27:07 +01:00
|
|
|
#[cfg(any(feature = "brotli", feature = "flate2-zlib", feature = "flate2-rust"))]
|
2019-03-28 20:32:59 +01:00
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::{IntoFuture, Stream};
|
2019-03-02 07:51:32 +01:00
|
|
|
|
2019-03-09 18:49:11 +01:00
|
|
|
use crate::app_service::{AppChain, AppEntry, AppInit, AppRouting, AppRoutingFactory};
|
2019-04-04 00:09:31 +02:00
|
|
|
use crate::config::{AppConfig, AppConfigInner, RouterConfig};
|
2019-03-17 04:17:27 +01:00
|
|
|
use crate::data::{Data, DataFactory};
|
2019-03-28 20:32:59 +01:00
|
|
|
use crate::dev::{Payload, PayloadStream, ResourceDef};
|
|
|
|
use crate::error::{Error, PayloadError};
|
2019-03-02 07:51:32 +01:00
|
|
|
use crate::resource::Resource;
|
2019-03-07 00:47:15 +01:00
|
|
|
use crate::route::Route;
|
|
|
|
use crate::service::{
|
|
|
|
HttpServiceFactory, ServiceFactory, ServiceFactoryWrapper, ServiceRequest,
|
|
|
|
ServiceResponse,
|
|
|
|
};
|
2019-03-02 07:51:32 +01:00
|
|
|
|
2019-03-11 00:35:38 +01:00
|
|
|
type HttpNewService<P> =
|
|
|
|
BoxedNewService<(), ServiceRequest<P>, ServiceResponse, Error, ()>;
|
2019-03-02 07:51:32 +01:00
|
|
|
|
2019-03-03 07:03:45 +01:00
|
|
|
/// Application builder - structure that follows the builder pattern
|
|
|
|
/// for building application instances.
|
2019-03-28 20:32:59 +01:00
|
|
|
pub struct App<In, Out, T>
|
2019-03-02 20:53:05 +01:00
|
|
|
where
|
2019-03-28 20:32:59 +01:00
|
|
|
T: NewService<Request = ServiceRequest<In>, Response = ServiceRequest<Out>>,
|
2019-03-02 20:53:05 +01:00
|
|
|
{
|
|
|
|
chain: T,
|
2019-03-17 04:17:27 +01:00
|
|
|
data: Vec<Box<DataFactory>>,
|
2019-03-09 23:06:24 +01:00
|
|
|
config: AppConfigInner,
|
2019-03-28 20:32:59 +01:00
|
|
|
_t: PhantomData<(In, Out)>,
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-03-28 20:32:59 +01:00
|
|
|
impl App<PayloadStream, PayloadStream, AppChain> {
|
2019-03-17 04:17:27 +01:00
|
|
|
/// Create application builder. Application can be configured with a builder-like pattern.
|
2019-03-02 07:51:32 +01:00
|
|
|
pub fn new() -> Self {
|
2019-03-02 20:53:05 +01:00
|
|
|
App {
|
|
|
|
chain: AppChain,
|
2019-03-17 04:17:27 +01:00
|
|
|
data: Vec::new(),
|
2019-03-09 23:06:24 +01:00
|
|
|
config: AppConfigInner::default(),
|
2019-03-02 20:53:05 +01:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 20:32:59 +01:00
|
|
|
impl<In, Out, T> App<In, Out, T>
|
2019-03-02 20:53:05 +01:00
|
|
|
where
|
2019-03-28 20:32:59 +01:00
|
|
|
In: 'static,
|
|
|
|
Out: 'static,
|
2019-03-02 20:53:05 +01:00
|
|
|
T: NewService<
|
2019-03-28 20:32:59 +01:00
|
|
|
Request = ServiceRequest<In>,
|
|
|
|
Response = ServiceRequest<Out>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 20:53:05 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
2019-04-10 21:43:31 +02:00
|
|
|
/// Set application data. Application data could be accessed
|
2019-03-17 04:17:27 +01:00
|
|
|
/// by using `Data<T>` extractor where `T` is data type.
|
2019-03-02 07:51:32 +01:00
|
|
|
///
|
|
|
|
/// **Note**: http server accepts an application factory rather than
|
|
|
|
/// an application instance. Http server constructs an application
|
2019-03-17 04:17:27 +01:00
|
|
|
/// instance for each thread, thus application data must be constructed
|
|
|
|
/// multiple times. If you want to share data between different
|
2019-03-02 07:51:32 +01:00
|
|
|
/// threads, a shared object should be used, e.g. `Arc`. Application
|
2019-03-17 04:17:27 +01:00
|
|
|
/// data does not need to be `Send` or `Sync`.
|
2019-03-03 07:11:24 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::cell::Cell;
|
2019-03-07 20:43:46 +01:00
|
|
|
/// use actix_web::{web, App};
|
2019-03-03 07:11:24 +01:00
|
|
|
///
|
2019-03-17 04:17:27 +01:00
|
|
|
/// struct MyData {
|
2019-03-03 07:11:24 +01:00
|
|
|
/// counter: Cell<usize>,
|
|
|
|
/// }
|
|
|
|
///
|
2019-03-17 04:17:27 +01:00
|
|
|
/// fn index(data: web::Data<MyData>) {
|
|
|
|
/// data.counter.set(data.counter.get() + 1);
|
2019-03-03 07:11:24 +01:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new()
|
2019-03-17 04:17:27 +01:00
|
|
|
/// .data(MyData{ counter: Cell::new(0) })
|
2019-03-07 00:47:15 +01:00
|
|
|
/// .service(
|
|
|
|
/// web::resource("/index.html").route(
|
|
|
|
/// web::get().to(index)));
|
2019-03-03 07:11:24 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-03-17 04:17:27 +01:00
|
|
|
pub fn data<S: 'static>(mut self, data: S) -> Self {
|
|
|
|
self.data.push(Box::new(Data::new(data)));
|
2019-03-02 07:51:32 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-17 04:17:27 +01:00
|
|
|
/// Set application data factory. This function is
|
|
|
|
/// similar to `.data()` but it accepts data factory. Data object get
|
2019-03-02 07:51:32 +01:00
|
|
|
/// constructed asynchronously during application initialization.
|
2019-03-28 20:32:59 +01:00
|
|
|
pub fn data_factory<F, R>(mut self, data: F) -> Self
|
2019-03-02 07:51:32 +01:00
|
|
|
where
|
2019-03-28 20:32:59 +01:00
|
|
|
F: Fn() -> R + 'static,
|
|
|
|
R: IntoFuture + 'static,
|
|
|
|
R::Error: std::fmt::Debug,
|
2019-03-02 07:51:32 +01:00
|
|
|
{
|
2019-03-17 04:17:27 +01:00
|
|
|
self.data.push(Box::new(data));
|
2019-03-02 07:51:32 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-04-02 21:51:16 +02:00
|
|
|
/// Registers middleware, in the form of a middleware component (type),
|
|
|
|
/// that runs during inbound and/or outbound processing in the request
|
|
|
|
/// lifecycle (request -> response), modifying request/response as
|
2019-04-01 20:52:05 +02:00
|
|
|
/// necessary, across all requests managed by the *Application*.
|
|
|
|
///
|
|
|
|
/// Use middleware when you need to read or modify *every* request or response in some way.
|
2019-04-01 15:30:11 +02:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_service::Service;
|
|
|
|
/// # use futures::Future;
|
|
|
|
/// use actix_web::{middleware, web, App};
|
|
|
|
/// use actix_web::http::{header::CONTENT_TYPE, HeaderValue};
|
|
|
|
///
|
|
|
|
/// fn index() -> &'static str {
|
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new()
|
|
|
|
/// .wrap(middleware::Logger::default())
|
|
|
|
/// .route("/index.html", web::get().to(index));
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-03-25 21:02:10 +01:00
|
|
|
pub fn wrap<M, B, F>(
|
2019-03-02 20:53:05 +01:00
|
|
|
self,
|
|
|
|
mw: F,
|
|
|
|
) -> AppRouter<
|
|
|
|
T,
|
2019-03-28 20:32:59 +01:00
|
|
|
Out,
|
2019-03-03 04:19:56 +01:00
|
|
|
B,
|
2019-03-02 20:53:05 +01:00
|
|
|
impl NewService<
|
2019-03-28 20:32:59 +01:00
|
|
|
Request = ServiceRequest<Out>,
|
2019-03-03 04:19:56 +01:00
|
|
|
Response = ServiceResponse<B>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 20:53:05 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
2019-03-05 06:37:57 +01:00
|
|
|
M: Transform<
|
2019-03-28 20:32:59 +01:00
|
|
|
AppRouting<Out>,
|
|
|
|
Request = ServiceRequest<Out>,
|
2019-03-03 04:19:56 +01:00
|
|
|
Response = ServiceResponse<B>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 20:53:05 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
2019-03-28 20:32:59 +01:00
|
|
|
F: IntoTransform<M, AppRouting<Out>>,
|
2019-03-02 20:53:05 +01:00
|
|
|
{
|
|
|
|
let fref = Rc::new(RefCell::new(None));
|
2019-03-30 15:56:09 +01:00
|
|
|
let endpoint = apply_transform(mw, AppEntry::new(fref.clone()));
|
2019-03-02 20:53:05 +01:00
|
|
|
AppRouter {
|
|
|
|
endpoint,
|
|
|
|
chain: self.chain,
|
2019-03-17 04:17:27 +01:00
|
|
|
data: self.data,
|
2019-03-02 20:53:05 +01:00
|
|
|
services: Vec::new(),
|
|
|
|
default: None,
|
|
|
|
factory_ref: fref,
|
2019-03-09 23:06:24 +01:00
|
|
|
config: self.config,
|
|
|
|
external: Vec::new(),
|
2019-03-02 20:53:05 +01:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 20:52:05 +02:00
|
|
|
/// Registers middleware, in the form of a closure, that runs during inbound
|
|
|
|
/// and/or outbound processing in the request lifecycle (request -> response),
|
|
|
|
/// modifying request/response as necessary, across all requests managed by
|
|
|
|
/// the *Application*.
|
|
|
|
///
|
|
|
|
/// Use middleware when you need to read or modify *every* request or response in some way.
|
2019-03-25 21:43:02 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_service::Service;
|
|
|
|
/// # use futures::Future;
|
|
|
|
/// use actix_web::{web, App};
|
|
|
|
/// use actix_web::http::{header::CONTENT_TYPE, HeaderValue};
|
|
|
|
///
|
|
|
|
/// fn index() -> &'static str {
|
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new()
|
|
|
|
/// .wrap_fn(|req, srv|
|
|
|
|
/// srv.call(req).map(|mut res| {
|
|
|
|
/// res.headers_mut().insert(
|
|
|
|
/// CONTENT_TYPE, HeaderValue::from_static("text/plain"),
|
|
|
|
/// );
|
|
|
|
/// res
|
|
|
|
/// }))
|
|
|
|
/// .route("/index.html", web::get().to(index));
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn wrap_fn<F, R, B>(
|
|
|
|
self,
|
|
|
|
mw: F,
|
|
|
|
) -> AppRouter<
|
|
|
|
T,
|
2019-03-28 20:32:59 +01:00
|
|
|
Out,
|
2019-03-25 21:43:02 +01:00
|
|
|
B,
|
|
|
|
impl NewService<
|
2019-03-28 20:32:59 +01:00
|
|
|
Request = ServiceRequest<Out>,
|
2019-03-25 21:43:02 +01:00
|
|
|
Response = ServiceResponse<B>,
|
|
|
|
Error = Error,
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
2019-03-28 20:32:59 +01:00
|
|
|
F: FnMut(ServiceRequest<Out>, &mut AppRouting<Out>) -> R + Clone,
|
2019-03-25 21:43:02 +01:00
|
|
|
R: IntoFuture<Item = ServiceResponse<B>, Error = Error>,
|
|
|
|
{
|
|
|
|
self.wrap(mw)
|
|
|
|
}
|
|
|
|
|
2019-03-02 20:53:05 +01:00
|
|
|
/// Register a request modifier. It can modify any request parameters
|
2019-03-26 23:14:32 +01:00
|
|
|
/// including request payload type.
|
2019-03-28 20:32:59 +01:00
|
|
|
pub fn chain<C, F, P>(
|
2019-03-02 20:53:05 +01:00
|
|
|
self,
|
2019-03-26 23:14:32 +01:00
|
|
|
chain: F,
|
2019-03-02 20:53:05 +01:00
|
|
|
) -> App<
|
2019-03-28 20:32:59 +01:00
|
|
|
In,
|
|
|
|
P,
|
2019-03-02 20:53:05 +01:00
|
|
|
impl NewService<
|
2019-03-28 20:32:59 +01:00
|
|
|
Request = ServiceRequest<In>,
|
|
|
|
Response = ServiceRequest<P>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 20:53:05 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
|
|
|
C: NewService<
|
2019-03-28 20:32:59 +01:00
|
|
|
Request = ServiceRequest<Out>,
|
|
|
|
Response = ServiceRequest<P>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 20:53:05 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
2019-03-09 18:49:11 +01:00
|
|
|
F: IntoNewService<C>,
|
2019-03-02 20:53:05 +01:00
|
|
|
{
|
|
|
|
let chain = self.chain.and_then(chain.into_new_service());
|
2019-03-02 07:51:32 +01:00
|
|
|
App {
|
2019-03-02 20:53:05 +01:00
|
|
|
chain,
|
2019-03-17 04:17:27 +01:00
|
|
|
data: self.data,
|
2019-03-09 23:06:24 +01:00
|
|
|
config: self.config,
|
2019-03-02 20:53:05 +01:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-04-04 00:09:31 +02:00
|
|
|
/// Run external configuration as part of the application building
|
|
|
|
/// process
|
|
|
|
///
|
|
|
|
/// This function is useful for moving parts of configuration to a
|
|
|
|
/// different module or even library. For example,
|
|
|
|
/// some of the resource's configuration could be moved to different module.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// use actix_web::{web, middleware, App, HttpResponse};
|
|
|
|
///
|
|
|
|
/// // this function could be located in different module
|
|
|
|
/// fn config<P>(cfg: &mut web::RouterConfig<P>) {
|
|
|
|
/// cfg.service(web::resource("/test")
|
|
|
|
/// .route(web::get().to(|| HttpResponse::Ok()))
|
|
|
|
/// .route(web::head().to(|| HttpResponse::MethodNotAllowed()))
|
|
|
|
/// );
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new()
|
|
|
|
/// .wrap(middleware::Logger::default())
|
|
|
|
/// .configure(config) // <- register resources
|
|
|
|
/// .route("/index.html", web::get().to(|| HttpResponse::Ok()));
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn configure<F>(mut self, f: F) -> AppRouter<T, Out, Body, AppEntry<Out>>
|
|
|
|
where
|
|
|
|
F: Fn(&mut RouterConfig<Out>),
|
|
|
|
{
|
|
|
|
let mut cfg = RouterConfig::new();
|
|
|
|
f(&mut cfg);
|
|
|
|
self.data.extend(cfg.data);
|
|
|
|
|
|
|
|
let fref = Rc::new(RefCell::new(None));
|
|
|
|
|
|
|
|
AppRouter {
|
|
|
|
chain: self.chain,
|
|
|
|
default: None,
|
|
|
|
endpoint: AppEntry::new(fref.clone()),
|
|
|
|
factory_ref: fref,
|
|
|
|
data: self.data,
|
|
|
|
config: self.config,
|
|
|
|
services: cfg.services,
|
|
|
|
external: cfg.external,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:47:15 +01:00
|
|
|
/// Configure route for a specific path.
|
|
|
|
///
|
|
|
|
/// This is a simplified version of the `App::service()` method.
|
2019-03-17 04:17:27 +01:00
|
|
|
/// This method can be used multiple times with same path, in that case
|
2019-03-07 00:47:15 +01:00
|
|
|
/// multiple resources with one route would be registered for same resource path.
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-07 23:01:52 +01:00
|
|
|
/// use actix_web::{web, App, HttpResponse};
|
2019-03-07 00:47:15 +01:00
|
|
|
///
|
2019-03-07 23:01:52 +01:00
|
|
|
/// fn index(data: web::Path<(String, String)>) -> &'static str {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new()
|
|
|
|
/// .route("/test1", web::get().to(index))
|
|
|
|
/// .route("/test2", web::post().to(|| HttpResponse::MethodNotAllowed()));
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn route(
|
|
|
|
self,
|
|
|
|
path: &str,
|
2019-03-28 20:32:59 +01:00
|
|
|
mut route: Route<Out>,
|
|
|
|
) -> AppRouter<T, Out, Body, AppEntry<Out>> {
|
2019-03-07 00:47:15 +01:00
|
|
|
self.service(
|
|
|
|
Resource::new(path)
|
|
|
|
.add_guards(route.take_guards())
|
|
|
|
.route(route),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register http service.
|
2019-03-17 05:35:02 +01:00
|
|
|
///
|
|
|
|
/// Http service is any type that implements `HttpServiceFactory` trait.
|
|
|
|
///
|
|
|
|
/// Actix web provides several services implementations:
|
|
|
|
///
|
|
|
|
/// * *Resource* is an entry in resource table which corresponds to requested URL.
|
|
|
|
/// * *Scope* is a set of resources with common root path.
|
|
|
|
/// * "StaticFiles" is a service for static files support
|
2019-03-28 20:32:59 +01:00
|
|
|
pub fn service<F>(self, service: F) -> AppRouter<T, Out, Body, AppEntry<Out>>
|
2019-03-06 07:10:08 +01:00
|
|
|
where
|
2019-03-28 20:32:59 +01:00
|
|
|
F: HttpServiceFactory<Out> + 'static,
|
2019-03-06 07:10:08 +01:00
|
|
|
{
|
|
|
|
let fref = Rc::new(RefCell::new(None));
|
2019-03-07 00:47:15 +01:00
|
|
|
|
2019-03-06 07:10:08 +01:00
|
|
|
AppRouter {
|
|
|
|
chain: self.chain,
|
|
|
|
default: None,
|
|
|
|
endpoint: AppEntry::new(fref.clone()),
|
|
|
|
factory_ref: fref,
|
2019-03-17 04:17:27 +01:00
|
|
|
data: self.data,
|
2019-03-09 23:06:24 +01:00
|
|
|
config: self.config,
|
2019-03-07 00:47:15 +01:00
|
|
|
services: vec![Box::new(ServiceFactoryWrapper::new(service))],
|
2019-03-09 23:06:24 +01:00
|
|
|
external: Vec::new(),
|
2019-03-06 07:10:08 +01:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 06:02:01 +01:00
|
|
|
/// Set server host name.
|
|
|
|
///
|
2019-03-17 04:17:27 +01:00
|
|
|
/// Host name is used by application router as a hostname for url
|
2019-03-04 06:02:01 +01:00
|
|
|
/// generation. Check [ConnectionInfo](./dev/struct.ConnectionInfo.
|
|
|
|
/// html#method.host) documentation for more information.
|
|
|
|
///
|
|
|
|
/// By default host name is set to a "localhost" value.
|
2019-03-09 19:53:00 +01:00
|
|
|
pub fn hostname(mut self, val: &str) -> Self {
|
2019-03-09 23:06:24 +01:00
|
|
|
self.config.host = val.to_owned();
|
2019-03-04 06:02:01 +01:00
|
|
|
self
|
|
|
|
}
|
2019-03-28 20:32:59 +01:00
|
|
|
|
|
|
|
#[cfg(any(feature = "brotli", feature = "flate2-zlib", feature = "flate2-rust"))]
|
|
|
|
/// Enable content compression and decompression.
|
|
|
|
pub fn enable_encoding(
|
|
|
|
self,
|
|
|
|
) -> AppRouter<
|
|
|
|
impl NewService<
|
|
|
|
Request = ServiceRequest<In>,
|
|
|
|
Response = ServiceRequest<Decoder<Payload<Out>>>,
|
|
|
|
Error = Error,
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
Decoder<Payload<Out>>,
|
|
|
|
Encoder<Body>,
|
|
|
|
impl NewService<
|
|
|
|
Request = ServiceRequest<Decoder<Payload<Out>>>,
|
|
|
|
Response = ServiceResponse<Encoder<Body>>,
|
|
|
|
Error = Error,
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
|
|
|
Out: Stream<Item = Bytes, Error = PayloadError>,
|
|
|
|
{
|
|
|
|
use crate::middleware::encoding::{Compress, Decompress};
|
|
|
|
|
|
|
|
self.chain(Decompress::new()).wrap(Compress::default())
|
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 23:45:56 +01:00
|
|
|
/// Application router builder - Structure that follows the builder pattern
|
|
|
|
/// for building application instances.
|
2019-03-02 20:53:05 +01:00
|
|
|
pub struct AppRouter<C, P, B, T> {
|
|
|
|
chain: C,
|
|
|
|
endpoint: T,
|
2019-03-07 00:47:15 +01:00
|
|
|
services: Vec<Box<ServiceFactory<P>>>,
|
|
|
|
default: Option<Rc<HttpNewService<P>>>,
|
2019-03-03 17:24:09 +01:00
|
|
|
factory_ref: Rc<RefCell<Option<AppRoutingFactory<P>>>>,
|
2019-03-17 04:17:27 +01:00
|
|
|
data: Vec<Box<DataFactory>>,
|
2019-03-09 23:06:24 +01:00
|
|
|
config: AppConfigInner,
|
|
|
|
external: Vec<ResourceDef>,
|
2019-03-02 20:53:05 +01:00
|
|
|
_t: PhantomData<(P, B)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C, P, B, T> AppRouter<C, P, B, T>
|
2019-03-02 07:51:32 +01:00
|
|
|
where
|
|
|
|
P: 'static,
|
|
|
|
B: MessageBody,
|
|
|
|
T: NewService<
|
2019-03-09 18:49:11 +01:00
|
|
|
Request = ServiceRequest<P>,
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse<B>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 07:51:32 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
2019-04-04 00:09:31 +02:00
|
|
|
/// Run external configuration as part of the application building
|
|
|
|
/// process
|
|
|
|
///
|
|
|
|
/// This function is useful for moving parts of configuration to a
|
|
|
|
/// different module or even library. For example,
|
|
|
|
/// some of the resource's configuration could be moved to different module.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// use actix_web::{web, middleware, App, HttpResponse};
|
|
|
|
///
|
|
|
|
/// // this function could be located in different module
|
|
|
|
/// fn config<P>(cfg: &mut web::RouterConfig<P>) {
|
|
|
|
/// cfg.service(web::resource("/test")
|
|
|
|
/// .route(web::get().to(|| HttpResponse::Ok()))
|
|
|
|
/// .route(web::head().to(|| HttpResponse::MethodNotAllowed()))
|
|
|
|
/// );
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new()
|
|
|
|
/// .wrap(middleware::Logger::default())
|
|
|
|
/// .configure(config) // <- register resources
|
|
|
|
/// .route("/index.html", web::get().to(|| HttpResponse::Ok()));
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn configure<F>(mut self, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: Fn(&mut RouterConfig<P>),
|
|
|
|
{
|
|
|
|
let mut cfg = RouterConfig::new();
|
|
|
|
f(&mut cfg);
|
|
|
|
self.data.extend(cfg.data);
|
|
|
|
self.services.extend(cfg.services);
|
|
|
|
self.external.extend(cfg.external);
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:47:15 +01:00
|
|
|
/// Configure route for a specific path.
|
2019-03-04 06:02:01 +01:00
|
|
|
///
|
2019-03-07 00:47:15 +01:00
|
|
|
/// This is a simplified version of the `App::service()` method.
|
|
|
|
/// This method can not be could multiple times, in that case
|
|
|
|
/// multiple resources with one route would be registered for same resource path.
|
2019-03-04 06:02:01 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-07 23:01:52 +01:00
|
|
|
/// use actix_web::{web, App, HttpResponse};
|
2019-03-04 06:02:01 +01:00
|
|
|
///
|
2019-03-07 23:01:52 +01:00
|
|
|
/// fn index(data: web::Path<(String, String)>) -> &'static str {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// "Welcome!"
|
2019-03-04 06:02:01 +01:00
|
|
|
/// }
|
2019-03-02 07:51:32 +01:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-03 07:03:45 +01:00
|
|
|
/// let app = App::new()
|
2019-03-07 00:47:15 +01:00
|
|
|
/// .route("/test1", web::get().to(index))
|
|
|
|
/// .route("/test2", web::post().to(|| HttpResponse::MethodNotAllowed()));
|
2019-03-02 07:51:32 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-03-07 00:47:15 +01:00
|
|
|
pub fn route(self, path: &str, mut route: Route<P>) -> Self {
|
|
|
|
self.service(
|
|
|
|
Resource::new(path)
|
|
|
|
.add_guards(route.take_guards())
|
|
|
|
.route(route),
|
|
|
|
)
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-03-07 00:47:15 +01:00
|
|
|
/// Register http service.
|
2019-03-02 07:51:32 +01:00
|
|
|
///
|
2019-03-07 00:47:15 +01:00
|
|
|
/// Http service is any type that implements `HttpServiceFactory` trait.
|
|
|
|
///
|
|
|
|
/// Actix web provides several services implementations:
|
|
|
|
///
|
2019-03-17 05:35:02 +01:00
|
|
|
/// * *Resource* is an entry in resource table which corresponds to requested URL.
|
2019-03-07 00:47:15 +01:00
|
|
|
/// * *Scope* is a set of resources with common root path.
|
|
|
|
/// * "StaticFiles" is a service for static files support
|
2019-03-06 07:10:08 +01:00
|
|
|
pub fn service<F>(mut self, factory: F) -> Self
|
2019-03-02 07:51:32 +01:00
|
|
|
where
|
2019-03-06 07:10:08 +01:00
|
|
|
F: HttpServiceFactory<P> + 'static,
|
2019-03-02 07:51:32 +01:00
|
|
|
{
|
2019-03-07 00:47:15 +01:00
|
|
|
self.services
|
|
|
|
.push(Box::new(ServiceFactoryWrapper::new(factory)));
|
2019-03-02 07:51:32 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-04-02 21:51:16 +02:00
|
|
|
/// Registers middleware, in the form of a middleware component (type),
|
|
|
|
/// that runs during inbound and/or outbound processing in the request
|
|
|
|
/// lifecycle (request -> response), modifying request/response as
|
2019-04-01 20:52:05 +02:00
|
|
|
/// necessary, across all requests managed by the *Route*.
|
|
|
|
///
|
|
|
|
/// Use middleware when you need to read or modify *every* request or response in some way.
|
|
|
|
///
|
2019-03-25 21:02:10 +01:00
|
|
|
pub fn wrap<M, B1, F>(
|
2019-03-02 07:51:32 +01:00
|
|
|
self,
|
|
|
|
mw: F,
|
2019-03-02 20:53:05 +01:00
|
|
|
) -> AppRouter<
|
|
|
|
C,
|
2019-03-02 07:51:32 +01:00
|
|
|
P,
|
|
|
|
B1,
|
|
|
|
impl NewService<
|
2019-03-09 18:49:11 +01:00
|
|
|
Request = ServiceRequest<P>,
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse<B1>,
|
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,
|
2019-03-09 18:49:11 +01:00
|
|
|
Request = ServiceRequest<P>,
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse<B1>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 07:51:32 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
B1: MessageBody,
|
2019-03-09 18:49:11 +01:00
|
|
|
F: IntoTransform<M, T::Service>,
|
2019-03-02 07:51:32 +01:00
|
|
|
{
|
2019-03-30 15:56:09 +01:00
|
|
|
let endpoint = apply_transform(mw, self.endpoint);
|
2019-03-02 20:53:05 +01:00
|
|
|
AppRouter {
|
2019-03-02 07:51:32 +01:00
|
|
|
endpoint,
|
2019-03-02 20:53:05 +01:00
|
|
|
chain: self.chain,
|
2019-03-17 04:17:27 +01:00
|
|
|
data: self.data,
|
2019-03-02 07:51:32 +01:00
|
|
|
services: self.services,
|
|
|
|
default: self.default,
|
|
|
|
factory_ref: self.factory_ref,
|
2019-03-09 23:06:24 +01:00
|
|
|
config: self.config,
|
|
|
|
external: self.external,
|
2019-03-02 07:51:32 +01:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 20:52:05 +02:00
|
|
|
/// Registers middleware, in the form of a closure, that runs during inbound
|
|
|
|
/// and/or outbound processing in the request lifecycle (request -> response),
|
|
|
|
/// modifying request/response as necessary, across all requests managed by
|
|
|
|
/// the *Route*.
|
|
|
|
///
|
|
|
|
/// Use middleware when you need to read or modify *every* request or response in some way.
|
|
|
|
///
|
2019-03-25 21:43:02 +01:00
|
|
|
pub fn wrap_fn<B1, F, R>(
|
|
|
|
self,
|
|
|
|
mw: F,
|
|
|
|
) -> AppRouter<
|
|
|
|
C,
|
|
|
|
P,
|
|
|
|
B1,
|
|
|
|
impl NewService<
|
|
|
|
Request = ServiceRequest<P>,
|
|
|
|
Response = ServiceResponse<B1>,
|
|
|
|
Error = Error,
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
|
|
|
B1: MessageBody,
|
|
|
|
F: FnMut(ServiceRequest<P>, &mut T::Service) -> R + Clone,
|
|
|
|
R: IntoFuture<Item = ServiceResponse<B1>, Error = Error>,
|
|
|
|
{
|
|
|
|
self.wrap(mw)
|
|
|
|
}
|
|
|
|
|
2019-03-24 19:29:35 +01:00
|
|
|
/// Default resource to be used if no matching resource could be found.
|
2019-03-07 00:47:15 +01:00
|
|
|
pub fn default_resource<F, U>(mut self, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: FnOnce(Resource<P>) -> Resource<P, U>,
|
|
|
|
U: NewService<
|
2019-03-09 18:49:11 +01:00
|
|
|
Request = ServiceRequest<P>,
|
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,
|
|
|
|
{
|
|
|
|
// create and configure default resource
|
|
|
|
self.default = Some(Rc::new(boxed::new_service(
|
|
|
|
f(Resource::new("")).into_new_service().map_init_err(|_| ()),
|
|
|
|
)));
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
/// Register an external resource.
|
|
|
|
///
|
|
|
|
/// External resources are useful for URL generation purposes only
|
|
|
|
/// and are never considered for matching at request time. Calls to
|
|
|
|
/// `HttpRequest::url_for()` will work as expected.
|
|
|
|
///
|
2019-03-09 23:06:24 +01:00
|
|
|
/// ```rust
|
|
|
|
/// use actix_web::{web, App, HttpRequest, HttpResponse, Result};
|
2019-03-02 07:51:32 +01:00
|
|
|
///
|
2019-03-09 23:06:24 +01:00
|
|
|
/// fn index(req: HttpRequest) -> Result<HttpResponse> {
|
|
|
|
/// let url = req.url_for("youtube", &["asdlkjqme"])?;
|
|
|
|
/// assert_eq!(url.as_str(), "https://youtube.com/watch/asdlkjqme");
|
2019-03-02 07:51:32 +01:00
|
|
|
/// Ok(HttpResponse::Ok().into())
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new()
|
2019-03-09 23:06:24 +01:00
|
|
|
/// .service(web::resource("/index.html").route(
|
|
|
|
/// web::get().to(index)))
|
|
|
|
/// .external_resource("youtube", "https://youtube.com/watch/{video_id}");
|
2019-03-02 07:51:32 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-03-09 23:06:24 +01:00
|
|
|
pub fn external_resource<N, U>(mut self, name: N, url: U) -> Self
|
2019-03-02 07:51:32 +01:00
|
|
|
where
|
|
|
|
N: AsRef<str>,
|
|
|
|
U: AsRef<str>,
|
|
|
|
{
|
2019-03-09 23:06:24 +01:00
|
|
|
let mut rdef = ResourceDef::new(url.as_ref());
|
|
|
|
*rdef.name_mut() = name.as_ref().to_string();
|
|
|
|
self.external.push(rdef);
|
2019-03-02 07:51:32 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 18:49:11 +01:00
|
|
|
impl<C, T, P: 'static, B: MessageBody> IntoNewService<AppInit<C, T, P, B>, ServerConfig>
|
2019-03-05 19:08:08 +01:00
|
|
|
for AppRouter<C, P, B, T>
|
2019-03-02 07:51:32 +01:00
|
|
|
where
|
|
|
|
T: NewService<
|
2019-03-09 18:49:11 +01:00
|
|
|
Request = ServiceRequest<P>,
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse<B>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 07:51:32 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
2019-03-02 20:53:05 +01:00
|
|
|
C: NewService<
|
2019-03-09 18:49:11 +01:00
|
|
|
Request = ServiceRequest,
|
2019-03-02 20:53:05 +01:00
|
|
|
Response = ServiceRequest<P>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-02 20:53:05 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
2019-03-02 07:51:32 +01:00
|
|
|
{
|
2019-03-09 18:49:11 +01:00
|
|
|
fn into_new_service(self) -> AppInit<C, T, P, B> {
|
2019-03-02 20:53:05 +01:00
|
|
|
AppInit {
|
|
|
|
chain: self.chain,
|
2019-03-17 04:17:27 +01:00
|
|
|
data: self.data,
|
2019-03-09 18:49:11 +01:00
|
|
|
endpoint: self.endpoint,
|
|
|
|
services: RefCell::new(self.services),
|
2019-03-09 23:06:24 +01:00
|
|
|
external: RefCell::new(self.external),
|
2019-03-09 18:49:11 +01:00
|
|
|
default: self.default,
|
|
|
|
factory_ref: self.factory_ref,
|
2019-03-09 23:06:24 +01:00
|
|
|
config: RefCell::new(AppConfig(Rc::new(self.config))),
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
2019-03-02 20:53:05 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-04 06:40:03 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-03-09 18:49:11 +01:00
|
|
|
use actix_service::Service;
|
2019-03-25 21:43:02 +01:00
|
|
|
use futures::{Future, IntoFuture};
|
2019-03-09 18:49:11 +01:00
|
|
|
|
2019-03-04 06:40:03 +01:00
|
|
|
use super::*;
|
2019-03-25 21:43:02 +01:00
|
|
|
use crate::http::{header, HeaderValue, Method, StatusCode};
|
|
|
|
use crate::service::{ServiceRequest, ServiceResponse};
|
|
|
|
use crate::test::{block_on, call_success, init_service, TestRequest};
|
|
|
|
use crate::{web, Error, HttpResponse};
|
2019-03-04 06:40:03 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_default_resource() {
|
2019-03-07 00:47:15 +01:00
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(web::resource("/test").to(|| HttpResponse::Ok())),
|
2019-03-06 04:03:59 +01:00
|
|
|
);
|
2019-03-04 06:40:03 +01:00
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
2019-03-04 22:25:35 +01:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 06:40:03 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/blah").to_request();
|
2019-03-04 22:25:35 +01:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 06:40:03 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
2019-03-07 00:47:15 +01:00
|
|
|
let mut srv = init_service(
|
2019-03-06 04:03:59 +01:00
|
|
|
App::new()
|
2019-03-07 00:47:15 +01:00
|
|
|
.service(web::resource("/test").to(|| HttpResponse::Ok()))
|
|
|
|
.service(
|
|
|
|
web::resource("/test2")
|
|
|
|
.default_resource(|r| r.to(|| HttpResponse::Created()))
|
|
|
|
.route(web::get().to(|| HttpResponse::Ok())),
|
|
|
|
)
|
2019-03-06 04:03:59 +01:00
|
|
|
.default_resource(|r| r.to(|| HttpResponse::MethodNotAllowed())),
|
|
|
|
);
|
2019-03-04 06:40:03 +01:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/blah").to_request();
|
2019-03-04 22:25:35 +01:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 06:40:03 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
2019-03-04 22:25:35 +01:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test2").to_request();
|
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test2")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
2019-03-04 06:40:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-03-17 04:17:27 +01:00
|
|
|
fn test_data_factory() {
|
2019-03-07 20:43:46 +01:00
|
|
|
let mut srv =
|
2019-03-17 04:17:27 +01:00
|
|
|
init_service(App::new().data_factory(|| Ok::<_, ()>(10usize)).service(
|
|
|
|
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
2019-03-07 20:43:46 +01:00
|
|
|
));
|
2019-03-04 06:40:03 +01:00
|
|
|
let req = TestRequest::default().to_request();
|
2019-03-04 22:25:35 +01:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 06:40:03 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
2019-03-07 20:43:46 +01:00
|
|
|
let mut srv =
|
2019-03-17 04:17:27 +01:00
|
|
|
init_service(App::new().data_factory(|| Ok::<_, ()>(10u32)).service(
|
|
|
|
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
2019-03-07 20:43:46 +01:00
|
|
|
));
|
2019-03-04 06:40:03 +01:00
|
|
|
let req = TestRequest::default().to_request();
|
2019-03-04 22:25:35 +01:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 06:40:03 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
2019-03-25 21:43:02 +01:00
|
|
|
|
|
|
|
fn md<S, P, B>(
|
|
|
|
req: ServiceRequest<P>,
|
|
|
|
srv: &mut S,
|
|
|
|
) -> impl IntoFuture<Item = ServiceResponse<B>, Error = Error>
|
|
|
|
where
|
|
|
|
S: Service<
|
|
|
|
Request = ServiceRequest<P>,
|
|
|
|
Response = ServiceResponse<B>,
|
|
|
|
Error = Error,
|
|
|
|
>,
|
|
|
|
{
|
|
|
|
srv.call(req).map(|mut res| {
|
|
|
|
res.headers_mut()
|
|
|
|
.insert(header::CONTENT_TYPE, HeaderValue::from_static("0001"));
|
|
|
|
res
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_wrap() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new()
|
|
|
|
.wrap(md)
|
|
|
|
.route("/test", web::get().to(|| HttpResponse::Ok())),
|
|
|
|
);
|
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
|
|
|
let resp = call_success(&mut srv, req);
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("0001")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_router_wrap() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new()
|
|
|
|
.route("/test", web::get().to(|| HttpResponse::Ok()))
|
|
|
|
.wrap(md),
|
|
|
|
);
|
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
|
|
|
let resp = call_success(&mut srv, req);
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("0001")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_wrap_fn() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new()
|
|
|
|
.wrap_fn(|req, srv| {
|
|
|
|
srv.call(req).map(|mut res| {
|
|
|
|
res.headers_mut().insert(
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
HeaderValue::from_static("0001"),
|
|
|
|
);
|
|
|
|
res
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.service(web::resource("/test").to(|| HttpResponse::Ok())),
|
|
|
|
);
|
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
|
|
|
let resp = call_success(&mut srv, req);
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("0001")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_router_wrap_fn() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new()
|
|
|
|
.route("/test", web::get().to(|| HttpResponse::Ok()))
|
|
|
|
.wrap_fn(|req, srv| {
|
|
|
|
srv.call(req).map(|mut res| {
|
|
|
|
res.headers_mut().insert(
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
HeaderValue::from_static("0001"),
|
|
|
|
);
|
|
|
|
res
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
|
|
|
let resp = call_success(&mut srv, req);
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("0001")
|
|
|
|
);
|
|
|
|
}
|
2019-03-04 06:40:03 +01:00
|
|
|
}
|