Skip to content

Commit

Permalink
add fallback support and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomijaga committed Dec 10, 2024
1 parent c53b8e4 commit 0eb12dc
Show file tree
Hide file tree
Showing 16 changed files with 1,270 additions and 193 deletions.
29 changes: 17 additions & 12 deletions .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Makefile CI

on:
on:
push:
branches:
- main
Expand Down Expand Up @@ -35,23 +35,28 @@ jobs:
~/.cache/mops
~/mops
- uses: aviate-labs/setup-dfx@v0.2.3
with:
dfx-version: 0.15.0
- name: Install dfx
uses: dfinity/setup-dfx@main
- name: Confirm successful installation
run: dfx --version

- name: Install dfx cache
run: dfx cache install

- name: Install mops & mocv
run: |
npm -g i mocv
npm --yes -g i ic-mops
mops i
npm --yes -g i ic-mops
mops i
mops toolchain init
- name: install wasmtime
run: |
curl https://wasmtime.dev/install.sh -sSf | bash
echo "$HOME/.wasmtime/bin" >> $GITHUB_PATH
# set moc path for dfx to use
echo "DFX_MOC_PATH=$(mops toolchain bin moc)" >> $GITHUB_ENV
- name: Detect warnings
run: make check

- name: Install zx to run scripts
run: npm install -g zx

- name: Run Tests
run: make test
run: make canister-tests
17 changes: 11 additions & 6 deletions bench/lib.bench.mo
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Text "mo:base/Text";
import Iter "mo:base/Iter";
import Debug "mo:base/Debug";
import Prelude "mo:base/Prelude";
import Nat16 "mo:base/Nat16";
import Buffer "mo:base/Buffer";
import Sha256 "mo:sha2/Sha256";

import Bench "mo:bench";
import Fuzz "mo:fuzz";
Expand All @@ -13,7 +13,7 @@ import HttpTypes "mo:http-types";

module {

func random_endpoint(fuzz : Fuzz.Fuzzer) : (CertifiedAssets.EndpointRecord, Blob) {
func random_endpoint(fuzz : Fuzz.Fuzzer, sha256 : Sha256.Digest) : (CertifiedAssets.EndpointRecord, Blob) {

let status_codes = [200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429, 431, 451, 500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511];

Expand Down Expand Up @@ -57,10 +57,14 @@ module {
let include_random_blob = fuzz.nat.randomRange(0, 10) > 5;
let include_request_certification = fuzz.nat.randomRange(0, 10) > 10;

let endpoint = CertifiedAssets.Endpoint(path, null);

let body = if (include_random_blob) { blob } else { hello_world_blob };

sha256.writeBlob(body);
let hash = sha256.sum();
sha256.reset();

let endpoint = CertifiedAssets.Endpoint(path, null).hash(hash);

ignore endpoint.body(body);

if (include_status) {
Expand Down Expand Up @@ -109,14 +113,15 @@ module {
]);

let fuzz = Fuzz.Fuzz();
let sha256 = Sha256.Digest(#sha256);
let limit = 1000;
let cert_store = CertifiedAssets.init_stable_store();
let certs = CertifiedAssets.CertifiedAssets(?cert_store);
let certs = CertifiedAssets.CertifiedAssets(cert_store);
let endpoint_records = Buffer.Buffer<(CertifiedAssets.EndpointRecord)>(limit);
let endpoint_bodies = Buffer.Buffer<(Blob)>(limit);

for (_ in Iter.range(0, limit)) {
let (endpoint_record, blob) = random_endpoint(fuzz);
let (endpoint_record, blob) = random_endpoint(fuzz, sha256);
endpoint_records.add(endpoint_record);
endpoint_bodies.add(blob);
};
Expand Down
49 changes: 36 additions & 13 deletions demo/main.mo
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Array "mo:base/Array";
import Buffer "mo:base/Buffer";
import Debug "mo:base/Debug";
import Iter "mo:base/Iter";
import Text "mo:base/Text";
Expand All @@ -18,7 +17,7 @@ actor {
type Vector<A> = Vector.Vector<A>;

stable let st_certs = CertifiedAssets.init_stable_store();
let certs = CertifiedAssets.CertifiedAssets(?st_certs);
let certs = CertifiedAssets.CertifiedAssets(st_certs);

let { thash } = Map;

Expand Down Expand Up @@ -120,7 +119,7 @@ actor {
).status(200).is_fallback_path();
certs.certify(homepage_endpoint);

let homepage_endpoint2 = CertifiedAssets.Endpoint("/home", ?Text.encodeUtf8(_homepage)).no_request_certification().response_header(
let homepage_endpoint2 = CertifiedAssets.Endpoint("/home", ?Text.encodeUtf8(_homepage)).response_header(
"Content-Type",
"text/html",
).response_header(
Expand Down Expand Up @@ -192,6 +191,13 @@ actor {
};

certify_endpoints_page();

let hello_page = CertifiedAssets.Endpoint("/hello", ?Text.encodeUtf8("👋 Hello World!")).no_request_certification().response_header(
"Content-Type",
"text/plain",
).status(200);

certs.certify(hello_page);
};

func endpoints_json() : Text {
Expand Down Expand Up @@ -279,7 +285,7 @@ actor {
assert req.body == "";
assert req.method == "GET";

Debug.print("url: " # url.path.original);
Debug.print("http_request: " # debug_show req);

let response : HttpTypes.Response = switch (url.path.original, url.queryObj.get("size")) {
case ("/endpoints", _) {
Expand All @@ -291,6 +297,15 @@ actor {
upgrade = null;
};
};
case ("/hello", _) {
{
status_code = 200;
body = Text.encodeUtf8("👋 Hello World!");
headers = [("Content-Type", "text/plain")];
streaming_strategy = null;
upgrade = null;
};
};
case ("" or "/" or "/home" or "/home/" or "/h o m e" or "/home/index.html", _) {

let _homepage = homepage();
Expand All @@ -313,9 +328,13 @@ actor {
case (_) Debug.trap("Invalid size: " # text_size);
};

let teams = teams_json(?size);

Debug.print("teams: " # teams);

{
status_code = 200;
body = Text.encodeUtf8(teams_json(?size));
body = Text.encodeUtf8(teams);
headers = [("Content-Type", "application/json")];
streaming_strategy = null;
upgrade = null;
Expand All @@ -332,10 +351,18 @@ actor {
};
case (_) {
// path -> '/v1/teams/:team'
var response : HttpTypes.Response = {
status_code = 404;
body = Text.encodeUtf8("Not Found");
headers = [];
streaming_strategy = null;
upgrade = null;
};

if (Text.startsWith(url.path.original, #text "/v1/teams/")) {
let team = url.path.array[2];
if (Map.has(teams, thash, team)) {
return {
response := {
status_code = 200;
body = Text.encodeUtf8(single_team_json(team));
headers = [("Content-Type", "application/json")];
Expand All @@ -344,13 +371,9 @@ actor {
};
};
};
return {
status_code = 404;
body = Text.encodeUtf8("Not Found");
headers = [];
streaming_strategy = null;
upgrade = null;
};

response;

};
};

Expand Down
6 changes: 3 additions & 3 deletions dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"type": "motoko",
"main": "./demo/main.mo"
},
"actor_test": {
"canister-tests": {
"type": "motoko",
"main": "./tests/CertifiedAssets.Test.mo"
"main": "./tests/CertifiedAssets.CanisterTests.mo"
}
},
"networks": {
"local": {
"bind": "127.0.0.1:8000",
"bind": "127.0.0.1:4943",
"type": "ephemeral"
}
}
Expand Down
5 changes: 4 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ docs: set-moc-version
$(MocvPath)/mo-doc --format plain

bench: set-dfx-moc-path
mops bench
mops bench

canister-tests:
zx -i ./z-scripts/canister-tests.mjs
8 changes: 5 additions & 3 deletions mops.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ description = "A library for certifying assets served via HTTP, ensuring the sec
repository = "https://github.com/NatLabs/certified-assets"
version = "0.3.5"
license = "MIT"
keywords = ["assets", "http", "certification"]
keywords = [ "assets", "http", "certification" ]

[dependencies]
base = "0.12.1"
base = "0.13.3"
ic-certification = "0.1.3"
sha2 = "0.1.0"
http-parser = "0.3.3"
http-types = "1.0.1"
encoding = "https://github.com/aviate-labs/encoding.mo#v0.4.1@2711d18727e954b11afc0d37945608512b5fbce2"
serde = "3.1.0"
map = "9.0.1"
vector = "0.4.0"
vector = "0.4.1"

[dev-dependencies]
test = "2.0.0"
bench = "1.0.0"
fuzz = "0.2.1"
itertools = "0.2.2"

[toolchain]
wasmtime = "24.0.0"
moc = "0.13.2"
Loading

0 comments on commit 0eb12dc

Please sign in to comment.