-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
superdeno.oak.test.ts
148 lines (130 loc) · 4.2 KB
/
superdeno.oak.test.ts
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// deno-lint-ignore-file no-explicit-any
import { getFreePort } from "../deps.ts";
import { expect, Oak } from "./deps.ts";
import { describe, it, random } from "./utils.ts";
import { superdeno, Test } from "../mod.ts";
const { Application, Router } = Oak;
const bootstrapOakServerTest = async (
{ configureApp, assertionsDelegate, done }: {
configureApp: (
{ app, router }: { app: Oak.Application; router: Oak.Router },
) => void;
assertionsDelegate: (
{ app, url, controller, done }: {
app: Oak.Application;
url: string;
controller: AbortController;
done: () => void;
},
) => void;
done: () => void;
},
) => {
const router = new Router();
const app = new Application();
configureApp({ app, router });
app.use(router.routes());
app.use(router.allowedMethods());
const controller = new AbortController();
const signal = controller.signal;
const freePort = await getFreePort(random(1024, 49151));
app.addEventListener("listen", ({ hostname, port, secure }: any) => {
const protocol = secure ? "https" : "http";
const url = `${protocol}://${hostname}:${port}`;
assertionsDelegate({ app, url, controller, done });
});
await app.listen({ hostname: "127.0.0.1", port: freePort, signal });
};
describe("Oak: superdeno(url)", () => {
it("Oak: superdeno(url): should support open `superdeno(url)` format for web frameworks such as Oak", async (done) => {
await bootstrapOakServerTest({
configureApp: ({ router }) => {
router.get("/", (ctx) => {
ctx.response.body = "hello";
});
},
assertionsDelegate: ({ url, controller, done }) =>
superdeno(url)
.get("/")
.expect("hello", () => {
controller.abort();
done();
}),
done,
});
});
describe(".expect(status, body[, fn])", () => {
it("Oak: superdeno(url): .expect(status, body[, fn]): should assert the response body and status", async (done) => {
await bootstrapOakServerTest({
configureApp: ({ router }) => {
router.get("/", (ctx) => {
ctx.response.body = "foo";
});
},
assertionsDelegate: ({ url, controller, done }) =>
superdeno(url)
.get("/")
.expect(200, "foo", () => {
controller.abort();
done();
}),
done,
});
});
it("superdeno(app): .expect(status, body[, fn]): should assert the response body and error status'", async (done) => {
await bootstrapOakServerTest({
configureApp: ({ router }) => {
router.get("/", (ctx) => {
ctx.throw(400, "foo");
});
},
assertionsDelegate: ({ url, controller, done }) =>
superdeno(url)
.get("/")
.expect(400, "foo", () => {
controller.abort();
done();
}),
done,
});
});
});
describe(".end(cb)", () => {
it("Oak: superdeno(url): .end(cb): should set `this` to the test object when calling the `cb` in `.end(cb)`", async (done) => {
await bootstrapOakServerTest({
configureApp: ({ router }) => {
router.get("/", (ctx) => {
ctx.response.body = "hello";
});
},
assertionsDelegate: ({ url, controller, done }) => {
const test = superdeno(url).get("/");
test.end(function (this: Test) {
expect(test).toEqual(this);
controller.abort();
done();
});
},
done,
});
});
});
});
describe("Oak: superdeno(app.handle)", () => {
it("Oak: superdeno(app.handle): should support `superdeno(app.handle)` for web frameworks such as Oak", async () => {
const router = new Router();
const app = new Application();
router.get("/", (ctx) => {
ctx.response.body = "Hello Deno!";
});
app.use(router.routes());
app.use(router.allowedMethods());
/**
* Note that we have to bind `app` to the function otherwise `app.handle`
* doesn't maintain `this` from the `app`.
*/
await superdeno(app.handle.bind(app))
.get("/")
.expect("Hello Deno!");
});
});