Crate wiremocket

Source
Expand description

§wiremocket

Build Status Latest Version License:MIT docs.rs Coverage Status

‘wiremocket’ provides mocking so you can perform black-box testing of Rust applications that interact with websocket APIs. It’s heavily inspired by wiremock-rs and is an experimentation of how it could look like in a similar API. For a relevant wiremock issue look here.

There’s still some work to do, but this is very nearly at an initial version!

§How to install

cargo add wiremocket --dev

§Getting started

Here is an example of a wiremocket mock which makes sure all text messages are valid json:

use serde_json::json;
use tokio_tungstenite::connect_async;
use tracing_test::traced_test;
use tungstenite::Message;
use wiremocket::prelude::*;

#[tokio::test]
async fn only_json_matcher() {
    let server = MockServer::start().await;

    server
        .register(Mock::given(ValidJsonMatcher).expect(1..))
        .await;

    let (mut stream, response) = connect_async(server.uri()).await.unwrap();

    let val = json!({"hello": "world"});

    stream.send(Message::text(val.to_string())).await.unwrap();

    stream.send(Message::Close(None)).await.unwrap();

    std::mem::drop(stream);

    server.verify().await;
}

More advanced matching based on the stream of messages and more advanced response stream generation are also possible. Please check the docs for more details!

Re-exports§

pub use crate::mock::*;
pub use crate::server::*;
pub use crate::utils::*;

Modules§

match_state
Handles storing the state of the connection over time for temporal checking of the stream.
matchers
A collection of different matching strategies provided out-of-the-box by wiremocket.
mock
A Mock will match requests, check they match preconditions and initiate the server responses. This is the core of the server behaviour.
prelude
Re-exports every part of the public API for ease of use.
responder
Determine how a given mock responds to the client.
server
Code related to starting, running and interacting with the mock websocket server.
utils
Utility functions and types used by wiremocket.

Traits§

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