diff --git a/CHANGES.md b/CHANGES.md index 95f8b75e1..ead84ac8c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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` diff --git a/src/resource.rs b/src/resource.rs index b872049d0..b711fc322 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -189,11 +189,21 @@ where /// )); /// } /// ``` - pub fn data(mut self, data: U) -> Self { + pub fn data(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`. Use a `Data` extractor to retrieve its + /// value. + pub fn register_data(mut self, data: Data) -> 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, data2: web::Data, data3: web::Data| { + 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); + } } diff --git a/src/scope.rs b/src/scope.rs index 760fee478..06ebbd940 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -148,11 +148,20 @@ where /// ); /// } /// ``` - pub fn data(mut self, data: U) -> Self { + pub fn data(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`. Use a `Data` extractor to retrieve its value. + pub fn register_data(mut self, data: Data) -> 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| { + 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 =