Struct mockito::Mock

source ·
pub struct Mock { /* private fields */ }
Expand description

Stores information about a mocked request. Should be initialized via mockito::mock().

Implementations§

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();

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");

Allows matching a particular request body when responding with a mock.

Example
use mockito::mock;

let _m1 = mock("POST", "/").match_body(r#"{"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".

// Create a temporary file
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use rand;
use rand::Rng;

let random_bytes: Vec<u8> = (0..1024).map(|_| rand::random::<u8>()).collect();

let mut tmp_file = env::temp_dir();
tmp_file.push("test_file.txt");
let mut f_write = File::create(tmp_file.clone()).unwrap();
f_write.write_all(random_bytes.as_slice()).unwrap();
let mut f_read = File::open(tmp_file.clone()).unwrap();


// the following are equivalent ways of defining a mock matching
// a binary payload
let _b1 = mock("POST", "/").match_body(tmp_file.as_path()).create();
let _b3 = mock("POST", "/").match_body(random_bytes).create();
let _b2 = mock("POST", "/").match_body(&mut f_read).create();

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);

Sets a header of the mock response.

Example
use mockito::mock;

let _m = mock("GET", "/").with_header("content-type", "application/json");

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");

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"));

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");

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.

Sets the minimum amount of requests that this mock is supposed to receive. This is only enforced when calling the assert method.

Sets the maximum amount of requests that this mock is supposed to receive. This is only enforced when calling the assert method.

Asserts that the expected amount of requests (defaults to 1 request) were performed.

Returns whether the expected amount of requests (defaults to 1) were performed.

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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.