mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-30 18:34:36 +01:00
Impl Responder for (T, StatusCode) where T: Responder (#954)
This commit is contained in:
parent
69456991f6
commit
b1143168e5
@ -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
|
||||
|
@ -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")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user