Skip to content

Commit

Permalink
Merge pull request #100 from dax/add-mockserver-reset
Browse files Browse the repository at this point in the history
feat: Add MockServer reset function
  • Loading branch information
alexliesenfeld committed Feb 27, 2024
2 parents 228845d + 334aa67 commit 1ec91c8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
///
/// ...

Check failure on line 293 in src/api/server.rs

View workflow job for this annotation

GitHub Actions / build-remote-ubuntu-latest-1.70.0

unexpected token: `...`

Check failure on line 293 in src/api/server.rs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest-1.70.0

unexpected token: `...`
///
/// server.reset().await;

Check failure on line 295 in src/api/server.rs

View workflow job for this annotation

GitHub Actions / build-remote-ubuntu-latest-1.70.0

`await` is only allowed inside `async` functions and blocks

Check failure on line 295 in src/api/server.rs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest-1.70.0

`await` is only allowed inside `async` functions and blocks
///
/// 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 {
Expand Down
39 changes: 39 additions & 0 deletions tests/examples/getting_started_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 1ec91c8

Please sign in to comment.