HTTP mocking library for Rust.
Documentation · Crate · Report Bug · Request Feature · Changelog
Add httpmock
to Cargo.toml
:
toml
[dev-dependencies]
httpmock = "0.5.0"
You can then use httpmock
as follows:
```rust
use httpmock::MockServer;
use httpmock::Method::GET;
// Start a lightweight mock server. let server = MockServer::start();
// Create a mock on the server. let hellomock = server.mock(|when, then| { when.method(GET) .path("/translate") .queryparam("word", "hello"); then.status(200) .header("Content-Type", "text/html; charset=UTF-8") .body("Привет"); });
// Send an HTTP request to the mock server. This simulates your code. let response = isahc::get(server.url("/translate?word=hello")).unwrap();
// Ensure the specified mock was called exactly one time. hellomock.assert(); // Ensure the mock server did respond as specified. asserteq!(response.status(), 200); ```
The above example will spin up a lightweight HTTP mock server and configure it to respond to all GET
requests
to path /translate
with query parameter word=hello
. The corresponding HTTP response will contain the text body
Привет
.
See the reference docs for detailed API documentation.
You can find examples in the
httpmock
test directory.
The reference docs also contain a lot of examples.
httpmock
is free software: you can redistribute it and/or modify it under the terms of the MIT Public License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MIT Public License for more details.