feat: pass ConnInfo to various handler contexts (#121)

This commit is contained in:
Leo Kettmeir
2022-03-09 19:16:08 +01:00
committed by GitHub
parent 8d689938cb
commit 67b3eb470e
7 changed files with 92 additions and 36 deletions
+14 -1
View File
@@ -3,7 +3,20 @@ import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
import manifest from "./fixture_error/fresh.gen.ts";
const ctx = await ServerContext.fromManifest(manifest);
const router = ctx.handler();
const router = (req: Request) => {
return ctx.handler()(req, {
localAddr: {
transport: "tcp",
hostname: "127.0.0.1",
port: 80,
},
remoteAddr: {
transport: "tcp",
hostname: "127.0.0.1",
port: 80,
},
});
};
Deno.test("error page rendered", async () => {
const resp = await router(new Request("https://fresh.deno.dev/"));
+18 -16
View File
@@ -10,14 +10,15 @@ import * as $4 from "./routes/_middleware.ts";
import * as $5 from "./routes/_render.ts";
import * as $6 from "./routes/api/get_only.ts";
import * as $7 from "./routes/books/[id].tsx";
import * as $8 from "./routes/failure.ts";
import * as $9 from "./routes/index.tsx";
import * as $10 from "./routes/intercept.tsx";
import * as $11 from "./routes/intercept_args.tsx";
import * as $12 from "./routes/params.tsx";
import * as $13 from "./routes/props/[id].tsx";
import * as $14 from "./routes/static.tsx";
import * as $15 from "./routes/wildcard.tsx";
import * as $8 from "./routes/connInfo.ts";
import * as $9 from "./routes/failure.ts";
import * as $10 from "./routes/index.tsx";
import * as $11 from "./routes/intercept.tsx";
import * as $12 from "./routes/intercept_args.tsx";
import * as $13 from "./routes/params.tsx";
import * as $14 from "./routes/props/[id].tsx";
import * as $15 from "./routes/static.tsx";
import * as $16 from "./routes/wildcard.tsx";
import * as $$0 from "./islands/Test.tsx";
const manifest = {
@@ -30,14 +31,15 @@ const manifest = {
"./routes/_render.ts": $5,
"./routes/api/get_only.ts": $6,
"./routes/books/[id].tsx": $7,
"./routes/failure.ts": $8,
"./routes/index.tsx": $9,
"./routes/intercept.tsx": $10,
"./routes/intercept_args.tsx": $11,
"./routes/params.tsx": $12,
"./routes/props/[id].tsx": $13,
"./routes/static.tsx": $14,
"./routes/wildcard.tsx": $15,
"./routes/connInfo.ts": $8,
"./routes/failure.ts": $9,
"./routes/index.tsx": $10,
"./routes/intercept.tsx": $11,
"./routes/intercept_args.tsx": $12,
"./routes/params.tsx": $13,
"./routes/props/[id].tsx": $14,
"./routes/static.tsx": $15,
"./routes/wildcard.tsx": $16,
},
islands: {
"./islands/Test.tsx": $$0,
+7
View File
@@ -0,0 +1,7 @@
import { Handlers } from "../../../server.ts";
export const handler: Handlers = {
GET(_req, ctx) {
return new Response((ctx.remoteAddr as Deno.NetAddr).hostname);
},
};
+22 -1
View File
@@ -3,7 +3,20 @@ import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
import manifest from "./fixture/fresh.gen.ts";
const ctx = await ServerContext.fromManifest(manifest);
const router = ctx.handler();
const router = (req: Request) => {
return ctx.handler()(req, {
localAddr: {
transport: "tcp",
hostname: "127.0.0.1",
port: 80,
},
remoteAddr: {
transport: "tcp",
hostname: "127.0.0.1",
port: 80,
},
});
};
Deno.test("/ page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/"));
@@ -194,3 +207,11 @@ Deno.test("/params/:path*", async () => {
const body = await resp.text();
assertEquals(body, "bar/baz");
});
Deno.test("/connInfo", async () => {
const resp = await router(new Request("https://fresh.deno.dev/connInfo"));
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assertEquals(body, "127.0.0.1");
});