crux_http/testing/
response_builder.rs1use http::{HeaderValue, StatusCode};
2
3use crate::response::Response;
4
5pub struct ResponseBuilder<Body> {
9 response: Response<Body>,
10}
11
12impl ResponseBuilder<Vec<u8>> {
13 #[must_use]
15 pub fn ok() -> Self {
16 Self::with_status(200)
17 }
18
19 #[must_use]
25 pub fn with_status(status: u16) -> Self {
26 let status = StatusCode::from_u16(status).expect(
27 "ResponseBuilder::with_status called with an out-of-range code (must be 100–999)",
28 );
29 let response = Response::new_with_status(status);
30 Self { response }
31 }
32}
33
34impl<Body> ResponseBuilder<Body> {
35 pub fn body<NewBody>(self, body: NewBody) -> ResponseBuilder<NewBody> {
37 let response = self.response.with_body(body);
38 ResponseBuilder { response }
39 }
40
41 #[must_use]
46 pub fn header(
47 mut self,
48 name: impl http::header::IntoHeaderName,
49 value: impl AsRef<str>,
50 ) -> Self {
51 let value = HeaderValue::from_str(value.as_ref()).expect("invalid header value");
52 self.response.insert_header(name, value);
53 self
54 }
55
56 #[must_use]
64 pub fn append_header(
65 mut self,
66 name: impl http::header::IntoHeaderName,
67 value: impl AsRef<str>,
68 ) -> Self {
69 let value = HeaderValue::from_str(value.as_ref()).expect("invalid header value");
70 self.response.append_header(name, value);
71 self
72 }
73
74 pub fn build(self) -> Response<Body> {
76 self.response
77 }
78}