mirror of
https://github.com/fafhrd91/actix-web
synced 2025-08-31 17:07:01 +02:00
Adding app_data to ServiceConfig (#1758)
Co-authored-by: Rob Ede <robjtede@icloud.com> Co-authored-by: Augusto <augusto@flowciety.de>
This commit is contained in:
committed by
GitHub
parent
20078fe603
commit
7030bf5fe8
@@ -178,6 +178,7 @@ pub struct ServiceConfig {
|
||||
pub(crate) services: Vec<Box<dyn AppServiceFactory>>,
|
||||
pub(crate) data: Vec<Box<dyn DataFactory>>,
|
||||
pub(crate) external: Vec<ResourceDef>,
|
||||
pub(crate) extensions: Extensions,
|
||||
}
|
||||
|
||||
impl ServiceConfig {
|
||||
@@ -186,6 +187,7 @@ impl ServiceConfig {
|
||||
services: Vec::new(),
|
||||
data: Vec::new(),
|
||||
external: Vec::new(),
|
||||
extensions: Extensions::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,6 +200,14 @@ impl ServiceConfig {
|
||||
self
|
||||
}
|
||||
|
||||
/// Set arbitrary data item.
|
||||
///
|
||||
/// This is same as `App::data()` method.
|
||||
pub fn app_data<U: 'static>(&mut self, ext: U) -> &mut Self {
|
||||
self.extensions.insert(ext);
|
||||
self
|
||||
}
|
||||
|
||||
/// Configure route for a specific path.
|
||||
///
|
||||
/// This is same as `App::route()` method.
|
||||
@@ -254,13 +264,16 @@ mod tests {
|
||||
async fn test_data() {
|
||||
let cfg = |cfg: &mut ServiceConfig| {
|
||||
cfg.data(10usize);
|
||||
cfg.app_data(15u8);
|
||||
};
|
||||
|
||||
let mut srv =
|
||||
init_service(App::new().configure(cfg).service(
|
||||
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
||||
))
|
||||
.await;
|
||||
let mut srv = init_service(App::new().configure(cfg).service(
|
||||
web::resource("/").to(|_: web::Data<usize>, req: HttpRequest| {
|
||||
assert_eq!(*req.app_data::<u8>().unwrap(), 15u8);
|
||||
HttpResponse::Ok()
|
||||
}),
|
||||
))
|
||||
.await;
|
||||
let req = TestRequest::default().to_request();
|
||||
let resp = srv.call(req).await.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
Reference in New Issue
Block a user