Skip to content

Commit b8aa830

Browse files
authored
test(ic-http-certification): add more tests to example projects (#399)
#395 must be merged before this one
1 parent 911afa6 commit b8aa830

File tree

15 files changed

+828
-169
lines changed

15 files changed

+828
-169
lines changed

Cargo.lock

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/http-certification/assets/README.md

+47-15
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ thread_local! {
174174
}
175175

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

178179
fn certify_all_assets() {
179180
// 1. Define the asset certification configurations.
@@ -188,7 +189,7 @@ fn certify_all_assets() {
188189
content_type: Some("text/html".to_string()),
189190
headers: get_asset_headers(vec![(
190191
"cache-control".to_string(),
191-
"public, no-cache, no-store".to_string(),
192+
NO_CACHE_ASSET_CACHE_CONTROL.to_string(),
192193
)]),
193194
fallback_for: vec![AssetFallbackConfig {
194195
scope: "/".to_string(),
@@ -224,15 +225,6 @@ fn certify_all_assets() {
224225
)]),
225226
encodings: vec![],
226227
},
227-
AssetConfig::Pattern {
228-
pattern: "**/*.svg".to_string(),
229-
content_type: Some("image/svg+xml".to_string()),
230-
headers: get_asset_headers(vec![(
231-
"cache-control".to_string(),
232-
IMMUTABLE_ASSET_CACHE_CONTROL.to_string(),
233-
)]),
234-
encodings: vec![],
235-
},
236228
AssetConfig::Redirect {
237229
from: "/old-url".to_string(),
238230
to: "/".to_string(),
@@ -304,9 +296,21 @@ fn serve_metrics() -> HttpResponse<'static> {
304296
cycle_balance: canister_balance(),
305297
};
306298
let body = serde_json::to_vec(&metrics).expect("Failed to serialize metrics");
299+
let headers = get_asset_headers(vec![
300+
(
301+
CERTIFICATE_EXPRESSION_HEADER_NAME.to_string(),
302+
DefaultCelBuilder::skip_certification().to_string(),
303+
),
304+
("content-type".to_string(), "application/json".to_string()),
305+
(
306+
"cache-control".to_string(),
307+
NO_CACHE_ASSET_CACHE_CONTROL.to_string(),
308+
),
309+
]);
307310
let mut response = HttpResponse::builder()
308311
.with_status_code(200)
309312
.with_body(body)
313+
.with_headers(headers)
310314
.build();
311315

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

326-
let headers = get_asset_headers(vec![(
327-
CERTIFICATE_EXPRESSION_HEADER_NAME.to_string(),
328-
DefaultCelBuilder::skip_certification().to_string(),
329-
)]);
330-
response.headers_mut().extend_from_slice(&headers);
331330
response
332331
})
333332
})
334333
}
335334
```
335+
336+
## Testing the canister
337+
338+
To test the canister, you can use the `dfx` command-line tool. First, run DFX:
339+
340+
```shell
341+
dfx start --background --clean
342+
```
343+
344+
Then, deploy the canister:
345+
346+
```shell
347+
dfx deploy http_certification_assets_backend
348+
```
349+
350+
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:
351+
352+
```shell
353+
echo "http://$(dfx canister id http_certification_assets_backend).localhost:$(dfx info webserver-port)"
354+
```
355+
356+
Alternatively, to make a request with `curl`:
357+
358+
```shell
359+
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"
360+
```
361+
362+
## Resources
363+
364+
- [Example source code](https://github.com/dfinity/response-verification/tree/main/examples/http-certification/assets).
365+
- [`ic-asset-certification` crate](https://crates.io/crates/ic-asset-certification).
366+
- [`ic-asset-certification` docs](https://docs.rs/ic-asset-certification/latest/ic_asset_certification).
367+
- [`ic-asset-certification` source code](https://github.com/dfinity/response-verification/tree/main/packages/ic-asset-certification).

examples/http-certification/assets/src/backend/src/lib.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ thread_local! {
6262

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

6667
/// Rescursively collect all assets from the provided directory
6768
fn collect_assets<'content, 'path>(
@@ -91,7 +92,7 @@ fn certify_all_assets() {
9192
content_type: Some("text/html".to_string()),
9293
headers: get_asset_headers(vec![(
9394
"cache-control".to_string(),
94-
"public, no-cache, no-store".to_string(),
95+
NO_CACHE_ASSET_CACHE_CONTROL.to_string(),
9596
)]),
9697
fallback_for: vec![AssetFallbackConfig {
9798
scope: "/".to_string(),
@@ -127,15 +128,6 @@ fn certify_all_assets() {
127128
)]),
128129
encodings: vec![],
129130
},
130-
AssetConfig::Pattern {
131-
pattern: "**/*.svg".to_string(),
132-
content_type: Some("image/svg+xml".to_string()),
133-
headers: get_asset_headers(vec![(
134-
"cache-control".to_string(),
135-
IMMUTABLE_ASSET_CACHE_CONTROL.to_string(),
136-
)]),
137-
encodings: vec![],
138-
},
139131
AssetConfig::Redirect {
140132
from: "/old-url".to_string(),
141133
to: "/".to_string(),
@@ -179,9 +171,21 @@ fn serve_metrics() -> HttpResponse<'static> {
179171
cycle_balance: canister_balance(),
180172
};
181173
let body = serde_json::to_vec(&metrics).expect("Failed to serialize metrics");
174+
let headers = get_asset_headers(vec![
175+
(
176+
CERTIFICATE_EXPRESSION_HEADER_NAME.to_string(),
177+
DefaultCelBuilder::skip_certification().to_string(),
178+
),
179+
("content-type".to_string(), "application/json".to_string()),
180+
(
181+
"cache-control".to_string(),
182+
NO_CACHE_ASSET_CACHE_CONTROL.to_string(),
183+
),
184+
]);
182185
let mut response = HttpResponse::builder()
183186
.with_status_code(StatusCode::OK)
184187
.with_body(body)
188+
.with_headers(headers)
185189
.build();
186190

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

201-
let headers = get_asset_headers(vec![(
202-
CERTIFICATE_EXPRESSION_HEADER_NAME.to_string(),
203-
DefaultCelBuilder::skip_certification().to_string(),
204-
)]);
205-
response.headers_mut().extend_from_slice(&headers);
206205
response
207206
})
208207
})

0 commit comments

Comments
 (0)