refactor: use named status codes from std internally (#417)
Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
co-authored by
Luca Casonato
parent
ba20756c17
commit
1dc1f87fc3
@@ -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<string, unknown>;
|
||||
}
|
||||
@@ -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<RouterState> = (
|
||||
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<RouterState> = (
|
||||
req,
|
||||
ctx,
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { ServerContext } from "./context.ts";
|
||||
import { serve } from "./deps.ts";
|
||||
export { Status } from "./deps.ts";
|
||||
import {
|
||||
AppModule,
|
||||
ErrorPageModule,
|
||||
|
||||
+3
-2
@@ -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"] });
|
||||
|
||||
+3
-3
@@ -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",
|
||||
|
||||
+30
-30
@@ -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, "<div>Hello bar</div>");
|
||||
@@ -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, "<div>This is HTML</div>");
|
||||
});
|
||||
@@ -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, "<div>intercepted</div>");
|
||||
});
|
||||
@@ -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, "<div>Book 123</div>");
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user