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: update #7

Merged
merged 1 commit into from Jan 31, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,7 +1,7 @@
language: python

install:
- curl -L https://deno.land/x/install/install.py | python - v0.2.8
- curl -L https://deno.land/x/install/install.py | python - v0.2.9
- export PATH="$HOME/.deno/bin:$PATH"

script:
Expand Down
72 changes: 37 additions & 35 deletions abc_test.ts
@@ -1,5 +1,4 @@
import { assertEqual } from "https://deno.land/x/testing/mod.ts";
import { t } from "https://raw.githubusercontent.com/zhmushan/deno_test/master/index.ts";
import { assertEqual, test } from "https://deno.land/x/testing/mod.ts";
import { abc } from "abc.ts";
import { exit } from "deno";

Expand All @@ -22,45 +21,48 @@ const methods = [
"TRACE"
];

t("abc handler", async () => {
const app = abc();
app
.any("/string", c => data.string)
.any("/html", c => data.html)
.any("/json", c => data.json)
.any("/undefined_0", c => undefined)
.any("/undefined_1", c => {
c.string(data.undefined);
})
.start("0.0.0.0:4500");
test({
name: "abc handler",
async fn() {
const app = abc();
app
.any("/string", c => data.string)
.any("/html", c => data.html)
.any("/json", c => data.json)
.any("/undefined_0", c => undefined)
.any("/undefined_1", c => {
c.string(data.undefined);
})
.start("0.0.0.0:4500");

let res = await fetch("http://localhost:4500/string");
assertEqual(res.status, 200);
assertEqual(new TextDecoder().decode(await res.arrayBuffer()), data.string);
let res = await fetch("http://localhost:4500/string");
assertEqual(res.status, 200);
assertEqual(new TextDecoder().decode(await res.arrayBuffer()), data.string);

res = await fetch("http://localhost:4500/html");
assertEqual(res.status, 200);
assertEqual(new TextDecoder().decode(await res.arrayBuffer()), data.html);
res = await fetch("http://localhost:4500/html");
assertEqual(res.status, 200);
assertEqual(new TextDecoder().decode(await res.arrayBuffer()), data.html);

res = await fetch("http://localhost:4500/json");
assertEqual(res.status, 200);
assertEqual(
new TextDecoder().decode(await res.arrayBuffer()),
JSON.stringify(data.json)
);
res = await fetch("http://localhost:4500/json");
assertEqual(res.status, 200);
assertEqual(
new TextDecoder().decode(await res.arrayBuffer()),
JSON.stringify(data.json)
);

res = await fetch("http://localhost:4500/undefined_0");
assertEqual(res.status, 200);
assertEqual(new TextDecoder().decode(await res.arrayBuffer()), "");
res = await fetch("http://localhost:4500/undefined_0");
assertEqual(res.status, 200);
assertEqual(new TextDecoder().decode(await res.arrayBuffer()), "");

res = await fetch("http://localhost:4500/undefined_1");
assertEqual(res.status, 200);
assertEqual(
new TextDecoder().decode(await res.arrayBuffer()),
data.undefined
);
res = await fetch("http://localhost:4500/undefined_1");
assertEqual(res.status, 200);
assertEqual(
new TextDecoder().decode(await res.arrayBuffer()),
data.undefined
);

maybeCompleteTests();
maybeCompleteTests();
}
});

function maybeCompleteTests() {
Expand Down
72 changes: 40 additions & 32 deletions binder_test.ts
@@ -1,5 +1,4 @@
import { t } from "https://raw.githubusercontent.com/zhmushan/deno_test/master/index.ts";
import { assertEqual } from "https://deno.land/x/testing/mod.ts";
import { assertEqual, test } from "https://deno.land/x/testing/mod.ts";
import { binder } from "binder.ts";
import { context } from "context.ts";

Expand All @@ -16,38 +15,47 @@ class Obj {
}
}

t("binder urlencoded", async () => {
const c = injectContext({
body: () => new TextEncoder().encode("foo=foo"),
headers: new Headers({
"Content-Type": "application/x-www-form-urlencoded"
})
});
const obj = new Obj();
await binder().bind(obj, c);
assertEqual(obj, { foo: "foo", bar: undefined });
test({
name: "binder urlencoded",
async fn() {
const c = injectContext({
body: () => new TextEncoder().encode("foo=foo"),
headers: new Headers({
"Content-Type": "application/x-www-form-urlencoded"
})
});
const obj = new Obj();
await binder().bind(obj, c);
assertEqual(obj, { foo: "foo", bar: undefined });
}
});

t("binder json", async () => {
const c = injectContext({
body: () => new TextEncoder().encode(`{"foo": "foo"}`),
headers: new Headers({
"Content-Type": "application/json"
})
});
const obj = new Obj();
await binder().bind(obj, c);
assertEqual(obj, { foo: "foo", bar: undefined });
test({
name: "binder json",
async fn() {
const c = injectContext({
body: () => new TextEncoder().encode(`{"foo": "foo"}`),
headers: new Headers({
"Content-Type": "application/json"
})
});
const obj = new Obj();
await binder().bind(obj, c);
assertEqual(obj, { foo: "foo", bar: undefined });
}
});

// t("binder multipart", async () => {
// const c = injectContext({
// body: () => new TextEncoder().encode("foo=foo"),
// headers: new Headers({
// "Content-Type": "multipart/form-data"
// })
// });
// const obj = new Obj();
// await binder().bind(obj, c);
// assertEqual(obj, { foo: "foo", bar: undefined });
// test({
// name: "binder multipart",
// async fn() {
// const c = injectContext({
// body: () => new TextEncoder().encode("foo=foo"),
// headers: new Headers({
// "Content-Type": "multipart/form-data"
// })
// });
// const obj = new Obj();
// await binder().bind(obj, c);
// assertEqual(obj, { foo: "foo", bar: undefined });
// }
// });
54 changes: 31 additions & 23 deletions context_test.ts
@@ -1,34 +1,42 @@
import { t } from "https://raw.githubusercontent.com/zhmushan/deno_test/master/index.ts";
import { context } from "context.ts";
import { assertEqual } from "https://deno.land/x/testing/mod.ts";
import { assertEqual, test } from "https://deno.land/x/testing/mod.ts";

function injectContext(r = {}) {
return context(r as any);
}

t("context string", () => {
let c = injectContext();
c.string("{foo: 'bar'}");
assertEqual(c.response.status, 200);
assertEqual(c.response.body, new TextEncoder().encode("{foo: 'bar'}"));
assertEqual(c.response.headers.get("Content-Type"), "text/plain");
test({
name: "context string",
fn() {
let c = injectContext();
c.string("{foo: 'bar'}");
assertEqual(c.response.status, 200);
assertEqual(c.response.body, new TextEncoder().encode("{foo: 'bar'}"));
assertEqual(c.response.headers.get("Content-Type"), "text/plain");
}
});

t("context json", () => {
let c = injectContext();
c.json({ foo: "bar" });
assertEqual(c.response.status, 200);
assertEqual(
c.response.body,
new TextEncoder().encode(JSON.stringify({ foo: "bar" }))
);
assertEqual(c.response.headers.get("Content-Type"), "application/json");
test({
name: "context json",
fn() {
let c = injectContext();
c.json({ foo: "bar" });
assertEqual(c.response.status, 200);
assertEqual(
c.response.body,
new TextEncoder().encode(JSON.stringify({ foo: "bar" }))
);
assertEqual(c.response.headers.get("Content-Type"), "application/json");
}
});

t("context html", () => {
let c = injectContext();
c.html("{foo: 'bar'}");
assertEqual(c.response.status, 200);
assertEqual(c.response.body, new TextEncoder().encode("{foo: 'bar'}"));
assertEqual(c.response.headers.get("Content-Type"), "text/html");
test({
name: "context html",
fn() {
let c = injectContext();
c.html("{foo: 'bar'}");
assertEqual(c.response.status, 200);
assertEqual(c.response.body, new TextEncoder().encode("{foo: 'bar'}"));
assertEqual(c.response.headers.get("Content-Type"), "text/html");
}
});
5 changes: 1 addition & 4 deletions denolib.json
@@ -1,9 +1,6 @@
{
"name": "abc",
"author": "木杉",
"keywords": [
"http",
"framework"
],
"keywords": ["http", "framework"],
"entry": "mod.ts"
}
36 changes: 22 additions & 14 deletions parser_test.ts
@@ -1,24 +1,32 @@
import { t } from "https://raw.githubusercontent.com/zhmushan/deno_test/master/index.ts";
import { Parser } from "parser.ts";
import { assertEqual } from "https://deno.land/x/testing/mod.ts";
import { assertEqual, test } from "https://deno.land/x/testing/mod.ts";

t("parser urlencoded", () => {
const urlencodedCases: [[string, {}]] = [[`foo=bar`, { foo: "bar" }]];
for (const c of urlencodedCases) {
assertEqual(Parser.urlencoded(c[0]), [c[1], undefined]);
test({
name: "parser urlencoded",
fn() {
const urlencodedCases: [[string, {}]] = [[`foo=bar`, { foo: "bar" }]];
for (const c of urlencodedCases) {
assertEqual(Parser.urlencoded(c[0]), [c[1], undefined]);
}
}
});

t("parser json", () => {
const jsonCases: [[string, {}]] = [[`{"foo": "bar"}`, { foo: "bar" }]];
for (const c of jsonCases) {
assertEqual(Parser.json(c[0]), [c[1], undefined]);
test({
name: "parser json",
fn() {
const jsonCases: [[string, {}]] = [[`{"foo": "bar"}`, { foo: "bar" }]];
for (const c of jsonCases) {
assertEqual(Parser.json(c[0]), [c[1], undefined]);
}
}
});

// t("parser multipart", () => {
// const multipartCases: [[string, {}]] = [[`{"foo": "bar"}`, { foo: "bar" }]];
// for (const c of multipartCases) {
// assertEqual(Parser.multipart(c[0]), [c[1], undefined]);
// test({
// name: "parser multipart",
// fn() {
// const multipartCases: [[string, {}]] = [[`{"foo": "bar"}`, { foo: "bar" }]];
// for (const c of multipartCases) {
// assertEqual(Parser.multipart(c[0]), [c[1], undefined]);
// }
// }
// });
26 changes: 14 additions & 12 deletions router_test.ts
@@ -1,17 +1,19 @@
import { assertEqual } from "https://deno.land/x/testing/mod.ts";
import { t } from "https://raw.githubusercontent.com/zhmushan/deno_test/master/index.ts";
import { assertEqual, test } from "https://deno.land/x/testing/mod.ts";
import { Router } from "router.ts";
import { context } from "context.ts";

t("router", async () => {
const r = new Router();
r.add("GET", "/hello", async c => true);
let c = context({} as any);
assertEqual(await r.find("GET", "/hello", c)(c), true);
test({
name: "router",
async fn() {
const r = new Router();
r.add("GET", "/hello", async c => true);
let c = context({} as any);
assertEqual(await r.find("GET", "/hello", c)(c), true);

r.add("GET", "/hello/:p", async c => true);
c = context({} as any);
assertEqual(await r.find("GET", "/hello/a", c)(c), true);
assertEqual(await r.find("GET", "/hello/b", c)(c), true);
assertEqual(await r.find("GET", "/hello/a/a", c), undefined);
r.add("GET", "/hello/:p", async c => true);
c = context({} as any);
assertEqual(await r.find("GET", "/hello/a", c)(c), true);
assertEqual(await r.find("GET", "/hello/b", c)(c), true);
assertEqual(await r.find("GET", "/hello/a/a", c), undefined);
}
});
3 changes: 3 additions & 0 deletions test.ts
@@ -1,5 +1,8 @@
import { runTests } from "https://deno.land/x/testing/mod.ts";
import "abc_test.ts";
import "router_test.ts";
import "context_test.ts";
import "binder_test.ts";
import "parser_test.ts";

runTests();