From 63c494c8c512092a38c2eac3a1cbd7f98c3a6683 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Fri, 29 Sep 2023 16:04:25 +0200 Subject: [PATCH 1/3] Add wiremock-rs and stubr to the Rust solutions page --- _docs/solutions/rust.md | 71 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/_docs/solutions/rust.md b/_docs/solutions/rust.md index 4fee8bd3..2a210f45 100644 --- a/_docs/solutions/rust.md +++ b/_docs/solutions/rust.md @@ -4,5 +4,74 @@ title: "WireMock and Rust" meta_title: "Rust Solutions | WireMock" description: "Additional solutions for WireMock when using Rust" logo: /images/logos/technology/rust.svg -coming-soon: true --- + +## wiremock-rs. Server implementation in Rust + +[LukeMathWalker/wiremock-rs](https://github.com/LukeMathWalker/wiremock-rs) is an API Mock Server implementation in Rust. +It provides HTTP mocking to perform black-box testing of Rust applications that interact with third-party APIs. + +This project is inspired by WireMock and has the same name in the documentation, +but it is not compatible with WireMock when it comes to CLI, REST API or configuration files. +Please refer to its documentation for more details and guidelines. + +```rust +use wiremock::{MockServer, Mock, ResponseTemplate}; +use wiremock::matchers::{method, path}; + +#[async_std::main] +async fn main() { + // Start a background HTTP server on a random local port + let mock_server = MockServer::start().await; + + // Arrange the behaviour of the MockServer adding a Mock: + // when it receives a GET request on '/hello' it will respond with a 200. + Mock::given(method("GET")).and(path("/hello")) + .respond_with(ResponseTemplate::new(200)) + .mount(&mock_server).await; + + // Verify the response + let status = surf::get(format!("{}/hello", &mock_server.uri())) + .await.unwrap().status(); + assert_eq!(status.as_u16(), 200); +} +``` + +References: + +- Crates: [`wiremock`](https://crates.io/crates/wiremock) +- Documentation: [docs.rs/wiremock](https://docs.rs/wiremock/latest/wiremock/) +- GitHub: [LukeMathWalker/wiremock-rs](https://github.com/LukeMathWalker/wiremock-rs) + +## Stubr + +[Stubr](https://github.com/beltram/stubr) is an adaptation of `wiremock-rs` +supporting existing WireMock json stubs as input. +It aims at reaching feature parity with WireMock. +The project also provides support for gRPC and offers Docker images. + +```rust +use asserhttp::*; + +#[tokio::test] +async fn getting_started() { + // run a mock server with the stub 👇 + let stubr = stubr::Stubr::start("tests/stubs/hello.json").await; + // or use 'start_blocking' for a non-async version + + // the mock server started on a random port e.g. '127.0.0.1:43125' + // so we use the stub instance 'path' (or 'uri') method to get the address back + let uri = stubr.path("/hello"); + reqwest::get(uri).await + // (optional) use asserhttp for assertions + .expect_status_ok() + .expect_content_type_text() + .expect_body_text_eq("Hello stubr"); +} +``` + +References: + +- Crates: [`stubr`](https://crates.io/crates/stubr) +- [Documentation](https://beltram.github.io/stubr/html/) +- [GitHub Repository](https://github.com/beltram/stubr) From 5dfe76ee4e3f2677f4296e4c8b35680bf9ab130a Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Fri, 29 Sep 2023 16:07:30 +0200 Subject: [PATCH 2/3] Add Testcontainers for Rust reference --- _docs/solutions/rust.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/_docs/solutions/rust.md b/_docs/solutions/rust.md index 2a210f45..3c234b51 100644 --- a/_docs/solutions/rust.md +++ b/_docs/solutions/rust.md @@ -75,3 +75,11 @@ References: - Crates: [`stubr`](https://crates.io/crates/stubr) - [Documentation](https://beltram.github.io/stubr/html/) - [GitHub Repository](https://github.com/beltram/stubr) + +## Testcontainers module + +We are interested in providing a Testcontainers for Rust module that +would provide SDK for the official [WireMock Docker images](). +This module is on our roadmap but have not been published yet, +see [wiremock/ecosystem #8](https://github.com/wiremock/ecosystem/issues/8). +Contributions are welcome! From 734535568b03bf600bf450670e7d230d05bdd55a Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Fri, 29 Sep 2023 16:08:23 +0200 Subject: [PATCH 3/3] Fix the WireMock Docker link --- _docs/solutions/rust.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_docs/solutions/rust.md b/_docs/solutions/rust.md index 3c234b51..150db781 100644 --- a/_docs/solutions/rust.md +++ b/_docs/solutions/rust.md @@ -79,7 +79,7 @@ References: ## Testcontainers module We are interested in providing a Testcontainers for Rust module that -would provide SDK for the official [WireMock Docker images](). +would provide SDK for the official [WireMock Docker images](../../docker). This module is on our roadmap but have not been published yet, see [wiremock/ecosystem #8](https://github.com/wiremock/ecosystem/issues/8). Contributions are welcome!