From 1dc1f87fc394f4117b7e557470750fb3540819fb Mon Sep 17 00:00:00 2001 From: Abdulaziz Date: Sat, 30 Jul 2022 18:35:31 +0300 Subject: [PATCH] refactor: use named status codes from std internally (#417) Co-authored-by: Luca Casonato --- src/server/context.ts | 13 ++++++---- src/server/deps.ts | 1 + src/server/mod.ts | 1 + tests/cli_test.ts | 5 ++-- tests/error_test.ts | 6 ++--- tests/main_test.ts | 60 +++++++++++++++++++++---------------------- 6 files changed, 46 insertions(+), 40 deletions(-) diff --git a/src/server/context.ts b/src/server/context.ts index f658a3f2..42731ecd 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -4,6 +4,7 @@ import { fromFileUrl, RequestHandler, router, + Status, toFileUrl, typeByExtension, walk, @@ -32,7 +33,6 @@ import { import { render as internalRender } from "./render.tsx"; import { ContentSecurityPolicyDirectives, SELF } from "../runtime/csp.ts"; import { ASSET_CACHE_BUST_KEY, INTERNAL_PREFIX } from "../runtime/utils.ts"; - interface RouterState { state: Record; } @@ -278,7 +278,7 @@ export class ServerContext { const url = new URL(req.url); if (url.pathname.length > 1 && url.pathname.endsWith("/")) { url.pathname = url.pathname.slice(0, -1); - return Response.redirect(url.href, 307); + return Response.redirect(url.href, Status.TemporaryRedirect); } return withMiddlewares(req, connInfo, inner); }; @@ -449,7 +449,7 @@ export class ServerContext { }; for (const route of this.#routes) { - const createRender = genRender(route, 200); + const createRender = genRender(route, Status.OK); if (typeof route.handler === "function") { routes[route.pattern] = (req, ctx, params) => (route.handler as Handler)(req, { @@ -469,7 +469,7 @@ export class ServerContext { } } - const unknownHandlerRender = genRender(this.#notFound, 404); + const unknownHandlerRender = genRender(this.#notFound, Status.NotFound); const unknownHandler: router.Handler = ( req, ctx, @@ -482,7 +482,10 @@ export class ServerContext { }, ); - const errorHandlerRender = genRender(this.#error, 500); + const errorHandlerRender = genRender( + this.#error, + Status.InternalServerError, + ); const errorHandler: router.ErrorHandler = ( req, ctx, diff --git a/src/server/deps.ts b/src/server/deps.ts index 358b05b3..0f96d6b8 100644 --- a/src/server/deps.ts +++ b/src/server/deps.ts @@ -11,6 +11,7 @@ export type { Handler as RequestHandler, ServeInit, } from "https://deno.land/std@0.150.0/http/server.ts"; +export { Status } from "https://deno.land/std@0.150.0/http/http_status.ts"; export { typeByExtension, } from "https://deno.land/std@0.150.0/media_types/mod.ts"; diff --git a/src/server/mod.ts b/src/server/mod.ts index 71678274..bd9b24e2 100644 --- a/src/server/mod.ts +++ b/src/server/mod.ts @@ -1,5 +1,6 @@ import { ServerContext } from "./context.ts"; import { serve } from "./deps.ts"; +export { Status } from "./deps.ts"; import { AppModule, ErrorPageModule, diff --git a/tests/cli_test.ts b/tests/cli_test.ts index 08bfbabd..4520b6f6 100644 --- a/tests/cli_test.ts +++ b/tests/cli_test.ts @@ -1,3 +1,4 @@ +import { Status } from "../src/server/deps.ts"; import { assert, assertEquals, @@ -133,7 +134,7 @@ Deno.test({ // Access the root page const res = await fetch("http://localhost:8000"); await res.body?.cancel(); - assertEquals(res.status, 200); + assertEquals(res.status, Status.OK); // verify the island is revived. const browser = await puppeteer.launch({ @@ -282,7 +283,7 @@ Deno.test({ // Access the root page const res = await fetch("http://localhost:8000"); await res.body?.cancel(); - assertEquals(res.status, 200); + assertEquals(res.status, Status.OK); // verify the island is revived. const browser = await puppeteer.launch({ args: ["--no-sandbox"] }); diff --git a/tests/error_test.ts b/tests/error_test.ts index 88cf5792..f5768e1e 100644 --- a/tests/error_test.ts +++ b/tests/error_test.ts @@ -1,4 +1,4 @@ -import { ServerContext } from "../server.ts"; +import { ServerContext, Status } from "../server.ts"; import { REFRESH_JS_URL } from "../src/server/constants.ts"; import { assert, assertEquals, assertStringIncludes } from "./deps.ts"; import manifest from "./fixture_error/fresh.gen.ts"; @@ -22,7 +22,7 @@ const router = (req: Request) => { Deno.test("error page rendered", async () => { const resp = await router(new Request("https://fresh.deno.dev/")); assert(resp); - assertEquals(resp.status, 500); + assertEquals(resp.status, Status.InternalServerError); assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8"); const body = await resp.text(); assertStringIncludes( @@ -37,7 +37,7 @@ Deno.test("refresh.js rendered", async () => { new Request("https://fresh.deno.dev" + REFRESH_JS_URL), ); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); assertEquals( resp.headers.get("content-type"), "application/javascript; charset=utf-8", diff --git a/tests/main_test.ts b/tests/main_test.ts index d070b675..3b77146c 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -1,4 +1,4 @@ -import { ServerContext } from "../server.ts"; +import { ServerContext, Status } from "../server.ts"; import { assert, assertEquals, assertStringIncludes } from "./deps.ts"; import manifest from "./fixture/fresh.gen.ts"; import options from "./fixture/options.ts"; @@ -22,7 +22,7 @@ const router = (req: Request) => { Deno.test("/ page prerender", async () => { const resp = await router(new Request("https://fresh.deno.dev/")); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8"); assertEquals(resp.headers.get("server"), "fresh test server"); const body = await resp.text(); @@ -40,7 +40,7 @@ Deno.test("/ page prerender", async () => { Deno.test("/props/123 page prerender", async () => { const resp = await router(new Request("https://fresh.deno.dev/props/123")); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8"); const body = await resp.text(); assertStringIncludes( @@ -52,7 +52,7 @@ Deno.test("/props/123 page prerender", async () => { Deno.test("/[name] page prerender", async () => { const resp = await router(new Request("https://fresh.deno.dev/bar")); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8"); const body = await resp.text(); assertStringIncludes(body, "
Hello bar
"); @@ -64,7 +64,7 @@ Deno.test("/intercept - GET html", async () => { }); const resp = await router(req); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); const body = await resp.text(); assertStringIncludes(body, "
This is HTML
"); }); @@ -75,7 +75,7 @@ Deno.test("/intercept - GET text", async () => { }); const resp = await router(req); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); const body = await resp.text(); assertEquals(body, "This is plain text"); }); @@ -86,7 +86,7 @@ Deno.test("/intercept - POST", async () => { }); const resp = await router(req); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); const body = await resp.text(); assertEquals(body, "POST response"); }); @@ -97,7 +97,7 @@ Deno.test("/intercept - DELETE", async () => { }); const resp = await router(req); assert(resp); - assertEquals(resp.status, 405); + assertEquals(resp.status, Status.MethodNotAllowed); }); Deno.test("/intercept_args - GET html", async () => { @@ -106,7 +106,7 @@ Deno.test("/intercept_args - GET html", async () => { }); const resp = await router(req); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); const body = await resp.text(); assertStringIncludes(body, "
intercepted
"); }); @@ -118,13 +118,13 @@ Deno.test("/api/get_only - NOTAMETHOD", async () => { }), ); assert(resp); - assertEquals(resp.status, 405); + assertEquals(resp.status, Status.MethodNotAllowed); }); Deno.test("/api/xyz not found", async () => { const resp = await router(new Request("https://fresh.deno.dev/api/xyz")); assert(resp); - assertEquals(resp.status, 404); + assertEquals(resp.status, Status.NotFound); const body = await resp.text(); assertStringIncludes(body, "404 not found: /api/xyz"); }); @@ -132,7 +132,7 @@ Deno.test("/api/xyz not found", async () => { Deno.test("/static page prerender", async () => { const resp = await router(new Request("https://fresh.deno.dev/static")); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8"); const body = await resp.text(); assert(!body.includes(`main.js`)); @@ -145,7 +145,7 @@ Deno.test("/static page prerender", async () => { Deno.test("/books/:id page - /books/123", async () => { const resp = await router(new Request("https://fresh.deno.dev/books/123")); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8"); const body = await resp.text(); assertStringIncludes(body, "
Book 123
"); @@ -154,13 +154,13 @@ Deno.test("/books/:id page - /books/123", async () => { Deno.test("/books/:id page - /books/abc", async () => { const resp = await router(new Request("https://fresh.deno.dev/books/abc")); assert(resp); - assertEquals(resp.status, 404); + assertEquals(resp.status, Status.NotFound); }); Deno.test("redirect /pages/fresh/ to /pages/fresh", async () => { const resp = await router(new Request("https://fresh.deno.dev/pages/fresh/")); assert(resp); - assertEquals(resp.status, 307); + assertEquals(resp.status, Status.TemporaryRedirect); assertEquals( resp.headers.get("location"), "https://fresh.deno.dev/pages/fresh", @@ -170,7 +170,7 @@ Deno.test("redirect /pages/fresh/ to /pages/fresh", async () => { Deno.test("/failure", async () => { const resp = await router(new Request("https://fresh.deno.dev/failure")); assert(resp); - assertEquals(resp.status, 500); + assertEquals(resp.status, Status.InternalServerError); const body = await resp.text(); assert(body.includes("500 internal error: it errored!")); }); @@ -178,14 +178,14 @@ Deno.test("/failure", async () => { Deno.test("/foo/:path*", async () => { const resp = await router(new Request("https://fresh.deno.dev/foo/bar/baz")); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); const body = await resp.text(); assert(body.includes("bar/baz")); }); Deno.test("static file - by file path", async () => { const resp = await router(new Request("https://fresh.deno.dev/foo.txt")); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); const body = await resp.text(); assert(body.startsWith("bar")); const etag = resp.headers.get("etag"); @@ -202,7 +202,7 @@ Deno.test("static file - by file path", async () => { }, }), ); - assertEquals(resp2.status, 304); + assertEquals(resp2.status, Status.NotModified); assertEquals(resp2.headers.get("etag"), etag); assertEquals(resp2.headers.get("content-type"), "text/plain"); @@ -213,7 +213,7 @@ Deno.test("static file - by file path", async () => { }, }), ); - assertEquals(resp3.status, 304); + assertEquals(resp3.status, Status.NotModified); assertEquals(resp3.headers.get("etag"), etag); assertEquals(resp3.headers.get("content-type"), "text/plain"); }); @@ -233,7 +233,7 @@ Deno.test("static file - by 'hashed' path", async () => { new Request(`https://fresh.deno.dev${imgFilePath}`), ); const _ = await resp2.text(); - assertEquals(resp2.status, 200); + assertEquals(resp2.status, Status.OK); assertEquals( resp2.headers.get("cache-control"), "public, max-age=31536000, immutable", @@ -246,7 +246,7 @@ Deno.test("static file - by 'hashed' path", async () => { }, }), ); - assertEquals(resp3.status, 304); + assertEquals(resp3.status, Status.NotModified); // ensure asset hook is not applied on file explicitly excluded with attribute const imgFilePathWithNoCache = body.match( @@ -285,7 +285,7 @@ Deno.test("/params/:path*", async () => { new Request("https://fresh.deno.dev/params/bar/baz"), ); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); const body = await resp.text(); assertEquals(body, "bar/baz"); }); @@ -293,7 +293,7 @@ Deno.test("/params/:path*", async () => { Deno.test("/connInfo", async () => { const resp = await router(new Request("https://fresh.deno.dev/connInfo")); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); const body = await resp.text(); assertEquals(body, "127.0.0.1"); }); @@ -305,7 +305,7 @@ Deno.test({ new Request("https://fresh.deno.dev/middleware_root"), ); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); const body = await resp.text(); assertStringIncludes(body, "root_mw"); assert(!body.includes("layer1_mw")); @@ -319,7 +319,7 @@ Deno.test({ new Request("https://fresh.deno.dev/layeredMdw/layer2/abc"), ); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); const body = await resp.text(); console.log(body); assertStringIncludes(body, "root_mw"); @@ -332,7 +332,7 @@ Deno.test({ new Request("https://fresh.deno.dev/layeredMdw/layer2-no-mw/without_mw"), ); assert(resp1); - assertEquals(resp1.status, 200); + assertEquals(resp1.status, Status.OK); const body1 = await resp1.text(); assertStringIncludes(body1, "root_mw"); assertStringIncludes(body1, "layer1_mw"); @@ -349,7 +349,7 @@ Deno.test({ new Request("https://fresh.deno.dev/layeredMdw/layer2"), ); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); const body = await resp.text(); console.log(body); assertStringIncludes(body, "root_mw"); @@ -362,7 +362,7 @@ Deno.test({ new Request("https://fresh.deno.dev/layeredMdw/layer2-no-mw/without_mw"), ); assert(resp1); - assertEquals(resp1.status, 200); + assertEquals(resp1.status, Status.OK); const body1 = await resp1.text(); assertStringIncludes(body1, "root_mw"); assertStringIncludes(body1, "layer1_mw"); @@ -380,7 +380,7 @@ Deno.test({ new Request("https://fresh.deno.dev/layeredMdw/layer2/layer3/abc"), ); assert(resp); - assertEquals(resp.status, 200); + assertEquals(resp.status, Status.OK); const body = await resp.text(); assertStringIncludes(body, "root_mw"); assertStringIncludes(body, "layer1_mw");