mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 16:02:59 +01:00
parent
9afc3b6737
commit
0d54b6f38e
@ -737,6 +737,7 @@ impl<S: 'static> Iterator for App<S> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use body::{Body, Binary};
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::HttpResponse;
|
use httpresponse::HttpResponse;
|
||||||
@ -957,4 +958,21 @@ mod tests {
|
|||||||
let response = srv.execute(request.send()).unwrap();
|
let response = srv.execute(request.send()).unwrap();
|
||||||
assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_option_responder() {
|
||||||
|
let mut app = App::new()
|
||||||
|
.resource("/none", |r| r.f(|_| -> Option<&'static str> { None }))
|
||||||
|
.resource("/some", |r| r.f(|_| Some("some")))
|
||||||
|
.finish();
|
||||||
|
|
||||||
|
let req = TestRequest::with_uri("/none").finish();
|
||||||
|
let resp = app.run(req);
|
||||||
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
||||||
|
|
||||||
|
let req = TestRequest::with_uri("/some").finish();
|
||||||
|
let resp = app.run(req);
|
||||||
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
||||||
|
assert_eq!(resp.as_msg().body(), &Body::Binary(Binary::Slice(b"some")));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ use futures::future::{err, ok, Future};
|
|||||||
use futures::{Async, Poll};
|
use futures::{Async, Poll};
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
|
use http::StatusCode;
|
||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::HttpResponse;
|
use httpresponse::HttpResponse;
|
||||||
|
|
||||||
@ -132,6 +133,26 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Responder for Option<T>
|
||||||
|
where
|
||||||
|
T: Responder,
|
||||||
|
{
|
||||||
|
type Item = AsyncResult<HttpResponse>;
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn respond_to<S: 'static>(
|
||||||
|
self, req: &HttpRequest<S>,
|
||||||
|
) -> Result<AsyncResult<HttpResponse>, Error> {
|
||||||
|
match self {
|
||||||
|
Some(t) => match t.respond_to(req) {
|
||||||
|
Ok(val) => Ok(val.into()),
|
||||||
|
Err(err) => Err(err.into()),
|
||||||
|
},
|
||||||
|
None => Ok(req.build_response(StatusCode::NOT_FOUND).finish().into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Convenience trait that converts `Future` object to a `Boxed` future
|
/// Convenience trait that converts `Future` object to a `Boxed` future
|
||||||
///
|
///
|
||||||
/// For example loading json from request's body is async operation.
|
/// For example loading json from request's body is async operation.
|
||||||
|
Loading…
Reference in New Issue
Block a user