2019-03-04 05:02:01 +00:00
|
|
|
use std::cell::RefCell;
|
2019-04-14 05:25:00 +00:00
|
|
|
use std::fmt;
|
2019-03-04 05:02:01 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use actix_http::Response;
|
|
|
|
use actix_router::{ResourceDef, ResourceInfo, Router};
|
|
|
|
use actix_service::boxed::{self, BoxedNewService, BoxedService};
|
|
|
|
use actix_service::{
|
2019-05-12 15:34:51 +00:00
|
|
|
apply_transform, IntoNewService, IntoTransform, NewService, Service, Transform,
|
2019-03-04 05:02:01 +00:00
|
|
|
};
|
|
|
|
use futures::future::{ok, Either, Future, FutureResult};
|
2019-03-25 20:43:02 +00:00
|
|
|
use futures::{Async, IntoFuture, Poll};
|
2019-03-04 05:02:01 +00:00
|
|
|
|
2019-04-15 14:32:49 +00:00
|
|
|
use crate::dev::{AppService, HttpServiceFactory};
|
2019-03-10 23:35:38 +00:00
|
|
|
use crate::error::Error;
|
2019-03-04 05:02:01 +00:00
|
|
|
use crate::guard::Guard;
|
|
|
|
use crate::resource::Resource;
|
2019-03-09 15:39:34 +00:00
|
|
|
use crate::rmap::ResourceMap;
|
2019-03-04 05:02:01 +00:00
|
|
|
use crate::route::Route;
|
2019-03-06 23:47:15 +00:00
|
|
|
use crate::service::{
|
|
|
|
ServiceFactory, ServiceFactoryWrapper, ServiceRequest, ServiceResponse,
|
|
|
|
};
|
2019-03-04 05:02:01 +00:00
|
|
|
|
2019-03-04 19:47:53 +00:00
|
|
|
type Guards = Vec<Box<Guard>>;
|
2019-04-13 21:50:54 +00:00
|
|
|
type HttpService = BoxedService<ServiceRequest, ServiceResponse, Error>;
|
|
|
|
type HttpNewService = BoxedNewService<(), ServiceRequest, ServiceResponse, Error, ()>;
|
2019-04-08 06:06:21 +00:00
|
|
|
type BoxedResponse = Either<
|
|
|
|
FutureResult<ServiceResponse, Error>,
|
|
|
|
Box<Future<Item = ServiceResponse, Error = Error>>,
|
|
|
|
>;
|
2019-03-04 05:02:01 +00:00
|
|
|
|
2019-03-17 04:35:02 +00:00
|
|
|
/// Resources scope.
|
2019-03-04 05:02:01 +00: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 21:25:35 +00:00
|
|
|
/// use actix_web::{web, App, HttpResponse};
|
2019-03-04 05:02:01 +00:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-06 23:47:15 +00:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::scope("/{project_id}/")
|
|
|
|
/// .service(web::resource("/path1").to(|| HttpResponse::Ok()))
|
|
|
|
/// .service(web::resource("/path2").route(web::get().to(|| HttpResponse::Ok())))
|
|
|
|
/// .service(web::resource("/path3").route(web::head().to(|| HttpResponse::MethodNotAllowed())))
|
|
|
|
/// );
|
2019-03-04 05:02:01 +00: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 21:50:54 +00:00
|
|
|
pub struct Scope<T = ScopeEndpoint> {
|
2019-03-04 05:02:01 +00:00
|
|
|
endpoint: T,
|
2019-03-06 23:47:15 +00:00
|
|
|
rdef: String,
|
2019-04-13 21:50:54 +00:00
|
|
|
services: Vec<Box<ServiceFactory>>,
|
2019-03-04 19:47:53 +00:00
|
|
|
guards: Vec<Box<Guard>>,
|
2019-04-13 21:50:54 +00:00
|
|
|
default: Rc<RefCell<Option<Rc<HttpNewService>>>>,
|
|
|
|
factory_ref: Rc<RefCell<Option<ScopeFactory>>>,
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl Scope {
|
2019-03-04 05:02:01 +00:00
|
|
|
/// Create a new scope
|
2019-04-13 21:50:54 +00:00
|
|
|
pub fn new(path: &str) -> Scope {
|
2019-03-04 05:02:01 +00:00
|
|
|
let fref = Rc::new(RefCell::new(None));
|
|
|
|
Scope {
|
|
|
|
endpoint: ScopeEndpoint::new(fref.clone()),
|
2019-03-06 23:47:15 +00:00
|
|
|
rdef: path.to_string(),
|
2019-03-04 19:47:53 +00:00
|
|
|
guards: Vec::new(),
|
2019-03-04 05:02:01 +00:00
|
|
|
services: Vec::new(),
|
|
|
|
default: Rc::new(RefCell::new(None)),
|
|
|
|
factory_ref: fref,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl<T> Scope<T>
|
2019-03-04 05:02:01 +00:00
|
|
|
where
|
|
|
|
T: NewService<
|
2019-05-12 15:34:51 +00:00
|
|
|
Config = (),
|
2019-04-13 21:50:54 +00:00
|
|
|
Request = ServiceRequest,
|
2019-03-04 05:02:01 +00:00
|
|
|
Response = ServiceResponse,
|
2019-03-10 23:35:38 +00:00
|
|
|
Error = Error,
|
2019-03-04 05:02:01 +00:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
2019-03-06 23:47:15 +00:00
|
|
|
/// Add match guard to a scope.
|
2019-03-04 05:02:01 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-07 22:01:52 +00:00
|
|
|
/// use actix_web::{web, guard, App, HttpRequest, HttpResponse};
|
2019-03-04 05:02:01 +00:00
|
|
|
///
|
2019-03-07 22:01:52 +00:00
|
|
|
/// fn index(data: web::Path<(String, String)>) -> &'static str {
|
2019-03-04 05:02:01 +00:00
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-06 23:47:15 +00:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::scope("/app")
|
2019-03-04 05:02:01 +00:00
|
|
|
/// .guard(guard::Header("content-type", "text/plain"))
|
2019-03-06 23:47:15 +00:00
|
|
|
/// .route("/test1", web::get().to(index))
|
2019-03-04 05:02:01 +00:00
|
|
|
/// .route("/test2", web::post().to(|r: HttpRequest| {
|
|
|
|
/// HttpResponse::MethodNotAllowed()
|
|
|
|
/// }))
|
2019-03-06 23:47:15 +00:00
|
|
|
/// );
|
2019-03-04 05:02:01 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn guard<G: Guard + 'static>(mut self, guard: G) -> Self {
|
2019-03-04 19:47:53 +00:00
|
|
|
self.guards.push(Box::new(guard));
|
2019-03-04 05:02:01 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-17 04:35:02 +00: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 05:02:01 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-06 23:47:15 +00:00
|
|
|
/// use actix_web::{web, App, HttpRequest};
|
2019-03-04 05:02:01 +00:00
|
|
|
///
|
|
|
|
/// struct AppState;
|
|
|
|
///
|
|
|
|
/// fn index(req: HttpRequest) -> &'static str {
|
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-06 23:47:15 +00:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::scope("/app").service(
|
|
|
|
/// web::scope("/v1")
|
|
|
|
/// .service(web::resource("/test1").to(index)))
|
|
|
|
/// );
|
2019-03-04 05:02:01 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-03-06 23:47:15 +00:00
|
|
|
pub fn service<F>(mut self, factory: F) -> Self
|
2019-03-04 05:02:01 +00:00
|
|
|
where
|
2019-04-13 21:50:54 +00:00
|
|
|
F: HttpServiceFactory + 'static,
|
2019-03-04 05:02:01 +00:00
|
|
|
{
|
|
|
|
self.services
|
2019-03-06 23:47:15 +00:00
|
|
|
.push(Box::new(ServiceFactoryWrapper::new(factory)));
|
2019-03-04 05:02:01 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configure route for a specific path.
|
|
|
|
///
|
2019-03-06 23:47:15 +00:00
|
|
|
/// This is a simplified version of the `Scope::service()` method.
|
2019-03-17 04:35:02 +00:00
|
|
|
/// This method can be called multiple times, in that case
|
2019-03-04 05:02:01 +00:00
|
|
|
/// multiple resources with one route would be registered for same resource path.
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-07 22:01:52 +00:00
|
|
|
/// use actix_web::{web, App, HttpResponse};
|
2019-03-04 05:02:01 +00:00
|
|
|
///
|
2019-03-07 22:01:52 +00:00
|
|
|
/// fn index(data: web::Path<(String, String)>) -> &'static str {
|
2019-03-04 05:02:01 +00:00
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-06 23:47:15 +00:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::scope("/app")
|
|
|
|
/// .route("/test1", web::get().to(index))
|
2019-03-04 05:02:01 +00:00
|
|
|
/// .route("/test2", web::post().to(|| HttpResponse::MethodNotAllowed()))
|
2019-03-06 23:47:15 +00:00
|
|
|
/// );
|
2019-03-04 05:02:01 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-04-13 21:50:54 +00:00
|
|
|
pub fn route(self, path: &str, mut route: Route) -> Self {
|
2019-03-06 23:47:15 +00:00
|
|
|
self.service(
|
|
|
|
Resource::new(path)
|
|
|
|
.add_guards(route.take_guards())
|
|
|
|
.route(route),
|
|
|
|
)
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-14 05:25:00 +00:00
|
|
|
/// Default service to be used if no matching route could be found.
|
2019-03-17 04:35:02 +00:00
|
|
|
///
|
|
|
|
/// If default resource is not registered, app's default resource is being used.
|
2019-04-14 05:25:00 +00:00
|
|
|
pub fn default_service<F, U>(mut self, f: F) -> Self
|
2019-03-04 05:02:01 +00:00
|
|
|
where
|
2019-04-14 05:25:00 +00:00
|
|
|
F: IntoNewService<U>,
|
2019-03-04 05:02:01 +00:00
|
|
|
U: NewService<
|
2019-05-12 15:34:51 +00:00
|
|
|
Config = (),
|
2019-04-13 21:50:54 +00:00
|
|
|
Request = ServiceRequest,
|
2019-03-04 05:02:01 +00:00
|
|
|
Response = ServiceResponse,
|
2019-03-10 23:35:38 +00:00
|
|
|
Error = Error,
|
2019-03-04 05:02:01 +00:00
|
|
|
> + 'static,
|
2019-04-14 05:25:00 +00:00
|
|
|
U::InitError: fmt::Debug,
|
2019-03-04 05:02:01 +00:00
|
|
|
{
|
|
|
|
// create and configure default resource
|
|
|
|
self.default = Rc::new(RefCell::new(Some(Rc::new(boxed::new_service(
|
2019-04-14 05:25:00 +00:00
|
|
|
f.into_new_service().map_init_err(|e| {
|
|
|
|
log::error!("Can not construct default service: {:?}", e)
|
|
|
|
}),
|
2019-03-04 05:02:01 +00:00
|
|
|
)))));
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-04-02 19:51:16 +00: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 19:10:28 +00: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 19:51:16 +00:00
|
|
|
/// Application level middleware, in that Scope-level middleware can not modify
|
2019-04-01 19:10:28 +00:00
|
|
|
/// ServiceResponse.
|
2019-03-04 05:02:01 +00:00
|
|
|
///
|
2019-04-01 18:52:05 +00:00
|
|
|
/// Use middleware when you need to read or modify *every* request in some way.
|
2019-03-25 20:02:10 +00:00
|
|
|
pub fn wrap<M, F>(
|
2019-03-04 05:02:01 +00:00
|
|
|
self,
|
|
|
|
mw: F,
|
|
|
|
) -> Scope<
|
|
|
|
impl NewService<
|
2019-05-12 15:34:51 +00:00
|
|
|
Config = (),
|
2019-04-13 21:50:54 +00:00
|
|
|
Request = ServiceRequest,
|
2019-03-04 05:02:01 +00:00
|
|
|
Response = ServiceResponse,
|
2019-03-10 23:35:38 +00:00
|
|
|
Error = Error,
|
2019-03-04 05:02:01 +00:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
2019-03-05 05:37:57 +00:00
|
|
|
M: Transform<
|
2019-03-04 05:02:01 +00:00
|
|
|
T::Service,
|
2019-04-13 21:50:54 +00:00
|
|
|
Request = ServiceRequest,
|
2019-03-04 05:02:01 +00:00
|
|
|
Response = ServiceResponse,
|
2019-03-10 23:35:38 +00:00
|
|
|
Error = Error,
|
2019-03-04 05:02:01 +00:00
|
|
|
InitError = (),
|
|
|
|
>,
|
2019-03-09 17:49:11 +00:00
|
|
|
F: IntoTransform<M, T::Service>,
|
2019-03-04 05:02:01 +00:00
|
|
|
{
|
2019-05-12 15:34:51 +00:00
|
|
|
let endpoint = apply_transform(mw, self.endpoint);
|
2019-03-04 05:02:01 +00:00
|
|
|
Scope {
|
|
|
|
endpoint,
|
|
|
|
rdef: self.rdef,
|
|
|
|
guards: self.guards,
|
|
|
|
services: self.services,
|
|
|
|
default: self.default,
|
|
|
|
factory_ref: self.factory_ref,
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 20:43:02 +00:00
|
|
|
|
2019-04-01 18:52:05 +00:00
|
|
|
/// Registers middleware, in the form of a closure, that runs during inbound
|
2019-04-02 19:51:16 +00:00
|
|
|
/// processing in the request lifecycle (request -> response), modifying
|
2019-04-25 18:14:32 +00:00
|
|
|
/// request as necessary, across all requests managed by the *Scope*.
|
2019-04-01 19:10:28 +00: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 20:43:02 +00: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().service(
|
|
|
|
/// web::scope("/app")
|
|
|
|
/// .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>(
|
|
|
|
self,
|
|
|
|
mw: F,
|
|
|
|
) -> Scope<
|
|
|
|
impl NewService<
|
2019-05-12 15:34:51 +00:00
|
|
|
Config = (),
|
2019-04-13 21:50:54 +00:00
|
|
|
Request = ServiceRequest,
|
2019-03-25 20:43:02 +00:00
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = Error,
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
2019-04-13 21:50:54 +00:00
|
|
|
F: FnMut(ServiceRequest, &mut T::Service) -> R + Clone,
|
2019-03-25 20:43:02 +00:00
|
|
|
R: IntoFuture<Item = ServiceResponse, Error = Error>,
|
|
|
|
{
|
|
|
|
self.wrap(mw)
|
|
|
|
}
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl<T> HttpServiceFactory for Scope<T>
|
2019-03-04 05:02:01 +00:00
|
|
|
where
|
|
|
|
T: NewService<
|
2019-05-12 15:34:51 +00:00
|
|
|
Config = (),
|
2019-04-13 21:50:54 +00:00
|
|
|
Request = ServiceRequest,
|
2019-03-06 23:47:15 +00:00
|
|
|
Response = ServiceResponse,
|
2019-03-10 23:35:38 +00:00
|
|
|
Error = Error,
|
2019-03-06 23:47:15 +00:00
|
|
|
InitError = (),
|
|
|
|
> + 'static,
|
2019-03-04 05:02:01 +00:00
|
|
|
{
|
2019-04-15 14:32:49 +00:00
|
|
|
fn register(self, config: &mut AppService) {
|
2019-03-09 15:39:34 +00:00
|
|
|
// update default resource if needed
|
2019-03-06 23:47:15 +00:00
|
|
|
if self.default.borrow().is_none() {
|
|
|
|
*self.default.borrow_mut() = Some(config.default_service());
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:39:34 +00:00
|
|
|
// register nested services
|
2019-03-06 23:47:15 +00:00
|
|
|
let mut cfg = config.clone_config();
|
|
|
|
self.services
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|mut srv| srv.register(&mut cfg));
|
|
|
|
|
2019-03-09 15:39:34 +00:00
|
|
|
let mut rmap = ResourceMap::new(ResourceDef::root_prefix(&self.rdef));
|
|
|
|
|
|
|
|
// complete scope pipeline creation
|
2019-03-04 05:02:01 +00:00
|
|
|
*self.factory_ref.borrow_mut() = Some(ScopeFactory {
|
|
|
|
default: self.default.clone(),
|
2019-03-04 19:47:53 +00:00
|
|
|
services: Rc::new(
|
2019-03-06 23:47:15 +00:00
|
|
|
cfg.into_services()
|
2019-05-05 02:43:49 +00:00
|
|
|
.1
|
2019-03-04 19:47:53 +00:00
|
|
|
.into_iter()
|
2019-03-09 15:39:34 +00:00
|
|
|
.map(|(mut rdef, srv, guards, nested)| {
|
|
|
|
rmap.add(&mut rdef, nested);
|
|
|
|
(rdef, srv, RefCell::new(guards))
|
|
|
|
})
|
2019-03-04 19:47:53 +00:00
|
|
|
.collect(),
|
|
|
|
),
|
2019-03-04 05:02:01 +00:00
|
|
|
});
|
|
|
|
|
2019-03-09 15:39:34 +00:00
|
|
|
// get guards
|
2019-03-06 23:47:15 +00:00
|
|
|
let guards = if self.guards.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(self.guards)
|
|
|
|
};
|
2019-03-09 15:39:34 +00:00
|
|
|
|
|
|
|
// register final service
|
2019-03-07 03:27:18 +00:00
|
|
|
config.register_service(
|
|
|
|
ResourceDef::root_prefix(&self.rdef),
|
|
|
|
guards,
|
|
|
|
self.endpoint,
|
2019-03-09 15:39:34 +00:00
|
|
|
Some(Rc::new(rmap)),
|
2019-03-07 03:27:18 +00:00
|
|
|
)
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
pub struct ScopeFactory {
|
|
|
|
services: Rc<Vec<(ResourceDef, HttpNewService, RefCell<Option<Guards>>)>>,
|
|
|
|
default: Rc<RefCell<Option<Rc<HttpNewService>>>>,
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl NewService for ScopeFactory {
|
2019-05-12 15:34:51 +00:00
|
|
|
type Config = ();
|
2019-04-13 21:50:54 +00:00
|
|
|
type Request = ServiceRequest;
|
2019-03-04 05:02:01 +00:00
|
|
|
type Response = ServiceResponse;
|
2019-03-10 23:35:38 +00:00
|
|
|
type Error = Error;
|
2019-03-04 05:02:01 +00:00
|
|
|
type InitError = ();
|
2019-04-13 21:50:54 +00:00
|
|
|
type Service = ScopeService;
|
|
|
|
type Future = ScopeFactoryResponse;
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
let default_fut = if let Some(ref default) = *self.default.borrow() {
|
|
|
|
Some(default.new_service(&()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
ScopeFactoryResponse {
|
|
|
|
fut: self
|
|
|
|
.services
|
|
|
|
.iter()
|
2019-03-04 19:47:53 +00:00
|
|
|
.map(|(path, service, guards)| {
|
2019-03-04 05:02:01 +00:00
|
|
|
CreateScopeServiceItem::Future(
|
|
|
|
Some(path.clone()),
|
2019-03-04 19:47:53 +00:00
|
|
|
guards.borrow_mut().take(),
|
2019-03-04 05:02:01 +00:00
|
|
|
service.new_service(&()),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
default: None,
|
|
|
|
default_fut,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 04:35:02 +00:00
|
|
|
/// Create scope service
|
2019-03-04 05:02:01 +00:00
|
|
|
#[doc(hidden)]
|
2019-04-13 21:50:54 +00:00
|
|
|
pub struct ScopeFactoryResponse {
|
|
|
|
fut: Vec<CreateScopeServiceItem>,
|
|
|
|
default: Option<HttpService>,
|
|
|
|
default_fut: Option<Box<Future<Item = HttpService, Error = ()>>>,
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
type HttpServiceFut = Box<Future<Item = HttpService, Error = ()>>;
|
2019-03-04 05:02:01 +00:00
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
enum CreateScopeServiceItem {
|
|
|
|
Future(Option<ResourceDef>, Option<Guards>, HttpServiceFut),
|
|
|
|
Service(ResourceDef, Option<Guards>, HttpService),
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl Future for ScopeFactoryResponse {
|
|
|
|
type Item = ScopeService;
|
2019-03-04 05:02:01 +00:00
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
let mut done = true;
|
|
|
|
|
|
|
|
if let Some(ref mut fut) = self.default_fut {
|
|
|
|
match fut.poll()? {
|
|
|
|
Async::Ready(default) => self.default = Some(default),
|
|
|
|
Async::NotReady => done = false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// poll http services
|
|
|
|
for item in &mut self.fut {
|
|
|
|
let res = match item {
|
2019-03-04 19:47:53 +00:00
|
|
|
CreateScopeServiceItem::Future(
|
|
|
|
ref mut path,
|
|
|
|
ref mut guards,
|
|
|
|
ref mut fut,
|
|
|
|
) => match fut.poll()? {
|
|
|
|
Async::Ready(service) => {
|
|
|
|
Some((path.take().unwrap(), guards.take(), service))
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
2019-03-04 19:47:53 +00:00
|
|
|
Async::NotReady => {
|
|
|
|
done = false;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CreateScopeServiceItem::Service(_, _, _) => continue,
|
2019-03-04 05:02:01 +00:00
|
|
|
};
|
|
|
|
|
2019-03-04 19:47:53 +00:00
|
|
|
if let Some((path, guards, service)) = res {
|
|
|
|
*item = CreateScopeServiceItem::Service(path, guards, service);
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if done {
|
|
|
|
let router = self
|
|
|
|
.fut
|
|
|
|
.drain(..)
|
|
|
|
.fold(Router::build(), |mut router, item| {
|
|
|
|
match item {
|
2019-03-04 19:47:53 +00:00
|
|
|
CreateScopeServiceItem::Service(path, guards, service) => {
|
2019-03-09 15:39:34 +00:00
|
|
|
router.rdef(path, service).2 = guards;
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
2019-03-04 19:47:53 +00:00
|
|
|
CreateScopeServiceItem::Future(_, _, _) => unreachable!(),
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
router
|
|
|
|
});
|
|
|
|
Ok(Async::Ready(ScopeService {
|
|
|
|
router: router.finish(),
|
|
|
|
default: self.default.take(),
|
|
|
|
_ready: None,
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
pub struct ScopeService {
|
|
|
|
router: Router<HttpService, Vec<Box<Guard>>>,
|
|
|
|
default: Option<HttpService>,
|
|
|
|
_ready: Option<(ServiceRequest, ResourceInfo)>,
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl Service for ScopeService {
|
|
|
|
type Request = ServiceRequest;
|
2019-03-04 05:02:01 +00:00
|
|
|
type Response = ServiceResponse;
|
2019-03-10 23:35:38 +00:00
|
|
|
type Error = Error;
|
2019-03-04 05:02:01 +00:00
|
|
|
type Future = Either<BoxedResponse, FutureResult<Self::Response, Self::Error>>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
fn call(&mut self, mut req: ServiceRequest) -> Self::Future {
|
2019-03-04 19:47:53 +00: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-03-04 05:02:01 +00:00
|
|
|
Either::A(srv.call(req))
|
|
|
|
} else if let Some(ref mut default) = self.default {
|
|
|
|
Either::A(default.call(req))
|
|
|
|
} else {
|
2019-03-26 22:14:32 +00:00
|
|
|
let req = req.into_parts().0;
|
2019-03-04 05:02:01 +00:00
|
|
|
Either::B(ok(ServiceResponse::new(req, Response::NotFound().finish())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2019-04-13 21:50:54 +00:00
|
|
|
pub struct ScopeEndpoint {
|
|
|
|
factory: Rc<RefCell<Option<ScopeFactory>>>,
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl ScopeEndpoint {
|
|
|
|
fn new(factory: Rc<RefCell<Option<ScopeFactory>>>) -> Self {
|
2019-03-04 05:02:01 +00:00
|
|
|
ScopeEndpoint { factory }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl NewService for ScopeEndpoint {
|
2019-05-12 15:34:51 +00:00
|
|
|
type Config = ();
|
2019-04-13 21:50:54 +00:00
|
|
|
type Request = ServiceRequest;
|
2019-03-04 05:02:01 +00:00
|
|
|
type Response = ServiceResponse;
|
2019-03-10 23:35:38 +00:00
|
|
|
type Error = Error;
|
2019-03-04 05:02:01 +00:00
|
|
|
type InitError = ();
|
2019-04-13 21:50:54 +00:00
|
|
|
type Service = ScopeService;
|
|
|
|
type Future = ScopeFactoryResponse;
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
self.factory.borrow_mut().as_mut().unwrap().new_service(&())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-03-06 23:47:15 +00:00
|
|
|
use actix_service::Service;
|
2019-03-04 05:02:01 +00:00
|
|
|
use bytes::Bytes;
|
2019-03-25 20:02:10 +00:00
|
|
|
use futures::{Future, IntoFuture};
|
2019-03-04 05:02:01 +00:00
|
|
|
|
2019-03-07 23:51:24 +00:00
|
|
|
use crate::dev::{Body, ResponseBody};
|
2019-03-25 20:02:10 +00:00
|
|
|
use crate::http::{header, HeaderValue, Method, StatusCode};
|
|
|
|
use crate::service::{ServiceRequest, ServiceResponse};
|
2019-04-15 14:44:07 +00:00
|
|
|
use crate::test::{block_on, call_service, init_service, TestRequest};
|
2019-03-25 20:02:10 +00:00
|
|
|
use crate::{guard, web, App, Error, HttpRequest, HttpResponse};
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_scope() {
|
2019-03-06 23:47:15 +00:00
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
|
|
|
.service(web::resource("/path1").to(|| HttpResponse::Ok())),
|
|
|
|
),
|
|
|
|
);
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_scope_root() {
|
2019-03-06 23:47:15 +00:00
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
|
|
|
.service(web::resource("").to(|| HttpResponse::Ok()))
|
|
|
|
.service(web::resource("/").to(|| HttpResponse::Created())),
|
|
|
|
),
|
|
|
|
);
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_scope_root2() {
|
2019-03-06 23:47:15 +00:00
|
|
|
let mut srv = init_service(App::new().service(
|
|
|
|
web::scope("/app/").service(web::resource("").to(|| HttpResponse::Ok())),
|
|
|
|
));
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_scope_root3() {
|
2019-03-06 23:47:15 +00:00
|
|
|
let mut srv = init_service(App::new().service(
|
|
|
|
web::scope("/app/").service(web::resource("/").to(|| HttpResponse::Ok())),
|
|
|
|
));
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_scope_route() {
|
2019-03-06 23:47:15 +00:00
|
|
|
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())),
|
|
|
|
),
|
|
|
|
);
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1")
|
|
|
|
.method(Method::DELETE)
|
|
|
|
.to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_scope_route_without_leading_slash() {
|
2019-03-06 23:47:15 +00:00
|
|
|
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-04 05:02:01 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1")
|
|
|
|
.method(Method::DELETE)
|
|
|
|
.to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-24 23:28:16 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 19:47:53 +00:00
|
|
|
#[test]
|
|
|
|
fn test_scope_guard() {
|
2019-03-06 23:47:15 +00:00
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
2019-03-04 19:47:53 +00:00
|
|
|
.guard(guard::Get())
|
2019-03-06 23:47:15 +00:00
|
|
|
.service(web::resource("/path1").to(|| HttpResponse::Ok())),
|
|
|
|
),
|
|
|
|
);
|
2019-03-04 19:47:53 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 19:47:53 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path1")
|
|
|
|
.method(Method::GET)
|
|
|
|
.to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 19:47:53 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_scope_variable_segment() {
|
2019-03-06 23:47:15 +00:00
|
|
|
let mut srv =
|
|
|
|
init_service(App::new().service(web::scope("/ab-{project}").service(
|
|
|
|
web::resource("/path1").to(|r: HttpRequest| {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.body(format!("project: {}", &r.match_info()["project"]))
|
|
|
|
}),
|
|
|
|
)));
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/ab-project1/path1").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
2019-04-02 20:35:01 +00:00
|
|
|
match resp.response().body() {
|
2019-03-04 05:02:01 +00:00
|
|
|
ResponseBody::Body(Body::Bytes(ref b)) => {
|
|
|
|
let bytes: Bytes = b.clone().into();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"project: project1"));
|
|
|
|
}
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/aa-project1/path1").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nested_scope() {
|
2019-03-06 23:47:15 +00:00
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
|
|
|
.service(web::scope("/t1").service(
|
|
|
|
web::resource("/path1").to(|| HttpResponse::Created()),
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
);
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/t1/path1").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nested_scope_no_slash() {
|
2019-03-06 23:47:15 +00:00
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
|
|
|
.service(web::scope("t1").service(
|
|
|
|
web::resource("/path1").to(|| HttpResponse::Created()),
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
);
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/t1/path1").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nested_scope_root() {
|
2019-03-06 23:47:15 +00:00
|
|
|
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-04 05:02:01 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/t1").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/t1/").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:47:53 +00:00
|
|
|
#[test]
|
|
|
|
fn test_nested_scope_filter() {
|
2019-03-06 23:47:15 +00:00
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app").service(
|
|
|
|
web::scope("/t1")
|
2019-03-04 19:47:53 +00:00
|
|
|
.guard(guard::Get())
|
2019-03-06 23:47:15 +00:00
|
|
|
.service(web::resource("/path1").to(|| HttpResponse::Ok())),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2019-03-04 19:47:53 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/t1/path1")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 19:47:53 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/t1/path1")
|
|
|
|
.method(Method::GET)
|
|
|
|
.to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 19:47:53 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nested_scope_with_variable_segment() {
|
2019-03-06 23:47:15 +00:00
|
|
|
let mut srv = init_service(App::new().service(web::scope("/app").service(
|
|
|
|
web::scope("/{project_id}").service(web::resource("/path1").to(
|
|
|
|
|r: HttpRequest| {
|
|
|
|
HttpResponse::Created()
|
|
|
|
.body(format!("project: {}", &r.match_info()["project_id"]))
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
)));
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/project_1/path1").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
|
|
|
|
2019-04-02 20:35:01 +00:00
|
|
|
match resp.response().body() {
|
2019-03-04 05:02:01 +00:00
|
|
|
ResponseBody::Body(Body::Bytes(ref b)) => {
|
|
|
|
let bytes: Bytes = b.clone().into();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"project: project_1"));
|
|
|
|
}
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nested2_scope_with_variable_segment() {
|
2019-03-06 23:47:15 +00:00
|
|
|
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| {
|
|
|
|
HttpResponse::Created().body(format!(
|
|
|
|
"project: {} - {}",
|
|
|
|
&r.match_info()["project"],
|
|
|
|
&r.match_info()["id"],
|
|
|
|
))
|
|
|
|
}),
|
|
|
|
)),
|
|
|
|
)));
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/test/1/path1").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
|
|
|
|
2019-04-02 20:35:01 +00:00
|
|
|
match resp.response().body() {
|
2019-03-04 05:02:01 +00:00
|
|
|
ResponseBody::Body(Body::Bytes(ref b)) => {
|
|
|
|
let bytes: Bytes = b.clone().into();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"project: test - 1"));
|
|
|
|
}
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/test/1/path2").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_default_resource() {
|
2019-03-06 23:47:15 +00:00
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
|
|
|
.service(web::resource("/path1").to(|| HttpResponse::Ok()))
|
2019-04-14 05:25:00 +00:00
|
|
|
.default_service(|r: ServiceRequest| {
|
|
|
|
r.into_response(HttpResponse::BadRequest())
|
|
|
|
}),
|
2019-03-06 23:47:15 +00:00
|
|
|
),
|
|
|
|
);
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/path2").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/path2").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_default_resource_propagation() {
|
2019-03-06 23:47:15 +00:00
|
|
|
let mut srv = init_service(
|
|
|
|
App::new()
|
2019-04-14 05:25:00 +00:00
|
|
|
.service(web::scope("/app1").default_service(
|
|
|
|
web::resource("").to(|| HttpResponse::BadRequest()),
|
|
|
|
))
|
2019-03-06 23:47:15 +00:00
|
|
|
.service(web::scope("/app2"))
|
2019-04-14 05:25:00 +00:00
|
|
|
.default_service(|r: ServiceRequest| {
|
|
|
|
r.into_response(HttpResponse::MethodNotAllowed())
|
|
|
|
}),
|
2019-03-06 23:47:15 +00:00
|
|
|
);
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/non-exist").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app1/non-exist").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app2/non-exist").to_request();
|
2019-03-04 21:25:35 +00:00
|
|
|
let resp = block_on(srv.call(req)).unwrap();
|
2019-03-04 05:02:01 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
|
|
|
}
|
2019-03-25 20:02:10 +00:00
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
fn md<S, B>(
|
|
|
|
req: ServiceRequest,
|
2019-03-25 20:02:10 +00:00
|
|
|
srv: &mut S,
|
|
|
|
) -> impl IntoFuture<Item = ServiceResponse<B>, Error = Error>
|
|
|
|
where
|
|
|
|
S: Service<
|
2019-04-13 21:50:54 +00:00
|
|
|
Request = ServiceRequest,
|
2019-03-25 20:02:10 +00:00
|
|
|
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_middleware() {
|
|
|
|
let mut srv =
|
|
|
|
init_service(App::new().service(web::scope("app").wrap(md).service(
|
|
|
|
web::resource("/test").route(web::get().to(|| HttpResponse::Ok())),
|
|
|
|
)));
|
|
|
|
let req = TestRequest::with_uri("/app/test").to_request();
|
2019-04-15 14:44:07 +00:00
|
|
|
let resp = call_service(&mut srv, req);
|
2019-03-25 20:02:10 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("0001")
|
|
|
|
);
|
|
|
|
}
|
2019-03-25 20:43:02 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_middleware_fn() {
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("app")
|
|
|
|
.wrap_fn(|req, srv| {
|
|
|
|
srv.call(req).map(|mut res| {
|
|
|
|
res.headers_mut().insert(
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
HeaderValue::from_static("0001"),
|
|
|
|
);
|
|
|
|
res
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.route("/test", web::get().to(|| HttpResponse::Ok())),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
let req = TestRequest::with_uri("/app/test").to_request();
|
2019-04-15 14:44:07 +00:00
|
|
|
let resp = call_service(&mut srv, req);
|
2019-03-25 20:43:02 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("0001")
|
|
|
|
);
|
|
|
|
}
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|