2019-03-04 06:02:01 +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::pin::Pin;
|
2019-03-04 06:02:01 +01:00
|
|
|
use std::rc::Rc;
|
2019-11-20 18:33:22 +01:00
|
|
|
use std::task::{Context, Poll};
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-05-12 18:42:05 +02:00
|
|
|
use actix_http::{Extensions, Response};
|
2019-03-04 06:02:01 +01:00
|
|
|
use actix_router::{ResourceDef, ResourceInfo, Router};
|
2019-11-26 06:25:50 +01:00
|
|
|
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
2019-03-04 06:02:01 +01:00
|
|
|
use actix_service::{
|
2019-11-20 18:33:22 +01:00
|
|
|
apply, apply_fn_factory, IntoServiceFactory, Service, ServiceFactory, Transform,
|
2019-03-04 06:02:01 +01:00
|
|
|
};
|
2019-11-20 18:33:22 +01:00
|
|
|
use futures::future::{ok, Either, Future, LocalBoxFuture, Ready};
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-06-03 19:41:32 +02:00
|
|
|
use crate::config::ServiceConfig;
|
2019-05-12 18:42:05 +02:00
|
|
|
use crate::data::Data;
|
2019-04-15 16:32:49 +02:00
|
|
|
use crate::dev::{AppService, HttpServiceFactory};
|
2019-03-11 00:35:38 +01:00
|
|
|
use crate::error::Error;
|
2019-03-04 06:02:01 +01:00
|
|
|
use crate::guard::Guard;
|
|
|
|
use crate::resource::Resource;
|
2019-03-09 16:39:34 +01:00
|
|
|
use crate::rmap::ResourceMap;
|
2019-03-04 06:02:01 +01:00
|
|
|
use crate::route::Route;
|
2019-03-07 00:47:15 +01:00
|
|
|
use crate::service::{
|
2019-11-20 18:33:22 +01:00
|
|
|
AppServiceFactory, ServiceFactoryWrapper, ServiceRequest, ServiceResponse,
|
2019-03-07 00:47:15 +01:00
|
|
|
};
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-07-17 11:48:37 +02:00
|
|
|
type Guards = Vec<Box<dyn Guard>>;
|
2019-11-26 06:25:50 +01:00
|
|
|
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
|
|
|
type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
2019-11-20 18:33:22 +01:00
|
|
|
type BoxedResponse = LocalBoxFuture<'static, Result<ServiceResponse, Error>>;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-03-17 05:35:02 +01:00
|
|
|
/// Resources scope.
|
2019-03-04 06:02:01 +01:00
|
|
|
///
|
|
|
|
/// Scope is a set of resources with common root path.
|
|
|
|
/// Scopes collect multiple paths under a common path prefix.
|
|
|
|
/// Scope path can contain variable path segments as resources.
|
|
|
|
/// Scope prefix is always complete path segment, i.e `/app` would
|
|
|
|
/// be converted to a `/app/` and it would not match `/app` path.
|
|
|
|
///
|
|
|
|
/// You can get variable path segments from `HttpRequest::match_info()`.
|
|
|
|
/// `Path` extractor also is able to extract scope level variable segments.
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-04 22:25:35 +01:00
|
|
|
/// use actix_web::{web, App, HttpResponse};
|
2019-03-04 06:02:01 +01:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::scope("/{project_id}/")
|
2019-11-21 16:34:04 +01:00
|
|
|
/// .service(web::resource("/path1").to(|| async { HttpResponse::Ok() }))
|
2019-03-07 00:47:15 +01:00
|
|
|
/// .service(web::resource("/path2").route(web::get().to(|| HttpResponse::Ok())))
|
|
|
|
/// .service(web::resource("/path3").route(web::head().to(|| HttpResponse::MethodNotAllowed())))
|
|
|
|
/// );
|
2019-03-04 06:02:01 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// In the above example three routes get registered:
|
|
|
|
/// * /{project_id}/path1 - reponds to all http method
|
|
|
|
/// * /{project_id}/path2 - `GET` requests
|
|
|
|
/// * /{project_id}/path3 - `HEAD` requests
|
|
|
|
///
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct Scope<T = ScopeEndpoint> {
|
2019-03-04 06:02:01 +01:00
|
|
|
endpoint: T,
|
2019-03-07 00:47:15 +01:00
|
|
|
rdef: String,
|
2019-05-12 18:42:05 +02:00
|
|
|
data: Option<Extensions>,
|
2019-11-20 18:33:22 +01:00
|
|
|
services: Vec<Box<dyn AppServiceFactory>>,
|
2019-07-17 11:48:37 +02:00
|
|
|
guards: Vec<Box<dyn Guard>>,
|
2019-04-13 23:50:54 +02:00
|
|
|
default: Rc<RefCell<Option<Rc<HttpNewService>>>>,
|
2019-06-03 19:41:32 +02:00
|
|
|
external: Vec<ResourceDef>,
|
2019-04-13 23:50:54 +02:00
|
|
|
factory_ref: Rc<RefCell<Option<ScopeFactory>>>,
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl Scope {
|
2019-03-04 06:02:01 +01:00
|
|
|
/// Create a new scope
|
2019-04-13 23:50:54 +02:00
|
|
|
pub fn new(path: &str) -> Scope {
|
2019-03-04 06:02:01 +01:00
|
|
|
let fref = Rc::new(RefCell::new(None));
|
|
|
|
Scope {
|
|
|
|
endpoint: ScopeEndpoint::new(fref.clone()),
|
2019-03-07 00:47:15 +01:00
|
|
|
rdef: path.to_string(),
|
2019-05-12 18:42:05 +02:00
|
|
|
data: None,
|
2019-03-04 20:47:53 +01:00
|
|
|
guards: Vec::new(),
|
2019-03-04 06:02:01 +01:00
|
|
|
services: Vec::new(),
|
|
|
|
default: Rc::new(RefCell::new(None)),
|
2019-06-03 19:41:32 +02:00
|
|
|
external: Vec::new(),
|
2019-03-04 06:02:01 +01:00
|
|
|
factory_ref: fref,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl<T> Scope<T>
|
2019-03-04 06:02:01 +01:00
|
|
|
where
|
2019-11-20 18:33:22 +01:00
|
|
|
T: ServiceFactory<
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-04 06:02:01 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-04 06:02:01 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
2019-03-07 00:47:15 +01:00
|
|
|
/// Add match guard to a scope.
|
2019-03-04 06:02:01 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-07 23:01:52 +01:00
|
|
|
/// use actix_web::{web, guard, App, HttpRequest, HttpResponse};
|
2019-03-04 06:02:01 +01:00
|
|
|
///
|
2019-11-21 16:34:04 +01:00
|
|
|
/// async fn index(data: web::Path<(String, String)>) -> &'static str {
|
2019-03-04 06:02:01 +01:00
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::scope("/app")
|
2019-03-04 06:02:01 +01:00
|
|
|
/// .guard(guard::Header("content-type", "text/plain"))
|
2019-03-07 00:47:15 +01:00
|
|
|
/// .route("/test1", web::get().to(index))
|
2019-03-04 06:02:01 +01:00
|
|
|
/// .route("/test2", web::post().to(|r: HttpRequest| {
|
|
|
|
/// HttpResponse::MethodNotAllowed()
|
|
|
|
/// }))
|
2019-03-07 00:47:15 +01:00
|
|
|
/// );
|
2019-03-04 06:02:01 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn guard<G: Guard + 'static>(mut self, guard: G) -> Self {
|
2019-03-04 20:47:53 +01:00
|
|
|
self.guards.push(Box::new(guard));
|
2019-03-04 06:02:01 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-05-12 18:42:05 +02:00
|
|
|
/// Set or override application data. Application data could be accessed
|
|
|
|
/// by using `Data<T>` extractor where `T` is data type.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::cell::Cell;
|
2019-11-20 05:05:16 +01:00
|
|
|
/// use actix_web::{web, App, HttpResponse, Responder};
|
2019-05-12 18:42:05 +02:00
|
|
|
///
|
|
|
|
/// struct MyData {
|
|
|
|
/// counter: Cell<usize>,
|
|
|
|
/// }
|
|
|
|
///
|
2019-11-20 05:05:16 +01:00
|
|
|
/// async fn index(data: web::Data<MyData>) -> impl Responder {
|
2019-05-12 18:42:05 +02:00
|
|
|
/// data.counter.set(data.counter.get() + 1);
|
2019-11-20 05:05:16 +01:00
|
|
|
/// HttpResponse::Ok()
|
2019-05-12 18:42:05 +02:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::scope("/app")
|
|
|
|
/// .data(MyData{ counter: Cell::new(0) })
|
|
|
|
/// .service(
|
|
|
|
/// web::resource("/index.html").route(
|
|
|
|
/// web::get().to(index)))
|
|
|
|
/// );
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-09-18 02:36:39 +02:00
|
|
|
pub fn data<U: 'static>(self, data: U) -> Self {
|
|
|
|
self.register_data(Data::new(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set or override application data.
|
|
|
|
///
|
|
|
|
/// This method has the same effect as [`Scope::data`](#method.data), except
|
|
|
|
/// that instead of taking a value of some type `T`, it expects a value of
|
|
|
|
/// type `Data<T>`. Use a `Data<T>` extractor to retrieve its value.
|
|
|
|
pub fn register_data<U: 'static>(mut self, data: Data<U>) -> Self {
|
2019-05-12 18:42:05 +02:00
|
|
|
if self.data.is_none() {
|
|
|
|
self.data = Some(Extensions::new());
|
|
|
|
}
|
2019-09-18 02:36:39 +02:00
|
|
|
self.data.as_mut().unwrap().insert(data);
|
2019-05-12 18:42:05 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-06-03 19:41:32 +02:00
|
|
|
/// Run external configuration as part of the scope 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(cfg: &mut web::ServiceConfig) {
|
|
|
|
/// 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())
|
|
|
|
/// .service(
|
|
|
|
/// web::scope("/api")
|
|
|
|
/// .configure(config)
|
|
|
|
/// )
|
|
|
|
/// .route("/index.html", web::get().to(|| HttpResponse::Ok()));
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn configure<F>(mut self, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut ServiceConfig),
|
|
|
|
{
|
|
|
|
let mut cfg = ServiceConfig::new();
|
|
|
|
f(&mut cfg);
|
|
|
|
self.services.extend(cfg.services);
|
|
|
|
self.external.extend(cfg.external);
|
|
|
|
|
|
|
|
if !cfg.data.is_empty() {
|
2019-07-17 08:58:42 +02:00
|
|
|
let mut data = self.data.unwrap_or_else(Extensions::new);
|
2019-06-03 19:41:32 +02:00
|
|
|
|
|
|
|
for value in cfg.data.iter() {
|
|
|
|
value.create(&mut data);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.data = Some(data);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-17 05:35:02 +01:00
|
|
|
/// Register http service.
|
|
|
|
///
|
|
|
|
/// This is similar to `App's` service registration.
|
|
|
|
///
|
|
|
|
/// 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-04 06:02:01 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-07 00:47:15 +01:00
|
|
|
/// use actix_web::{web, App, HttpRequest};
|
2019-03-04 06:02:01 +01:00
|
|
|
///
|
|
|
|
/// struct AppState;
|
|
|
|
///
|
2019-11-21 16:34:04 +01:00
|
|
|
/// async fn index(req: HttpRequest) -> &'static str {
|
2019-03-04 06:02:01 +01:00
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::scope("/app").service(
|
|
|
|
/// web::scope("/v1")
|
|
|
|
/// .service(web::resource("/test1").to(index)))
|
|
|
|
/// );
|
2019-03-04 06:02:01 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-03-07 00:47:15 +01:00
|
|
|
pub fn service<F>(mut self, factory: F) -> Self
|
2019-03-04 06:02:01 +01:00
|
|
|
where
|
2019-04-13 23:50:54 +02:00
|
|
|
F: HttpServiceFactory + 'static,
|
2019-03-04 06:02:01 +01:00
|
|
|
{
|
|
|
|
self.services
|
2019-03-07 00:47:15 +01:00
|
|
|
.push(Box::new(ServiceFactoryWrapper::new(factory)));
|
2019-03-04 06:02:01 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configure route for a specific path.
|
|
|
|
///
|
2019-03-07 00:47:15 +01:00
|
|
|
/// This is a simplified version of the `Scope::service()` method.
|
2019-03-17 05:35:02 +01:00
|
|
|
/// This method can be called multiple times, in that case
|
2019-03-04 06:02:01 +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-04 06:02:01 +01:00
|
|
|
///
|
2019-11-21 16:34:04 +01:00
|
|
|
/// async fn index(data: web::Path<(String, String)>) -> &'static str {
|
2019-03-04 06:02:01 +01:00
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::scope("/app")
|
|
|
|
/// .route("/test1", web::get().to(index))
|
2019-03-04 06:02:01 +01:00
|
|
|
/// .route("/test2", web::post().to(|| HttpResponse::MethodNotAllowed()))
|
2019-03-07 00:47:15 +01:00
|
|
|
/// );
|
2019-03-04 06:02:01 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-04-13 23:50:54 +02:00
|
|
|
pub fn route(self, path: &str, mut route: Route) -> Self {
|
2019-03-07 00:47:15 +01:00
|
|
|
self.service(
|
|
|
|
Resource::new(path)
|
|
|
|
.add_guards(route.take_guards())
|
|
|
|
.route(route),
|
|
|
|
)
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-04-14 07:25:00 +02:00
|
|
|
/// Default service to be used if no matching route could be found.
|
2019-03-17 05:35:02 +01:00
|
|
|
///
|
|
|
|
/// If default resource is not registered, app's default resource is being used.
|
2019-04-14 07:25:00 +02:00
|
|
|
pub fn default_service<F, U>(mut self, f: F) -> Self
|
2019-03-04 06:02:01 +01:00
|
|
|
where
|
2019-11-20 18:33:22 +01:00
|
|
|
F: IntoServiceFactory<U>,
|
|
|
|
U: ServiceFactory<
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-04 06:02:01 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-04 06:02:01 +01:00
|
|
|
> + 'static,
|
2019-04-14 07:25:00 +02:00
|
|
|
U::InitError: fmt::Debug,
|
2019-03-04 06:02:01 +01:00
|
|
|
{
|
|
|
|
// create and configure default resource
|
2019-11-20 18:33:22 +01:00
|
|
|
self.default = Rc::new(RefCell::new(Some(Rc::new(boxed::factory(
|
|
|
|
f.into_factory().map_init_err(|e| {
|
2019-04-14 07:25:00 +02:00
|
|
|
log::error!("Can not construct default service: {:?}", e)
|
|
|
|
}),
|
2019-03-04 06:02:01 +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 processing in the request
|
|
|
|
/// lifecycle (request -> response), modifying request as
|
2019-04-01 21:10:28 +02:00
|
|
|
/// necessary, across all requests managed by the *Scope*. Scope-level
|
|
|
|
/// middleware is more limited in what it can modify, relative to Route or
|
2019-04-02 21:51:16 +02:00
|
|
|
/// Application level middleware, in that Scope-level middleware can not modify
|
2019-04-01 21:10:28 +02:00
|
|
|
/// ServiceResponse.
|
2019-03-04 06:02:01 +01:00
|
|
|
///
|
2019-04-01 20:52:05 +02:00
|
|
|
/// Use middleware when you need to read or modify *every* request in some way.
|
2019-11-20 18:33:22 +01:00
|
|
|
pub fn wrap<M>(
|
2019-03-04 06:02:01 +01:00
|
|
|
self,
|
2019-11-20 18:33:22 +01:00
|
|
|
mw: M,
|
2019-03-04 06:02:01 +01:00
|
|
|
) -> Scope<
|
2019-11-20 18:33:22 +01:00
|
|
|
impl ServiceFactory<
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-04 06:02:01 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-04 06:02:01 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
2019-03-05 06:37:57 +01:00
|
|
|
M: Transform<
|
2019-03-04 06:02:01 +01:00
|
|
|
T::Service,
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-04 06:02:01 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-04 06:02:01 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
|
|
|
Scope {
|
2019-11-20 18:33:22 +01:00
|
|
|
endpoint: apply(mw, self.endpoint),
|
2019-03-04 06:02:01 +01:00
|
|
|
rdef: self.rdef,
|
2019-05-12 18:42:05 +02:00
|
|
|
data: self.data,
|
2019-03-04 06:02:01 +01:00
|
|
|
guards: self.guards,
|
|
|
|
services: self.services,
|
|
|
|
default: self.default,
|
2019-06-03 19:41:32 +02:00
|
|
|
external: self.external,
|
2019-03-04 06:02:01 +01:00
|
|
|
factory_ref: self.factory_ref,
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 21:43:02 +01:00
|
|
|
|
2019-04-01 20:52:05 +02:00
|
|
|
/// Registers middleware, in the form of a closure, that runs during inbound
|
2019-04-02 21:51:16 +02:00
|
|
|
/// processing in the request lifecycle (request -> response), modifying
|
2019-04-25 20:14:32 +02:00
|
|
|
/// request as necessary, across all requests managed by the *Scope*.
|
2019-04-01 21:10:28 +02:00
|
|
|
/// Scope-level middleware is more limited in what it can modify, relative
|
|
|
|
/// to Route or Application level middleware, in that Scope-level middleware
|
|
|
|
/// can not modify ServiceResponse.
|
2019-03-25 21:43:02 +01:00
|
|
|
///
|
|
|
|
/// ```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 21:43:02 +01:00
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::scope("/app")
|
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 21:43:02 +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 21:43:02 +01:00
|
|
|
/// .route("/index.html", web::get().to(index)));
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn wrap_fn<F, R>(
|
|
|
|
self,
|
|
|
|
mw: F,
|
|
|
|
) -> Scope<
|
2019-11-20 18:33:22 +01:00
|
|
|
impl ServiceFactory<
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-25 21:43:02 +01:00
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = Error,
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
2019-04-13 23:50:54 +02:00
|
|
|
F: FnMut(ServiceRequest, &mut T::Service) -> R + Clone,
|
2019-11-20 18:33:22 +01:00
|
|
|
R: Future<Output = Result<ServiceResponse, Error>>,
|
2019-03-25 21:43:02 +01:00
|
|
|
{
|
2019-11-20 18:33:22 +01:00
|
|
|
Scope {
|
|
|
|
endpoint: apply_fn_factory(self.endpoint, mw),
|
|
|
|
rdef: self.rdef,
|
|
|
|
data: self.data,
|
|
|
|
guards: self.guards,
|
|
|
|
services: self.services,
|
|
|
|
default: self.default,
|
|
|
|
external: self.external,
|
|
|
|
factory_ref: self.factory_ref,
|
|
|
|
}
|
2019-03-25 21:43:02 +01:00
|
|
|
}
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl<T> HttpServiceFactory for Scope<T>
|
2019-03-04 06:02:01 +01:00
|
|
|
where
|
2019-11-20 18:33:22 +01:00
|
|
|
T: ServiceFactory<
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
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-03-04 06:02:01 +01:00
|
|
|
{
|
2019-05-12 18:42:05 +02:00
|
|
|
fn register(mut self, config: &mut AppService) {
|
2019-03-09 16:39:34 +01:00
|
|
|
// update default resource if needed
|
2019-03-07 00:47:15 +01:00
|
|
|
if self.default.borrow().is_none() {
|
|
|
|
*self.default.borrow_mut() = Some(config.default_service());
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 16:39:34 +01:00
|
|
|
// register nested services
|
2019-03-07 00:47:15 +01:00
|
|
|
let mut cfg = config.clone_config();
|
|
|
|
self.services
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|mut srv| srv.register(&mut cfg));
|
|
|
|
|
2019-03-09 16:39:34 +01:00
|
|
|
let mut rmap = ResourceMap::new(ResourceDef::root_prefix(&self.rdef));
|
|
|
|
|
2019-06-03 19:41:32 +02:00
|
|
|
// external resources
|
|
|
|
for mut rdef in std::mem::replace(&mut self.external, Vec::new()) {
|
|
|
|
rmap.add(&mut rdef, None);
|
|
|
|
}
|
|
|
|
|
2019-05-12 18:42:05 +02:00
|
|
|
// custom app data storage
|
|
|
|
if let Some(ref mut ext) = self.data {
|
|
|
|
config.set_service_data(ext);
|
|
|
|
}
|
|
|
|
|
2019-03-09 16:39:34 +01:00
|
|
|
// complete scope pipeline creation
|
2019-03-04 06:02:01 +01:00
|
|
|
*self.factory_ref.borrow_mut() = Some(ScopeFactory {
|
2019-07-17 08:58:42 +02:00
|
|
|
data: self.data.take().map(Rc::new),
|
2019-03-04 06:02:01 +01:00
|
|
|
default: self.default.clone(),
|
2019-03-04 20:47:53 +01:00
|
|
|
services: Rc::new(
|
2019-03-07 00:47:15 +01:00
|
|
|
cfg.into_services()
|
2019-05-05 04:43:49 +02:00
|
|
|
.1
|
2019-03-04 20:47:53 +01:00
|
|
|
.into_iter()
|
2019-03-09 16:39:34 +01:00
|
|
|
.map(|(mut rdef, srv, guards, nested)| {
|
|
|
|
rmap.add(&mut rdef, nested);
|
|
|
|
(rdef, srv, RefCell::new(guards))
|
|
|
|
})
|
2019-03-04 20:47:53 +01:00
|
|
|
.collect(),
|
|
|
|
),
|
2019-03-04 06:02:01 +01:00
|
|
|
});
|
|
|
|
|
2019-03-09 16:39:34 +01:00
|
|
|
// get guards
|
2019-03-07 00:47:15 +01:00
|
|
|
let guards = if self.guards.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(self.guards)
|
|
|
|
};
|
2019-03-09 16:39:34 +01:00
|
|
|
|
|
|
|
// register final service
|
2019-03-07 04:27:18 +01:00
|
|
|
config.register_service(
|
|
|
|
ResourceDef::root_prefix(&self.rdef),
|
|
|
|
guards,
|
|
|
|
self.endpoint,
|
2019-03-09 16:39:34 +01:00
|
|
|
Some(Rc::new(rmap)),
|
2019-03-07 04:27:18 +01:00
|
|
|
)
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct ScopeFactory {
|
2019-05-12 18:42:05 +02:00
|
|
|
data: Option<Rc<Extensions>>,
|
2019-04-13 23:50:54 +02:00
|
|
|
services: Rc<Vec<(ResourceDef, HttpNewService, RefCell<Option<Guards>>)>>,
|
|
|
|
default: Rc<RefCell<Option<Rc<HttpNewService>>>>,
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 18:33:22 +01:00
|
|
|
impl ServiceFactory for ScopeFactory {
|
2019-05-12 17:34:51 +02:00
|
|
|
type Config = ();
|
2019-04-13 23:50:54 +02:00
|
|
|
type Request = ServiceRequest;
|
2019-03-04 06:02:01 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2019-03-04 06:02:01 +01:00
|
|
|
type InitError = ();
|
2019-04-13 23:50:54 +02:00
|
|
|
type Service = ScopeService;
|
|
|
|
type Future = ScopeFactoryResponse;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-12-02 16:37:13 +01:00
|
|
|
fn new_service(&self, _: ()) -> Self::Future {
|
2019-03-04 06:02:01 +01:00
|
|
|
let default_fut = if let Some(ref default) = *self.default.borrow() {
|
2019-12-02 16:37:13 +01:00
|
|
|
Some(default.new_service(()))
|
2019-03-04 06:02:01 +01:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
ScopeFactoryResponse {
|
|
|
|
fut: self
|
|
|
|
.services
|
|
|
|
.iter()
|
2019-03-04 20:47:53 +01:00
|
|
|
.map(|(path, service, guards)| {
|
2019-03-04 06:02:01 +01:00
|
|
|
CreateScopeServiceItem::Future(
|
|
|
|
Some(path.clone()),
|
2019-03-04 20:47:53 +01:00
|
|
|
guards.borrow_mut().take(),
|
2019-12-02 16:37:13 +01:00
|
|
|
service.new_service(()),
|
2019-03-04 06:02:01 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
default: None,
|
2019-05-12 18:42:05 +02:00
|
|
|
data: self.data.clone(),
|
2019-03-04 06:02:01 +01:00
|
|
|
default_fut,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 05:35:02 +01:00
|
|
|
/// Create scope service
|
2019-03-04 06:02:01 +01:00
|
|
|
#[doc(hidden)]
|
2019-11-20 18:33:22 +01:00
|
|
|
#[pin_project::pin_project]
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct ScopeFactoryResponse {
|
|
|
|
fut: Vec<CreateScopeServiceItem>,
|
2019-05-12 18:42:05 +02:00
|
|
|
data: Option<Rc<Extensions>>,
|
2019-04-13 23:50:54 +02:00
|
|
|
default: Option<HttpService>,
|
2019-11-20 18:33:22 +01:00
|
|
|
default_fut: Option<LocalBoxFuture<'static, Result<HttpService, ()>>>,
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 18:33:22 +01:00
|
|
|
type HttpServiceFut = LocalBoxFuture<'static, Result<HttpService, ()>>;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
enum CreateScopeServiceItem {
|
|
|
|
Future(Option<ResourceDef>, Option<Guards>, HttpServiceFut),
|
|
|
|
Service(ResourceDef, Option<Guards>, HttpService),
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl Future for ScopeFactoryResponse {
|
2019-11-20 18:33:22 +01:00
|
|
|
type Output = Result<ScopeService, ()>;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-20 18:33:22 +01:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
2019-03-04 06:02:01 +01:00
|
|
|
let mut done = true;
|
|
|
|
|
|
|
|
if let Some(ref mut fut) = self.default_fut {
|
2019-11-20 18:33:22 +01:00
|
|
|
match Pin::new(fut).poll(cx)? {
|
|
|
|
Poll::Ready(default) => self.default = Some(default),
|
|
|
|
Poll::Pending => done = false,
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// poll http services
|
|
|
|
for item in &mut self.fut {
|
|
|
|
let res = match item {
|
2019-03-04 20:47:53 +01:00
|
|
|
CreateScopeServiceItem::Future(
|
|
|
|
ref mut path,
|
|
|
|
ref mut guards,
|
|
|
|
ref mut fut,
|
2019-11-20 18:33:22 +01:00
|
|
|
) => match Pin::new(fut).poll(cx)? {
|
|
|
|
Poll::Ready(service) => {
|
2019-03-04 20:47:53 +01:00
|
|
|
Some((path.take().unwrap(), guards.take(), service))
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
2019-11-20 18:33:22 +01:00
|
|
|
Poll::Pending => {
|
2019-03-04 20:47:53 +01:00
|
|
|
done = false;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CreateScopeServiceItem::Service(_, _, _) => continue,
|
2019-03-04 06:02:01 +01:00
|
|
|
};
|
|
|
|
|
2019-03-04 20:47:53 +01:00
|
|
|
if let Some((path, guards, service)) = res {
|
|
|
|
*item = CreateScopeServiceItem::Service(path, guards, service);
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if done {
|
|
|
|
let router = self
|
|
|
|
.fut
|
|
|
|
.drain(..)
|
|
|
|
.fold(Router::build(), |mut router, item| {
|
|
|
|
match item {
|
2019-03-04 20:47:53 +01:00
|
|
|
CreateScopeServiceItem::Service(path, guards, service) => {
|
2019-03-09 16:39:34 +01:00
|
|
|
router.rdef(path, service).2 = guards;
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
2019-03-04 20:47:53 +01:00
|
|
|
CreateScopeServiceItem::Future(_, _, _) => unreachable!(),
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
router
|
|
|
|
});
|
2019-11-20 18:33:22 +01:00
|
|
|
Poll::Ready(Ok(ScopeService {
|
2019-05-12 18:42:05 +02:00
|
|
|
data: self.data.clone(),
|
2019-03-04 06:02:01 +01:00
|
|
|
router: router.finish(),
|
|
|
|
default: self.default.take(),
|
|
|
|
_ready: None,
|
|
|
|
}))
|
|
|
|
} else {
|
2019-11-20 18:33:22 +01:00
|
|
|
Poll::Pending
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct ScopeService {
|
2019-05-12 18:42:05 +02:00
|
|
|
data: Option<Rc<Extensions>>,
|
2019-07-17 11:48:37 +02:00
|
|
|
router: Router<HttpService, Vec<Box<dyn Guard>>>,
|
2019-04-13 23:50:54 +02:00
|
|
|
default: Option<HttpService>,
|
|
|
|
_ready: Option<(ServiceRequest, ResourceInfo)>,
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl Service for ScopeService {
|
|
|
|
type Request = ServiceRequest;
|
2019-03-04 06:02:01 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2019-11-20 18:33:22 +01:00
|
|
|
type Future = Either<BoxedResponse, Ready<Result<Self::Response, Self::Error>>>;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-20 18:33:22 +01:00
|
|
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Ok(()))
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
fn call(&mut self, mut req: ServiceRequest) -> Self::Future {
|
2019-03-04 20:47:53 +01:00
|
|
|
let res = self.router.recognize_mut_checked(&mut req, |req, guards| {
|
|
|
|
if let Some(ref guards) = guards {
|
|
|
|
for f in guards {
|
|
|
|
if !f.check(req.head()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some((srv, _info)) = res {
|
2019-05-12 18:42:05 +02:00
|
|
|
if let Some(ref data) = self.data {
|
|
|
|
req.set_data_container(data.clone());
|
|
|
|
}
|
2019-11-20 18:33:22 +01:00
|
|
|
Either::Left(srv.call(req))
|
2019-03-04 06:02:01 +01:00
|
|
|
} else if let Some(ref mut default) = self.default {
|
2019-11-20 18:33:22 +01:00
|
|
|
Either::Left(default.call(req))
|
2019-03-04 06:02:01 +01:00
|
|
|
} else {
|
2019-03-26 23:14:32 +01:00
|
|
|
let req = req.into_parts().0;
|
2019-11-20 18:33:22 +01:00
|
|
|
Either::Right(ok(ServiceResponse::new(req, Response::NotFound().finish())))
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct ScopeEndpoint {
|
|
|
|
factory: Rc<RefCell<Option<ScopeFactory>>>,
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl ScopeEndpoint {
|
|
|
|
fn new(factory: Rc<RefCell<Option<ScopeFactory>>>) -> Self {
|
2019-03-04 06:02:01 +01:00
|
|
|
ScopeEndpoint { factory }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 18:33:22 +01:00
|
|
|
impl ServiceFactory for ScopeEndpoint {
|
2019-05-12 17:34:51 +02:00
|
|
|
type Config = ();
|
2019-04-13 23:50:54 +02:00
|
|
|
type Request = ServiceRequest;
|
2019-03-04 06:02:01 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2019-03-04 06:02:01 +01:00
|
|
|
type InitError = ();
|
2019-04-13 23:50:54 +02:00
|
|
|
type Service = ScopeService;
|
|
|
|
type Future = ScopeFactoryResponse;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-12-02 16:37:13 +01:00
|
|
|
fn new_service(&self, _: ()) -> Self::Future {
|
|
|
|
self.factory.borrow_mut().as_mut().unwrap().new_service(())
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-03-07 00:47:15 +01:00
|
|
|
use actix_service::Service;
|
2019-03-04 06:02:01 +01:00
|
|
|
use bytes::Bytes;
|
2019-11-20 18:33:22 +01:00
|
|
|
use futures::future::ok;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-03-08 00:51:24 +01:00
|
|
|
use crate::dev::{Body, ResponseBody};
|
2019-03-25 21:02:10 +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;
|
|
|
|
use crate::test::{call_service, init_service, read_body, TestRequest};
|
|
|
|
use crate::{guard, web, App, HttpRequest, HttpResponse};
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_scope() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
|
|
|
.service(web::resource("/path1").to(|| HttpResponse::Ok())),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/path1").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_scope_root() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
|
|
|
.service(web::resource("").to(|| HttpResponse::Ok()))
|
|
|
|
.service(web::resource("/").to(|| HttpResponse::Created())),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_scope_root2() {
|
|
|
|
let mut srv = init_service(App::new().service(
|
|
|
|
web::scope("/app/").service(web::resource("").to(|| HttpResponse::Ok())),
|
|
|
|
))
|
|
|
|
.await;
|
2019-11-20 18:33:22 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
2019-11-20 18:33:22 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_scope_root3() {
|
|
|
|
let mut srv = init_service(App::new().service(
|
|
|
|
web::scope("/app/").service(web::resource("/").to(|| HttpResponse::Ok())),
|
|
|
|
))
|
|
|
|
.await;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_scope_route() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("app")
|
|
|
|
.route("/path1", web::get().to(|| HttpResponse::Ok()))
|
|
|
|
.route("/path1", web::delete().to(|| HttpResponse::Ok())),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1")
|
|
|
|
.method(Method::DELETE)
|
|
|
|
.to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_scope_route_without_leading_slash() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("app").service(
|
|
|
|
web::resource("path1")
|
|
|
|
.route(web::get().to(|| HttpResponse::Ok()))
|
|
|
|
.route(web::delete().to(|| HttpResponse::Ok())),
|
2019-03-07 00:47:15 +01:00
|
|
|
),
|
2019-11-26 06:25:50 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1")
|
|
|
|
.method(Method::DELETE)
|
|
|
|
.to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_scope_guard() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
|
|
|
.guard(guard::Get())
|
|
|
|
.service(web::resource("/path1").to(|| HttpResponse::Ok())),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1")
|
|
|
|
.method(Method::GET)
|
|
|
|
.to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-03-04 20:47:53 +01:00
|
|
|
}
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_scope_variable_segment() {
|
|
|
|
let mut srv =
|
|
|
|
init_service(App::new().service(web::scope("/ab-{project}").service(
|
|
|
|
web::resource("/path1").to(|r: HttpRequest| {
|
|
|
|
async move {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.body(format!("project: {}", &r.match_info()["project"]))
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)))
|
|
|
|
.await;
|
2019-11-20 18:33:22 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/ab-project1/path1").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-11-20 18:33:22 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
match resp.response().body() {
|
|
|
|
ResponseBody::Body(Body::Bytes(ref b)) => {
|
|
|
|
let bytes: Bytes = b.clone().into();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"project: project1"));
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
2019-11-26 06:25:50 +01:00
|
|
|
_ => panic!(),
|
|
|
|
}
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/aa-project1/path1").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_nested_scope() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
|
|
|
.service(web::scope("/t1").service(
|
2019-03-07 00:47:15 +01:00
|
|
|
web::resource("/path1").to(|| HttpResponse::Created()),
|
|
|
|
)),
|
2019-11-26 06:25:50 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/t1/path1").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_nested_scope_no_slash() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
|
|
|
.service(web::scope("t1").service(
|
2019-03-07 00:47:15 +01:00
|
|
|
web::resource("/path1").to(|| HttpResponse::Created()),
|
|
|
|
)),
|
2019-11-26 06:25:50 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/t1/path1").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_nested_scope_root() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app").service(
|
|
|
|
web::scope("/t1")
|
|
|
|
.service(web::resource("").to(|| HttpResponse::Ok()))
|
|
|
|
.service(web::resource("/").to(|| HttpResponse::Created())),
|
2019-03-07 00:47:15 +01:00
|
|
|
),
|
2019-11-26 06:25:50 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/t1").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/t1/").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_nested_scope_filter() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app").service(
|
|
|
|
web::scope("/t1")
|
|
|
|
.guard(guard::Get())
|
|
|
|
.service(web::resource("/path1").to(|| HttpResponse::Ok())),
|
2019-03-07 00:47:15 +01:00
|
|
|
),
|
2019-11-26 06:25:50 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/t1/path1")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/t1/path1")
|
|
|
|
.method(Method::GET)
|
|
|
|
.to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-03-04 20:47:53 +01:00
|
|
|
}
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_nested_scope_with_variable_segment() {
|
|
|
|
let mut srv = init_service(App::new().service(web::scope("/app").service(
|
|
|
|
web::scope("/{project_id}").service(web::resource("/path1").to(
|
|
|
|
|r: HttpRequest| {
|
|
|
|
async move {
|
|
|
|
HttpResponse::Created()
|
|
|
|
.body(format!("project: {}", &r.match_info()["project_id"]))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
)))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/project_1/path1").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
|
|
|
|
|
|
|
match resp.response().body() {
|
|
|
|
ResponseBody::Body(Body::Bytes(ref b)) => {
|
|
|
|
let bytes: Bytes = b.clone().into();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"project: project_1"));
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
2019-11-26 06:25:50 +01:00
|
|
|
_ => panic!(),
|
|
|
|
}
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_nested2_scope_with_variable_segment() {
|
|
|
|
let mut srv = init_service(App::new().service(web::scope("/app").service(
|
|
|
|
web::scope("/{project}").service(web::scope("/{id}").service(
|
|
|
|
web::resource("/path1").to(|r: HttpRequest| {
|
|
|
|
async move {
|
|
|
|
HttpResponse::Created().body(format!(
|
|
|
|
"project: {} - {}",
|
|
|
|
&r.match_info()["project"],
|
|
|
|
&r.match_info()["id"],
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)),
|
|
|
|
)))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/test/1/path1").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
|
|
|
|
|
|
|
match resp.response().body() {
|
|
|
|
ResponseBody::Body(Body::Bytes(ref b)) => {
|
|
|
|
let bytes: Bytes = b.clone().into();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"project: test - 1"));
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
2019-11-26 06:25:50 +01:00
|
|
|
_ => panic!(),
|
|
|
|
}
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/test/1/path2").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_default_resource() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
|
|
|
.service(web::resource("/path1").to(|| HttpResponse::Ok()))
|
|
|
|
.default_service(|r: ServiceRequest| {
|
|
|
|
ok(r.into_response(HttpResponse::BadRequest()))
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/path2").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/path2").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_default_resource_propagation() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new()
|
|
|
|
.service(web::scope("/app1").default_service(
|
|
|
|
web::resource("").to(|| HttpResponse::BadRequest()),
|
|
|
|
))
|
|
|
|
.service(web::scope("/app2"))
|
|
|
|
.default_service(|r: ServiceRequest| {
|
|
|
|
ok(r.into_response(HttpResponse::MethodNotAllowed()))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.await;
|
2019-11-20 18:33:22 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/non-exist").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
2019-11-20 18:33:22 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app1/non-exist").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
2019-03-25 21:02:10 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app2/non-exist").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
2019-03-25 21:02:10 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_middleware() {
|
|
|
|
let mut srv =
|
|
|
|
init_service(
|
2019-11-20 18:33:22 +01:00
|
|
|
App::new().service(
|
|
|
|
web::scope("app")
|
|
|
|
.wrap(DefaultHeaders::new().header(
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
HeaderValue::from_static("0001"),
|
|
|
|
))
|
|
|
|
.service(
|
|
|
|
web::resource("/test")
|
|
|
|
.route(web::get().to(|| HttpResponse::Ok())),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/test").to_request();
|
|
|
|
let resp = call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("0001")
|
|
|
|
);
|
2019-03-25 21:02:10 +01:00
|
|
|
}
|
2019-03-25 21:43:02 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_middleware_fn() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("app")
|
|
|
|
.wrap_fn(|req, srv| {
|
|
|
|
let fut = srv.call(req);
|
|
|
|
async move {
|
|
|
|
let mut res = fut.await?;
|
|
|
|
res.headers_mut().insert(
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
HeaderValue::from_static("0001"),
|
|
|
|
);
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.route("/test", web::get().to(|| HttpResponse::Ok())),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/test").to_request();
|
|
|
|
let resp = call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("0001")
|
|
|
|
);
|
2019-03-25 21:43:02 +01:00
|
|
|
}
|
2019-05-12 18:42:05 +02:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_override_data() {
|
|
|
|
let mut srv = init_service(App::new().data(1usize).service(
|
|
|
|
web::scope("app").data(10usize).route(
|
|
|
|
"/t",
|
|
|
|
web::get().to(|data: web::Data<usize>| {
|
|
|
|
assert_eq!(*data, 10);
|
|
|
|
let _ = data.clone();
|
|
|
|
HttpResponse::Ok()
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
))
|
|
|
|
.await;
|
2019-09-18 02:36:39 +02:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/t").to_request();
|
|
|
|
let resp = call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-09-18 02:36:39 +02:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_override_register_data() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().register_data(web::Data::new(1usize)).service(
|
|
|
|
web::scope("app")
|
|
|
|
.register_data(web::Data::new(10usize))
|
|
|
|
.route(
|
|
|
|
"/t",
|
|
|
|
web::get().to(|data: web::Data<usize>| {
|
|
|
|
assert_eq!(*data, 10);
|
|
|
|
let _ = data.clone();
|
|
|
|
HttpResponse::Ok()
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2019-05-12 18:42:05 +02:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/t").to_request();
|
|
|
|
let resp = call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-05-12 18:42:05 +02:00
|
|
|
}
|
2019-06-03 19:12:37 +02:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_scope_config() {
|
|
|
|
let mut srv =
|
|
|
|
init_service(App::new().service(web::scope("/app").configure(|s| {
|
|
|
|
s.route("/path1", web::get().to(|| HttpResponse::Ok()));
|
|
|
|
})))
|
|
|
|
.await;
|
2019-06-03 19:12:37 +02:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/path1").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-06-03 19:41:32 +02:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_scope_config_2() {
|
|
|
|
let mut srv =
|
|
|
|
init_service(App::new().service(web::scope("/app").configure(|s| {
|
|
|
|
s.service(web::scope("/v1").configure(|s| {
|
|
|
|
s.route("/", web::get().to(|| HttpResponse::Ok()));
|
|
|
|
}));
|
|
|
|
})))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/v1/").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-06-03 19:12:37 +02:00
|
|
|
}
|
2019-06-15 18:12:20 +02:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_url_for_external() {
|
|
|
|
let mut srv =
|
|
|
|
init_service(App::new().service(web::scope("/app").configure(|s| {
|
|
|
|
s.service(web::scope("/v1").configure(|s| {
|
|
|
|
s.external_resource(
|
|
|
|
"youtube",
|
|
|
|
"https://youtube.com/watch/{video_id}",
|
|
|
|
);
|
|
|
|
s.route(
|
|
|
|
"/",
|
|
|
|
web::get().to(|req: HttpRequest| {
|
|
|
|
async move {
|
|
|
|
HttpResponse::Ok().body(format!(
|
|
|
|
"{}",
|
|
|
|
req.url_for("youtube", &["xxxxxx"])
|
|
|
|
.unwrap()
|
|
|
|
.as_str()
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}));
|
|
|
|
})))
|
2019-11-20 18:33:22 +01:00
|
|
|
.await;
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/v1/").to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
let body = read_body(resp).await;
|
|
|
|
assert_eq!(body, &b"https://youtube.com/watch/xxxxxx"[..]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_url_for_nested() {
|
|
|
|
let mut srv = init_service(App::new().service(web::scope("/a").service(
|
|
|
|
web::scope("/b").service(web::resource("/c/{stuff}").name("c").route(
|
|
|
|
web::get().to(|req: HttpRequest| {
|
|
|
|
async move {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.body(format!("{}", req.url_for("c", &["12345"]).unwrap()))
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)),
|
|
|
|
)))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/a/b/c/test").to_request();
|
|
|
|
let resp = call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
let body = read_body(resp).await;
|
|
|
|
assert_eq!(
|
|
|
|
body,
|
|
|
|
Bytes::from_static(b"http://localhost:8080/a/b/c/12345")
|
|
|
|
);
|
2019-06-15 18:12:20 +02:00
|
|
|
}
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|