-
Notifications
You must be signed in to change notification settings - Fork 754
Expand file tree
/
Copy pathcontext_test.tsx
More file actions
108 lines (88 loc) · 3.26 KB
/
Copy pathcontext_test.tsx
File metadata and controls
108 lines (88 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { expect } from "@std/expect";
import { Context } from "./context.ts";
import { App } from "fresh";
import { asset } from "fresh/runtime";
import { FakeServer } from "./test_utils.ts";
import { BUILD_ID } from "@fresh/build-id";
import { parseHtml } from "../tests/test_utils.tsx";
Deno.test("FreshReqContext.prototype.redirect", () => {
let res = Context.prototype.redirect("/");
expect(res.status).toEqual(302);
expect(res.headers.get("Location")).toEqual("/");
res = Context.prototype.redirect("//evil.com");
expect(res.status).toEqual(302);
expect(res.headers.get("Location")).toEqual("/evil.com");
res = Context.prototype.redirect("//evil.com/foo//bar");
expect(res.status).toEqual(302);
expect(res.headers.get("Location")).toEqual("/evil.com/foo/bar");
res = Context.prototype.redirect("https://deno.com");
expect(res.status).toEqual(302);
expect(res.headers.get("Location")).toEqual("https://deno.com");
res = Context.prototype.redirect("/", 307);
expect(res.status).toEqual(307);
});
Deno.test("render asset()", async () => {
const app = new App()
.get("/", (ctx) =>
ctx.render(
<>
<p class="raw">{asset("/foo")}</p>
<img src="/foo" srcset="/foo-bar" />
<source src="/foo" />
</>,
));
const server = new FakeServer(app.handler());
const res = await server.get("/");
const doc = parseHtml(await res.text());
expect(doc.querySelector(".raw")!.textContent).toContain(BUILD_ID);
expect(doc.querySelector("img")!.src).toContain(BUILD_ID);
expect(doc.querySelector("img")!.srcset).toContain(BUILD_ID);
expect(doc.querySelector("source")!.src).toContain(BUILD_ID);
});
Deno.test("ctx.render - throw with no arguments", async () => {
const app = new App()
// deno-lint-ignore no-explicit-any
.get("/", (ctx) => (ctx as any).render());
const server = new FakeServer(app.handler());
const res = await server.get("/");
await res.body?.cancel();
expect(res.status).toEqual(500);
});
Deno.test("ctx.render - throw with invalid first arg", async () => {
const app = new App()
// deno-lint-ignore no-explicit-any
.get("/", (ctx) => (ctx as any).render({}));
const server = new FakeServer(app.handler());
const res = await server.get("/");
await res.body?.cancel();
expect(res.status).toEqual(500);
});
Deno.test("ctx.isPartial - should indicate whether request is partial or not", async () => {
const isPartials: boolean[] = [];
const app = new App()
.get("/", (ctx) => {
isPartials.push(ctx.isPartial);
return new Response("ok");
});
const server = new FakeServer(app.handler());
await server.get("/");
await server.get("/?fresh-partial");
expect(isPartials).toEqual([false, true]);
});
Deno.test("ctx.route - should contain matched route", async () => {
let route: string | null = null;
const app = new App()
.use((ctx) => {
route = ctx.route;
return ctx.next();
})
.get("/foo/bar", () => new Response("ok"))
.get("/foo/:id", () => new Response("ok"));
const server = new FakeServer(app.handler());
await server.get("/invalid");
expect(route).toEqual(null);
await server.get("/foo/bar");
expect(route).toEqual("/foo/bar");
await server.get("/foo/123");
expect(route).toEqual("/foo/:id");
});