pub struct Response<Body> { /* private fields */ }Expand description
An HTTP Response that will be passed to an app’s update function.
§A Response never carries an error status
Holding one of these means the server did not reject the request. crux_http
converts every 4xx and 5xx response into an HttpError::Http
— keeping the headers and body — and delivers it on the Err side, so
status is always a 1xx, 2xx or 3xx. A match arm that checks it for
failure is dead code:
fn on_result(result: crux_http::Result<Response<Vec<u8>>>) {
match result {
// this arm cannot see a 4xx or 5xx, so don't test the status here
Ok(_response) => saved(),
Err(error) => {
// the server's own message, e.g. {"error": "…"}, not just "409 Conflict"
let message = error
.body_json::<serde_json::Value>()
.ok()
.and_then(|body| body["error"].as_str().map(str::to_string))
.unwrap_or_else(|| error.to_string());
show_error(&message);
}
}
}
// a rejection, as a feature receives it
on_result(crux_http::testing::rejection(409, r#"{"error":"already booked"}"#));The matching testing rule: build success cases with
ResponseBuilder, and rejections with
rejection.
Implementations§
Source§impl<Body> Response<Body>
impl<Body> Response<Body>
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Get the HTTP status code.
Never a client (4xx) or server (5xx) error: those are delivered as an
HttpError::Http on the Err side, not as a Response,
so there is nothing to be learned by testing this for failure. Handle rejections
there instead — see the type docs.
§Examples
assert_eq!(res.status(), 200);Sourcepub fn version(&self) -> Option<Version>
pub fn version(&self) -> Option<Version>
Get the HTTP protocol version.
§Examples
use crux_http::http::Version;
assert_eq!(res.version(), Some(Version::HTTP_11));Sourcepub fn header_all(&self, name: impl AsHeaderName) -> GetAll<'_, HeaderValue>
pub fn header_all(&self, name: impl AsHeaderName) -> GetAll<'_, HeaderValue>
Get all values for a header name.
Sourcepub fn header_mut(
&mut self,
name: impl AsHeaderName,
) -> Option<&mut HeaderValue>
pub fn header_mut( &mut self, name: impl AsHeaderName, ) -> Option<&mut HeaderValue>
Get an HTTP header mutably.
Sourcepub fn remove_header(&mut self, name: impl AsHeaderName) -> Option<HeaderValue>
pub fn remove_header(&mut self, name: impl AsHeaderName) -> Option<HeaderValue>
Remove a header.
Sourcepub fn insert_header(
&mut self,
name: impl IntoHeaderName,
value: HeaderValue,
) -> Option<HeaderValue>
pub fn insert_header( &mut self, name: impl IntoHeaderName, value: HeaderValue, ) -> Option<HeaderValue>
Insert an HTTP header, replacing any existing value.
Returns the previous value for that header name, if any.
Sourcepub fn append_header(
&mut self,
name: impl IntoHeaderName,
value: HeaderValue,
) -> bool
pub fn append_header( &mut self, name: impl IntoHeaderName, value: HeaderValue, ) -> bool
Append an HTTP header, keeping any existing values.
Returns true if the value was appended to an existing entry, false if it was the first
value for that name.
Sourcepub fn iter(&self) -> Iter<'_, HeaderValue>
pub fn iter(&self) -> Iter<'_, HeaderValue>
An iterator visiting all header (name, value) pairs in arbitrary order.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, HeaderValue>
pub fn iter_mut(&mut self) -> IterMut<'_, HeaderValue>
An iterator visiting all header (name, value) pairs with mutable values.
Sourcepub fn header_names(&self) -> Keys<'_, HeaderValue>
pub fn header_names(&self) -> Keys<'_, HeaderValue>
An iterator visiting all header names in arbitrary order.
Sourcepub fn header_values(&self) -> Values<'_, HeaderValue>
pub fn header_values(&self) -> Values<'_, HeaderValue>
An iterator visiting all header values in arbitrary order.
Sourcepub fn content_type(&self) -> Option<Mime>
pub fn content_type(&self) -> Option<Mime>
Get the response content type as a Mime.
§Examples
assert_eq!(res.content_type(), Some(mime::APPLICATION_JSON));pub fn body(&self) -> Option<&Body>
pub fn take_body(&mut self) -> Option<Body>
pub fn with_body<NewBody>(self, body: NewBody) -> Response<NewBody>
Source§impl Response<Vec<u8>>
impl Response<Vec<u8>>
Sourcepub fn body_bytes(&mut self) -> Result<Vec<u8>>
pub fn body_bytes(&mut self) -> Result<Vec<u8>>
Reads the entire request body into a byte buffer.
§Errors
Returns HttpError::BodyAlreadyTaken if the body has already been taken — this and
the other body_* readers each take it, so only the first call can succeed.
§Examples
let bytes: Vec<u8> = res.body_bytes()?;