mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-21 09:45:41 +00:00
Make TestOffchainExt panic on unknown requests (#3710)
* Make `TestOffchainExt` panic on unknown requests * Fix test
This commit is contained in:
@@ -1842,7 +1842,7 @@ mod tests {
|
|||||||
body: vec![1, 2, 3, 4],
|
body: vec![1, 2, 3, 4],
|
||||||
headers: vec![("X-Auth".to_owned(), "test".to_owned())],
|
headers: vec![("X-Auth".to_owned(), "test".to_owned())],
|
||||||
sent: true,
|
sent: true,
|
||||||
response: vec![1, 2, 3],
|
response: Some(vec![1, 2, 3]),
|
||||||
response_headers: vec![("X-Auth".to_owned(), "hello".to_owned())],
|
response_headers: vec![("X-Auth".to_owned(), "hello".to_owned())],
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ pub struct PendingRequest {
|
|||||||
/// Has the request been sent already.
|
/// Has the request been sent already.
|
||||||
pub sent: bool,
|
pub sent: bool,
|
||||||
/// Response body
|
/// Response body
|
||||||
pub response: Vec<u8>,
|
pub response: Option<Vec<u8>>,
|
||||||
/// Number of bytes already read from the response body.
|
/// Number of bytes already read from the response body.
|
||||||
pub read: usize,
|
pub read: usize,
|
||||||
/// Response headers
|
/// Response headers
|
||||||
@@ -89,7 +89,7 @@ impl State {
|
|||||||
*req,
|
*req,
|
||||||
expected,
|
expected,
|
||||||
);
|
);
|
||||||
req.response = response.into();
|
req.response = Some(response.into());
|
||||||
req.response_headers = response_headers.into_iter().collect();
|
req.response_headers = response_headers.into_iter().collect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -97,7 +97,7 @@ impl State {
|
|||||||
|
|
||||||
fn fulfill_expected(&mut self, id: u16) {
|
fn fulfill_expected(&mut self, id: u16) {
|
||||||
if let Some(mut req) = self.expected_requests.remove(&RequestId(id)) {
|
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![]);
|
let headers = std::mem::replace(&mut req.response_headers, vec![]);
|
||||||
self.fulfill_pending_request(id, req, response, headers);
|
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,
|
/// 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.
|
/// 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) {
|
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);
|
self.expected_requests.insert(RequestId(id), expected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -254,7 +257,8 @@ impl offchain::Externalities for TestOffchainExt {
|
|||||||
let state = self.0.read();
|
let state = self.0.read();
|
||||||
|
|
||||||
ids.iter().map(|id| match state.requests.get(id) {
|
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,
|
None => RequestStatus::Invalid,
|
||||||
_ => RequestStatus::Finished(200),
|
_ => RequestStatus::Finished(200),
|
||||||
}).collect()
|
}).collect()
|
||||||
@@ -281,13 +285,17 @@ impl offchain::Externalities for TestOffchainExt {
|
|||||||
) -> Result<usize, HttpError> {
|
) -> Result<usize, HttpError> {
|
||||||
let mut state = self.0.write();
|
let mut state = self.0.write();
|
||||||
if let Some(req) = state.requests.get_mut(&request_id) {
|
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.
|
// Remove the pending request as per spec.
|
||||||
state.requests.remove(&request_id);
|
state.requests.remove(&request_id);
|
||||||
Ok(0)
|
Ok(0)
|
||||||
} else {
|
} else {
|
||||||
let read = std::cmp::min(buffer.len(), req.response[req.read..].len());
|
let read = std::cmp::min(buffer.len(), response[req.read..].len());
|
||||||
buffer[0..read].copy_from_slice(&req.response[req.read..read]);
|
buffer[0..read].copy_from_slice(&response[req.read..read]);
|
||||||
req.read += read;
|
req.read += read;
|
||||||
Ok(read)
|
Ok(read)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user