[−][src]Struct mockito::Mock
Stores information about a mocked request. Should be initialized via mockito::mock()
.
Methods
impl Mock
[src]
pub fn match_query<M: Into<Matcher>>(self, query: M) -> Self
[src]
Allows matching against the query part when responding with a mock.
Note that you can also specify the query as part of the path argument
in a mock
call, in which case an exact match will be performed.
Any future calls of Mock#match_query
will override the query matcher.
Example
use mockito::{mock, Matcher}; // This will match requests containing the URL-encoded // query parameter `greeting=good%20day` let _m1 = mock("GET", "/test") .match_query(Matcher::UrlEncoded("greeting".into(), "good day".into())) .create(); // This will match requests containing the URL-encoded // query parameters `hello=world` and `greeting=good%20day` let _m2 = mock("GET", "/test") .match_query(Matcher::AllOf(vec![ Matcher::UrlEncoded("hello".into(), "world".into()), Matcher::UrlEncoded("greeting".into(), "good day".into()) ])) .create(); // You can achieve similar results with the regex matcher let _m3 = mock("GET", "/test") .match_query(Matcher::Regex("hello=world".into())) .create();
pub fn match_header<M: Into<Matcher>>(self, field: &str, value: M) -> Self
[src]
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");
pub fn match_body<M: Into<Matcher>>(self, body: M) -> Self
[src]
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".
pub fn with_status(self, status: usize) -> Self
[src]
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);
pub fn with_header(self, field: &str, value: &str) -> Self
[src]
Sets a header of the mock response.
Example
use mockito::mock; let _m = mock("GET", "/").with_header("content-type", "application/json");
pub fn with_body<StrOrBytes: AsRef<[u8]>>(self, body: StrOrBytes) -> Self
[src]
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");
pub fn with_body_from_fn(
self,
cb: impl Fn(&mut dyn Write) -> Result<()> + Send + Sync + 'static
) -> Self
[src]
self,
cb: impl Fn(&mut dyn Write) -> Result<()> + Send + Sync + 'static
) -> Self
Sets the body of the mock response dynamically. The response will use chunked transfer encoding.
The function must be thread-safe. If it's a closure, it can't be borrowing its context.
Use move
closures and Arc
to share any data.
Example
use mockito::mock; let _m = mock("GET", "/").with_body_from_fn(|w| w.write_all(b"hello world"));
pub fn with_body_from_file(self, path: impl AsRef<Path>) -> Self
[src]
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");
pub fn expect(self, hits: usize) -> Self
[src]
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.
pub fn assert(&self)
[src]
Asserts that the expected amount of requests (defaults to 1 request) were performed.
pub fn create(self) -> Self
[src]
Registers the mock to the server - your mock will be served only after calling this method.
Example
use mockito::mock; let _m = mock("GET", "/").with_body("hello world").create();
Trait Implementations
impl Clone for Mock
[src]
fn clone(&self) -> Mock
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl PartialEq<Mock> for Mock
[src]
impl Drop for Mock
[src]
impl Display for Mock
[src]
impl Debug for Mock
[src]
Auto Trait Implementations
impl Sync for Mock
impl Send for Mock
impl Unpin for Mock
impl !RefUnwindSafe for Mock
impl !UnwindSafe for Mock
Blanket Implementations
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,