1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-30 18:34:36 +01:00

Implement register_data method on Resource and Scope. (#1094)

* Implement `register_data` method on `Resource` and `Scope`.

* Split Scope::register_data tests out from Scope::data tests.

* CHANGES.md: Mention {Scope,Resource}::register_data.
This commit is contained in:
Jim Blandy 2019-09-17 17:36:39 -07:00 committed by Nikolay Kim
parent b3783b403e
commit 58c7065f08
3 changed files with 76 additions and 5 deletions

View File

@ -1,10 +1,18 @@
# Changes
## not released yet
### Added
* Add `Scope::register_data` and `Resource::register_data` methods, parallel to
`App::register_data`.
## [1.0.8] - 2019-09-xx
### Added
* Add `middleware::Conditon` that conditionally enables another middleware
* Add `middleware::Condition` that conditionally enables another middleware
### Fixed
* Allow to re-construct `ServiceRequest` from `HttpRequest` and `Payload`

View File

@ -189,11 +189,21 @@ where
/// ));
/// }
/// ```
pub fn data<U: 'static>(mut self, data: U) -> Self {
pub fn data<U: 'static>(self, data: U) -> Self {
self.register_data(Data::new(data))
}
/// Set or override application data.
///
/// This method has the same effect as [`Resource::data`](#method.data),
/// except that instead of taking a value of some type `T`, it expects a
/// value of type `Data<T>`. Use a `Data<T>` extractor to retrieve its
/// value.
pub fn register_data<U: 'static>(mut self, data: Data<U>) -> Self {
if self.data.is_none() {
self.data = Some(Extensions::new());
}
self.data.as_mut().unwrap().insert(Data::new(data));
self.data.as_mut().unwrap().insert(data);
self
}
@ -763,4 +773,30 @@ mod tests {
let resp = call_service(&mut srv, req);
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
}
#[test]
fn test_data() {
let mut srv = init_service(
App::new()
.data(1.0f64)
.data(1usize)
.register_data(web::Data::new('-'))
.service(
web::resource("/test")
.data(10usize)
.register_data(web::Data::new('*'))
.guard(guard::Get())
.to(|data1: web::Data<usize>, data2: web::Data<char>, data3: web::Data<f64>| {
assert_eq!(*data1, 10);
assert_eq!(*data2, '*');
assert_eq!(*data3, 1.0);
HttpResponse::Ok()
}),
)
);
let req = TestRequest::get().uri("/test").to_request();
let resp = call_service(&mut srv, req);
assert_eq!(resp.status(), StatusCode::OK);
}
}

View File

@ -148,11 +148,20 @@ where
/// );
/// }
/// ```
pub fn data<U: 'static>(mut self, data: U) -> Self {
pub fn data<U: 'static>(self, data: U) -> Self {
self.register_data(Data::new(data))
}
/// Set or override application data.
///
/// This method has the same effect as [`Scope::data`](#method.data), except
/// that instead of taking a value of some type `T`, it expects a value of
/// type `Data<T>`. Use a `Data<T>` extractor to retrieve its value.
pub fn register_data<U: 'static>(mut self, data: Data<U>) -> Self {
if self.data.is_none() {
self.data = Some(Extensions::new());
}
self.data.as_mut().unwrap().insert(Data::new(data));
self.data.as_mut().unwrap().insert(data);
self
}
@ -1082,6 +1091,24 @@ mod tests {
assert_eq!(resp.status(), StatusCode::OK);
}
#[test]
fn test_override_register_data() {
let mut srv = init_service(App::new().register_data(web::Data::new(1usize)).service(
web::scope("app").register_data(web::Data::new(10usize)).route(
"/t",
web::get().to(|data: web::Data<usize>| {
assert_eq!(*data, 10);
let _ = data.clone();
HttpResponse::Ok()
}),
),
));
let req = TestRequest::with_uri("/app/t").to_request();
let resp = call_service(&mut srv, req);
assert_eq!(resp.status(), StatusCode::OK);
}
#[test]
fn test_scope_config() {
let mut srv =