ic-pocket-canister-runtime 0.3.0

Canisters runtime on the Internet Computer using Pocket IC
Documentation

Internet Computer portal DFinity Forum GitHub license

ic-pocket-canister-runtime

Implementation of the ic_canister_runtime crate's Runtime trait for PocketIC allowing to mock HTTPs outcalls.

Usage

Add this to your Cargo.toml (see crates.io for the latest version):

ic-canister-runtime = "0.1.0"
ic-pocket-canister-runtime = "0.1.0"

Then, use the library to mock HTTP outcalls for canister deployed with PocketIC, as follows:

use ic_canister_runtime::Runtime;
use ic_pocket_canister_runtime::{
    AnyCanisterHttpRequestMatcher, CanisterHttpReply, MockHttpOutcallsBuilder,
    MockHttpRuntime
};
use pocket_ic::nonblocking::PocketIc;

let mocks = MockHttpOutcallsBuilder::new()
    .given(AnyCanisterHttpRequestMatcher)
    .respond_with(
        CanisterHttpReply::with_status(200)
            .with_body(r#"{"data": "Hello, World!", "headers": {"X-Id": "42"}}"#)
    );

let pocket_ic = PocketIc::new().await;
let runtime = MockHttpRuntime::new(&pocket_ic, Principal::anonymous())
    .with_http_mocks(mocks.build());

let http_request_result: String = runtime
    .update_call(canister_id, "make_http_post_request", (), 0)
    .await
    .expect("Call to `http_canister` failed");

assert!(http_request_result.contains("Hello, World!"));
assert!(http_request_result.contains("\"X-Id\": \"42\""));

See the Rust documentation for more details.

License

This project is licensed under the Apache License 2.0.