pub struct MockHttpRequest {
pub method: Method,
pub uri: Uri,
pub headers: HeaderMap,
pub body: Bytes,
pub cookies: HashMap<String, String>,
pub query_params: HashMap<String, String>,
}Expand description
Mock HTTP request for testing server functions.
This provides a fluent API for building HTTP requests without an actual HTTP layer, useful for testing server functions directly.
Fields§
§method: MethodThe HTTP method.
uri: UriThe request URI.
headers: HeaderMapRequest headers.
body: BytesRequest body as bytes.
Cookies extracted from headers.
query_params: HashMap<String, String>Query parameters.
Implementations§
Source§impl MockHttpRequest
impl MockHttpRequest
Sourcepub fn new(method: Method, uri: &str) -> Self
pub fn new(method: Method, uri: &str) -> Self
Create a new mock request with the given method and URI.
Sourcepub fn with_json<T: Serialize>(self, body: &T) -> Self
pub fn with_json<T: Serialize>(self, body: &T) -> Self
Set the request body as JSON.
This serializes the value to JSON and sets the appropriate Content-Type header.
§Panics
Panics if the value cannot be serialized to JSON. Since this is a test utility, invalid input indicates a test setup error that should be caught immediately.
Sourcepub fn with_form<T: Serialize>(self, body: &T) -> Self
pub fn with_form<T: Serialize>(self, body: &T) -> Self
Set the request body as form data.
This serializes the value as URL-encoded form data.
§Panics
Panics if the value cannot be serialized as form data. Since this is a test utility, invalid input indicates a test setup error that should be caught immediately.
Sourcepub fn with_header(self, name: &str, value: &str) -> Self
pub fn with_header(self, name: &str, value: &str) -> Self
Add a request header.
Sourcepub fn with_headers<'a>(
self,
headers: impl IntoIterator<Item = (&'a str, &'a str)>,
) -> Self
pub fn with_headers<'a>( self, headers: impl IntoIterator<Item = (&'a str, &'a str)>, ) -> Self
Add multiple headers.
Add a cookie.
Add multiple cookies.
Sourcepub fn with_query(self, name: &str, value: &str) -> Self
pub fn with_query(self, name: &str, value: &str) -> Self
Add a query parameter.
Sourcepub fn with_query_params<'a>(
self,
params: impl IntoIterator<Item = (&'a str, &'a str)>,
) -> Self
pub fn with_query_params<'a>( self, params: impl IntoIterator<Item = (&'a str, &'a str)>, ) -> Self
Add multiple query parameters.
Sourcepub fn with_bearer_token(self, token: &str) -> Self
pub fn with_bearer_token(self, token: &str) -> Self
Set the Authorization header with a Bearer token.
Sourcepub fn with_basic_auth(self, username: &str, password: &str) -> Self
pub fn with_basic_auth(self, username: &str, password: &str) -> Self
Set the Authorization header with Basic auth.
Sourcepub fn with_content_type(self, content_type: &str) -> Self
pub fn with_content_type(self, content_type: &str) -> Self
Set the Content-Type header.
Sourcepub fn with_accept(self, accept: &str) -> Self
pub fn with_accept(self, accept: &str) -> Self
Set the Accept header.
Sourcepub fn uri_string(&self) -> String
pub fn uri_string(&self) -> String
Get the full URI as a string.
Sourcepub fn get_header(&self, name: &str) -> Option<&str>
pub fn get_header(&self, name: &str) -> Option<&str>
Get a header value.
Get a cookie value.
Sourcepub fn json<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>
pub fn json<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>
Parse the body as JSON.
Sourcepub fn form<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>
pub fn form<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>
Parse the body as form data.
Sourcepub fn text(&self) -> Result<String, FromUtf8Error>
pub fn text(&self) -> Result<String, FromUtf8Error>
Get the body as a string.
Trait Implementations§
Source§impl Clone for MockHttpRequest
impl Clone for MockHttpRequest
Source§fn clone(&self) -> MockHttpRequest
fn clone(&self) -> MockHttpRequest
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for MockHttpRequest
impl Debug for MockHttpRequest
Auto Trait Implementations§
impl !Freeze for MockHttpRequest
impl RefUnwindSafe for MockHttpRequest
impl Send for MockHttpRequest
impl Sync for MockHttpRequest
impl Unpin for MockHttpRequest
impl UnsafeUnpin for MockHttpRequest
impl UnwindSafe for MockHttpRequest
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
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>
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>
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>
ReadEndian::read_from_little_endian().