pub struct MockHttpResponse {
pub status: StatusCode,
pub headers: HeaderMap,
pub body: Bytes,
}Expand description
Mock HTTP response for testing.
This represents a response that can be returned from a server function or used for assertions.
Fields§
§status: StatusCodeHTTP status code.
headers: HeaderMapResponse headers.
body: BytesResponse body.
Implementations§
Source§impl MockHttpResponse
impl MockHttpResponse
Sourcepub fn new(status: StatusCode) -> Self
pub fn new(status: StatusCode) -> Self
Create a new mock response with the given status.
Sourcepub fn no_content() -> Self
pub fn no_content() -> Self
Create a no content (204 No Content) response.
Sourcepub fn bad_request() -> Self
pub fn bad_request() -> Self
Create a bad request (400) response.
Create an unauthorized (401) response.
Sourcepub fn internal_error() -> Self
pub fn internal_error() -> Self
Create an internal server error (500) response.
Sourcepub fn with_status(self, status: StatusCode) -> Self
pub fn with_status(self, status: StatusCode) -> Self
Set the status code.
Sourcepub fn with_header(self, name: &str, value: &str) -> Self
pub fn with_header(self, name: &str, value: &str) -> Self
Add a header.
Add a Set-Cookie header.
Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Check if the response is successful (2xx).
Sourcepub fn is_client_error(&self) -> bool
pub fn is_client_error(&self) -> bool
Check if the response is a client error (4xx).
Sourcepub fn is_server_error(&self) -> bool
pub fn is_server_error(&self) -> bool
Check if the response is a server error (5xx).
Sourcepub fn get_header(&self, name: &str) -> Option<&str>
pub fn get_header(&self, name: &str) -> Option<&str>
Get a header value.
Sourcepub fn json_body<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>
pub fn json_body<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>
Parse the body as JSON.
Sourcepub fn text_body(&self) -> Result<String, FromUtf8Error>
pub fn text_body(&self) -> Result<String, FromUtf8Error>
Get the body as a string.
Trait Implementations§
Source§impl Clone for MockHttpResponse
impl Clone for MockHttpResponse
Source§fn clone(&self) -> MockHttpResponse
fn clone(&self) -> MockHttpResponse
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for MockHttpResponse
impl Debug for MockHttpResponse
Auto Trait Implementations§
impl !Freeze for MockHttpResponse
impl RefUnwindSafe for MockHttpResponse
impl Send for MockHttpResponse
impl Sync for MockHttpResponse
impl Unpin for MockHttpResponse
impl UnsafeUnpin for MockHttpResponse
impl UnwindSafe for MockHttpResponse
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian().Source§impl<E> ServerFnErrorAssertions<E> for Ewhere
E: Debug,
impl<E> ServerFnErrorAssertions<E> for Ewhere
E: Debug,
Source§fn should_contain_message(&self, expected: &str)where
E: Display,
fn should_contain_message(&self, expected: &str)where
E: Display,
Assert that the error message contains the specified text.
Source§fn should_have_message(&self, expected: &str)where
E: Display,
fn should_have_message(&self, expected: &str)where
E: Display,
Assert that the error message matches exactly.