-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reset functionality to mock server
- Loading branch information
1 parent
1ec91c8
commit 4d5284b
Showing
4 changed files
with
86 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use httpmock::prelude::*; | ||
use isahc::{get, get_async}; | ||
Check warning on line 2 in tests/examples/reset_tests.rs GitHub Actions / build-remote-ubuntu-latest-1.70.0
|
||
|
||
#[async_std::test] | ||
async fn reset_server_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; | ||
Check failure on line 20 in tests/examples/reset_tests.rs GitHub Actions / build-remote-ubuntu-latest-1.70.0
|
||
|
||
// 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); | ||
} | ||
|