Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Fix mocking multiple http calls in the same function call #6510

Merged
merged 8 commits into from
Jul 1, 2020
8 changes: 6 additions & 2 deletions primitives/core/src/offchain/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ impl OffchainStorage for TestPersistentOffchainDB {
pub struct OffchainState {
/// A list of pending requests.
pub requests: BTreeMap<RequestId, PendingRequest>,
/// Request counter
pub request_counter: u16,
expected_requests: BTreeMap<RequestId, PendingRequest>,
/// Persistent local storage
pub persistent_storage: TestPersistentOffchainDB,
Expand Down Expand Up @@ -156,10 +158,12 @@ impl OffchainState {
}

fn fulfill_expected(&mut self, id: u16) {
if let Some(mut req) = self.expected_requests.remove(&RequestId(id)) {
let response = req.response.take().expect("Response checked while added.");
if let Some(mut req) = self.expected_requests.remove(&RequestId(self.request_counter)) {
let response = req.response.take().expect("Response checked when added.");
let headers = std::mem::take(&mut req.response_headers);
self.fulfill_pending_request(id, req, response, headers);

self.request_counter = self.request_counter.checked_add(1).expect("The max number of mocked requests is u16::MAX");
dvdplm marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down