1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 13:29:24 +00:00

Impl Responder for (T, StatusCode) where T: Responder (#954)

This commit is contained in:
messense 2019-07-11 16:42:58 +08:00 committed by Nikolay Kim
parent 69456991f6
commit b1143168e5
2 changed files with 45 additions and 0 deletions

View file

@ -2,6 +2,10 @@
## [1.0.4] - TBD
### Added
* Add `Responder` impl for `(T, StatusCode) where T: Responder`
### Changed
* Upgrade `rand` dependency version to 0.7

View file

@ -137,6 +137,22 @@ impl Responder for () {
}
}
impl<T> Responder for (T, StatusCode)
where
T: Responder,
{
type Error = T::Error;
type Future = CustomResponderFut<T>;
fn respond_to(self, req: &HttpRequest) -> Self::Future {
CustomResponderFut {
fut: self.0.respond_to(req).into_future(),
status: Some(self.1),
headers: None,
}
}
}
impl Responder for &'static str {
type Error = Error;
type Future = FutureResult<Response, Error>;
@ -624,4 +640,29 @@ pub(crate) mod tests {
HeaderValue::from_static("json")
);
}
#[test]
fn test_tuple_responder_with_status_code() {
let req = TestRequest::default().to_http_request();
let res = block_on(
("test".to_string(), StatusCode::BAD_REQUEST).respond_to(&req)
)
.unwrap();
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
assert_eq!(res.body().bin_ref(), b"test");
let req = TestRequest::default().to_http_request();
let res = block_on(
("test".to_string(), StatusCode::OK)
.with_header("content-type", "json")
.respond_to(&req)
)
.unwrap();
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.body().bin_ref(), b"test");
assert_eq!(
res.headers().get(CONTENT_TYPE).unwrap(),
HeaderValue::from_static("json")
);
}
}