35 个版本
0.6.2 | 2023 年 6 月 29 日 |
---|---|
0.6.0-rc.1 | 2023 年 2 月 21 日 |
0.5.0 | 2022 年 12 月 2 日 |
0.5.0-rc.2 | 2022 年 10 月 19 日 |
0.3.4 | 2021 年 3 月 23 日 |
#245 in 测试
305 每月下载量
用于 2 crates
595KB
13K SLoC
stubr
Wiremock 重新编写为 Rust
阅读完整文档 这里
lib.rs
:
此 crate 提出了一个重新实现 Wiremock 的方案。它的目的是将 Wiremock stubs 转换为 wiremock-rs mocks。
您还可以使用 stubr-build 在生产项目和消费项目之间共享 stubs。
还提供 cli 版本。
使用方法
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();
}
宏
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();
}
配置
通过 Config
结构,可以全局配置 Stubr
服务器。
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,
};
依赖关系
~27–49MB
~890K SLoC