Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(ic-http-certification): add more tests to example projects #399

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 47 additions & 15 deletions examples/http-certification/assets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ thread_local! {
}

const IMMUTABLE_ASSET_CACHE_CONTROL: &str = "public, max-age=31536000, immutable";
const NO_CACHE_ASSET_CACHE_CONTROL: &str = "public, no-cache, no-store";

fn certify_all_assets() {
// 1. Define the asset certification configurations.
Expand All @@ -188,7 +189,7 @@ fn certify_all_assets() {
content_type: Some("text/html".to_string()),
headers: get_asset_headers(vec![(
"cache-control".to_string(),
"public, no-cache, no-store".to_string(),
NO_CACHE_ASSET_CACHE_CONTROL.to_string(),
)]),
fallback_for: vec![AssetFallbackConfig {
scope: "/".to_string(),
Expand Down Expand Up @@ -224,15 +225,6 @@ fn certify_all_assets() {
)]),
encodings: vec![],
},
AssetConfig::Pattern {
pattern: "**/*.svg".to_string(),
content_type: Some("image/svg+xml".to_string()),
headers: get_asset_headers(vec![(
"cache-control".to_string(),
IMMUTABLE_ASSET_CACHE_CONTROL.to_string(),
)]),
encodings: vec![],
},
AssetConfig::Redirect {
from: "/old-url".to_string(),
to: "/".to_string(),
Expand Down Expand Up @@ -304,9 +296,21 @@ fn serve_metrics() -> HttpResponse<'static> {
cycle_balance: canister_balance(),
};
let body = serde_json::to_vec(&metrics).expect("Failed to serialize metrics");
let headers = get_asset_headers(vec![
(
CERTIFICATE_EXPRESSION_HEADER_NAME.to_string(),
DefaultCelBuilder::skip_certification().to_string(),
),
("content-type".to_string(), "application/json".to_string()),
(
"cache-control".to_string(),
NO_CACHE_ASSET_CACHE_CONTROL.to_string(),
),
]);
let mut response = HttpResponse::builder()
.with_status_code(200)
.with_body(body)
.with_headers(headers)
.build();

HTTP_TREE.with(|tree| {
Expand All @@ -323,13 +327,41 @@ fn serve_metrics() -> HttpResponse<'static> {
&metrics_tree_path.to_expr_path(),
);

let headers = get_asset_headers(vec![(
CERTIFICATE_EXPRESSION_HEADER_NAME.to_string(),
DefaultCelBuilder::skip_certification().to_string(),
)]);
response.headers_mut().extend_from_slice(&headers);
response
})
})
}
```

## Testing the canister

To test the canister, you can use the `dfx` command-line tool. First, run DFX:

```shell
dfx start --background --clean
```

Then, deploy the canister:

```shell
dfx deploy http_certification_assets_backend
```

You can now access the canister's assets by navigating to the canister's URL in a web browser. The URL can also be found using the following command:

```shell
echo "http://$(dfx canister id http_certification_assets_backend).localhost:$(dfx info webserver-port)"
```

Alternatively, to make a request with `curl`:

```shell
curl "http://$(dfx canister id http_certification_assets_backend).localhost:$(dfx info webserver-port)" --resolve "$(dfx canister id http_certification_assets_backend).localhost:$(dfx info webserver-port):127.0.0.1"
```

## Resources

- [Example source code](https://github.com/dfinity/response-verification/tree/main/examples/http-certification/assets).
- [`ic-asset-certification` crate](https://crates.io/crates/ic-asset-certification).
- [`ic-asset-certification` docs](https://docs.rs/ic-asset-certification/latest/ic_asset_certification).
- [`ic-asset-certification` source code](https://github.com/dfinity/response-verification/tree/main/packages/ic-asset-certification).
29 changes: 14 additions & 15 deletions examples/http-certification/assets/src/backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ thread_local! {

static ASSETS_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/../frontend/dist");
const IMMUTABLE_ASSET_CACHE_CONTROL: &str = "public, max-age=31536000, immutable";
const NO_CACHE_ASSET_CACHE_CONTROL: &str = "public, no-cache, no-store";

/// Rescursively collect all assets from the provided directory
fn collect_assets<'content, 'path>(
Expand Down Expand Up @@ -91,7 +92,7 @@ fn certify_all_assets() {
content_type: Some("text/html".to_string()),
headers: get_asset_headers(vec![(
"cache-control".to_string(),
"public, no-cache, no-store".to_string(),
NO_CACHE_ASSET_CACHE_CONTROL.to_string(),
)]),
fallback_for: vec![AssetFallbackConfig {
scope: "/".to_string(),
Expand Down Expand Up @@ -127,15 +128,6 @@ fn certify_all_assets() {
)]),
encodings: vec![],
},
AssetConfig::Pattern {
pattern: "**/*.svg".to_string(),
content_type: Some("image/svg+xml".to_string()),
headers: get_asset_headers(vec![(
"cache-control".to_string(),
IMMUTABLE_ASSET_CACHE_CONTROL.to_string(),
)]),
encodings: vec![],
},
AssetConfig::Redirect {
from: "/old-url".to_string(),
to: "/".to_string(),
Expand Down Expand Up @@ -179,9 +171,21 @@ fn serve_metrics() -> HttpResponse<'static> {
cycle_balance: canister_balance(),
};
let body = serde_json::to_vec(&metrics).expect("Failed to serialize metrics");
let headers = get_asset_headers(vec![
(
CERTIFICATE_EXPRESSION_HEADER_NAME.to_string(),
DefaultCelBuilder::skip_certification().to_string(),
),
("content-type".to_string(), "application/json".to_string()),
(
"cache-control".to_string(),
NO_CACHE_ASSET_CACHE_CONTROL.to_string(),
),
]);
let mut response = HttpResponse::builder()
.with_status_code(StatusCode::OK)
.with_body(body)
.with_headers(headers)
.build();

HTTP_TREE.with(|tree| {
Expand All @@ -198,11 +202,6 @@ fn serve_metrics() -> HttpResponse<'static> {
&metrics_tree_path.to_expr_path(),
);

let headers = get_asset_headers(vec![(
CERTIFICATE_EXPRESSION_HEADER_NAME.to_string(),
DefaultCelBuilder::skip_certification().to_string(),
)]);
response.headers_mut().extend_from_slice(&headers);
response
})
})
Expand Down
Loading
Loading