Struct mockito::Mock
[−]
[src]
pub struct Mock { /* fields omitted */ }
Stores information about a mocked request. Should be initialized via mockito::mock()
.
Methods
impl Mock
[src]
fn match_header(&mut self, field: &str, value: &str) -> &mut 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; mock("GET", "/").match_header("content-type", "application/json");
Like most other Mock
methods, it allows chanining:
Example
use mockito::mock; mock("GET", "/") .match_header("content-type", "application/json") .match_header("authorization", "password");
fn with_status(&mut self, status: usize) -> &mut Self
Sets the status code of the mock response. The default status code is 200.
Example
use mockito::mock; mock("GET", "/").with_status(201);
fn with_header(&mut self, field: &str, value: &str) -> &mut Self
Sets a header of the mock response.
Example
use mockito::mock; mock("GET", "/").with_header("content-type", "application/json");
fn with_body(&mut self, body: &str) -> &mut Self
Sets the body of the mock response. Its Content-Length
is handled automatically.
Example
use mockito::mock; mock("GET", "/").with_body("hello world");
fn with_body_from_file(&mut self, path: &str) -> &mut 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; mock("GET", "/").with_body_from_file("tests/files/simple.http");
fn create(&mut self) -> &mut Self
Registers the mock to the server - your mock will be served only after calling this method.
Example
use mockito::mock; mock("GET", "/").with_body("hello world").create();
fn create_for<F: Fn() -> ()>(&mut self, environment: F) -> &mut Self
Registers the mock to the server, executes the passed closure and removes the mock afterwards.
NOTE: During the closure lifetime, the mock might still be available to seperate threads.
Example
use std::thread::{sleep}; use std::time::Duration; use mockito::mock; mock("GET", "/").with_body("hello world").create_for(|| { // This mock will only be available for the next 10 seconds sleep(Duration::new(10, 0)); });
fn remove(&self)
Removes the current mock from the server.
Example
use mockito::mock; let mut mock = mock("GET", "/"); mock.with_body("hello world").create(); // stuff mock.remove();
fn matches(&self, request: &Request) -> bool
Trait Implementations
impl PartialEq for Mock
[src]
fn eq(&self, __arg_0: &Mock) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Mock) -> bool
This method tests for !=
.