pub struct ResponseBuilder<Body> { /* private fields */ }Expand description
Allows users to build an http response.
This is mostly expected to be useful in tests rather than application code.
Only responses a feature can actually receive are buildable: a
Response never carries a 4xx or 5xx status, because crux_http
converts those to HttpError::Http before the app sees
them. Use rejection to build a rejection.
Implementations§
Source§impl ResponseBuilder<Vec<u8>>
impl ResponseBuilder<Vec<u8>>
Sourcepub fn with_status(status: u16) -> Self
pub fn with_status(status: u16) -> Self
Constructs a new ResponseBuilder with the specified status code.
§Panics
Panics if status is outside the valid HTTP range (100–999), or if it is a client
(4xx) or server (5xx) error. Such a response is not a state any app can observe —
crux_http delivers it as an HttpError::Http on the
Err side, so a test that builds one asserts against a branch the app can never
take. Build the rejection a feature really receives with
rejection:
let result = rejection::<Vec<u8>>(409, r#"{"error":"already booked"}"#);
assert_eq!(result.unwrap_err().code(), Some(409));Source§impl<Body> ResponseBuilder<Body>
impl<Body> ResponseBuilder<Body>
Sourcepub fn body<NewBody>(self, body: NewBody) -> ResponseBuilder<NewBody>
pub fn body<NewBody>(self, body: NewBody) -> ResponseBuilder<NewBody>
Sets the body of the Response.
Sourcepub fn header(self, name: impl IntoHeaderName, value: impl AsRef<str>) -> Self
pub fn header(self, name: impl IntoHeaderName, value: impl AsRef<str>) -> Self
Sets a header on the response, replacing any existing value for that name.
§Panics
Panics if value is not a valid header value.
Sourcepub fn append_header(
self,
name: impl IntoHeaderName,
value: impl AsRef<str>,
) -> Self
pub fn append_header( self, name: impl IntoHeaderName, value: impl AsRef<str>, ) -> Self
Appends a header value, keeping any existing values for that name.
Use this when building responses with multiple values for the same header
(e.g. Set-Cookie).
§Panics
Panics if value is not a valid header value.