Skip to content

Commit

Permalink
Test if contract rejects invalid oracle response
Browse files Browse the repository at this point in the history
A reminder that the oracle's HTTP response also needs to be verified.
  • Loading branch information
jvff committed Mar 8, 2025
1 parent 87fe665 commit 243e8ae
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/how-to/perform-http-requests/tests/http_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,33 @@ async fn contract_accepts_valid_http_response_from_oracle() -> anyhow::Result<()

Ok(())
}

/// Tests if the contract rejects an invalid HTTP response it obtains from the service acting as an
/// oracle.
#[test_log::test(tokio::test)]
#[should_panic(expected = "Failed to execute block")]
async fn contract_rejects_invalid_http_response_from_oracle() {
const HTTP_RESPONSE_BODY: &str = "Invalid response";

let http_server =
HttpServer::start(Router::new().route("/", get(|| async { HTTP_RESPONSE_BODY })))
.await
.expect("Failed to start test HTTP server");
let port = http_server.port();
let url = format!("http://localhost:{port}/");

let (validator, application_id, chain) =
TestValidator::with_current_application::<Abi, _, _>(url, ()).await;

validator
.change_resource_control_policy(|policy| {
policy
.http_request_allow_list
.insert("localhost".to_owned());
})
.await;

chain
.graphql_mutation(application_id, "mutation { performHttpRequestAsOracle }")
.await;
}

0 comments on commit 243e8ae

Please sign in to comment.