Crates.io | wiremocket |
lib.rs | wiremocket |
version | |
source | src |
created_at | 2025-03-03 05:07:01.700588+00 |
updated_at | 2025-03-03 05:07:01.700588+00 |
description | Websocket mocking to test Rust applications. |
homepage | |
repository | https://github.com/xd009642/wiremocket |
max_upload_size | |
id | 1575116 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
'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!
cargo add wiremocket --dev
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!