1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#![doc = include_str!("../docs/response.md")]
use axum_core::body::boxed;
use bytes::Bytes;
use http::{header, HeaderValue};
use http_body::Full;
mod redirect;
pub mod sse;
#[doc(no_inline)]
#[cfg(feature = "json")]
pub use crate::Json;
#[doc(inline)]
pub use axum_core::response::{Headers, IntoResponse, Response};
#[doc(inline)]
pub use self::{redirect::Redirect, sse::Sse};
#[derive(Clone, Copy, Debug)]
pub struct Html<T>(pub T);
impl<T> IntoResponse for Html<T>
where
T: Into<Full<Bytes>>,
{
fn into_response(self) -> Response {
let mut res = Response::new(boxed(self.0.into()));
res.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_static(mime::TEXT_HTML_UTF_8.as_ref()),
);
res
}
}
impl<T> From<T> for Html<T> {
fn from(inner: T) -> Self {
Self(inner)
}
}
#[cfg(test)]
mod tests {
use super::*;
use http::{
header::{HeaderMap, HeaderName},
StatusCode,
};
use http_body::Empty;
#[test]
fn test_merge_headers() {
struct MyResponse;
impl IntoResponse for MyResponse {
fn into_response(self) -> Response {
let mut resp = Response::new(boxed(Empty::new()));
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));
}
}