refactor: use named status codes from std internally (#417)

Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
Abdulaziz
2022-07-30 15:35:31 +00:00
committed by GitHub
co-authored by Luca Casonato
parent ba20756c17
commit 1dc1f87fc3
6 changed files with 46 additions and 40 deletions
+8 -5
View File
@@ -4,6 +4,7 @@ import {
fromFileUrl, fromFileUrl,
RequestHandler, RequestHandler,
router, router,
Status,
toFileUrl, toFileUrl,
typeByExtension, typeByExtension,
walk, walk,
@@ -32,7 +33,6 @@ import {
import { render as internalRender } from "./render.tsx"; import { render as internalRender } from "./render.tsx";
import { ContentSecurityPolicyDirectives, SELF } from "../runtime/csp.ts"; import { ContentSecurityPolicyDirectives, SELF } from "../runtime/csp.ts";
import { ASSET_CACHE_BUST_KEY, INTERNAL_PREFIX } from "../runtime/utils.ts"; import { ASSET_CACHE_BUST_KEY, INTERNAL_PREFIX } from "../runtime/utils.ts";
interface RouterState { interface RouterState {
state: Record<string, unknown>; state: Record<string, unknown>;
} }
@@ -278,7 +278,7 @@ export class ServerContext {
const url = new URL(req.url); const url = new URL(req.url);
if (url.pathname.length > 1 && url.pathname.endsWith("/")) { if (url.pathname.length > 1 && url.pathname.endsWith("/")) {
url.pathname = url.pathname.slice(0, -1); 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); return withMiddlewares(req, connInfo, inner);
}; };
@@ -449,7 +449,7 @@ export class ServerContext {
}; };
for (const route of this.#routes) { for (const route of this.#routes) {
const createRender = genRender(route, 200); const createRender = genRender(route, Status.OK);
if (typeof route.handler === "function") { if (typeof route.handler === "function") {
routes[route.pattern] = (req, ctx, params) => routes[route.pattern] = (req, ctx, params) =>
(route.handler as Handler)(req, { (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<RouterState> = ( const unknownHandler: router.Handler<RouterState> = (
req, req,
ctx, 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<RouterState> = ( const errorHandler: router.ErrorHandler<RouterState> = (
req, req,
ctx, ctx,
+1
View File
@@ -11,6 +11,7 @@ export type {
Handler as RequestHandler, Handler as RequestHandler,
ServeInit, ServeInit,
} from "https://deno.land/std@0.150.0/http/server.ts"; } 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 { export {
typeByExtension, typeByExtension,
} from "https://deno.land/std@0.150.0/media_types/mod.ts"; } from "https://deno.land/std@0.150.0/media_types/mod.ts";
+1
View File
@@ -1,5 +1,6 @@
import { ServerContext } from "./context.ts"; import { ServerContext } from "./context.ts";
import { serve } from "./deps.ts"; import { serve } from "./deps.ts";
export { Status } from "./deps.ts";
import { import {
AppModule, AppModule,
ErrorPageModule, ErrorPageModule,
+3 -2
View File
@@ -1,3 +1,4 @@
import { Status } from "../src/server/deps.ts";
import { import {
assert, assert,
assertEquals, assertEquals,
@@ -133,7 +134,7 @@ Deno.test({
// Access the root page // Access the root page
const res = await fetch("http://localhost:8000"); const res = await fetch("http://localhost:8000");
await res.body?.cancel(); await res.body?.cancel();
assertEquals(res.status, 200); assertEquals(res.status, Status.OK);
// verify the island is revived. // verify the island is revived.
const browser = await puppeteer.launch({ const browser = await puppeteer.launch({
@@ -282,7 +283,7 @@ Deno.test({
// Access the root page // Access the root page
const res = await fetch("http://localhost:8000"); const res = await fetch("http://localhost:8000");
await res.body?.cancel(); await res.body?.cancel();
assertEquals(res.status, 200); assertEquals(res.status, Status.OK);
// verify the island is revived. // verify the island is revived.
const browser = await puppeteer.launch({ args: ["--no-sandbox"] }); const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
+3 -3
View File
@@ -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 { REFRESH_JS_URL } from "../src/server/constants.ts";
import { assert, assertEquals, assertStringIncludes } from "./deps.ts"; import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
import manifest from "./fixture_error/fresh.gen.ts"; import manifest from "./fixture_error/fresh.gen.ts";
@@ -22,7 +22,7 @@ const router = (req: Request) => {
Deno.test("error page rendered", async () => { Deno.test("error page rendered", async () => {
const resp = await router(new Request("https://fresh.deno.dev/")); const resp = await router(new Request("https://fresh.deno.dev/"));
assert(resp); assert(resp);
assertEquals(resp.status, 500); assertEquals(resp.status, Status.InternalServerError);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8"); assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text(); const body = await resp.text();
assertStringIncludes( assertStringIncludes(
@@ -37,7 +37,7 @@ Deno.test("refresh.js rendered", async () => {
new Request("https://fresh.deno.dev" + REFRESH_JS_URL), new Request("https://fresh.deno.dev" + REFRESH_JS_URL),
); );
assert(resp); assert(resp);
assertEquals(resp.status, 200); assertEquals(resp.status, Status.OK);
assertEquals( assertEquals(
resp.headers.get("content-type"), resp.headers.get("content-type"),
"application/javascript; charset=utf-8", "application/javascript; charset=utf-8",
+30 -30
View File
@@ -1,4 +1,4 @@
import { ServerContext } from "../server.ts"; import { ServerContext, Status } from "../server.ts";
import { assert, assertEquals, assertStringIncludes } from "./deps.ts"; import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
import manifest from "./fixture/fresh.gen.ts"; import manifest from "./fixture/fresh.gen.ts";
import options from "./fixture/options.ts"; import options from "./fixture/options.ts";
@@ -22,7 +22,7 @@ const router = (req: Request) => {
Deno.test("/ page prerender", async () => { Deno.test("/ page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/")); const resp = await router(new Request("https://fresh.deno.dev/"));
assert(resp); 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("content-type"), "text/html; charset=utf-8");
assertEquals(resp.headers.get("server"), "fresh test server"); assertEquals(resp.headers.get("server"), "fresh test server");
const body = await resp.text(); const body = await resp.text();
@@ -40,7 +40,7 @@ Deno.test("/ page prerender", async () => {
Deno.test("/props/123 page prerender", async () => { Deno.test("/props/123 page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/props/123")); const resp = await router(new Request("https://fresh.deno.dev/props/123"));
assert(resp); 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("content-type"), "text/html; charset=utf-8");
const body = await resp.text(); const body = await resp.text();
assertStringIncludes( assertStringIncludes(
@@ -52,7 +52,7 @@ Deno.test("/props/123 page prerender", async () => {
Deno.test("/[name] page prerender", async () => { Deno.test("/[name] page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/bar")); const resp = await router(new Request("https://fresh.deno.dev/bar"));
assert(resp); 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("content-type"), "text/html; charset=utf-8");
const body = await resp.text(); const body = await resp.text();
assertStringIncludes(body, "<div>Hello bar</div>"); assertStringIncludes(body, "<div>Hello bar</div>");
@@ -64,7 +64,7 @@ Deno.test("/intercept - GET html", async () => {
}); });
const resp = await router(req); const resp = await router(req);
assert(resp); assert(resp);
assertEquals(resp.status, 200); assertEquals(resp.status, Status.OK);
const body = await resp.text(); const body = await resp.text();
assertStringIncludes(body, "<div>This is HTML</div>"); assertStringIncludes(body, "<div>This is HTML</div>");
}); });
@@ -75,7 +75,7 @@ Deno.test("/intercept - GET text", async () => {
}); });
const resp = await router(req); const resp = await router(req);
assert(resp); assert(resp);
assertEquals(resp.status, 200); assertEquals(resp.status, Status.OK);
const body = await resp.text(); const body = await resp.text();
assertEquals(body, "This is plain text"); assertEquals(body, "This is plain text");
}); });
@@ -86,7 +86,7 @@ Deno.test("/intercept - POST", async () => {
}); });
const resp = await router(req); const resp = await router(req);
assert(resp); assert(resp);
assertEquals(resp.status, 200); assertEquals(resp.status, Status.OK);
const body = await resp.text(); const body = await resp.text();
assertEquals(body, "POST response"); assertEquals(body, "POST response");
}); });
@@ -97,7 +97,7 @@ Deno.test("/intercept - DELETE", async () => {
}); });
const resp = await router(req); const resp = await router(req);
assert(resp); assert(resp);
assertEquals(resp.status, 405); assertEquals(resp.status, Status.MethodNotAllowed);
}); });
Deno.test("/intercept_args - GET html", async () => { Deno.test("/intercept_args - GET html", async () => {
@@ -106,7 +106,7 @@ Deno.test("/intercept_args - GET html", async () => {
}); });
const resp = await router(req); const resp = await router(req);
assert(resp); assert(resp);
assertEquals(resp.status, 200); assertEquals(resp.status, Status.OK);
const body = await resp.text(); const body = await resp.text();
assertStringIncludes(body, "<div>intercepted</div>"); assertStringIncludes(body, "<div>intercepted</div>");
}); });
@@ -118,13 +118,13 @@ Deno.test("/api/get_only - NOTAMETHOD", async () => {
}), }),
); );
assert(resp); assert(resp);
assertEquals(resp.status, 405); assertEquals(resp.status, Status.MethodNotAllowed);
}); });
Deno.test("/api/xyz not found", async () => { Deno.test("/api/xyz not found", async () => {
const resp = await router(new Request("https://fresh.deno.dev/api/xyz")); const resp = await router(new Request("https://fresh.deno.dev/api/xyz"));
assert(resp); assert(resp);
assertEquals(resp.status, 404); assertEquals(resp.status, Status.NotFound);
const body = await resp.text(); const body = await resp.text();
assertStringIncludes(body, "404 not found: /api/xyz"); assertStringIncludes(body, "404 not found: /api/xyz");
}); });
@@ -132,7 +132,7 @@ Deno.test("/api/xyz not found", async () => {
Deno.test("/static page prerender", async () => { Deno.test("/static page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/static")); const resp = await router(new Request("https://fresh.deno.dev/static"));
assert(resp); 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("content-type"), "text/html; charset=utf-8");
const body = await resp.text(); const body = await resp.text();
assert(!body.includes(`main.js`)); assert(!body.includes(`main.js`));
@@ -145,7 +145,7 @@ Deno.test("/static page prerender", async () => {
Deno.test("/books/:id page - /books/123", async () => { Deno.test("/books/:id page - /books/123", async () => {
const resp = await router(new Request("https://fresh.deno.dev/books/123")); const resp = await router(new Request("https://fresh.deno.dev/books/123"));
assert(resp); 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("content-type"), "text/html; charset=utf-8");
const body = await resp.text(); const body = await resp.text();
assertStringIncludes(body, "<div>Book 123</div>"); assertStringIncludes(body, "<div>Book 123</div>");
@@ -154,13 +154,13 @@ Deno.test("/books/:id page - /books/123", async () => {
Deno.test("/books/:id page - /books/abc", async () => { Deno.test("/books/:id page - /books/abc", async () => {
const resp = await router(new Request("https://fresh.deno.dev/books/abc")); const resp = await router(new Request("https://fresh.deno.dev/books/abc"));
assert(resp); assert(resp);
assertEquals(resp.status, 404); assertEquals(resp.status, Status.NotFound);
}); });
Deno.test("redirect /pages/fresh/ to /pages/fresh", async () => { Deno.test("redirect /pages/fresh/ to /pages/fresh", async () => {
const resp = await router(new Request("https://fresh.deno.dev/pages/fresh/")); const resp = await router(new Request("https://fresh.deno.dev/pages/fresh/"));
assert(resp); assert(resp);
assertEquals(resp.status, 307); assertEquals(resp.status, Status.TemporaryRedirect);
assertEquals( assertEquals(
resp.headers.get("location"), resp.headers.get("location"),
"https://fresh.deno.dev/pages/fresh", "https://fresh.deno.dev/pages/fresh",
@@ -170,7 +170,7 @@ Deno.test("redirect /pages/fresh/ to /pages/fresh", async () => {
Deno.test("/failure", async () => { Deno.test("/failure", async () => {
const resp = await router(new Request("https://fresh.deno.dev/failure")); const resp = await router(new Request("https://fresh.deno.dev/failure"));
assert(resp); assert(resp);
assertEquals(resp.status, 500); assertEquals(resp.status, Status.InternalServerError);
const body = await resp.text(); const body = await resp.text();
assert(body.includes("500 internal error: it errored!")); assert(body.includes("500 internal error: it errored!"));
}); });
@@ -178,14 +178,14 @@ Deno.test("/failure", async () => {
Deno.test("/foo/:path*", async () => { Deno.test("/foo/:path*", async () => {
const resp = await router(new Request("https://fresh.deno.dev/foo/bar/baz")); const resp = await router(new Request("https://fresh.deno.dev/foo/bar/baz"));
assert(resp); assert(resp);
assertEquals(resp.status, 200); assertEquals(resp.status, Status.OK);
const body = await resp.text(); const body = await resp.text();
assert(body.includes("bar/baz")); assert(body.includes("bar/baz"));
}); });
Deno.test("static file - by file path", async () => { Deno.test("static file - by file path", async () => {
const resp = await router(new Request("https://fresh.deno.dev/foo.txt")); 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(); const body = await resp.text();
assert(body.startsWith("bar")); assert(body.startsWith("bar"));
const etag = resp.headers.get("etag"); 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("etag"), etag);
assertEquals(resp2.headers.get("content-type"), "text/plain"); 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("etag"), etag);
assertEquals(resp3.headers.get("content-type"), "text/plain"); 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}`), new Request(`https://fresh.deno.dev${imgFilePath}`),
); );
const _ = await resp2.text(); const _ = await resp2.text();
assertEquals(resp2.status, 200); assertEquals(resp2.status, Status.OK);
assertEquals( assertEquals(
resp2.headers.get("cache-control"), resp2.headers.get("cache-control"),
"public, max-age=31536000, immutable", "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 // ensure asset hook is not applied on file explicitly excluded with attribute
const imgFilePathWithNoCache = body.match( const imgFilePathWithNoCache = body.match(
@@ -285,7 +285,7 @@ Deno.test("/params/:path*", async () => {
new Request("https://fresh.deno.dev/params/bar/baz"), new Request("https://fresh.deno.dev/params/bar/baz"),
); );
assert(resp); assert(resp);
assertEquals(resp.status, 200); assertEquals(resp.status, Status.OK);
const body = await resp.text(); const body = await resp.text();
assertEquals(body, "bar/baz"); assertEquals(body, "bar/baz");
}); });
@@ -293,7 +293,7 @@ Deno.test("/params/:path*", async () => {
Deno.test("/connInfo", async () => { Deno.test("/connInfo", async () => {
const resp = await router(new Request("https://fresh.deno.dev/connInfo")); const resp = await router(new Request("https://fresh.deno.dev/connInfo"));
assert(resp); assert(resp);
assertEquals(resp.status, 200); assertEquals(resp.status, Status.OK);
const body = await resp.text(); const body = await resp.text();
assertEquals(body, "127.0.0.1"); assertEquals(body, "127.0.0.1");
}); });
@@ -305,7 +305,7 @@ Deno.test({
new Request("https://fresh.deno.dev/middleware_root"), new Request("https://fresh.deno.dev/middleware_root"),
); );
assert(resp); assert(resp);
assertEquals(resp.status, 200); assertEquals(resp.status, Status.OK);
const body = await resp.text(); const body = await resp.text();
assertStringIncludes(body, "root_mw"); assertStringIncludes(body, "root_mw");
assert(!body.includes("layer1_mw")); assert(!body.includes("layer1_mw"));
@@ -319,7 +319,7 @@ Deno.test({
new Request("https://fresh.deno.dev/layeredMdw/layer2/abc"), new Request("https://fresh.deno.dev/layeredMdw/layer2/abc"),
); );
assert(resp); assert(resp);
assertEquals(resp.status, 200); assertEquals(resp.status, Status.OK);
const body = await resp.text(); const body = await resp.text();
console.log(body); console.log(body);
assertStringIncludes(body, "root_mw"); assertStringIncludes(body, "root_mw");
@@ -332,7 +332,7 @@ Deno.test({
new Request("https://fresh.deno.dev/layeredMdw/layer2-no-mw/without_mw"), new Request("https://fresh.deno.dev/layeredMdw/layer2-no-mw/without_mw"),
); );
assert(resp1); assert(resp1);
assertEquals(resp1.status, 200); assertEquals(resp1.status, Status.OK);
const body1 = await resp1.text(); const body1 = await resp1.text();
assertStringIncludes(body1, "root_mw"); assertStringIncludes(body1, "root_mw");
assertStringIncludes(body1, "layer1_mw"); assertStringIncludes(body1, "layer1_mw");
@@ -349,7 +349,7 @@ Deno.test({
new Request("https://fresh.deno.dev/layeredMdw/layer2"), new Request("https://fresh.deno.dev/layeredMdw/layer2"),
); );
assert(resp); assert(resp);
assertEquals(resp.status, 200); assertEquals(resp.status, Status.OK);
const body = await resp.text(); const body = await resp.text();
console.log(body); console.log(body);
assertStringIncludes(body, "root_mw"); assertStringIncludes(body, "root_mw");
@@ -362,7 +362,7 @@ Deno.test({
new Request("https://fresh.deno.dev/layeredMdw/layer2-no-mw/without_mw"), new Request("https://fresh.deno.dev/layeredMdw/layer2-no-mw/without_mw"),
); );
assert(resp1); assert(resp1);
assertEquals(resp1.status, 200); assertEquals(resp1.status, Status.OK);
const body1 = await resp1.text(); const body1 = await resp1.text();
assertStringIncludes(body1, "root_mw"); assertStringIncludes(body1, "root_mw");
assertStringIncludes(body1, "layer1_mw"); assertStringIncludes(body1, "layer1_mw");
@@ -380,7 +380,7 @@ Deno.test({
new Request("https://fresh.deno.dev/layeredMdw/layer2/layer3/abc"), new Request("https://fresh.deno.dev/layeredMdw/layer2/layer3/abc"),
); );
assert(resp); assert(resp);
assertEquals(resp.status, 200); assertEquals(resp.status, Status.OK);
const body = await resp.text(); const body = await resp.text();
assertStringIncludes(body, "root_mw"); assertStringIncludes(body, "root_mw");
assertStringIncludes(body, "layer1_mw"); assertStringIncludes(body, "layer1_mw");