mirror of https://github.com/fafhrd91/actix-web
Adding support also for Scope and more tests in the Extensions
This commit is contained in:
parent
9d09ee9d44
commit
fbbedbf53f
|
@ -66,6 +66,11 @@ impl Extensions {
|
||||||
pub fn extend(&mut self, other: Extensions) {
|
pub fn extend(&mut self, other: Extensions) {
|
||||||
self.map.extend(other.map);
|
self.map.extend(other.map);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if no extension is registered
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.map.is_empty()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Extensions {
|
impl fmt::Debug for Extensions {
|
||||||
|
@ -93,6 +98,8 @@ mod tests {
|
||||||
fn test_clear() {
|
fn test_clear() {
|
||||||
let mut map = Extensions::new();
|
let mut map = Extensions::new();
|
||||||
|
|
||||||
|
assert!(map.is_empty());
|
||||||
|
|
||||||
map.insert::<i8>(8);
|
map.insert::<i8>(8);
|
||||||
map.insert::<i16>(16);
|
map.insert::<i16>(16);
|
||||||
map.insert::<i32>(32);
|
map.insert::<i32>(32);
|
||||||
|
@ -100,12 +107,14 @@ mod tests {
|
||||||
assert!(map.contains::<i8>());
|
assert!(map.contains::<i8>());
|
||||||
assert!(map.contains::<i16>());
|
assert!(map.contains::<i16>());
|
||||||
assert!(map.contains::<i32>());
|
assert!(map.contains::<i32>());
|
||||||
|
assert!(!map.is_empty());
|
||||||
|
|
||||||
map.clear();
|
map.clear();
|
||||||
|
|
||||||
assert!(!map.contains::<i8>());
|
assert!(!map.contains::<i8>());
|
||||||
assert!(!map.contains::<i16>());
|
assert!(!map.contains::<i16>());
|
||||||
assert!(!map.contains::<i32>());
|
assert!(!map.contains::<i32>());
|
||||||
|
assert!(map.is_empty());
|
||||||
|
|
||||||
map.insert::<i8>(10);
|
map.insert::<i8>(10);
|
||||||
assert_eq!(*map.get::<i8>().unwrap(), 10);
|
assert_eq!(*map.get::<i8>().unwrap(), 10);
|
||||||
|
@ -183,4 +192,34 @@ mod tests {
|
||||||
assert_eq!(extensions.get::<bool>(), None);
|
assert_eq!(extensions.get::<bool>(), None);
|
||||||
assert_eq!(extensions.get(), Some(&MyType(10)));
|
assert_eq!(extensions.get(), Some(&MyType(10)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_extend() {
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
struct MyType(i32);
|
||||||
|
|
||||||
|
let mut extensions = Extensions::new();
|
||||||
|
|
||||||
|
extensions.insert(5i32);
|
||||||
|
extensions.insert(MyType(10));
|
||||||
|
|
||||||
|
let mut other = Extensions::new();
|
||||||
|
|
||||||
|
other.insert(15i32);
|
||||||
|
other.insert(20u8);
|
||||||
|
|
||||||
|
extensions.extend(other);
|
||||||
|
|
||||||
|
assert_eq!(extensions.get(), Some(&15i32));
|
||||||
|
assert_eq!(extensions.get_mut(), Some(&mut 15i32));
|
||||||
|
|
||||||
|
assert_eq!(extensions.remove::<i32>(), Some(15i32));
|
||||||
|
assert!(extensions.get::<i32>().is_none());
|
||||||
|
|
||||||
|
assert_eq!(extensions.get::<bool>(), None);
|
||||||
|
assert_eq!(extensions.get(), Some(&MyType(10)));
|
||||||
|
|
||||||
|
assert_eq!(extensions.get(), Some(&20u8));
|
||||||
|
assert_eq!(extensions.get_mut(), Some(&20u8));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -264,12 +264,12 @@ mod tests {
|
||||||
async fn test_data() {
|
async fn test_data() {
|
||||||
let cfg = |cfg: &mut ServiceConfig| {
|
let cfg = |cfg: &mut ServiceConfig| {
|
||||||
cfg.data(10usize);
|
cfg.data(10usize);
|
||||||
cfg.app_data(10usize);
|
cfg.app_data(15u8);
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut srv = init_service(App::new().configure(cfg).service(
|
let mut srv = init_service(App::new().configure(cfg).service(
|
||||||
web::resource("/").to(|_: web::Data<usize>, req: HttpRequest| {
|
web::resource("/").to(|_: web::Data<usize>, req: HttpRequest| {
|
||||||
assert_eq!(*req.app_data::<usize>().unwrap(), 10usize);
|
assert_eq!(*req.app_data::<u8>().unwrap(), 15u8);
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
}),
|
}),
|
||||||
))
|
))
|
||||||
|
|
|
@ -209,6 +209,12 @@ where
|
||||||
|
|
||||||
self.data = Some(data);
|
self.data = Some(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !cfg.extensions.is_empty() {
|
||||||
|
let mut data = self.data.unwrap_or_else(Extensions::new);
|
||||||
|
data.extend(cfg.extensions);
|
||||||
|
self.data = Some(data);
|
||||||
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue