I'm trying to arrange some unit tests for a local http server connectivity my node communicates with.
While the node
has no troubles while sending requests, I face difficulties trying to build basic unit tests for this functionality.
My code looks as follows:
let (mut offchain, _offchain_state) = TestOffchainExt::with_offchain_db(ext.offchain_db());
ext.register_extension(OffchainWorkerExt::new(offchain.clone()));
if let Ok(request) = offchain.http_request_start("GET", "http://127.0.0.1:9800", &[]) {
offchain.http_response_wait(&[request], None);
}
which yields a panic in
primitives/core/src/offchain/testing.rs -> fn http_response_wait(...)
I also tried to run a similar logic inside the externalities context like so:
ext.register_extension(OffchainWorkerExt::new(offchain.clone()));
ext.execute_with(|| {
Request::get("http://127.0.0.1:9800").send().map(PendingRequest::wait);
// snip
with exactly the same result.
And I can see that on the server side no request arrives. On the other hand when I run the node everything works fine, so I guess this is not a server problem and I must have missed something.
PendingRequest
in/primitives/core/src/offchain/testing.rs
is created and inserted into the offchain state directly without the Request being sent in the functionfn http_request_start
. At least I was unable to found a code equivalent toRequest::sent(...)
which normally creates aPendingRequest
instance.should_send_a_basic_request_and_get_response
test from/primitives/runtime/src/offchain/http.rs
which manipulates the state and fullfills the request, unfortunately, it seems to pass always, no matter what the port number is and even if the local server is down./primitives/core/src/offchain/testing.rs/http_request_start
calls for a panic!. Even if it would succeed, still my underlying problem is that my pallet uses offchain worker that implements a protocol based on http and, at this point, it looks like I would need to modify the working code in order to just adapt it for testing. I'm more inclined to try to modify relevant code in Substrate itself, could you give hints?