From ef570d71e8730f9400d5924ae3f37fbb787f45b1 Mon Sep 17 00:00:00 2001 From: syumai Date: Mon, 4 May 2020 01:01:33 +0900 Subject: [PATCH 1/2] Release 0.12.0 --- Makefile | 6 ++--- README.md | 4 +-- dem.json | 2 +- helpers_test.ts | 26 ++++++++----------- io.ts | 1 + mod.ts | 5 ++-- response.ts | 11 ++++---- serve_test.ts | 15 +++++------ static_test.ts | 16 +++++------- vendor/https/deno.land/std/encoding/utf8.ts | 2 +- vendor/https/deno.land/std/flags/mod.ts | 2 +- vendor/https/deno.land/std/http/server.ts | 2 +- vendor/https/deno.land/std/testing/asserts.ts | 2 +- 13 files changed, 43 insertions(+), 51 deletions(-) create mode 100644 io.ts diff --git a/Makefile b/Makefile index 1d1ae52..21a64b7 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/README.md b/README.md index aec99c9..445dfd7 100644 --- a/README.md +++ b/README.md @@ -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"), @@ -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 diff --git a/dem.json b/dem.json index f9fd2ef..4e16b73 100644 --- a/dem.json +++ b/dem.json @@ -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", diff --git a/helpers_test.ts b/helpers_test.ts index f945a84..9f9ac56 100644 --- a/helpers_test.ts +++ b/helpers_test.ts @@ -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(); -})(); +}); diff --git a/io.ts b/io.ts new file mode 100644 index 0000000..2c1da84 --- /dev/null +++ b/io.ts @@ -0,0 +1 @@ +export type ReadCloser = Deno.Reader & Deno.Closer; diff --git a/mod.ts b/mod.ts index 85b30d4..3705bd7 100644 --- a/mod.ts +++ b/mod.ts @@ -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, @@ -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" && diff --git a/response.ts b/response.ts index b603259..e25ad02 100644 --- a/response.ts +++ b/response.ts @@ -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 = @@ -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 @@ -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 { @@ -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 { @@ -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" && diff --git a/serve_test.ts b/serve_test.ts index 6bfc683..056aabd 100644 --- a/serve_test.ts +++ b/serve_test.ts @@ -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; @@ -114,13 +113,15 @@ const testCases: Array = [ // }, ]; -const app = new App(testPort); -app.serve(); +// const appCloser = createAppCloser(app, testCases.length); 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 }; @@ -139,11 +140,7 @@ for (const tc of testCases) { const { path, method } = tc.registered; app.unregister(path, method); + app.close(); }, }); } - -(async () => { - await runTests(); - app.close(); -})(); diff --git a/static_test.ts b/static_test.ts index 0325c96..b1049ca 100644 --- a/static_test.ts +++ b/static_test.ts @@ -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; @@ -48,19 +44,19 @@ const testCases: Array = [ ]; 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(); -})(); diff --git a/vendor/https/deno.land/std/encoding/utf8.ts b/vendor/https/deno.land/std/encoding/utf8.ts index e3ea52e..93c368e 100644 --- a/vendor/https/deno.land/std/encoding/utf8.ts +++ b/vendor/https/deno.land/std/encoding/utf8.ts @@ -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'; diff --git a/vendor/https/deno.land/std/flags/mod.ts b/vendor/https/deno.land/std/flags/mod.ts index c8e695b..a27d696 100644 --- a/vendor/https/deno.land/std/flags/mod.ts +++ b/vendor/https/deno.land/std/flags/mod.ts @@ -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'; diff --git a/vendor/https/deno.land/std/http/server.ts b/vendor/https/deno.land/std/http/server.ts index f6276b3..05bdbd4 100644 --- a/vendor/https/deno.land/std/http/server.ts +++ b/vendor/https/deno.land/std/http/server.ts @@ -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'; diff --git a/vendor/https/deno.land/std/testing/asserts.ts b/vendor/https/deno.land/std/testing/asserts.ts index ecfce31..fa70707 100644 --- a/vendor/https/deno.land/std/testing/asserts.ts +++ b/vendor/https/deno.land/std/testing/asserts.ts @@ -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'; From 9b3d1049055cd11a5061ae30153d91da327d1d85 Mon Sep 17 00:00:00 2001 From: syumai Date: Mon, 4 May 2020 01:03:29 +0900 Subject: [PATCH 2/2] Removed unused line --- serve_test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/serve_test.ts b/serve_test.ts index 056aabd..e35b4cf 100644 --- a/serve_test.ts +++ b/serve_test.ts @@ -113,8 +113,6 @@ const testCases: Array = [ // }, ]; -// const appCloser = createAppCloser(app, testCases.length); - for (const tc of testCases) { Deno.test({ name: tc.name,