From 334aa67d402a11054f7f942b9df5b2f53ff773d1 Mon Sep 17 00:00:00 2001 From: David Rousselie Date: Mon, 26 Feb 2024 15:32:56 +0100 Subject: [PATCH] feat: Add MockServer reset function Sometimes it can be useful to change the behavior of the MockServer after some Mocks have already been setup. This PR adds a `reset()` function to clear the existing `Mock`s and their recorded calls history. --- src/api/server.rs | 42 +++++++++++++++++++++++++ tests/examples/getting_started_tests.rs | 39 +++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/src/api/server.rs b/src/api/server.rs index e665ede1..9560bae0 100644 --- a/src/api/server.rs +++ b/src/api/server.rs @@ -272,6 +272,48 @@ impl MockServer { server: self, } } + + /// Delete all [Mock](struct.Mock.html) object on the mock server and their call history. + /// + /// **Example**: + /// ``` + /// use isahc::get; + /// + /// let server = httpmock::MockServer::start(); + /// + /// let mock = server.mock(|when, then| { + /// when.path("/hello"); + /// then.status(200); + /// }); + /// + /// get(server.url("/hello")).unwrap(); + /// + /// mock.assert(); + /// + /// ... + /// + /// server.reset().await; + /// + /// let mock = server.mock(|when, then| { + /// when.path("/hello"); + /// then.status(404); + /// }); + /// + /// // This will now return a 404 + /// get(server.url("/hello")).unwrap(); + /// + /// mock.assert(); + /// ``` + pub async fn reset(&self) { + if let Some(server_adapter) = &self.server_adapter { + with_retry(5, || server_adapter.delete_all_mocks()) + .await + .expect("Cannot reset mock server (task: delete mocks)."); + with_retry(5, || server_adapter.delete_history()) + .await + .expect("Cannot reset mock server (task: delete request history)."); + } + } } impl Drop for MockServer { diff --git a/tests/examples/getting_started_tests.rs b/tests/examples/getting_started_tests.rs index 3c19badf..59342d7d 100644 --- a/tests/examples/getting_started_tests.rs +++ b/tests/examples/getting_started_tests.rs @@ -50,3 +50,42 @@ async fn async_getting_started_test() { // Ensure the mock server did respond as specified above. assert_eq!(response.status(), 200); } + +#[async_std::test] +async fn reset_test() { + // Start a lightweight mock server. + let server = MockServer::start(); + + // Create a mock on the server that will be reset later + server.mock(|when, then| { + when.method("GET") + .path("/translate") + .query_param("word", "hello"); + then.status(500) + .header("content-type", "text/html; charset=UTF-8") + .body("Привет"); + }); + + // Delete all previously created mocks + server.reset().await; + + // Create a new mock that will replace the previous one + let hello_mock = server.mock(|when, then| { + when.method("GET") + .path("/translate") + .query_param("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 = get(server.url("/translate?word=hello")).unwrap(); + + // Ensure the specified mock was called. + hello_mock.assert(); + + // Ensure the mock server did respond as specified. + assert_eq!(response.status(), 200); +} +