mirror of
https://github.com/actix/actix-web.git
synced 2024-12-21 07:36:43 +00:00
allow to get app data via HttpRequest
This commit is contained in:
parent
5b06f2bee5
commit
548f6f89bf
1 changed files with 45 additions and 2 deletions
|
@ -8,6 +8,7 @@ use actix_http::{Error, Extensions, HttpMessage, Message, Payload, RequestHead};
|
||||||
use actix_router::{Path, Url};
|
use actix_router::{Path, Url};
|
||||||
|
|
||||||
use crate::config::AppConfig;
|
use crate::config::AppConfig;
|
||||||
|
use crate::data::Data;
|
||||||
use crate::error::UrlGenerationError;
|
use crate::error::UrlGenerationError;
|
||||||
use crate::extract::FromRequest;
|
use crate::extract::FromRequest;
|
||||||
use crate::info::ConnectionInfo;
|
use crate::info::ConnectionInfo;
|
||||||
|
@ -100,6 +101,16 @@ impl HttpRequest {
|
||||||
&self.config
|
&self.config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get an application data stored with `App::data()` method during
|
||||||
|
/// application configuration.
|
||||||
|
pub fn app_data<T: 'static>(&self) -> Option<Data<T>> {
|
||||||
|
if let Some(st) = self.config.extensions().get::<Data<T>>() {
|
||||||
|
Some(st.clone())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Generate url for named resource
|
/// Generate url for named resource
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
|
@ -239,8 +250,9 @@ impl fmt::Debug for HttpRequest {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::dev::{ResourceDef, ResourceMap};
|
use crate::dev::{ResourceDef, ResourceMap};
|
||||||
use crate::http::header;
|
use crate::http::{header, StatusCode};
|
||||||
use crate::test::TestRequest;
|
use crate::test::{call_success, init_service, TestRequest};
|
||||||
|
use crate::{web, App, HttpResponse};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_debug() {
|
fn test_debug() {
|
||||||
|
@ -356,4 +368,35 @@ mod tests {
|
||||||
"https://youtube.com/watch/oHg5SJYRHA0"
|
"https://youtube.com/watch/oHg5SJYRHA0"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_app_data() {
|
||||||
|
let mut srv = init_service(App::new().data(10usize).service(
|
||||||
|
web::resource("/").to(|req: HttpRequest| {
|
||||||
|
if req.app_data::<usize>().is_some() {
|
||||||
|
HttpResponse::Ok()
|
||||||
|
} else {
|
||||||
|
HttpResponse::BadRequest()
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
));
|
||||||
|
|
||||||
|
let req = TestRequest::default().to_request();
|
||||||
|
let resp = call_success(&mut srv, req);
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let mut srv = init_service(App::new().data(10u32).service(
|
||||||
|
web::resource("/").to(|req: HttpRequest| {
|
||||||
|
if req.app_data::<usize>().is_some() {
|
||||||
|
HttpResponse::Ok()
|
||||||
|
} else {
|
||||||
|
HttpResponse::BadRequest()
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
));
|
||||||
|
|
||||||
|
let req = TestRequest::default().to_request();
|
||||||
|
let resp = call_success(&mut srv, req);
|
||||||
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue