2021-12-07 17:26:28 +01:00
|
|
|
use std::{cell::RefCell, fmt, future::Future, marker::PhantomData, mem, rc::Rc};
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2021-12-22 16:48:59 +01:00
|
|
|
use actix_http::{
|
|
|
|
body::{BoxBody, MessageBody},
|
|
|
|
Extensions,
|
|
|
|
};
|
2021-01-09 19:06:49 +01:00
|
|
|
use actix_router::{ResourceDef, Router};
|
2019-03-04 06:02:01 +01:00
|
|
|
use actix_service::{
|
2021-12-04 20:40:47 +01:00
|
|
|
apply, apply_fn_factory, boxed, IntoServiceFactory, Service, ServiceFactory,
|
|
|
|
ServiceFactoryExt, Transform,
|
2019-03-04 06:02:01 +01:00
|
|
|
};
|
2021-01-02 20:40:31 +01:00
|
|
|
use futures_core::future::LocalBoxFuture;
|
2021-01-09 19:06:49 +01:00
|
|
|
use futures_util::future::join_all;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2021-06-24 16:10:51 +02:00
|
|
|
use crate::{
|
|
|
|
config::ServiceConfig,
|
|
|
|
data::Data,
|
2021-12-04 20:40:47 +01:00
|
|
|
dev::AppService,
|
2021-06-24 16:10:51 +02:00
|
|
|
guard::Guard,
|
|
|
|
rmap::ResourceMap,
|
2021-12-04 20:40:47 +01:00
|
|
|
service::{
|
|
|
|
AppServiceFactory, BoxedHttpService, BoxedHttpServiceFactory, HttpServiceFactory,
|
|
|
|
ServiceFactoryWrapper, ServiceRequest, ServiceResponse,
|
|
|
|
},
|
2021-06-24 16:10:51 +02:00
|
|
|
Error, Resource, Route,
|
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-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.
|
|
|
|
///
|
2021-03-25 09:45:52 +01:00
|
|
|
/// ```
|
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}/")
|
2021-09-01 10:53:26 +02:00
|
|
|
/// .service(web::resource("/path1").to(|| async { "OK" }))
|
2019-03-07 00:47:15 +01:00
|
|
|
/// .service(web::resource("/path2").route(web::get().to(|| HttpResponse::Ok())))
|
2021-09-01 10:53:26 +02:00
|
|
|
/// .service(web::resource("/path3").route(web::head().to(HttpResponse::MethodNotAllowed)))
|
2019-03-07 00:47:15 +01:00
|
|
|
/// );
|
2019-03-04 06:02:01 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// In the above example three routes get registered:
|
2020-04-14 03:33:19 +02:00
|
|
|
/// * /{project_id}/path1 - responds to all http method
|
2019-03-04 06:02:01 +01:00
|
|
|
/// * /{project_id}/path2 - `GET` requests
|
|
|
|
/// * /{project_id}/path3 - `HEAD` requests
|
2021-12-07 17:26:28 +01:00
|
|
|
pub struct Scope<T = ScopeEndpoint, B = BoxBody> {
|
2019-03-04 06:02:01 +01:00
|
|
|
endpoint: T,
|
2019-03-07 00:47:15 +01:00
|
|
|
rdef: String,
|
2021-01-09 19:06:49 +01:00
|
|
|
app_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>>,
|
2021-12-04 20:40:47 +01:00
|
|
|
default: Option<Rc<BoxedHttpServiceFactory>>,
|
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>>>,
|
2021-12-07 17:26:28 +01:00
|
|
|
_phantom: PhantomData<B>,
|
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 {
|
2021-06-24 16:10:51 +02:00
|
|
|
let factory_ref = Rc::new(RefCell::new(None));
|
|
|
|
|
2019-03-04 06:02:01 +01:00
|
|
|
Scope {
|
2021-06-24 16:10:51 +02:00
|
|
|
endpoint: ScopeEndpoint::new(Rc::clone(&factory_ref)),
|
2019-03-07 00:47:15 +01:00
|
|
|
rdef: path.to_string(),
|
2021-01-09 19:06:49 +01:00
|
|
|
app_data: None,
|
2019-03-04 20:47:53 +01:00
|
|
|
guards: Vec::new(),
|
2019-03-04 06:02:01 +01:00
|
|
|
services: Vec::new(),
|
2021-01-09 19:06:49 +01:00
|
|
|
default: None,
|
2019-06-03 19:41:32 +02:00
|
|
|
external: Vec::new(),
|
2021-06-24 16:10:51 +02:00
|
|
|
factory_ref,
|
2021-12-07 17:26:28 +01:00
|
|
|
_phantom: Default::default(),
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 17:26:28 +01:00
|
|
|
impl<T, B> Scope<T, B>
|
2019-03-04 06:02:01 +01:00
|
|
|
where
|
2019-11-20 18:33:22 +01:00
|
|
|
T: ServiceFactory<
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceRequest,
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2021-12-07 17:26:28 +01:00
|
|
|
Response = ServiceResponse<B>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-04 06:02:01 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
2021-12-07 17:26:28 +01:00
|
|
|
B: 'static,
|
2019-03-04 06:02:01 +01:00
|
|
|
{
|
2019-03-07 00:47:15 +01:00
|
|
|
/// Add match guard to a scope.
|
2019-03-04 06:02:01 +01:00
|
|
|
///
|
2021-03-25 09:45:52 +01:00
|
|
|
/// ```
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-24 16:10:51 +02:00
|
|
|
/// Add scope data.
|
2019-05-12 18:42:05 +02:00
|
|
|
///
|
2021-06-24 16:10:51 +02:00
|
|
|
/// Data of different types from parent contexts will still be accessible. Any `Data<T>` types
|
|
|
|
/// set here can be extracted in handlers using the `Data<T>` extractor.
|
|
|
|
///
|
|
|
|
/// # Examples
|
2021-03-25 09:45:52 +01:00
|
|
|
/// ```
|
2019-05-12 18:42:05 +02:00
|
|
|
/// use std::cell::Cell;
|
2021-06-24 16:10:51 +02:00
|
|
|
/// use actix_web::{web, App, HttpRequest, HttpResponse, Responder};
|
2019-05-12 18:42:05 +02:00
|
|
|
///
|
|
|
|
/// struct MyData {
|
2021-06-24 16:10:51 +02:00
|
|
|
/// count: std::cell::Cell<usize>,
|
2019-05-12 18:42:05 +02:00
|
|
|
/// }
|
|
|
|
///
|
2021-06-24 16:10:51 +02:00
|
|
|
/// async fn handler(req: HttpRequest, counter: web::Data<MyData>) -> impl Responder {
|
|
|
|
/// // note this cannot use the Data<T> extractor because it was not added with it
|
|
|
|
/// let incr = *req.app_data::<usize>().unwrap();
|
|
|
|
/// assert_eq!(incr, 3);
|
2019-05-12 18:42:05 +02:00
|
|
|
///
|
2021-06-24 16:10:51 +02:00
|
|
|
/// // update counter using other value from app data
|
|
|
|
/// counter.count.set(counter.count.get() + incr);
|
|
|
|
///
|
|
|
|
/// HttpResponse::Ok().body(counter.count.get().to_string())
|
2019-05-12 18:42:05 +02:00
|
|
|
/// }
|
2019-09-18 02:36:39 +02:00
|
|
|
///
|
2021-06-24 16:10:51 +02:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::scope("/app")
|
|
|
|
/// .app_data(3usize)
|
|
|
|
/// .app_data(web::Data::new(MyData { count: Default::default() }))
|
|
|
|
/// .route("/", web::get().to(handler))
|
|
|
|
/// );
|
|
|
|
/// ```
|
2019-12-20 12:13:09 +01:00
|
|
|
pub fn app_data<U: 'static>(mut self, data: U) -> Self {
|
2021-01-16 00:37:33 +01:00
|
|
|
self.app_data
|
|
|
|
.get_or_insert_with(Extensions::new)
|
|
|
|
.insert(data);
|
|
|
|
|
2019-05-12 18:42:05 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-06-24 16:10:51 +02:00
|
|
|
/// Add scope data after wrapping in `Data<T>`.
|
|
|
|
///
|
|
|
|
/// Deprecated in favor of [`app_data`](Self::app_data).
|
|
|
|
#[deprecated(since = "4.0.0", note = "Use `.app_data(Data::new(val))` instead.")]
|
|
|
|
pub fn data<U: 'static>(self, data: U) -> Self {
|
|
|
|
self.app_data(Data::new(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Run external configuration as part of the scope building process.
|
2019-06-03 19:41:32 +02:00
|
|
|
///
|
2021-06-24 16:10:51 +02:00
|
|
|
/// This function is useful for moving parts of configuration to a different module or library.
|
|
|
|
/// For example, some of the resource's configuration could be moved to different module.
|
2019-06-03 19:41:32 +02:00
|
|
|
///
|
2021-03-25 09:45:52 +01:00
|
|
|
/// ```
|
2019-06-03 19:41:32 +02:00
|
|
|
/// 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()));
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-06-24 16:10:51 +02:00
|
|
|
pub fn configure<F>(mut self, cfg_fn: F) -> Self
|
2019-06-03 19:41:32 +02:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut ServiceConfig),
|
|
|
|
{
|
|
|
|
let mut cfg = ServiceConfig::new();
|
2021-06-24 16:10:51 +02:00
|
|
|
cfg_fn(&mut cfg);
|
|
|
|
|
2019-06-03 19:41:32 +02:00
|
|
|
self.services.extend(cfg.services);
|
|
|
|
self.external.extend(cfg.external);
|
|
|
|
|
2021-06-24 16:10:51 +02:00
|
|
|
// TODO: add Extensions::is_empty check and conditionally insert data
|
2021-01-09 19:06:49 +01:00
|
|
|
self.app_data
|
2020-10-26 18:02:45 +01:00
|
|
|
.get_or_insert_with(Extensions::new)
|
2021-01-16 00:37:33 +01:00
|
|
|
.extend(cfg.app_data);
|
2021-06-24 16:10:51 +02:00
|
|
|
|
2019-06-03 19:41:32 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-02-10 13:12:03 +01:00
|
|
|
/// Register HTTP service.
|
2019-03-17 05:35:02 +01:00
|
|
|
///
|
|
|
|
/// This is similar to `App's` service registration.
|
|
|
|
///
|
2021-02-10 13:12:03 +01:00
|
|
|
/// Actix Web provides several services implementations:
|
2019-03-17 05:35:02 +01:00
|
|
|
///
|
|
|
|
/// * *Resource* is an entry in resource table which corresponds to requested URL.
|
|
|
|
/// * *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
|
|
|
///
|
2021-03-25 09:45:52 +01:00
|
|
|
/// ```
|
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.
|
|
|
|
///
|
2021-03-25 09:45:52 +01:00
|
|
|
/// ```
|
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
|
2021-01-04 00:47:04 +01:00
|
|
|
F: IntoServiceFactory<U, ServiceRequest>,
|
2019-11-20 18:33:22 +01:00
|
|
|
U: ServiceFactory<
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceRequest,
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2019-03-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
|
2021-01-09 19:06:49 +01:00
|
|
|
self.default = Some(Rc::new(boxed::factory(f.into_factory().map_init_err(
|
|
|
|
|e| log::error!("Can not construct default service: {:?}", e),
|
|
|
|
))));
|
2019-03-04 06:02:01 +01:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-12-07 17:26:28 +01:00
|
|
|
/// Registers middleware, in the form of a middleware component (type), that runs during inbound
|
|
|
|
/// processing in the request life-cycle (request -> response), modifying request as necessary,
|
|
|
|
/// across all requests managed by the *Scope*.
|
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.
|
2021-12-07 17:26:28 +01:00
|
|
|
pub fn wrap<M, B1>(
|
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<
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceRequest,
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2021-12-07 17:26:28 +01:00
|
|
|
Response = ServiceResponse<B1>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-04 06:02:01 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
2021-12-07 17:26:28 +01:00
|
|
|
B1,
|
2019-03-04 06:02:01 +01:00
|
|
|
>
|
|
|
|
where
|
2019-03-05 06:37:57 +01:00
|
|
|
M: Transform<
|
2019-03-04 06:02:01 +01:00
|
|
|
T::Service,
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceRequest,
|
2021-12-07 17:26:28 +01:00
|
|
|
Response = ServiceResponse<B1>,
|
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,
|
2021-01-09 19:06:49 +01:00
|
|
|
app_data: self.app_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,
|
2021-12-07 17:26:28 +01:00
|
|
|
_phantom: PhantomData,
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-25 21:43:02 +01:00
|
|
|
|
2021-12-07 17:26:28 +01:00
|
|
|
/// Registers middleware, in the form of a closure, that runs during inbound processing in the
|
|
|
|
/// request life-cycle (request -> response), modifying request as necessary, across all
|
|
|
|
/// requests managed by the *Scope*.
|
2019-03-25 21:43:02 +01:00
|
|
|
///
|
2021-12-07 17:26:28 +01:00
|
|
|
/// # Examples
|
2021-03-25 09:45:52 +01:00
|
|
|
/// ```
|
2019-03-25 21:43:02 +01:00
|
|
|
/// use actix_service::Service;
|
|
|
|
/// use actix_web::{web, App};
|
2021-12-05 15:37:20 +01:00
|
|
|
/// use actix_web::http::header::{CONTENT_TYPE, HeaderValue};
|
2019-03-25 21:43:02 +01:00
|
|
|
///
|
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)));
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-07 17:26:28 +01:00
|
|
|
pub fn wrap_fn<F, R, B1>(
|
2019-03-25 21:43:02 +01:00
|
|
|
self,
|
|
|
|
mw: F,
|
|
|
|
) -> Scope<
|
2019-11-20 18:33:22 +01:00
|
|
|
impl ServiceFactory<
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceRequest,
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2021-12-07 17:26:28 +01:00
|
|
|
Response = ServiceResponse<B1>,
|
2019-03-25 21:43:02 +01:00
|
|
|
Error = Error,
|
|
|
|
InitError = (),
|
|
|
|
>,
|
2021-12-07 17:26:28 +01:00
|
|
|
B1,
|
2019-03-25 21:43:02 +01:00
|
|
|
>
|
|
|
|
where
|
2021-02-07 02:00:40 +01:00
|
|
|
F: Fn(ServiceRequest, &T::Service) -> R + Clone,
|
2021-12-07 17:26:28 +01:00
|
|
|
R: Future<Output = Result<ServiceResponse<B1>, 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,
|
2021-01-09 19:06:49 +01:00
|
|
|
app_data: self.app_data,
|
2019-11-20 18:33:22 +01:00
|
|
|
guards: self.guards,
|
|
|
|
services: self.services,
|
|
|
|
default: self.default,
|
|
|
|
external: self.external,
|
|
|
|
factory_ref: self.factory_ref,
|
2021-12-07 17:26:28 +01:00
|
|
|
_phantom: PhantomData,
|
2019-11-20 18:33:22 +01:00
|
|
|
}
|
2019-03-25 21:43:02 +01:00
|
|
|
}
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2021-12-22 16:48:59 +01:00
|
|
|
impl<T, B> HttpServiceFactory for Scope<T, B>
|
2019-03-04 06:02:01 +01:00
|
|
|
where
|
2019-11-20 18:33:22 +01:00
|
|
|
T: ServiceFactory<
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceRequest,
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2021-12-22 16:48:59 +01:00
|
|
|
Response = ServiceResponse<B>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-07 00:47:15 +01:00
|
|
|
InitError = (),
|
|
|
|
> + 'static,
|
2021-12-22 16:48:59 +01:00
|
|
|
B: MessageBody + '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
|
2021-01-09 19:06:49 +01:00
|
|
|
let default = self.default.unwrap_or_else(|| 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
|
2021-06-24 16:10:51 +02:00
|
|
|
for mut rdef in mem::take(&mut self.external) {
|
2019-06-03 19:41:32 +02:00
|
|
|
rmap.add(&mut rdef, None);
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-01-09 19:06:49 +01:00
|
|
|
default,
|
2020-10-10 02:26:05 +02:00
|
|
|
services: cfg
|
|
|
|
.into_services()
|
|
|
|
.1
|
|
|
|
.into_iter()
|
|
|
|
.map(|(mut rdef, srv, guards, nested)| {
|
|
|
|
rmap.add(&mut rdef, nested);
|
|
|
|
(rdef, srv, RefCell::new(guards))
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.into_boxed_slice()
|
|
|
|
.into(),
|
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
|
|
|
|
2021-06-25 14:19:42 +02:00
|
|
|
let scope_data = self.app_data.map(Rc::new);
|
|
|
|
|
|
|
|
// wraps endpoint service (including middleware) call and injects app data for this scope
|
|
|
|
let endpoint = apply_fn_factory(self.endpoint, move |mut req: ServiceRequest, srv| {
|
|
|
|
if let Some(ref data) = scope_data {
|
|
|
|
req.add_data_container(Rc::clone(data));
|
|
|
|
}
|
|
|
|
|
2021-12-22 16:48:59 +01:00
|
|
|
let fut = srv.call(req);
|
|
|
|
|
|
|
|
async { Ok(fut.await?.map_into_boxed_body()) }
|
2021-06-25 14:19:42 +02:00
|
|
|
});
|
|
|
|
|
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,
|
2021-06-25 14:19:42 +02:00
|
|
|
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 {
|
2021-12-04 20:40:47 +01:00
|
|
|
services: Rc<
|
|
|
|
[(
|
|
|
|
ResourceDef,
|
|
|
|
BoxedHttpServiceFactory,
|
|
|
|
RefCell<Option<Guards>>,
|
|
|
|
)],
|
|
|
|
>,
|
|
|
|
default: Rc<BoxedHttpServiceFactory>,
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 00:47:04 +01:00
|
|
|
impl ServiceFactory<ServiceRequest> for ScopeFactory {
|
2019-03-04 06:02:01 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2021-01-09 19:06:49 +01:00
|
|
|
type Config = ();
|
2019-04-13 23:50:54 +02:00
|
|
|
type Service = ScopeService;
|
2021-01-09 19:06:49 +01:00
|
|
|
type InitError = ();
|
|
|
|
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2019-12-02 16:37:13 +01:00
|
|
|
fn new_service(&self, _: ()) -> Self::Future {
|
2021-01-09 19:06:49 +01:00
|
|
|
// construct default service factory future
|
|
|
|
let default_fut = self.default.new_service(());
|
|
|
|
|
|
|
|
// construct all services factory future with it's resource def and guards.
|
2021-02-12 00:03:17 +01:00
|
|
|
let factory_fut = join_all(self.services.iter().map(|(path, factory, guards)| {
|
|
|
|
let path = path.clone();
|
|
|
|
let guards = guards.borrow_mut().take();
|
|
|
|
let factory_fut = factory.new_service(());
|
|
|
|
async move {
|
|
|
|
let service = factory_fut.await?;
|
|
|
|
Ok((path, guards, service))
|
|
|
|
}
|
|
|
|
}));
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2021-01-09 19:06:49 +01:00
|
|
|
Box::pin(async move {
|
|
|
|
let default = default_fut.await?;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2021-01-09 19:06:49 +01:00
|
|
|
// build router from the factory future result.
|
|
|
|
let router = factory_fut
|
|
|
|
.await
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Result<Vec<_>, _>>()?
|
2019-03-04 06:02:01 +01:00
|
|
|
.drain(..)
|
2021-01-09 19:06:49 +01:00
|
|
|
.fold(Router::build(), |mut router, (path, guards, service)| {
|
|
|
|
router.rdef(path, service).2 = guards;
|
2019-03-04 06:02:01 +01:00
|
|
|
router
|
2021-01-09 19:06:49 +01:00
|
|
|
})
|
|
|
|
.finish();
|
|
|
|
|
2021-06-25 14:19:42 +02:00
|
|
|
Ok(ScopeService { router, default })
|
2021-01-09 19:06:49 +01:00
|
|
|
})
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct ScopeService {
|
2021-12-04 20:40:47 +01:00
|
|
|
router: Router<BoxedHttpService, Vec<Box<dyn Guard>>>,
|
|
|
|
default: BoxedHttpService,
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 00:47:04 +01:00
|
|
|
impl Service<ServiceRequest> for ScopeService {
|
2019-03-04 06:02:01 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2021-01-02 20:40:31 +01:00
|
|
|
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2021-01-09 19:06:49 +01:00
|
|
|
actix_service::always_ready!();
|
2019-03-04 06:02:01 +01:00
|
|
|
|
2021-02-07 02:00:40 +01:00
|
|
|
fn call(&self, mut req: ServiceRequest) -> Self::Future {
|
2021-08-06 23:42:31 +02:00
|
|
|
let res = self.router.recognize_fn(&mut req, |req, guards| {
|
2019-03-04 20:47:53 +01:00
|
|
|
if let Some(ref guards) = guards {
|
|
|
|
for f in guards {
|
|
|
|
if !f.check(req.head()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some((srv, _info)) = res {
|
2021-01-02 20:40:31 +01:00
|
|
|
srv.call(req)
|
2019-03-04 06:02:01 +01:00
|
|
|
} else {
|
2021-01-09 19:06:49 +01:00
|
|
|
self.default.call(req)
|
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 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 00:47:04 +01:00
|
|
|
impl ServiceFactory<ServiceRequest> for ScopeEndpoint {
|
2019-03-04 06:02:01 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2021-01-04 00:47:04 +01:00
|
|
|
type Config = ();
|
2019-04-13 23:50:54 +02:00
|
|
|
type Service = ScopeService;
|
2021-01-04 00:47:04 +01:00
|
|
|
type InitError = ();
|
2021-01-09 19:06:49 +01:00
|
|
|
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
|
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;
|
2021-04-01 16:26:13 +02:00
|
|
|
use actix_utils::future::ok;
|
2019-03-04 06:02:01 +01:00
|
|
|
use bytes::Bytes;
|
|
|
|
|
2021-12-22 08:58:37 +01:00
|
|
|
use super::*;
|
2021-06-25 14:19:42 +02:00
|
|
|
use crate::{
|
|
|
|
guard,
|
2021-12-05 15:37:20 +01:00
|
|
|
http::{
|
|
|
|
header::{self, HeaderValue},
|
|
|
|
Method, StatusCode,
|
|
|
|
},
|
2021-12-22 08:58:37 +01:00
|
|
|
middleware::{Compat, DefaultHeaders},
|
2021-06-25 14:19:42 +02:00
|
|
|
service::{ServiceRequest, ServiceResponse},
|
2021-12-04 20:40:47 +01:00
|
|
|
test::{assert_body_eq, call_service, init_service, read_body, TestRequest},
|
2021-06-25 14:19:42 +02:00
|
|
|
web, App, HttpMessage, HttpRequest, HttpResponse,
|
|
|
|
};
|
2019-11-26 06:25:50 +01:00
|
|
|
|
2021-12-22 08:58:37 +01:00
|
|
|
#[test]
|
|
|
|
fn can_be_returned_from_fn() {
|
|
|
|
fn my_scope() -> Scope {
|
|
|
|
web::scope("/test")
|
|
|
|
.service(web::resource("").route(web::get().to(|| async { "hello" })))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn my_compat_scope() -> Scope<
|
|
|
|
impl ServiceFactory<
|
|
|
|
ServiceRequest,
|
|
|
|
Config = (),
|
|
|
|
Response = ServiceResponse,
|
|
|
|
Error = Error,
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
> {
|
|
|
|
web::scope("/test-compat")
|
2021-12-22 16:00:32 +01:00
|
|
|
.wrap_fn(|req, srv| {
|
|
|
|
let fut = srv.call(req);
|
|
|
|
async { Ok(fut.await?.map_into_right_body::<()>()) }
|
|
|
|
})
|
|
|
|
.wrap(Compat::noop())
|
2021-12-22 08:58:37 +01:00
|
|
|
.service(web::resource("").route(web::get().to(|| async { "hello" })))
|
|
|
|
}
|
|
|
|
|
|
|
|
App::new().service(my_scope()).service(my_compat_scope());
|
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_scope() {
|
2021-02-12 00:03:17 +01:00
|
|
|
let 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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
2020-07-22 01:28:33 +02:00
|
|
|
.service(web::resource("").to(HttpResponse::Ok))
|
|
|
|
.service(web::resource("/").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").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() {
|
2021-02-12 00:03:17 +01:00
|
|
|
let srv = init_service(
|
|
|
|
App::new()
|
|
|
|
.service(web::scope("/app/").service(web::resource("").to(HttpResponse::Ok))),
|
|
|
|
)
|
2019-11-26 06:25:50 +01:00
|
|
|
.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() {
|
2021-02-12 00:03:17 +01:00
|
|
|
let srv = init_service(
|
|
|
|
App::new()
|
|
|
|
.service(web::scope("/app/").service(web::resource("/").to(HttpResponse::Ok))),
|
|
|
|
)
|
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").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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new().service(
|
|
|
|
web::scope("app")
|
2020-07-22 01:28:33 +02:00
|
|
|
.route("/path1", web::get().to(HttpResponse::Ok))
|
|
|
|
.route("/path1", web::delete().to(HttpResponse::Ok)),
|
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::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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new().service(
|
|
|
|
web::scope("app").service(
|
|
|
|
web::resource("path1")
|
2020-07-22 01:28:33 +02:00
|
|
|
.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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
|
|
|
.guard(guard::Get())
|
2020-07-22 01:28:33 +02:00
|
|
|
.service(web::resource("/path1").to(HttpResponse::Ok)),
|
2019-11-26 06:25:50 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(App::new().service(web::scope("/ab-{project}").service(
|
|
|
|
web::resource("/path1").to(|r: HttpRequest| {
|
2021-02-12 00:03:17 +01:00
|
|
|
HttpResponse::Ok().body(format!("project: {}", &r.match_info()["project"]))
|
2021-02-07 02:00:40 +01:00
|
|
|
}),
|
|
|
|
)))
|
|
|
|
.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();
|
2021-12-04 20:40:47 +01:00
|
|
|
let res = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
assert_body_eq!(res, b"project: project1");
|
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();
|
2021-12-04 20:40:47 +01:00
|
|
|
let res = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(res.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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(App::new().service(web::scope("/app").service(
|
2020-07-22 01:28:33 +02:00
|
|
|
web::scope("/t1").service(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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(App::new().service(web::scope("/app").service(
|
2020-07-22 01:28:33 +02:00
|
|
|
web::scope("t1").service(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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new().service(
|
|
|
|
web::scope("/app").service(
|
|
|
|
web::scope("/t1")
|
2020-07-22 01:28:33 +02:00
|
|
|
.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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new().service(
|
|
|
|
web::scope("/app").service(
|
|
|
|
web::scope("/t1")
|
|
|
|
.guard(guard::Get())
|
2020-07-22 01:28:33 +02:00
|
|
|
.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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(App::new().service(web::scope("/app").service(
|
2019-11-26 06:25:50 +01:00
|
|
|
web::scope("/{project_id}").service(web::resource("/path1").to(
|
2020-09-11 12:29:17 +02:00
|
|
|
|r: HttpRequest| {
|
2020-04-14 03:33:19 +02:00
|
|
|
HttpResponse::Created()
|
|
|
|
.body(format!("project: {}", &r.match_info()["project_id"]))
|
2019-11-26 06:25:50 +01:00
|
|
|
},
|
|
|
|
)),
|
|
|
|
)))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/project_1/path1").to_request();
|
2021-12-04 20:40:47 +01:00
|
|
|
let res = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(res.status(), StatusCode::CREATED);
|
|
|
|
assert_body_eq!(res, b"project: project_1");
|
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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(App::new().service(web::scope("/app").service(
|
2019-11-26 06:25:50 +01:00
|
|
|
web::scope("/{project}").service(web::scope("/{id}").service(
|
2020-09-11 12:29:17 +02:00
|
|
|
web::resource("/path1").to(|r: HttpRequest| {
|
2020-04-14 03:33:19 +02:00
|
|
|
HttpResponse::Created().body(format!(
|
|
|
|
"project: {} - {}",
|
|
|
|
&r.match_info()["project"],
|
|
|
|
&r.match_info()["id"],
|
|
|
|
))
|
2019-11-26 06:25:50 +01:00
|
|
|
}),
|
|
|
|
)),
|
|
|
|
)))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/test/1/path1").to_request();
|
2021-12-04 20:40:47 +01:00
|
|
|
let res = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(res.status(), StatusCode::CREATED);
|
|
|
|
assert_body_eq!(res, b"project: test - 1");
|
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();
|
2021-12-04 20:40:47 +01:00
|
|
|
let res = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(res.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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new().service(
|
|
|
|
web::scope("/app")
|
2020-07-22 01:28:33 +02:00
|
|
|
.service(web::resource("/path1").to(HttpResponse::Ok))
|
2019-11-26 06:25:50 +01:00
|
|
|
.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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new()
|
2021-06-25 14:19:42 +02:00
|
|
|
.service(web::scope("/app1").default_service(web::to(HttpResponse::BadRequest)))
|
2019-11-26 06:25:50 +01:00
|
|
|
.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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2020-07-22 01:28:33 +02:00
|
|
|
App::new().service(
|
|
|
|
web::scope("app")
|
|
|
|
.wrap(
|
2021-02-12 00:03:17 +01:00
|
|
|
DefaultHeaders::new()
|
2021-12-13 17:08:08 +01:00
|
|
|
.add((header::CONTENT_TYPE, HeaderValue::from_static("0001"))),
|
2020-07-22 01:28:33 +02:00
|
|
|
)
|
2021-02-12 00:03:17 +01:00
|
|
|
.service(web::resource("/test").route(web::get().to(HttpResponse::Ok))),
|
2020-07-22 01:28:33 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2019-11-20 18:33:22 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/app/test").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("0001")
|
|
|
|
);
|
2019-03-25 21:02:10 +01:00
|
|
|
}
|
2019-03-25 21:43:02 +01:00
|
|
|
|
2021-12-22 16:48:59 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_middleware_body_type() {
|
|
|
|
// Compile test that Scope accepts any body type; test for `EitherBody`
|
|
|
|
let srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("app")
|
|
|
|
.wrap_fn(|req, srv| {
|
|
|
|
let fut = srv.call(req);
|
|
|
|
async { Ok(fut.await?.map_into_right_body::<()>()) }
|
|
|
|
})
|
|
|
|
.service(web::resource("/test").route(web::get().to(|| async { "hello" }))),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// test if `MessageBody::try_into_bytes()` is preserved across scope layer
|
|
|
|
use actix_http::body::MessageBody as _;
|
|
|
|
let req = TestRequest::with_uri("/app/test").to_request();
|
|
|
|
let resp = call_service(&srv, req).await;
|
|
|
|
let body = resp.into_body();
|
|
|
|
assert_eq!(body.try_into_bytes().unwrap(), b"hello".as_ref());
|
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_middleware_fn() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new().service(
|
|
|
|
web::scope("app")
|
|
|
|
.wrap_fn(|req, srv| {
|
|
|
|
let fut = srv.call(req);
|
|
|
|
async move {
|
|
|
|
let mut res = fut.await?;
|
2021-02-12 00:03:17 +01:00
|
|
|
res.headers_mut()
|
|
|
|
.insert(header::CONTENT_TYPE, HeaderValue::from_static("0001"));
|
2019-11-26 06:25:50 +01:00
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
})
|
2020-07-22 01:28:33 +02:00
|
|
|
.route("/test", web::get().to(HttpResponse::Ok)),
|
2019-11-26 06:25:50 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/test").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("0001")
|
|
|
|
);
|
2019-03-25 21:43:02 +01:00
|
|
|
}
|
2019-05-12 18:42:05 +02:00
|
|
|
|
2021-06-25 14:19:42 +02:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_middleware_app_data() {
|
|
|
|
let srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("app")
|
|
|
|
.app_data(1usize)
|
|
|
|
.wrap_fn(|req, srv| {
|
|
|
|
assert_eq!(req.app_data::<usize>(), Some(&1usize));
|
|
|
|
req.extensions_mut().insert(1usize);
|
|
|
|
srv.call(req)
|
|
|
|
})
|
|
|
|
.route("/test", web::get().to(HttpResponse::Ok))
|
|
|
|
.default_service(|req: ServiceRequest| async move {
|
|
|
|
let (req, _) = req.into_parts();
|
|
|
|
|
|
|
|
assert_eq!(req.extensions().get::<usize>(), Some(&1));
|
|
|
|
|
|
|
|
Ok(ServiceResponse::new(
|
|
|
|
req,
|
|
|
|
HttpResponse::BadRequest().finish(),
|
|
|
|
))
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/test").to_request();
|
|
|
|
let resp = call_service(&srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/default").to_request();
|
|
|
|
let resp = call_service(&srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
2021-06-24 16:10:51 +02:00
|
|
|
// allow deprecated {App, Scope}::data
|
2021-06-22 16:50:58 +02:00
|
|
|
#[allow(deprecated)]
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_override_data() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(App::new().data(1usize).service(
|
2019-11-26 06:25:50 +01:00
|
|
|
web::scope("app").data(10usize).route(
|
|
|
|
"/t",
|
|
|
|
web::get().to(|data: web::Data<usize>| {
|
2019-12-20 12:45:35 +01:00
|
|
|
assert_eq!(**data, 10);
|
2019-11-26 06:25:50 +01:00
|
|
|
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();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-09-18 02:36:39 +02:00
|
|
|
}
|
|
|
|
|
2021-06-24 16:10:51 +02:00
|
|
|
// allow deprecated `{App, Scope}::data`
|
2021-06-22 16:50:58 +02:00
|
|
|
#[allow(deprecated)]
|
2020-04-14 03:33:19 +02:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_override_data_default_service() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(App::new().data(1usize).service(
|
2020-04-14 03:33:19 +02:00
|
|
|
web::scope("app").data(10usize).default_service(web::to(
|
|
|
|
|data: web::Data<usize>| {
|
|
|
|
assert_eq!(**data, 10);
|
|
|
|
HttpResponse::Ok()
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/app/t").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2020-04-14 03:33:19 +02:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
2019-12-20 12:13:09 +01:00
|
|
|
async fn test_override_app_data() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(App::new().app_data(web::Data::new(1usize)).service(
|
2019-12-20 12:13:09 +01:00
|
|
|
web::scope("app").app_data(web::Data::new(10usize)).route(
|
|
|
|
"/t",
|
|
|
|
web::get().to(|data: web::Data<usize>| {
|
2019-12-20 12:45:35 +01:00
|
|
|
assert_eq!(**data, 10);
|
2019-12-20 12:13:09 +01:00
|
|
|
HttpResponse::Ok()
|
|
|
|
}),
|
2019-11-26 06:25:50 +01:00
|
|
|
),
|
2019-12-20 12:13:09 +01:00
|
|
|
))
|
2019-11-26 06:25:50 +01:00
|
|
|
.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();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let 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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let 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;
|
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);
|
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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let 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| {
|
2021-02-12 00:03:17 +01:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.body(req.url_for("youtube", &["xxxxxx"]).unwrap().to_string())
|
2021-02-07 02:00:40 +01:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}));
|
|
|
|
})))
|
|
|
|
.await;
|
2019-11-20 18:33:22 +01:00
|
|
|
|
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() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(App::new().service(web::scope("/a").service(
|
2019-11-26 06:25:50 +01:00
|
|
|
web::scope("/b").service(web::resource("/c/{stuff}").name("c").route(
|
2020-09-11 12:29:17 +02:00
|
|
|
web::get().to(|req: HttpRequest| {
|
2020-04-14 03:33:19 +02:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.body(format!("{}", req.url_for("c", &["12345"]).unwrap()))
|
2019-11-26 06:25:50 +01:00
|
|
|
}),
|
|
|
|
)),
|
|
|
|
)))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/a/b/c/test").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
let 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
|
|
|
}
|
2021-08-30 21:05:49 +02:00
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn dynamic_scopes() {
|
|
|
|
let srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/{a}/").service(
|
|
|
|
web::scope("/{b}/")
|
|
|
|
.route("", web::get().to(|_: HttpRequest| HttpResponse::Created()))
|
|
|
|
.route(
|
|
|
|
"/",
|
|
|
|
web::get().to(|_: HttpRequest| HttpResponse::Accepted()),
|
|
|
|
)
|
|
|
|
.route("/{c}", web::get().to(|_: HttpRequest| HttpResponse::Ok())),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// note the unintuitive behavior with trailing slashes on scopes with dynamic segments
|
|
|
|
let req = TestRequest::with_uri("/a//b//c").to_request();
|
|
|
|
let resp = call_service(&srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/a//b/").to_request();
|
|
|
|
let resp = call_service(&srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/a//b//").to_request();
|
|
|
|
let resp = call_service(&srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::ACCEPTED);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/a//b//c/d").to_request();
|
|
|
|
let resp = call_service(&srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
|
|
|
let srv = init_service(
|
|
|
|
App::new().service(
|
|
|
|
web::scope("/{a}").service(
|
|
|
|
web::scope("/{b}")
|
|
|
|
.route("", web::get().to(|_: HttpRequest| HttpResponse::Created()))
|
|
|
|
.route(
|
|
|
|
"/",
|
|
|
|
web::get().to(|_: HttpRequest| HttpResponse::Accepted()),
|
|
|
|
)
|
|
|
|
.route("/{c}", web::get().to(|_: HttpRequest| HttpResponse::Ok())),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/a/b/c").to_request();
|
|
|
|
let resp = call_service(&srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/a/b").to_request();
|
|
|
|
let resp = call_service(&srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/a/b/").to_request();
|
|
|
|
let resp = call_service(&srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::ACCEPTED);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/a/b/c/d").to_request();
|
|
|
|
let resp = call_service(&srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
}
|
2019-03-04 06:02:01 +01:00
|
|
|
}
|