pub struct Mock { /* private fields */ }Expand description
Stores information about a mocked request. Should be initialized via mockito::mock().
Implementations
sourceimpl Mock
impl Mock
sourcepub fn match_header<M: Into<Matcher>>(self, field: &str, value: M) -> Self
pub fn match_header<M: Into<Matcher>>(self, field: &str, value: M) -> Self
Allows matching a particular request header when responding with a mock.
When matching a request, the field letter case is ignored.
Example
use mockito::mock;
let _m = mock("GET", "/").match_header("content-type", "application/json");Like most other Mock methods, it allows chanining:
Example
use mockito::mock;
let _m = mock("GET", "/")
.match_header("content-type", "application/json")
.match_header("authorization", "password");sourcepub fn match_body<M: Into<Matcher>>(self, body: M) -> Self
pub fn match_body<M: Into<Matcher>>(self, body: M) -> Self
Allows matching a particular request body when responding with a mock.
Example
use mockito::mock;
let _m1 = mock("POST", "/").match_body("{'hello':'world'}").with_body("json").create();
let _m2 = mock("POST", "/").match_body("hello=world").with_body("form").create();
// Requests passing "{'hello':'world'}" inside the body will be responded with "json".
// Requests passing "hello=world" inside the body will be responded with "form".sourcepub fn with_status(self, status: usize) -> Self
pub fn with_status(self, status: usize) -> Self
Sets the status code of the mock response. The default status code is 200.
Example
use mockito::mock;
let _m = mock("GET", "/").with_status(201);sourcepub fn with_header(self, field: &str, value: &str) -> Self
pub fn with_header(self, field: &str, value: &str) -> Self
Sets a header of the mock response.
Example
use mockito::mock;
let _m = mock("GET", "/").with_header("content-type", "application/json");sourcepub fn with_body<StrOrBytes: AsRef<[u8]>>(self, body: StrOrBytes) -> Self
pub fn with_body<StrOrBytes: AsRef<[u8]>>(self, body: StrOrBytes) -> Self
Sets the body of the mock response. Its Content-Length is handled automatically.
Example
use mockito::mock;
let _m = mock("GET", "/").with_body("hello world");sourcepub fn with_body_from_file(self, path: &str) -> Self
pub fn with_body_from_file(self, path: &str) -> Self
Sets the body of the mock response from the contents of a file stored under path.
Its Content-Length is handled automatically.
Example
use mockito::mock;
let _m = mock("GET", "/").with_body_from_file("tests/files/simple.http");sourcepub fn expect(self, hits: usize) -> Self
pub fn expect(self, hits: usize) -> Self
Sets the expected amount of requests that this mock is supposed to receive.
This is only enforced when calling the assert method.
Defaults to 1 request.