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

fixing bug with missing path type #4

Merged
merged 2 commits into from
May 16, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
run: echo y | sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"

- name: Start dfx
run: dfx start --background --clean
run: dfx start --background --clean --host 127.0.0.1:4943

- name: Deploy test canister
run: dfx deploy test
Expand Down
6 changes: 5 additions & 1 deletion dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
},
"test": {
"type": "motoko",
"main": "examples/test/test.mo"
"main": "examples/test/test.mo",
"declarations": {
"output": "declarations",
"node_compatibility": true
}
}
},
"defaults": {
Expand Down
2 changes: 1 addition & 1 deletion mops.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "server"
version = "0.2.1"
version = "0.2.2"
description = "An express-like server for Motoko"
repository = "https://github.com/krpeacock/server"

Expand Down
7 changes: 5 additions & 2 deletions src/lib.mo
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ module {

public type SerializedEntries = ([(HttpRequest, (HttpResponse, Nat))], [(AssetTypes.Key, Assets.StableAsset)], [Principal]);

public type Path = Assets.Path;
public type Contents = Assets.Contents;

// Public Functions

// Compare two requests
public func compareRequests(req1 : HttpRequest, req2 : HttpRequest) : Bool {
req1.url == req2.url;
Expand Down Expand Up @@ -392,7 +395,7 @@ module {
case (?response) {
{
status_code = response.status_code;
headers = Array.append(response.headers, [cache.certificationHeader(request)]);
headers = joinArrays(response.headers, [cache.certificationHeader(request)]);
body = response.body;
streaming_strategy = response.streaming_strategy;
upgrade = null;
Expand Down
23 changes: 0 additions & 23 deletions test/actor.js

This file was deleted.

94 changes: 48 additions & 46 deletions test/server.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import { expect, test, describe, afterAll } from "vitest";
import fetch from "isomorphic-fetch";
import canisterIds from "../.dfx/local/canister_ids.json";
const helloCanisterId = canisterIds.test.local;

const express = require("express");
const app = express();

app.get("/hi", (req, res) => {
res.send("hi");
app.get(`/hi`, (req, res) => {
res.send(`hi`);
});

app.get("/json", (req, res) => {
res.json({ hello: "world" });
app.get(`/json`, (req, res) => {
res.json({ hello: `world` });
});

app.get("/404", (req, res) => {
res.status(404).send("Not found");
app.get(`/404`, (req, res) => {
res.status(404).send(`Not found`);
});

app.get("/", (req, res) => {
res.send("<html><body><h1>hello world</h1></body></html>");
app.get(`/`, (req, res) => {
res.send(`<html><body><h1>hello world</h1></body></html>`);
});

app.get("/queryParams", (req, res) => {
app.get(`/queryParams`, (req, res) => {
res.json(req.query);
});

Expand All @@ -38,106 +40,106 @@ const awaitText = async (url, options) => {
return text;
};

test("should handle a basic greeting", async () => {
test(`should handle a basic greeting`, async () => {
const text = await awaitText(
"http://127.0.0.1:4943/hi?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai"
`http://127.0.0.1:4943/hi?canisterId=${helloCanisterId}`
);

expect(text).toBe("hi");
expect(text).toBe(`hi`);
});

test("should serve html", async () => {
test(`should serve html`, async () => {
const text = await awaitText(
"http://127.0.0.1:4943/?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai"
`http://127.0.0.1:4943/?canisterId=${helloCanisterId}`
);
expect(text).toMatchSnapshot();
});

describe("headers", () => {
test("plaintext", async () => {
describe(`headers`, () => {
test(`plaintext`, async () => {
const response = await fetch(
"http://127.0.0.1:4943/hi?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai"
`http://127.0.0.1:4943/hi?canisterId=${helloCanisterId}`
);
expect(response.headers.get("content-type")).toBe("text/plain");
expect(response.headers.get(`content-type`)).toBe(`text/plain`);
});

test.skip("json", async () => {
test.skip(`json`, async () => {
const response = await fetch(
"http://127.0.0.1:4943/json?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai"
`http://127.0.0.1:4943/json?canisterId=${helloCanisterId}`
);
expect(response.headers.get("content-type")).toBe("application/json");
expect(response.headers.get(`content-type`)).toBe(`application/json`);
});

test("html", async () => {
test(`html`, async () => {
const response = await fetch(
"http://127.0.0.1:4943/?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai"
`http://127.0.0.1:4943/?canisterId=${helloCanisterId}`
);
expect(response.headers.get("content-type")).toBe("text/html");
expect(response.headers.get(`content-type`)).toBe(`text/html`);
});

test.skip("404", async () => {
test.skip(`404`, async () => {
const response = await fetch(
"http://127.0.0.1:4943/404?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai"
`http://127.0.0.1:4943/404?canisterId=${helloCanisterId}`
);
expect(response.headers.get("content-type")).toBe("text/plain");
expect(response.headers.get(`content-type`)).toBe(`text/plain`);

const text = await response.text();
expect(text).toBe("404");
expect(text).toBe(`404`);

expect(response.status).toBe(404);
});
});

describe("compare with express", () => {
test("should handle a basic greeting", async () => {
const text = await awaitText("http://127.0.0.1:4999/hi");
describe(`compare with express`, () => {
test(`should handle a basic greeting`, async () => {
const text = await awaitText(`http://127.0.0.1:4999/hi`);
const canisterText = await awaitText(
"http://127.0.0.1:4943/hi?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai"
`http://127.0.0.1:4943/hi?canisterId=${helloCanisterId}`
);
expect(text).toBe(canisterText);
});

test("should serve json", async () => {
const json = await awaitJson("http://127.0.0.1:4999/json");
test(`should serve json`, async () => {
const json = await awaitJson(`http://127.0.0.1:4999/json`);
const canisterJson = await awaitJson(
"http://127.0.0.1:4943/json?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai"
`http://127.0.0.1:4943/json?canisterId=${helloCanisterId}`
);
expect(json).toEqual(canisterJson);
});

test("should serve html", async () => {
const text = await awaitText("http://127.0.0.1:4999/");
test(`should serve html`, async () => {
const text = await awaitText(`http://127.0.0.1:4999/`);
const canisterText = await awaitText(
"http://127.0.0.1:4943/?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai"
`http://127.0.0.1:4943/?canisterId=${helloCanisterId}`
);
expect(text).toBe(canisterText);
});

test("should serve 404", async () => {
const response = await fetch("http://127.0.0.1:4999/404");
test(`should serve 404`, async () => {
const response = await fetch(`http://127.0.0.1:4999/404`);
const canisterResponse = await fetch(
"http://127.0.0.1:4943/404?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai"
`http://127.0.0.1:4943/404?canisterId=${helloCanisterId}`
);

expect(response.status).toBe(canisterResponse.status);
expect(response.statusText).toBe(canisterResponse.statusText);
expect(await response.text()).toBe(await canisterResponse.text());
});

test("should handle query params", async () => {
test(`should handle query params`, async () => {
const json = await awaitJson(
"http://127.0.0.1:4999/queryParams?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai&foo=bar"
`http://127.0.0.1:4999/queryParams?canisterId=${helloCanisterId}&foo=bar`
);
const canisterJson = await awaitJson(
"http://127.0.0.1:4943/queryParams?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai&foo=bar"
`http://127.0.0.1:4943/queryParams?canisterId=${helloCanisterId}&foo=bar`
);
expect(json).toEqual(canisterJson);

const json2 = await awaitJson(
"http://127.0.0.1:4999/queryParams?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai&foo=bar&baz=qux"
`http://127.0.0.1:4999/queryParams?canisterId=${helloCanisterId}&foo=bar&baz=qux`
);
const canisterJson2 = await awaitJson(
"http://127.0.0.1:4943/queryParams?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai&foo=bar&baz=qux"
`http://127.0.0.1:4943/queryParams?canisterId=${helloCanisterId}&foo=bar&baz=qux`
);
expect(json2).toEqual(canisterJson2);
});
Expand Down