mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-27 10:39:03 +02: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:
@ -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);
|
||||
}
|
||||
}
|
||||
|
31
src/scope.rs
31
src/scope.rs
@ -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 =
|
||||
|
Reference in New Issue
Block a user