Closed mreinigjr closed 2 years ago
You should be able to implement this using the up_to_n_times
method. If you create two mocks, each with up_to_n_times(1)
, wiremock uses the first mock for your first request and the second mock for the second request. Example:
use wiremock::matchers::method;
use wiremock::{Mock, MockServer, ResponseTemplate};
#[async_std::main]
async fn main() {
let mock_server = MockServer::start().await;
Mock::given(method("GET"))
.respond_with(ResponseTemplate::new(200).set_body_string("TOKEN_1".to_string()))
.up_to_n_times(1)
.mount(&mock_server)
.await;
Mock::given(method("GET"))
.respond_with(ResponseTemplate::new(200).set_body_string("TOKEN_2".to_string()))
.up_to_n_times(1)
.mount(&mock_server)
.await;
let body = surf::get(&mock_server.uri())
.await
.unwrap()
.body_string()
.await
.unwrap();
assert_eq!(&body, "TOKEN_1");
let body = surf::get(&mock_server.uri())
.await
.unwrap()
.body_string()
.await
.unwrap();
assert_eq!(&body, "TOKEN_2");
}
Sorry @mreinigjr, I meant to answer this but it fell out of my to-do list. The answer by @SebRollen is the recommended strategy to handle your needs.
Perfect! Thanks @SebRollen and @LukeMathWalker and thanks @LukeMathWalker for your book! It has been extremely helpful and I look forward to you completing it.
Is there a way to have different responses for consecutive api calls to the same endpoint and with the same request data? For example, simulating the refreshing of an api access token. Each call will be identical, but the response will come back with a new api access token and a new expiration date. Right now I am just confirming that I call the endpoint more than once, which really does suffice, but I was wondering if it would be possible to change the response with each consecutive call.