1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-08-02 10:45:05 +00:00

Merge branch 'master' into master

This commit is contained in:
Douman 2018-08-09 08:13:22 +03:00 committed by GitHub
commit 5713d93158
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 18 deletions

View file

@ -101,6 +101,12 @@ impl<T> Path<T> {
}
}
impl<T> From<T> for Path<T> {
fn from(inner: T) -> Path<T> {
Path{inner}
}
}
impl<T, S> FromRequest<S> for Path<T>
where
T: DeserializeOwned,

View file

@ -81,6 +81,15 @@ impl<S> HttpRequest<S> {
}
}
/// Construct new http request with empty state.
pub fn drop_state(&self) -> HttpRequest {
HttpRequest {
state: Rc::new(()),
req: self.req.as_ref().map(|r| r.clone()),
resource: self.resource.clone(),
}
}
#[inline]
/// Construct new http request with new RouteInfo.
pub(crate) fn with_route_info(&self, mut resource: ResourceInfo) -> HttpRequest<S> {

View file

@ -290,19 +290,6 @@ impl<S: 'static> Router<S> {
}
}
#[cfg(test)]
pub(crate) fn route_info(&self, req: &Request, prefix: u16) -> ResourceInfo {
let mut params = Params::with_url(req.url());
params.set_tail(prefix);
ResourceInfo {
params,
prefix: 0,
rmap: self.rmap.clone(),
resource: ResourceId::Default,
}
}
#[cfg(test)]
pub(crate) fn default_route_info(&self) -> ResourceInfo {
ResourceInfo {

View file

@ -676,8 +676,6 @@ impl<S: 'static> TestRequest<S> {
/// This method generates `HttpRequest` instance and runs handler
/// with generated request.
///
/// This method panics is handler returns actor or async result.
pub fn run<H: Handler<S>>(self, h: &H) -> Result<HttpResponse, Error> {
let req = self.finish();
let resp = h.handle(&req);
@ -686,7 +684,10 @@ impl<S: 'static> TestRequest<S> {
Ok(resp) => match resp.into().into() {
AsyncResultItem::Ok(resp) => Ok(resp),
AsyncResultItem::Err(err) => Err(err),
AsyncResultItem::Future(_) => panic!("Async handler is not supported."),
AsyncResultItem::Future(fut) => {
let mut sys = System::new("test");
sys.block_on(fut)
}
},
Err(err) => Err(err.into()),
}
@ -706,8 +707,8 @@ impl<S: 'static> TestRequest<S> {
let req = self.finish();
let fut = h(req.clone());
let mut core = Runtime::new().unwrap();
match core.block_on(fut) {
let mut sys = System::new("test");
match sys.block_on(fut) {
Ok(r) => match r.respond_to(&req) {
Ok(reply) => match reply.into().into() {
AsyncResultItem::Ok(resp) => Ok(resp),
@ -718,4 +719,25 @@ impl<S: 'static> TestRequest<S> {
Err(err) => Err(err),
}
}
/// This method generates `HttpRequest` instance and executes handler
pub fn execute<F, R>(self, f: F) -> Result<HttpResponse, Error>
where F: FnOnce(&HttpRequest<S>) -> R,
R: Responder + 'static,
{
let req = self.finish();
let resp = f(&req);
match resp.respond_to(&req) {
Ok(resp) => match resp.into().into() {
AsyncResultItem::Ok(resp) => Ok(resp),
AsyncResultItem::Err(err) => Err(err),
AsyncResultItem::Future(fut) => {
let mut sys = System::new("test");
sys.block_on(fut)
}
},
Err(err) => Err(err.into()),
}
}
}