Skip to content

Commit

Permalink
Add reset functionality to mock server
Browse files Browse the repository at this point in the history
  • Loading branch information
alexliesenfeld committed Feb 28, 2024
1 parent ddc95d9 commit acfd732
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Version 0.7.1

- A new reset method was added that resets a mock server. Thanks for providing the [pull request](https://github.com/alexliesenfeld/httpmock/pull/100) for this feature, [@dax](https://github.com/dax).
- A new [MockServer::reset](https://docs.rs/httpmock/latest/httpmock/struct.MockServer.html#method.reset) method was added that resets a mock server. Thanks for providing the [pull request](https://github.com/alexliesenfeld/httpmock/pull/100) for this feature, [@dax](https://github.com/dax).

## Version 0.7.0

Expand Down
27 changes: 11 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
## Features

* Simple, expressive, fluent API.
* Many built-in helpers for easy request matching.
* Many built-in helpers for easy request matching ([Regex](https://docs.rs/regex/), JSON, [serde](https://crates.io/crates/serde), cookies, and more).
* Parallel test execution.
* Extensible request matching.
* Fully asynchronous core with synchronous and asynchronous APIs.
* [Advanced verification and debugging support](https://alexliesenfeld.github.io/posts/mocking-http--services-in-rust/#creating-mocks).
* Network delay simulation.
* [Advanced verification and debugging support](https://alexliesenfeld.github.io/posts/mocking-http--services-in-rust/#creating-mocks) (including diff generation between actual and expected HTTP request values)
* Fault and network delay simulation.
* Support for [Regex](https://docs.rs/regex/) matching, JSON, [serde](https://crates.io/crates/serde), cookies, and more.
* Standalone mode with an accompanying [Docker image](https://hub.docker.com/r/alexliesenfeld/httpmock).
* Support for [mock specification based on YAML files](https://github.com/alexliesenfeld/httpmock/tree/master#file-based-mock-specification).
* Support for [mock configuration using YAML files](https://github.com/alexliesenfeld/httpmock/tree/master#file-based-mock-specification).

## Getting Started

Expand All @@ -59,7 +59,7 @@ use httpmock::prelude::*;
let server = MockServer::start();

// Create a mock on the server.
let hello_mock = server.mock(|when, then| {
let mock = server.mock(|when, then| {
when.method(GET)
.path("/translate")
.query_param("word", "hello");
Expand All @@ -73,7 +73,7 @@ let response = isahc::get(server.url("/translate?word=hello")).unwrap();

// Ensure the specified mock was called exactly one time (or fail with a
// detailed error description).
hello_mock.assert();
mock.assert();

// Ensure the mock server did respond as specified.
assert_eq!(response.status(), 200);
Expand All @@ -83,6 +83,11 @@ The above example will spin up a lightweight HTTP mock server and configure it t
to path `/translate` with query parameter `word=hello`. The corresponding HTTP response will contain the text body
`Привет`.

In case the request fails, `httpmock` would show you a detailed error description including a diff between the
expected and the actual HTTP request:

![colored-diff.png](docs/diff.png)

# Usage

See the [reference docs](https://docs.rs/httpmock/) for detailed API documentation.
Expand All @@ -105,16 +110,6 @@ This is especially useful if you want to use `httpmock` in system or end-to-end

Please refer to [the docs](https://docs.rs/httpmock/0.5.8/httpmock/#standalone-mode) for more information

### File Based Mock Specification

For convenience, the standalone mode also allows you to use YAML files for mock specification, so you do not need to
use Rust or any other programming language at all. The mock specification file schema is very similar to the `httpmock`
Rust API, so it's easy to jump between the two. Please find an example mock specification file
[here](https://github.com/alexliesenfeld/httpmock/blob/master/tests/resources/static_yaml_mock.yaml).

Please refer to [the docs](https://github.com/alexliesenfeld/httpmock/blob/master/src/lib.rs#L185-L201)
for more information.

## License

`httpmock` is free software: you can redistribute it and/or modify it under the terms of the MIT Public License.
Expand Down
Binary file added docs/diff.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/examples/getting_started_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async fn async_getting_started_test() {
.await;

// Send an HTTP request to the mock server. This simulates your code.
let url = format!("http://{}/hello", server.address());
let url = format!("http://{}/hello-rustaceans", server.address());
let response = get_async(&url).await.unwrap();

// Ensure the specified mock responded exactly one time.
Expand Down
4 changes: 2 additions & 2 deletions tests/examples/reset_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use httpmock::prelude::*;
use isahc::{get, get_async};
use isahc::get;

#[async_std::test]
async fn reset_server_test() {
Expand All @@ -17,7 +17,7 @@ async fn reset_server_test() {
});

// Delete all previously created mocks
server.reset().await;
server.reset_async().await;

// Create a new mock that will replace the previous one
let hello_mock = server.mock(|when, then| {
Expand Down

0 comments on commit acfd732

Please sign in to comment.