Skip to content

Commit

Permalink
Offchain testing: Fix reading response (paritytech#10294)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkchr authored and grishasobol committed Mar 28, 2022
1 parent 79f95ab commit 9d69473
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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

0 comments on commit 9d69473

Please sign in to comment.