35 releases

0.6.2 Jun 29, 2023
0.6.0-rc.1 Feb 21, 2023
0.5.0 Dec 2, 2022
0.5.0-rc.2 Oct 19, 2022
0.3.4 Mar 23, 2021

#342 in Testing

Download history 44/week @ 2024-01-05 32/week @ 2024-01-12 103/week @ 2024-01-19 114/week @ 2024-01-26 112/week @ 2024-02-02 24/week @ 2024-02-09 130/week @ 2024-02-16 118/week @ 2024-02-23 73/week @ 2024-03-01 55/week @ 2024-03-08 153/week @ 2024-03-15 112/week @ 2024-03-22 94/week @ 2024-03-29 81/week @ 2024-04-05 60/week @ 2024-04-12 48/week @ 2024-04-19

325 downloads per month
Used in 2 crates

Apache-2.0

595KB
13K SLoC

stubr

Wiremock rewritten in Rust


Read the full documentation here


lib.rs:

This crate proposes a reimplementation of Wiremock. Its aims at converting Wiremock stubs into wiremock-rs mocks.

You can also use stubr-build to share stubs between a producer project and a consumer one.

Also available as a cli.

use it

use isahc;
use stubr::*;
use asserhttp::*;

#[async_std::test]
async fn simple_async() {
    // supply a directory containing json stubs. Invalid files are just ignored
    let stubr = Stubr::start("tests/stubs").await;
    // or just mount a single file
    let stubr = Stubr::start("tests/stubs/hello.json").await;
    // or configure it (more configurations to come)
    let stubr = Stubr::start_with("tests/stubs", Config { port: Some(8080), ..Default::default() }).await;
    isahc::get_async(stubr.uri()).await.expect_status_ok();
}

#[test]
fn simple_blocking() {
    // can also be used in a blocking way
    let stubr = Stubr::start_blocking("tests/stubs");
    let stubr = Stubr::start_blocking_with("tests/stubs", Config { port: Some(8080), ..Default::default() });
    isahc::get(stubr.uri()).expect_status_ok();
}

macro

use isahc;
use stubr::*;
use asserhttp::*;

#[async_std::test]
#[stubr::mock] // <- takes all stubs under "tests/stubs"
async fn with_macro() {
    surf::get(stubr.uri()).await.expect_status_ok();
}

#[async_std::test]
#[stubr::mock("pets", port = 4321)] // <- takes all stubs under "tests/stubs/pets"
async fn with_path_and_port() {
    surf::get(stubr.uri()).await.expect_status_ok();
}

configuration

A Stubr server can be configured globally thanks to Config struct.

use stubr::Config;
let config = Config {
    // server port, defaults to random
    port: Some(8080),
    // enable verbose logs
    verbose: true,
    // global delay in milliseconds. Supersedes any locally defined one.
    global_delay: Some(2000),
    // delay in milliseconds added to any locally defined one. Simulates network latencies.
    latency: Some(2000),
    // Enables verification via https://docs.rs/wiremock/latest/wiremock/struct.Mock.html#method.expect
    verify: true,
};

Dependencies

~27–47MB
~891K SLoC