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

Offchain testing: Fix reading response #10294

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion primitives/core/src/offchain/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl offchain::Externalities for TestOffchainExt {
Ok(0)
} else {
let read = std::cmp::min(buffer.len(), response[req.read..].len());
buffer[0..read].copy_from_slice(&response[req.read..read]);
buffer[0..read].copy_from_slice(&response[req.read..req.read + read]);
req.read += read;
Ok(read)
}
Expand Down
32 changes: 32 additions & 0 deletions primitives/runtime/src/offchain/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,38 @@ mod tests {
})
}

#[test]
fn should_send_huge_response() {
let (offchain, state) = testing::TestOffchainExt::new();
let mut t = TestExternalities::default();
t.register_extension(OffchainWorkerExt::new(offchain));

t.execute_with(|| {
let request: Request = Request::get("http://localhost:1234");
let pending = request.add_header("X-Auth", "hunter2").send().unwrap();
// make sure it's sent correctly
state.write().fulfill_pending_request(
0,
testing::PendingRequest {
method: "GET".into(),
uri: "http://localhost:1234".into(),
headers: vec![("X-Auth".into(), "hunter2".into())],
sent: true,
..Default::default()
},
vec![0; 5923],
None,
);

// wait
let response = pending.wait().unwrap();

let body = response.body();
assert_eq!(body.clone().collect::<Vec<_>>(), vec![0; 5923]);
assert_eq!(body.error(), &None);
})
}

#[test]
fn should_send_a_post_request() {
let (offchain, state) = testing::TestOffchainExt::new();
Expand Down