Skip to content

Commit

Permalink
Merge pull request #70 from syumai/release-0.12.0
Browse files Browse the repository at this point in the history
Release 0.12.0
  • Loading branch information
syumai committed May 3, 2020
2 parents e78f4f7 + 9b3d104 commit f235df3
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 52 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ SHELL=/bin/bash
TARGET_SRC=$(shell shopt -s globstar && ls ./**/*.{ts,js,tsx} | grep -v ./vendor | grep -v ./testdata)

test:
deno run --allow-net serve_test.ts
deno run --allow-net --allow-read static_test.ts
deno run --allow-net helpers_test.ts
deno test --allow-net serve_test.ts
deno test --allow-net --allow-read static_test.ts
deno test --allow-net helpers_test.ts

lint:
deno fmt --check $(TARGET_SRC)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
post,
redirect,
contentType,
} from "https://denopkg.com/syumai/dinatra@0.10.1/mod.ts";
} from "https://denopkg.com/syumai/dinatra@0.12.0/mod.ts";

app(
get("/hello", () => "hello"),
Expand Down Expand Up @@ -156,7 +156,7 @@ type HeaderMap =
};

// ResponseBody is a type of response body.
type ResponseBody = string | Deno.ReadCloser | Deno.Reader;
type ResponseBody = string | ReadCloser | Deno.Reader;

/*
* Types of Response
Expand Down
2 changes: 1 addition & 1 deletion dem.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"protocol": "https",
"path": "deno.land/std",
"version": "v0.40.0",
"version": "v0.42.0",
"files": [
"/encoding/utf8.ts",
"/flags/mod.ts",
Expand Down
26 changes: 11 additions & 15 deletions helpers_test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
import { App, get } from "./mod.ts";
import { redirect } from "./helpers.ts";
const { test, runTests } = Deno;

const testPort = 8376;
const host = `http://localhost:${testPort}`;
const app = new App(testPort);

app.serve();
Deno.test("Redirection does return new endpoint", async () => {
const app = new App(testPort);
app.serve();

test("Redirection does return new endpoint", async () => {
const original_endpoint = "/original";
const new_endpoint = "/new_endpoint";
const expected_body = `body at ${new_endpoint}`;
const originalEndpoint = "/original";
const newEndpoint = "/new_endpoint";
const expectedBody = `body at ${newEndpoint}`;

app.register(get(original_endpoint, () => redirect(new_endpoint)));
app.register(get(new_endpoint, () => expected_body));
app.register(get(originalEndpoint, () => redirect(newEndpoint)));
app.register(get(newEndpoint, () => expectedBody));

const response = await fetch(
`${host}${original_endpoint}`,
`${host}${originalEndpoint}`,
{ method: "GET" },
);

assertEquals(response.status, 200);
assertEquals((await response.text()), expected_body);
});
assertEquals((await response.text()), expectedBody);

(async () => {
await runTests();
app.close();
})();
});
1 change: 1 addition & 0 deletions io.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ReadCloser = Deno.Reader & Deno.Closer;
5 changes: 3 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Method, Handler, HandlerConfig } from "./handler.ts";
import { Params, parseURLSearchParams } from "./params.ts";
import { defaultPort } from "./constants.ts";
import { detectedContentType } from "./mime.ts";
import { ReadCloser } from "./io.ts";
export { contentType, detectedContentType } from "./mime.ts";
export {
get,
Expand Down Expand Up @@ -231,8 +232,8 @@ export class App {
}
}

function isReadCloser(obj: any): obj is Deno.ReadCloser {
const o = obj as Deno.ReadCloser;
function isReadCloser(obj: any): obj is ReadCloser {
const o = obj as ReadCloser;
return (
typeof o === "object" &&
typeof o.read === "function" &&
Expand Down
11 changes: 6 additions & 5 deletions response.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { encode } from "./vendor/https/deno.land/std/encoding/utf8.ts";
import { ReadCloser } from "./io.ts";

// HeaderMap is a type of response headers.
type HeaderMap =
Expand All @@ -8,7 +9,7 @@ type HeaderMap =
};

// ResponseBody is a type of response body.
type ResponseBody = string | Deno.ReadCloser | Deno.Reader;
type ResponseBody = string | ReadCloser | Deno.Reader;

/*
* Types of Response
Expand All @@ -31,7 +32,7 @@ export type Response =
interface HTTPResponse {
status?: number;
headers?: Headers;
body?: Uint8Array | Deno.ReadCloser | Deno.Reader;
body?: Uint8Array | ReadCloser | Deno.Reader;
}

export function processResponse(res: Response): HTTPResponse {
Expand All @@ -53,7 +54,7 @@ export function processResponse(res: Response): HTTPResponse {
rawBody = res;
}

let body: Uint8Array | Deno.ReadCloser | Deno.Reader;
let body: Uint8Array | ReadCloser | Deno.Reader;
if (typeof rawBody === "string") {
body = encode(rawBody);
} else {
Expand Down Expand Up @@ -83,8 +84,8 @@ function isNumberResponse(res: Response): res is number {
return typeof res === "number";
}

function isReadCloserResponse(res: Response): res is Deno.ReadCloser {
const r = res as Deno.ReadCloser;
function isReadCloserResponse(res: Response): res is ReadCloser {
const r = res as ReadCloser;
return (
typeof r === "object" &&
typeof r.read === "function" &&
Expand Down
15 changes: 5 additions & 10 deletions serve_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
import { App, get, post } from "./mod.ts";
import { HandlerConfig, Method } from "./handler.ts";
const { test, runTests } = Deno;

interface RequestInit {
body?: string | FormData;
Expand Down Expand Up @@ -114,13 +113,13 @@ const testCases: Array<testCase> = [
// },
];

const app = new App(testPort);
app.serve();

for (const tc of testCases) {
test({
Deno.test({
name: tc.name,
async fn() {
const app = new App(testPort);
app.serve();

app.register(tc.registered);

const reqInit: RequestInit = { method: tc.method };
Expand All @@ -139,11 +138,7 @@ for (const tc of testCases) {

const { path, method } = tc.registered;
app.unregister(path, method);
app.close();
},
});
}

(async () => {
await runTests();
app.close();
})();
16 changes: 6 additions & 10 deletions static_test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
import { App } from "./mod.ts";
const { test, runTests } = Deno;

const testPort = 8376;
const host = `http://localhost:${testPort}`;

const app = new App(testPort, true, "testdata/static");
app.serve();

interface testCase {
name: string;
path: string;
Expand Down Expand Up @@ -48,19 +44,19 @@ const testCases: Array<testCase> = [
];

for (const tc of testCases) {
test({
Deno.test({
name: tc.name,
async fn() {
const app = new App(testPort, true, "testdata/static");
app.serve();

const res = await fetch(`${host}/${tc.path}`);
const actual = await res.text();
const contentLength = res.headers.get("content-length");
assertEquals(actual, tc.expected);
assertEquals(contentLength, tc.expected.length.toString());

app.close();
},
});
}

(async () => {
await runTests();
app.close();
})();
2 changes: 1 addition & 1 deletion vendor/https/deno.land/std/encoding/utf8.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from 'https://deno.land/std@v0.40.0/encoding/utf8.ts';
export * from 'https://deno.land/std@v0.42.0/encoding/utf8.ts';
2 changes: 1 addition & 1 deletion vendor/https/deno.land/std/flags/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from 'https://deno.land/std@v0.40.0/flags/mod.ts';
export * from 'https://deno.land/std@v0.42.0/flags/mod.ts';
2 changes: 1 addition & 1 deletion vendor/https/deno.land/std/http/server.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from 'https://deno.land/std@v0.40.0/http/server.ts';
export * from 'https://deno.land/std@v0.42.0/http/server.ts';
2 changes: 1 addition & 1 deletion vendor/https/deno.land/std/testing/asserts.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from 'https://deno.land/std@v0.40.0/testing/asserts.ts';
export * from 'https://deno.land/std@v0.42.0/testing/asserts.ts';

0 comments on commit f235df3

Please sign in to comment.