Skip to content
Snippets Groups Projects
Commit ba98168b authored by Bastian Köcher's avatar Bastian Köcher Committed by GitHub
Browse files

Make `TestOffchainExt` panic on unknown requests (#3710)

* Make `TestOffchainExt` panic on unknown requests

* Fix test
parent 0da34de3
No related merge requests found
......@@ -1842,7 +1842,7 @@ mod tests {
body: vec![1, 2, 3, 4],
headers: vec![("X-Auth".to_owned(), "test".to_owned())],
sent: true,
response: vec![1, 2, 3],
response: Some(vec![1, 2, 3]),
response_headers: vec![("X-Auth".to_owned(), "hello".to_owned())],
..Default::default()
},
......
......@@ -48,7 +48,7 @@ pub struct PendingRequest {
/// Has the request been sent already.
pub sent: bool,
/// Response body
pub response: Vec<u8>,
pub response: Option<Vec<u8>>,
/// Number of bytes already read from the response body.
pub read: usize,
/// Response headers
......@@ -89,7 +89,7 @@ impl State {
*req,
expected,
);
req.response = response.into();
req.response = Some(response.into());
req.response_headers = response_headers.into_iter().collect();
}
}
......@@ -97,7 +97,7 @@ impl State {
fn fulfill_expected(&mut self, id: u16) {
if let Some(mut req) = self.expected_requests.remove(&RequestId(id)) {
let response = std::mem::replace(&mut req.response, vec![]);
let response = req.response.take().expect("Response checked while added.");
let headers = std::mem::replace(&mut req.response_headers, vec![]);
self.fulfill_pending_request(id, req, response, headers);
}
......@@ -110,6 +110,9 @@ impl State {
/// Expected request has to be fulfilled before this struct is dropped,
/// the `response` and `response_headers` fields will be used to return results to the callers.
pub fn expect_request(&mut self, id: u16, expected: PendingRequest) {
if expected.response.is_none() {
panic!("Expected request needs to have a response.");
}
self.expected_requests.insert(RequestId(id), expected);
}
}
......@@ -254,7 +257,8 @@ impl offchain::Externalities for TestOffchainExt {
let state = self.0.read();
ids.iter().map(|id| match state.requests.get(id) {
Some(req) if req.response.is_empty() => RequestStatus::DeadlineReached,
Some(req) if req.response.is_none() =>
panic!("No `response` provided for request with id: {:?}", id),
None => RequestStatus::Invalid,
_ => RequestStatus::Finished(200),
}).collect()
......@@ -281,13 +285,17 @@ impl offchain::Externalities for TestOffchainExt {
) -> Result<usize, HttpError> {
let mut state = self.0.write();
if let Some(req) = state.requests.get_mut(&request_id) {
if req.read >= req.response.len() {
let response = req.response
.as_mut()
.expect(&format!("No response provided for request: {:?}", request_id));
if req.read >= response.len() {
// Remove the pending request as per spec.
state.requests.remove(&request_id);
Ok(0)
} else {
let read = std::cmp::min(buffer.len(), req.response[req.read..].len());
buffer[0..read].copy_from_slice(&req.response[req.read..read]);
let read = std::cmp::min(buffer.len(), response[req.read..].len());
buffer[0..read].copy_from_slice(&response[req.read..read]);
req.read += read;
Ok(read)
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment