2019-03-04 05:02:01 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
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-03-05 05:37:57 +00:00
|
|
|
ApplyTransform, IntoNewService, IntoTransform, NewService, Service, Transform,
|
2019-03-04 05:02:01 +00:00
|
|
|
};
|
|
|
|
use futures::future::{ok, Either, Future, FutureResult};
|
|
|
|
use futures::{Async, Poll};
|
|
|
|
|
|
|
|
use crate::guard::Guard;
|
|
|
|
use crate::resource::Resource;
|
|
|
|
use crate::route::Route;
|
|
|
|
use crate::service::{ServiceRequest, ServiceResponse};
|
|
|
|
|
2019-03-04 19:47:53 +00:00
|
|
|
type Guards = Vec<Box<Guard>>;
|
2019-03-04 05:02:01 +00:00
|
|
|
type HttpService<P> = BoxedService<ServiceRequest<P>, ServiceResponse, ()>;
|
|
|
|
type HttpNewService<P> = BoxedNewService<(), ServiceRequest<P>, ServiceResponse, (), ()>;
|
|
|
|
type BoxedResponse = Box<Future<Item = ServiceResponse, Error = ()>>;
|
|
|
|
|
|
|
|
/// Resources scope
|
|
|
|
///
|
|
|
|
/// 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() {
|
|
|
|
/// let app = App::new().scope("/{project_id}/", |scope| {
|
|
|
|
/// scope
|
|
|
|
/// .resource("/path1", |r| r.to(|| HttpResponse::Ok()))
|
2019-03-04 21:25:35 +00:00
|
|
|
/// .resource("/path2", |r| r.route(web::get().to(|| HttpResponse::Ok())))
|
|
|
|
/// .resource("/path3", |r| r.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
|
|
|
|
///
|
|
|
|
pub struct Scope<P, T = ScopeEndpoint<P>> {
|
|
|
|
endpoint: T,
|
|
|
|
rdef: ResourceDef,
|
2019-03-04 19:47:53 +00:00
|
|
|
services: Vec<(ResourceDef, HttpNewService<P>, Option<Guards>)>,
|
|
|
|
guards: Vec<Box<Guard>>,
|
2019-03-04 05:02:01 +00:00
|
|
|
default: Rc<RefCell<Option<Rc<HttpNewService<P>>>>>,
|
|
|
|
defaults: Vec<Rc<RefCell<Option<Rc<HttpNewService<P>>>>>>,
|
|
|
|
factory_ref: Rc<RefCell<Option<ScopeFactory<P>>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: 'static> Scope<P> {
|
|
|
|
/// Create a new scope
|
|
|
|
pub fn new(path: &str) -> Scope<P> {
|
|
|
|
let fref = Rc::new(RefCell::new(None));
|
|
|
|
let rdef = ResourceDef::prefix(&insert_slash(path));
|
|
|
|
Scope {
|
|
|
|
endpoint: ScopeEndpoint::new(fref.clone()),
|
|
|
|
rdef: rdef.clone(),
|
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)),
|
|
|
|
defaults: Vec::new(),
|
|
|
|
factory_ref: fref,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: 'static, T> Scope<P, T>
|
|
|
|
where
|
|
|
|
T: NewService<
|
2019-03-05 18:08:08 +00:00
|
|
|
ServiceRequest<P>,
|
2019-03-04 05:02:01 +00:00
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = (),
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn rdef(&self) -> &ResourceDef {
|
|
|
|
&self.rdef
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add guard to a scope.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_web::{web, guard, App, HttpRequest, HttpResponse, extract::Path};
|
|
|
|
///
|
|
|
|
/// fn index(data: Path<(String, String)>) -> &'static str {
|
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new().scope("/app", |scope| {
|
|
|
|
/// scope
|
|
|
|
/// .guard(guard::Header("content-type", "text/plain"))
|
|
|
|
/// .route("/test1",web::get().to(index))
|
|
|
|
/// .route("/test2", web::post().to(|r: HttpRequest| {
|
|
|
|
/// HttpResponse::MethodNotAllowed()
|
|
|
|
/// }))
|
|
|
|
/// });
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create nested scope.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_web::{App, HttpRequest};
|
|
|
|
///
|
|
|
|
/// struct AppState;
|
|
|
|
///
|
|
|
|
/// fn index(req: HttpRequest) -> &'static str {
|
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new().scope("/app", |scope| {
|
|
|
|
/// scope.nested("/v1", |scope| scope.resource("/test1", |r| r.to(index)))
|
|
|
|
/// });
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn nested<F>(mut self, path: &str, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: FnOnce(Scope<P>) -> Scope<P>,
|
|
|
|
{
|
2019-03-04 19:47:53 +00:00
|
|
|
let mut scope = f(Scope::new(path));
|
2019-03-04 05:02:01 +00:00
|
|
|
let rdef = scope.rdef().clone();
|
2019-03-04 19:47:53 +00:00
|
|
|
let guards = scope.take_guards();
|
2019-03-04 05:02:01 +00:00
|
|
|
self.defaults.push(scope.get_default());
|
|
|
|
self.services
|
2019-03-04 19:47:53 +00:00
|
|
|
.push((rdef, boxed::new_service(scope.into_new_service()), guards));
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configure route for a specific path.
|
|
|
|
///
|
|
|
|
/// This is a simplified version of the `Scope::resource()` method.
|
|
|
|
/// This method can not be could multiple times, in that case
|
|
|
|
/// multiple resources with one route would be registered for same resource path.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_web::{web, App, HttpResponse, extract::Path};
|
|
|
|
///
|
|
|
|
/// fn index(data: Path<(String, String)>) -> &'static str {
|
|
|
|
/// "Welcome!"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new().scope("/app", |scope| {
|
|
|
|
/// scope.route("/test1", web::get().to(index))
|
|
|
|
/// .route("/test2", web::post().to(|| HttpResponse::MethodNotAllowed()))
|
|
|
|
/// });
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn route(self, path: &str, route: Route<P>) -> Self {
|
|
|
|
self.resource(path, move |r| r.route(route))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// configure resource for a specific path.
|
|
|
|
///
|
|
|
|
/// This method is similar to an `App::resource()` method.
|
|
|
|
/// Resources may have variable path segments. Resource path uses scope
|
|
|
|
/// path as a path prefix.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_web::*;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new().scope("/api", |scope| {
|
|
|
|
/// scope.resource("/users/{userid}/{friend}", |r| {
|
|
|
|
/// r.route(web::get().to(|| HttpResponse::Ok()))
|
|
|
|
/// .route(web::head().to(|| HttpResponse::MethodNotAllowed()))
|
|
|
|
/// .route(web::route()
|
|
|
|
/// .guard(guard::Any(guard::Get()).or(guard::Put()))
|
|
|
|
/// .guard(guard::Header("Content-Type", "text/plain"))
|
|
|
|
/// .to(|| HttpResponse::Ok()))
|
|
|
|
/// })
|
|
|
|
/// });
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn resource<F, U>(mut self, path: &str, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: FnOnce(Resource<P>) -> Resource<P, U>,
|
|
|
|
U: NewService<
|
2019-03-05 18:08:08 +00:00
|
|
|
ServiceRequest<P>,
|
2019-03-04 05:02:01 +00:00
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = (),
|
|
|
|
InitError = (),
|
|
|
|
> + 'static,
|
|
|
|
{
|
|
|
|
// add resource
|
|
|
|
let rdef = ResourceDef::new(&insert_slash(path));
|
|
|
|
let resource = f(Resource::new());
|
|
|
|
self.defaults.push(resource.get_default());
|
2019-03-04 19:47:53 +00:00
|
|
|
self.services.push((
|
|
|
|
rdef,
|
|
|
|
boxed::new_service(resource.into_new_service()),
|
|
|
|
None,
|
|
|
|
));
|
2019-03-04 05:02:01 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Default resource to be used if no matching route could be found.
|
|
|
|
pub fn default_resource<F, U>(mut self, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: FnOnce(Resource<P>) -> Resource<P, U>,
|
|
|
|
U: NewService<
|
2019-03-05 18:08:08 +00:00
|
|
|
ServiceRequest<P>,
|
2019-03-04 05:02:01 +00:00
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = (),
|
|
|
|
InitError = (),
|
|
|
|
> + 'static,
|
|
|
|
{
|
|
|
|
// create and configure default resource
|
|
|
|
self.default = Rc::new(RefCell::new(Some(Rc::new(boxed::new_service(
|
|
|
|
f(Resource::new()).into_new_service().map_init_err(|_| ()),
|
|
|
|
)))));
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register a scope middleware
|
|
|
|
///
|
|
|
|
/// This is similar to `App's` middlewares, but
|
|
|
|
/// middleware is not allowed to change response type (i.e modify response's body).
|
|
|
|
/// Middleware get invoked on scope level.
|
|
|
|
pub fn middleware<M, F>(
|
|
|
|
self,
|
|
|
|
mw: F,
|
|
|
|
) -> Scope<
|
|
|
|
P,
|
|
|
|
impl NewService<
|
2019-03-05 18:08:08 +00:00
|
|
|
ServiceRequest<P>,
|
2019-03-04 05:02:01 +00:00
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = (),
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
>
|
|
|
|
where
|
2019-03-05 05:37:57 +00:00
|
|
|
M: Transform<
|
2019-03-04 05:02:01 +00:00
|
|
|
T::Service,
|
2019-03-05 18:08:08 +00:00
|
|
|
ServiceRequest<P>,
|
2019-03-04 05:02:01 +00:00
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = (),
|
|
|
|
InitError = (),
|
|
|
|
>,
|
2019-03-05 18:08:08 +00:00
|
|
|
F: IntoTransform<M, T::Service, ServiceRequest<P>>,
|
2019-03-04 05:02:01 +00:00
|
|
|
{
|
2019-03-05 05:37:57 +00:00
|
|
|
let endpoint = ApplyTransform::new(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,
|
|
|
|
defaults: self.defaults,
|
|
|
|
factory_ref: self.factory_ref,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn get_default(&self) -> Rc<RefCell<Option<Rc<HttpNewService<P>>>>> {
|
|
|
|
self.default.clone()
|
|
|
|
}
|
2019-03-04 19:47:53 +00:00
|
|
|
|
|
|
|
pub(crate) fn take_guards(&mut self) -> Option<Vec<Box<Guard>>> {
|
|
|
|
if self.guards.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(std::mem::replace(&mut self.guards, Vec::new()))
|
|
|
|
}
|
|
|
|
}
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 05:40:03 +00:00
|
|
|
pub(crate) fn insert_slash(path: &str) -> String {
|
2019-03-04 05:02:01 +00:00
|
|
|
let mut path = path.to_owned();
|
|
|
|
if !path.is_empty() && !path.starts_with('/') {
|
|
|
|
path.insert(0, '/');
|
|
|
|
};
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2019-03-05 18:08:08 +00:00
|
|
|
impl<P, T> IntoNewService<T, ServiceRequest<P>> for Scope<P, T>
|
2019-03-04 05:02:01 +00:00
|
|
|
where
|
|
|
|
T: NewService<
|
2019-03-05 18:08:08 +00:00
|
|
|
ServiceRequest<P>,
|
2019-03-04 05:02:01 +00:00
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = (),
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
|
|
|
fn into_new_service(self) -> T {
|
|
|
|
// update resource default service
|
|
|
|
if let Some(ref d) = *self.default.as_ref().borrow() {
|
|
|
|
for default in &self.defaults {
|
|
|
|
if default.borrow_mut().is_none() {
|
|
|
|
*default.borrow_mut() = Some(d.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*self.factory_ref.borrow_mut() = Some(ScopeFactory {
|
|
|
|
default: self.default.clone(),
|
2019-03-04 19:47:53 +00:00
|
|
|
services: Rc::new(
|
|
|
|
self.services
|
|
|
|
.into_iter()
|
|
|
|
.map(|(rdef, srv, guards)| (rdef, srv, RefCell::new(guards)))
|
|
|
|
.collect(),
|
|
|
|
),
|
2019-03-04 05:02:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
self.endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ScopeFactory<P> {
|
2019-03-04 19:47:53 +00:00
|
|
|
services: Rc<Vec<(ResourceDef, HttpNewService<P>, RefCell<Option<Guards>>)>>,
|
2019-03-04 05:02:01 +00:00
|
|
|
default: Rc<RefCell<Option<Rc<HttpNewService<P>>>>>,
|
|
|
|
}
|
|
|
|
|
2019-03-05 18:08:08 +00:00
|
|
|
impl<P: 'static> NewService<ServiceRequest<P>> for ScopeFactory<P> {
|
2019-03-04 05:02:01 +00:00
|
|
|
type Response = ServiceResponse;
|
|
|
|
type Error = ();
|
|
|
|
type InitError = ();
|
|
|
|
type Service = ScopeService<P>;
|
|
|
|
type Future = ScopeFactoryResponse<P>;
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create app service
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct ScopeFactoryResponse<P> {
|
|
|
|
fut: Vec<CreateScopeServiceItem<P>>,
|
|
|
|
default: Option<HttpService<P>>,
|
|
|
|
default_fut: Option<Box<Future<Item = HttpService<P>, Error = ()>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
type HttpServiceFut<P> = Box<Future<Item = HttpService<P>, Error = ()>>;
|
|
|
|
|
|
|
|
enum CreateScopeServiceItem<P> {
|
2019-03-04 19:47:53 +00:00
|
|
|
Future(Option<ResourceDef>, Option<Guards>, HttpServiceFut<P>),
|
|
|
|
Service(ResourceDef, Option<Guards>, HttpService<P>),
|
2019-03-04 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<P> Future for ScopeFactoryResponse<P> {
|
|
|
|
type Item = ScopeService<P>;
|
|
|
|
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) => {
|
|
|
|
router.rdef(path, service);
|
|
|
|
router.set_user_data(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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ScopeService<P> {
|
2019-03-04 19:47:53 +00:00
|
|
|
router: Router<HttpService<P>, Vec<Box<Guard>>>,
|
2019-03-04 05:02:01 +00:00
|
|
|
default: Option<HttpService<P>>,
|
|
|
|
_ready: Option<(ServiceRequest<P>, ResourceInfo)>,
|
|
|
|
}
|
|
|
|
|
2019-03-05 18:08:08 +00:00
|
|
|
impl<P> Service<ServiceRequest<P>> for ScopeService<P> {
|
2019-03-04 05:02:01 +00:00
|
|
|
type Response = ServiceResponse;
|
|
|
|
type Error = ();
|
|
|
|
type Future = Either<BoxedResponse, FutureResult<Self::Response, Self::Error>>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, mut req: ServiceRequest<P>) -> 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 {
|
|
|
|
let req = req.into_request();
|
|
|
|
Either::B(ok(ServiceResponse::new(req, Response::NotFound().finish())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct ScopeEndpoint<P> {
|
|
|
|
factory: Rc<RefCell<Option<ScopeFactory<P>>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P> ScopeEndpoint<P> {
|
|
|
|
fn new(factory: Rc<RefCell<Option<ScopeFactory<P>>>>) -> Self {
|
|
|
|
ScopeEndpoint { factory }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 18:08:08 +00:00
|
|
|
impl<P: 'static> NewService<ServiceRequest<P>> for ScopeEndpoint<P> {
|
2019-03-04 05:02:01 +00:00
|
|
|
type Response = ServiceResponse;
|
|
|
|
type Error = ();
|
|
|
|
type InitError = ();
|
|
|
|
type Service = ScopeService<P>;
|
|
|
|
type Future = ScopeFactoryResponse<P>;
|
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
self.factory.borrow_mut().as_mut().unwrap().new_service(&())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use actix_http::body::{Body, ResponseBody};
|
|
|
|
use actix_http::http::{Method, StatusCode};
|
|
|
|
use actix_service::{IntoNewService, NewService, Service};
|
|
|
|
use bytes::Bytes;
|
|
|
|
|
2019-03-06 03:03:59 +00:00
|
|
|
use crate::test::{self, block_on, TestRequest};
|
2019-03-04 19:47:53 +00:00
|
|
|
use crate::{guard, web, App, HttpRequest, HttpResponse};
|
2019-03-04 05:02:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_scope() {
|
2019-03-06 03:03:59 +00:00
|
|
|
let mut srv = test::init_service(App::new().scope("/app", |scope| {
|
|
|
|
scope.resource("/path1", |r| r.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 03:03:59 +00:00
|
|
|
let mut srv = test::init_service(App::new().scope("/app", |scope| {
|
|
|
|
scope
|
|
|
|
.resource("", |r| r.to(|| HttpResponse::Ok()))
|
|
|
|
.resource("/", |r| r.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 03:03:59 +00:00
|
|
|
let mut srv = test::init_service(App::new().scope("/app/", |scope| {
|
|
|
|
scope.resource("", |r| r.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() {
|
|
|
|
let app = App::new()
|
|
|
|
.scope("/app/", |scope| {
|
|
|
|
scope.resource("/", |r| r.to(|| HttpResponse::Ok()))
|
|
|
|
})
|
|
|
|
.into_new_service();
|
2019-03-04 21:25:35 +00:00
|
|
|
let mut srv = block_on(app.new_service(&())).unwrap();
|
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() {
|
|
|
|
let app = App::new()
|
|
|
|
.scope("app", |scope| {
|
|
|
|
scope.resource("/path1", |r| {
|
|
|
|
r.route(web::get().to(|| HttpResponse::Ok()))
|
|
|
|
.route(web::delete().to(|| HttpResponse::Ok()))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.into_new_service();
|
2019-03-04 21:25:35 +00:00
|
|
|
let mut srv = block_on(app.new_service(&())).unwrap();
|
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() {
|
|
|
|
let app = App::new()
|
|
|
|
.scope("app", |scope| {
|
|
|
|
scope.resource("path1", |r| {
|
|
|
|
r.route(web::get().to(|| HttpResponse::Ok()))
|
|
|
|
.route(web::delete().to(|| HttpResponse::Ok()))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.into_new_service();
|
2019-03-04 21:25:35 +00:00
|
|
|
let mut srv = block_on(app.new_service(&())).unwrap();
|
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);
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:47:53 +00:00
|
|
|
#[test]
|
|
|
|
fn test_scope_guard() {
|
|
|
|
let app = App::new()
|
|
|
|
.scope("/app", |scope| {
|
|
|
|
scope
|
|
|
|
.guard(guard::Get())
|
|
|
|
.resource("/path1", |r| r.to(|| HttpResponse::Ok()))
|
|
|
|
})
|
|
|
|
.into_new_service();
|
2019-03-04 21:25:35 +00:00
|
|
|
let mut srv = block_on(app.new_service(&())).unwrap();
|
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() {
|
|
|
|
let app = App::new()
|
|
|
|
.scope("/ab-{project}", |scope| {
|
|
|
|
scope.resource("/path1", |r| {
|
|
|
|
r.to(|r: HttpRequest| {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.body(format!("project: {}", &r.match_info()["project"]))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.into_new_service();
|
2019-03-04 21:25:35 +00:00
|
|
|
let mut srv = block_on(app.new_service(&())).unwrap();
|
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);
|
|
|
|
|
|
|
|
match resp.body() {
|
|
|
|
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() {
|
|
|
|
let app = App::new()
|
|
|
|
.scope("/app", |scope| {
|
|
|
|
scope.nested("/t1", |scope| {
|
|
|
|
scope.resource("/path1", |r| r.to(|| HttpResponse::Created()))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.into_new_service();
|
2019-03-04 21:25:35 +00:00
|
|
|
let mut srv = block_on(app.new_service(&())).unwrap();
|
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() {
|
|
|
|
let app = App::new()
|
|
|
|
.scope("/app", |scope| {
|
|
|
|
scope.nested("t1", |scope| {
|
|
|
|
scope.resource("/path1", |r| r.to(|| HttpResponse::Created()))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.into_new_service();
|
2019-03-04 21:25:35 +00:00
|
|
|
let mut srv = block_on(app.new_service(&())).unwrap();
|
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() {
|
|
|
|
let app = App::new()
|
|
|
|
.scope("/app", |scope| {
|
|
|
|
scope.nested("/t1", |scope| {
|
|
|
|
scope
|
|
|
|
.resource("", |r| r.to(|| HttpResponse::Ok()))
|
|
|
|
.resource("/", |r| r.to(|| HttpResponse::Created()))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.into_new_service();
|
2019-03-04 21:25:35 +00:00
|
|
|
let mut srv = block_on(app.new_service(&())).unwrap();
|
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() {
|
|
|
|
let app = App::new()
|
|
|
|
.scope("/app", |scope| {
|
|
|
|
scope.nested("/t1", |scope| {
|
|
|
|
scope
|
|
|
|
.guard(guard::Get())
|
|
|
|
.resource("/path1", |r| r.to(|| HttpResponse::Ok()))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.into_new_service();
|
2019-03-04 21:25:35 +00:00
|
|
|
let mut srv = block_on(app.new_service(&())).unwrap();
|
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() {
|
|
|
|
let app = App::new()
|
|
|
|
.scope("/app", |scope| {
|
|
|
|
scope.nested("/{project_id}", |scope| {
|
|
|
|
scope.resource("/path1", |r| {
|
|
|
|
r.to(|r: HttpRequest| {
|
|
|
|
HttpResponse::Created().body(format!(
|
|
|
|
"project: {}",
|
|
|
|
&r.match_info()["project_id"]
|
|
|
|
))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.into_new_service();
|
2019-03-04 21:25:35 +00:00
|
|
|
let mut srv = block_on(app.new_service(&())).unwrap();
|
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);
|
|
|
|
|
|
|
|
match resp.body() {
|
|
|
|
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() {
|
|
|
|
let app = App::new()
|
|
|
|
.scope("/app", |scope| {
|
|
|
|
scope.nested("/{project}", |scope| {
|
|
|
|
scope.nested("/{id}", |scope| {
|
|
|
|
scope.resource("/path1", |r| {
|
|
|
|
r.to(|r: HttpRequest| {
|
|
|
|
HttpResponse::Created().body(format!(
|
|
|
|
"project: {} - {}",
|
|
|
|
&r.match_info()["project"],
|
|
|
|
&r.match_info()["id"],
|
|
|
|
))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.into_new_service();
|
2019-03-04 21:25:35 +00:00
|
|
|
let mut srv = block_on(app.new_service(&())).unwrap();
|
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);
|
|
|
|
|
|
|
|
match resp.body() {
|
|
|
|
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() {
|
|
|
|
let app = App::new()
|
|
|
|
.scope("/app", |scope| {
|
|
|
|
scope
|
|
|
|
.resource("/path1", |r| r.to(|| HttpResponse::Ok()))
|
|
|
|
.default_resource(|r| r.to(|| HttpResponse::BadRequest()))
|
|
|
|
})
|
|
|
|
.into_new_service();
|
2019-03-04 21:25:35 +00:00
|
|
|
let mut srv = block_on(app.new_service(&())).unwrap();
|
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() {
|
|
|
|
let app = App::new()
|
|
|
|
.scope("/app1", |scope| {
|
|
|
|
scope.default_resource(|r| r.to(|| HttpResponse::BadRequest()))
|
|
|
|
})
|
|
|
|
.scope("/app2", |scope| scope)
|
|
|
|
.default_resource(|r| r.to(|| HttpResponse::MethodNotAllowed()))
|
|
|
|
.into_new_service();
|
2019-03-04 21:25:35 +00:00
|
|
|
let mut srv = block_on(app.new_service(&())).unwrap();
|
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);
|
|
|
|
}
|
|
|
|
}
|