Fixed the implementation of IntoResponse of (HeaderMap, T) and (StatusCode, HeaderMap, T) would ignore headers from T (#137)

Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
This commit is contained in:
Sunli 2021-08-06 16:31:38 +08:00 committed by GitHub
parent 9fdbd42fba
commit a0ac8a5b78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 2 deletions

View file

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Re-export `http` crate and `hyper::Server`. ([#110](https://github.com/tokio-rs/axum/pull/110))
- Fix `Query` and `Form` extractors giving bad request error when query string is empty. ([#117](https://github.com/tokio-rs/axum/pull/117))
- Add `Path` extractor. ([#124](https://github.com/tokio-rs/axum/pull/124))
- Fixed the implementation of `IntoResponse` of `(HeaderMap, T)` and `(StatusCode, HeaderMap, T)` would ignore headers from `T` ([#137](https://github.com/tokio-rs/axum/pull/137))
## Breaking changes

View file

@ -158,7 +158,7 @@ where
{
fn into_response(self) -> Response<Body> {
let mut res = self.1.into_response();
*res.headers_mut() = self.0;
res.headers_mut().extend(self.0);
res
}
}
@ -170,7 +170,7 @@ where
fn into_response(self) -> Response<Body> {
let mut res = self.2.into_response();
*res.status_mut() = self.0;
*res.headers_mut() = self.1;
res.headers_mut().extend(self.1);
res
}
}
@ -264,3 +264,42 @@ impl<T> From<T> for Json<T> {
Self(inner)
}
}
#[cfg(test)]
mod tests {
use super::*;
use http::header::{HeaderMap, HeaderName};
#[test]
fn test_merge_headers() {
struct MyResponse;
impl IntoResponse for MyResponse {
fn into_response(self) -> Response<Body> {
let mut resp = Response::new(String::new().into());
resp.headers_mut()
.insert(HeaderName::from_static("a"), HeaderValue::from_static("1"));
resp
}
}
fn check(resp: impl IntoResponse) {
let resp = resp.into_response();
assert_eq!(
resp.headers().get(HeaderName::from_static("a")).unwrap(),
&HeaderValue::from_static("1")
);
assert_eq!(
resp.headers().get(HeaderName::from_static("b")).unwrap(),
&HeaderValue::from_static("2")
);
}
let headers: HeaderMap =
std::iter::once((HeaderName::from_static("b"), HeaderValue::from_static("2")))
.collect();
check((headers.clone(), MyResponse));
check((StatusCode::OK, headers, MyResponse));
}
}