Body
This section explains functions designed to set values in the response when a request meets all specified criteria.
status
Configures the HTTP response status code that the mock server will return.
Parameters
status
: Au16
HTTP status code that the mock server should return for the configured request.
Returns
Returns self
to allow chaining of method calls on the Mock
object.
Example
Demonstrates setting a 200 OK status for a request to the path /hello
.
body
Configures the HTTP response body that the mock server will return.
Parameters
body
: The content of the response body, provided as a type that can be referenced as a byte slice.
Returns
Returns self
to allow chaining of method calls on the Mock
object.
Example
Demonstrates setting a response body for a request to the path /hello
with a 200 OK status.
body_from_file
Configures the HTTP response body with content loaded from a specified file on the mock server.
Parameters
resource_file_path
: A string representing the path to the file whose contents will be used as the response body. The path can be absolute or relative to the server’s running directory.
Returns
Returns self
to allow chaining of method calls on the Mock
object.
Panics
Panics if the specified file cannot be read, or if the path provided cannot be resolved to an absolute path.
Example
Demonstrates setting the response body from a file for a request to the path /hello
with a 200 OK status.
json_body
Sets the JSON body for the HTTP response that will be returned by the mock server.
This function accepts a JSON object that must be serializable and deserializable by serde. Note that this method does not automatically set the “Content-Type” header to “application/json”. You will need to set this header manually if required.
Parameters
body
: The HTTP response body in the form of aserde_json::Value
object.
Returns
Returns self
to allow chaining of method calls on the Mock
object.
Example
Demonstrates how to set a JSON body and a matching “Content-Type” header for a mock response.
json_body_obj
Sets the JSON body that will be returned by the mock server using a serializable serde object.
This method converts the provided object into a JSON string. It does not automatically set the “Content-Type” header to “application/json”, so you must set this header manually if it’s needed.
Parameters
body
: A reference to an object that implements theserde::Serialize
trait.
Returns
Returns self
to allow chaining of method calls on the Mock
object.
Panics
Panics if the object cannot be serialized into a JSON string.
Example
Demonstrates setting a JSON body and the corresponding “Content-Type” header for a user object.
header
Sets an HTTP header that the mock server will return in the response.
This method configures a response header to be included when the mock server handles a request.
Parameters
name
: The name of the header to set.value
: The value of the header.
Returns
Returns self
to allow chaining of method calls on the Mock
object.
Example
Demonstrates setting the “Expires” header for a response to a request to the root path.