3

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.

4
  • I looked at it a little bit more and it seems that the PendingRequest in /primitives/core/src/offchain/testing.rs is created and inserted into the offchain state directly without the Request being sent in the function fn http_request_start. At least I was unable to found a code equivalent to Request::sent(...) which normally creates a PendingRequest instance. Commented Jul 1, 2023 at 18:45
  • I adopted 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. Commented Jul 2, 2023 at 21:46
  • This test might help you: github.com/paritytech/substrate/blob/master/client/executor/…
    – Alex Bean
    Commented Jul 3, 2023 at 7:34
  • @Alex Bean thank you for prompt reply. I adopted the suggested test for running under externalities, still it yields exactly the same result: the request is not sent and the /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? Commented Jul 3, 2023 at 14:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.