Trait wiremock::Match[][src]

pub trait Match: Send + Sync {
    fn matches(&self, request: &Request) -> bool;
}
Expand description

Anything that implements Match can be used to constrain when a Mock is activated.

Match can be used to extend the set of matchers provided out-of-the-box by wiremock to cater to your specific testing needs:

use wiremock::{Match, MockServer, Mock, Request, ResponseTemplate};
use wiremock::matchers::HeaderExactMatcher;
use std::convert::TryInto;

// Check that a header with the specified name exists and its value has an odd length.
pub struct OddHeaderMatcher(http_types::headers::HeaderName);

impl Match for OddHeaderMatcher {
    fn matches(&self, request: &Request) -> bool {
        match request.headers.get(&self.0) {
            // We are ignoring multi-valued headers for simplicity
            Some(values) => values[0].as_str().len() % 2 == 1,
            None => false
        }
    }
}

#[async_std::main]
async fn main() {
    // Arrange
    let mock_server = MockServer::start().await;

    Mock::given(OddHeaderMatcher("custom".try_into().unwrap()))
        .respond_with(ResponseTemplate::new(200))
        .mount(&mock_server)
        .await;
     
    // Even length
    let status = surf::get(&mock_server.uri())
        .header("custom", "even")
        .await
        .unwrap()
        .status();
    assert_eq!(status, 404);

    // Odd length
    let status = surf::get(&mock_server.uri())
        .header("custom", "odd")
        .await
        .unwrap()
        .status();
    assert_eq!(status, 200);
}

Anonymous functions that take a reference to a Request as input and return a boolean as output automatically implement the Match trait.

The previous example could be rewritten as follows:

use wiremock::{Match, MockServer, Mock, Request, ResponseTemplate};
use wiremock::matchers::HeaderExactMatcher;
use std::convert::TryInto;

#[async_std::main]
async fn main() {
    // Arrange
    let mock_server = MockServer::start().await;
     
    let header_name: http_types::headers::HeaderName = "custom".try_into().unwrap();
    // Check that a header with the specified name exists and its value has an odd length.
    let matcher = move |request: &Request| {
        match request.headers.get(&header_name) {
            Some(values) => values[0].as_str().len() % 2 == 1,
            None => false
        }
    };

    Mock::given(matcher)
        .respond_with(ResponseTemplate::new(200))
        .mount(&mock_server)
        .await;
     
    // Even length
    let status = surf::get(&mock_server.uri())
        .header("custom", "even")
        .await
        .unwrap()
        .status();
    assert_eq!(status, 404);

    // Odd length
    let status = surf::get(&mock_server.uri())
        .header("custom", "odd")
        .await
        .unwrap()
        .status();
    assert_eq!(status, 200);
}

Required methods

Given a reference to a Request, determine if it should match or not given a specific criterion.

Implementors