@server/next 0.20.8 → 0.20.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +1 -0
- package/package.json +1 -2
- package/readme.md +5 -5
- package/src/auth/NoSession.js +18 -0
- package/src/auth/auth-cookie.test.js +41 -0
- package/src/auth/auth-token.test.js +42 -0
- package/src/auth/auth.js +52 -0
- package/src/auth/index.js +24 -0
- package/src/auth/index.test.js +123 -0
- package/src/auth/logout.js +18 -0
- package/src/auth/providers/email.js +73 -0
- package/src/auth/providers/index.js +3 -0
- package/src/auth/session.js +18 -0
- package/src/auth/user.js +21 -0
- package/src/context/node.js +9 -5
- package/src/context/winter.js +4 -6
- package/src/errors/index.js +14 -0
- package/src/helpers/cookies.test.js +23 -0
- package/src/helpers/createCookies.js +13 -10
- package/src/helpers/index.js +1 -0
- package/src/helpers/parseHeaders.js +15 -0
- package/src/index.js +68 -3
- package/src/index.test.js +24 -30
- package/src/middle/assets.js +12 -0
- package/src/middle/index.js +7 -66
- package/src/parseResponse.js +3 -1
- package/src/reply.js +5 -2
- package/src/router.test.js +21 -22
- package/src/session.test.js +26 -26
- package/src/test/toSucceed.js +64 -0
- package/src/url.test.js +13 -11
- package/src/auth.test.js +0 -78
- package/src/context/findAuth.js +0 -32
- package/src/context/findSession.js +0 -34
package/src/index.js
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
getMachine,
|
|
11
11
|
handleRequest,
|
|
12
12
|
iterate,
|
|
13
|
+
parseHeaders,
|
|
13
14
|
} from "./helpers/index.js";
|
|
14
15
|
import middle from "./middle/index.js";
|
|
15
16
|
|
|
@@ -59,13 +60,34 @@ const validateOptions = (options, env = {}) => {
|
|
|
59
60
|
if (options.store && options.cookies) {
|
|
60
61
|
options.session = { store: options.store.prefix("session:") };
|
|
61
62
|
}
|
|
62
|
-
|
|
63
|
+
|
|
64
|
+
// AUTH
|
|
65
|
+
options.auth = options.auth || env.AUTH || null;
|
|
63
66
|
if (options.auth) {
|
|
64
67
|
if (typeof options.auth !== "object") {
|
|
65
|
-
|
|
68
|
+
const [type, provider] = options.auth.split(":");
|
|
69
|
+
options.auth = { type, provider };
|
|
70
|
+
}
|
|
71
|
+
if (typeof options.auth.provider === "string") {
|
|
72
|
+
options.auth.provider === options.auth.provider.split("|");
|
|
73
|
+
}
|
|
74
|
+
if (!options.auth.type) {
|
|
75
|
+
throw new Error("Auth options needs a type");
|
|
76
|
+
}
|
|
77
|
+
if (!options.auth.provider) {
|
|
78
|
+
throw new Error("Auth options needs a provider");
|
|
79
|
+
}
|
|
80
|
+
if (!options.auth.session && options.store) {
|
|
81
|
+
options.auth.session = options.store.prefix("auth:");
|
|
66
82
|
}
|
|
67
83
|
if (!options.auth.store && options.store) {
|
|
68
|
-
options.auth.store = options.store.prefix("
|
|
84
|
+
options.auth.store = options.store.prefix("user:");
|
|
85
|
+
}
|
|
86
|
+
if (!options.auth.cleanUser) {
|
|
87
|
+
options.auth.cleanUser = (fullUser) => {
|
|
88
|
+
const { password, ...user } = fullUser;
|
|
89
|
+
return user;
|
|
90
|
+
};
|
|
69
91
|
}
|
|
70
92
|
}
|
|
71
93
|
|
|
@@ -196,3 +218,46 @@ server.prototype.router = function (basePath, router) {
|
|
|
196
218
|
}
|
|
197
219
|
return this;
|
|
198
220
|
};
|
|
221
|
+
|
|
222
|
+
server.prototype.test = function () {
|
|
223
|
+
let cookie = "";
|
|
224
|
+
const fetch = async (path, options = {}) => {
|
|
225
|
+
if (!options.headers) options.headers = {};
|
|
226
|
+
if (options.body && typeof options.body !== "string") {
|
|
227
|
+
options.headers["content-type"] = "application/json";
|
|
228
|
+
options.body = JSON.stringify(options.body);
|
|
229
|
+
}
|
|
230
|
+
if (cookie && !options.headers.cookie) {
|
|
231
|
+
options.headers.cookie = cookie;
|
|
232
|
+
}
|
|
233
|
+
const res = await this.fetch(
|
|
234
|
+
new Request("http://localhost:3000" + path, options)
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
const headers = parseHeaders(res.headers);
|
|
238
|
+
let data;
|
|
239
|
+
if (headers["set-cookie"]) {
|
|
240
|
+
// TODO: this should really be a smart merge of the 2
|
|
241
|
+
cookie = headers["set-cookie"];
|
|
242
|
+
}
|
|
243
|
+
if (headers["content-type"]?.includes("application/json")) {
|
|
244
|
+
data = await res.json();
|
|
245
|
+
} else {
|
|
246
|
+
data = await res.text();
|
|
247
|
+
}
|
|
248
|
+
return { status: res.status, headers, data };
|
|
249
|
+
};
|
|
250
|
+
return {
|
|
251
|
+
app: this,
|
|
252
|
+
get: (path, options) => fetch(path, { method: "get", ...options }),
|
|
253
|
+
head: (path, options) => fetch(path, { method: "head", ...options }),
|
|
254
|
+
post: (path, body, options) =>
|
|
255
|
+
fetch(path, { method: "post", body, ...options }),
|
|
256
|
+
put: (path, body, options) =>
|
|
257
|
+
fetch(path, { method: "put", body, ...options }),
|
|
258
|
+
patch: (path, body, options) =>
|
|
259
|
+
fetch(path, { method: "patch", body, ...options }),
|
|
260
|
+
delete: (path, options) => fetch(path, { method: "delete", ...options }),
|
|
261
|
+
options: (path, options) => fetch(path, { method: "options", ...options }),
|
|
262
|
+
};
|
|
263
|
+
};
|
package/src/index.test.js
CHANGED
|
@@ -1,59 +1,53 @@
|
|
|
1
|
+
import "./test/toSucceed.js";
|
|
2
|
+
|
|
1
3
|
import server, { status } from "./index.js";
|
|
2
4
|
|
|
3
5
|
describe("return different types", () => {
|
|
4
|
-
const
|
|
6
|
+
const api = server()
|
|
5
7
|
.get("/", () => "Hello world")
|
|
6
8
|
.get("/text", () => "Hello world")
|
|
7
9
|
.get("/array", () => ["Hello world"])
|
|
8
10
|
.get("/object", () => ({ hello: "world" }))
|
|
9
|
-
.get("/status", () => 201)
|
|
11
|
+
.get("/status", () => 201)
|
|
12
|
+
.test();
|
|
10
13
|
|
|
11
14
|
it("can get the plain text", async () => {
|
|
12
|
-
const
|
|
13
|
-
expect(
|
|
15
|
+
const { data } = await api.get("/text");
|
|
16
|
+
expect(data).toBe("Hello world");
|
|
14
17
|
});
|
|
15
18
|
|
|
16
19
|
it("can get the array", async () => {
|
|
17
|
-
const
|
|
18
|
-
expect(
|
|
20
|
+
const { data } = await api.get("/array");
|
|
21
|
+
expect(data).toEqual(["Hello world"]);
|
|
19
22
|
});
|
|
20
23
|
|
|
21
24
|
it("can get the object", async () => {
|
|
22
|
-
const
|
|
23
|
-
expect(
|
|
25
|
+
const req = await api.get("/object");
|
|
26
|
+
expect(req).toSucceed({ hello: "world" });
|
|
24
27
|
});
|
|
25
28
|
|
|
26
29
|
it("can get the status", async () => {
|
|
27
|
-
const
|
|
28
|
-
expect(
|
|
30
|
+
const req = await api.get("/status");
|
|
31
|
+
expect(req).toSucceed();
|
|
32
|
+
expect(req.status).toBe(201);
|
|
29
33
|
});
|
|
30
34
|
});
|
|
31
35
|
|
|
32
36
|
describe("simple post works", () => {
|
|
33
|
-
const
|
|
37
|
+
const api = server()
|
|
38
|
+
.post("/", (ctx) => status(201).send(ctx.body))
|
|
39
|
+
.test();
|
|
34
40
|
|
|
35
41
|
it("can post new data", async () => {
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
body: "New Data",
|
|
40
|
-
})
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
expect(res.status).toBe(201);
|
|
44
|
-
expect(await res.text()).toBe("New Data");
|
|
42
|
+
const { data, status } = await api.post("/", "New Data");
|
|
43
|
+
expect(status).toBe(201);
|
|
44
|
+
expect(data).toBe("New Data");
|
|
45
45
|
});
|
|
46
46
|
|
|
47
47
|
it("will return JSON", async () => {
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
headers: { "content-type": "application/json" },
|
|
53
|
-
})
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
expect(res.status).toBe(201);
|
|
57
|
-
expect(await res.json()).toEqual({ hello: "world" });
|
|
48
|
+
const { data, status, headers } = await api.post("/", { hello: "world" });
|
|
49
|
+
expect(status).toBe(201);
|
|
50
|
+
expect(data).toEqual({ hello: "world" });
|
|
51
|
+
expect(headers["content-type"]).toBe("application/json");
|
|
58
52
|
});
|
|
59
53
|
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type } from "../reply.js";
|
|
2
|
+
|
|
3
|
+
export default async function assets(ctx) {
|
|
4
|
+
try {
|
|
5
|
+
// TODO: streaming
|
|
6
|
+
const asset = await ctx.options.public.read(ctx.url.pathname);
|
|
7
|
+
if (!asset) return;
|
|
8
|
+
return type(ctx.url.pathname.split(".").pop()).send(asset);
|
|
9
|
+
} catch (error) {
|
|
10
|
+
// NO-OP; if there's no file, keep going the normal flow
|
|
11
|
+
}
|
|
12
|
+
}
|
package/src/middle/index.js
CHANGED
|
@@ -1,71 +1,12 @@
|
|
|
1
|
-
import
|
|
1
|
+
import auth from "../auth/index.js";
|
|
2
|
+
import assets from "./assets.js";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import ServerError from "../ServerError.js";
|
|
6
|
-
|
|
7
|
-
export default function middle(ctx) {
|
|
4
|
+
export default async function middle(ctx) {
|
|
5
|
+
// Serve assets
|
|
8
6
|
if (ctx.options.public) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"*",
|
|
12
|
-
async function publicFolder(ctx) {
|
|
13
|
-
try {
|
|
14
|
-
const asset = await ctx.options.public.read(ctx.url.pathname);
|
|
15
|
-
if (asset) {
|
|
16
|
-
return type(ctx.url.pathname.split(".").pop()).send(asset);
|
|
17
|
-
}
|
|
18
|
-
} catch (error) {}
|
|
19
|
-
},
|
|
20
|
-
]);
|
|
7
|
+
// We need these before other endpoints
|
|
8
|
+
ctx.app.handlers.get.unshift(["*", "*", assets]);
|
|
21
9
|
}
|
|
22
10
|
|
|
23
|
-
|
|
24
|
-
if (ctx.auth?.providers?.includes("email")) {
|
|
25
|
-
ctx.app.post("/auth/register/email", async (ctx) => {
|
|
26
|
-
const { email, password, ...data } = ctx.body;
|
|
27
|
-
if (!email || !/@/.test(email)) throw new Error("Email needed");
|
|
28
|
-
if (!password || password.length < 8) throw new Error("Password needed");
|
|
29
|
-
if (await ctx.auth.store.has(email)) {
|
|
30
|
-
throw new Error("Email is already registered");
|
|
31
|
-
}
|
|
32
|
-
const id = createId();
|
|
33
|
-
const time = new Date().toISOString();
|
|
34
|
-
const pass = await argon2.hash(password);
|
|
35
|
-
await ctx.auth.store.set(email, { id, email, password: pass, ...data });
|
|
36
|
-
|
|
37
|
-
// TYPE GOES HERE
|
|
38
|
-
const token = await ctx.options.session.store.add({ id, email, time });
|
|
39
|
-
return status(201).json({ id, token, email, ...data });
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
ctx.app.post("/auth/logout", async (ctx) => {
|
|
43
|
-
if (ctx.auth.id) {
|
|
44
|
-
await ctx.options.session.store.del(ctx.auth.id);
|
|
45
|
-
}
|
|
46
|
-
return status(200).send();
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
ctx.app.post("/auth/login/email", async (ctx) => {
|
|
50
|
-
const { email, password } = ctx.body;
|
|
51
|
-
if (!email) throw ServerError.LOGIN_NO_EMAIL();
|
|
52
|
-
if (!/@/.test(email)) throw ServerError.LOGIN_INVALID_EMAIL();
|
|
53
|
-
if (!password) throw ServerError.LOGIN_NO_PASSWORD();
|
|
54
|
-
if (password.length < 8) throw ServerError.LOGIN_INVALID_PASSWORD();
|
|
55
|
-
|
|
56
|
-
const time = new Date().toISOString();
|
|
57
|
-
const user = await ctx.auth.store.get(email);
|
|
58
|
-
if (!user) throw ServerError.LOGIN_WRONG_EMAIL();
|
|
59
|
-
const isValid = await argon2.verify(user.password, password);
|
|
60
|
-
if (!isValid) throw ServerError.LOGIN_WRONG_PASSWORD();
|
|
61
|
-
|
|
62
|
-
// TYPE GOES HERE
|
|
63
|
-
const token = await ctx.options.session.store.add({
|
|
64
|
-
id: user.id,
|
|
65
|
-
email,
|
|
66
|
-
time,
|
|
67
|
-
});
|
|
68
|
-
return { id: user.id, token, email };
|
|
69
|
-
});
|
|
70
|
-
}
|
|
11
|
+
await auth.middle(ctx);
|
|
71
12
|
}
|
package/src/parseResponse.js
CHANGED
|
@@ -61,7 +61,9 @@ export default async function parseResponse(out, ctx) {
|
|
|
61
61
|
// Cookies to headers
|
|
62
62
|
if (ctx.options.cookies) {
|
|
63
63
|
if (Object.keys(ctx.res.cookies).length) {
|
|
64
|
-
|
|
64
|
+
createCookies(ctx.res.cookies).forEach((cookie) => {
|
|
65
|
+
ctx.res.headers.append("set-cookie", cookie);
|
|
66
|
+
});
|
|
65
67
|
}
|
|
66
68
|
}
|
|
67
69
|
|
package/src/reply.js
CHANGED
|
@@ -11,8 +11,11 @@ function Reply() {
|
|
|
11
11
|
|
|
12
12
|
// INTERNAL
|
|
13
13
|
Reply.prototype.generateHeaders = function () {
|
|
14
|
-
const
|
|
15
|
-
|
|
14
|
+
const headers = new Headers(this.res.headers);
|
|
15
|
+
createCookies(this.res.cookies).forEach((cookie) => {
|
|
16
|
+
headers.append("set-cookie", cookie);
|
|
17
|
+
});
|
|
18
|
+
return headers;
|
|
16
19
|
};
|
|
17
20
|
|
|
18
21
|
// PARTIAL
|
package/src/router.test.js
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
import server, { router, status } from "./index.js";
|
|
2
2
|
|
|
3
|
-
const url = (path, options = {}) =>
|
|
4
|
-
new Request("http://localhost:3000" + path, options);
|
|
5
|
-
|
|
6
3
|
describe("can route properly", () => {
|
|
7
|
-
const
|
|
4
|
+
const apiRouter = router()
|
|
8
5
|
.get("/hello", (ctx) => "Hello " + ctx.url.pathname)
|
|
9
6
|
.put("/hello", (ctx) => "Hello " + ctx.url.pathname)
|
|
10
7
|
.post("/hello", (ctx) => "Hello " + ctx.url.pathname);
|
|
11
8
|
|
|
12
9
|
const app = server()
|
|
13
|
-
.router("/",
|
|
14
|
-
.router("/api/",
|
|
10
|
+
.router("/", apiRouter)
|
|
11
|
+
.router("/api/", apiRouter)
|
|
15
12
|
.get("/", () => "Fallback");
|
|
13
|
+
const api = app.test();
|
|
16
14
|
|
|
17
15
|
// INTERNAL - so this might change in the future
|
|
18
16
|
it("has the correct structure", () => {
|
|
@@ -21,38 +19,39 @@ describe("can route properly", () => {
|
|
|
21
19
|
});
|
|
22
20
|
|
|
23
21
|
it("can get fallback when nothing matches", async () => {
|
|
24
|
-
const res = await
|
|
25
|
-
expect(
|
|
22
|
+
const res = await api.get("/");
|
|
23
|
+
expect(res.data).toBe("Fallback");
|
|
26
24
|
});
|
|
27
25
|
|
|
28
26
|
it("can get the base get", async () => {
|
|
29
|
-
const res = await
|
|
30
|
-
expect(
|
|
27
|
+
const res = await api.get("/hello");
|
|
28
|
+
expect(res.data).toBe("Hello /hello");
|
|
31
29
|
});
|
|
32
30
|
|
|
33
31
|
it("can get the nested get", async () => {
|
|
34
|
-
const res = await
|
|
35
|
-
expect(
|
|
32
|
+
const res = await api.get("/api/hello");
|
|
33
|
+
expect(res.data).toBe("Hello /api/hello");
|
|
36
34
|
});
|
|
37
35
|
|
|
38
36
|
it("can post to the base get", async () => {
|
|
39
|
-
const res = await
|
|
40
|
-
expect(
|
|
37
|
+
const res = await api.post("/hello");
|
|
38
|
+
expect(res.data).toBe("Hello /hello");
|
|
41
39
|
});
|
|
42
40
|
|
|
43
41
|
it("can post to the nested get", async () => {
|
|
44
|
-
const res = await
|
|
45
|
-
expect(
|
|
42
|
+
const res = await api.post("/api/hello");
|
|
43
|
+
expect(res.data).toBe("Hello /api/hello");
|
|
46
44
|
});
|
|
47
45
|
|
|
48
46
|
it("no status reuse", async () => {
|
|
49
|
-
const
|
|
47
|
+
const api = server()
|
|
50
48
|
.get("/a", () => status(201).send("hello"))
|
|
51
49
|
.get("/b", () => ({ hello: "bye" }))
|
|
52
|
-
.get("/", () => "Fallback")
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
50
|
+
.get("/", () => "Fallback")
|
|
51
|
+
.test();
|
|
52
|
+
const { status: statusA } = await api.get("/a");
|
|
53
|
+
expect(statusA).toBe(201);
|
|
54
|
+
const { status: statusB } = await api.get("/b");
|
|
55
|
+
expect(statusB).toBe(200);
|
|
57
56
|
});
|
|
58
57
|
});
|
package/src/session.test.js
CHANGED
|
@@ -2,64 +2,64 @@ import kv from "polystore";
|
|
|
2
2
|
|
|
3
3
|
import server from "./index.js";
|
|
4
4
|
|
|
5
|
-
const url = (path, options = {}) =>
|
|
6
|
-
new Request("http://localhost:3000" + path, {
|
|
7
|
-
headers: { cookie: "session=REqA2l022l8Q0tuIRtqLOPUy" },
|
|
8
|
-
...options,
|
|
9
|
-
});
|
|
10
|
-
|
|
11
5
|
describe("session", () => {
|
|
12
6
|
const store = kv(new Map());
|
|
13
|
-
const
|
|
7
|
+
const api = server({ store })
|
|
14
8
|
.get("/hello", (ctx) => "Hello " + ctx.session.a)
|
|
15
9
|
.post("/hello", (ctx) => {
|
|
16
10
|
if (!ctx.session.a) ctx.session.a = 0;
|
|
17
11
|
ctx.session.a += 1;
|
|
18
12
|
return "Bye " + ctx.session.a;
|
|
19
13
|
})
|
|
20
|
-
.get("/", () => "Fallback")
|
|
14
|
+
.get("/", () => "Fallback")
|
|
15
|
+
.test();
|
|
21
16
|
|
|
22
17
|
it("can get the nested get", async () => {
|
|
18
|
+
const cookie = "session=REqA2l022l8Q0tuIRtqLOPUy";
|
|
19
|
+
const options = { headers: { cookie } };
|
|
23
20
|
await store.set("session:REqA2l022l8Q0tuIRtqLOPUy", { a: 0 });
|
|
24
21
|
|
|
25
|
-
const res = await
|
|
26
|
-
expect(
|
|
22
|
+
const res = await api.get("/hello", options);
|
|
23
|
+
expect(res.data).toBe("Hello 0");
|
|
27
24
|
|
|
28
|
-
const res2 = await
|
|
29
|
-
expect(
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
const res2 = await api.post("/hello", {}, options);
|
|
26
|
+
expect(res2.data).toBe("Bye 1");
|
|
27
|
+
|
|
28
|
+
const res3 = await api.get("/hello", options);
|
|
29
|
+
expect(res3.data).toBe("Hello 1");
|
|
32
30
|
expect(await store.get("session:REqA2l022l8Q0tuIRtqLOPUy")).toEqual({
|
|
33
31
|
a: 1,
|
|
34
32
|
});
|
|
35
33
|
|
|
36
|
-
const res4 = await
|
|
37
|
-
expect(
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
const res4 = await api.post("/hello", {}, options);
|
|
35
|
+
expect(res4.data).toBe("Bye 2");
|
|
36
|
+
|
|
37
|
+
const res5 = await api.get("/hello", options);
|
|
38
|
+
expect(res5.data).toBe("Hello 2");
|
|
40
39
|
expect(await store.get("session:REqA2l022l8Q0tuIRtqLOPUy")).toEqual({
|
|
41
40
|
a: 2,
|
|
42
41
|
});
|
|
43
42
|
});
|
|
43
|
+
});
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
describe("missing store", () => {
|
|
46
|
+
const api = server({ store: null })
|
|
46
47
|
.get("/read", (ctx) => "Bye " + ctx.session.a)
|
|
47
48
|
.get("/write", (ctx) => {
|
|
48
49
|
ctx.session.a = "hello";
|
|
49
50
|
return "All good";
|
|
50
|
-
})
|
|
51
|
+
})
|
|
52
|
+
.test();
|
|
51
53
|
|
|
52
54
|
it("cannot read a session without a store", async () => {
|
|
53
|
-
const res = await
|
|
54
|
-
const body = await res.text();
|
|
55
|
+
const res = await api.get("/read");
|
|
55
56
|
expect(res.status).toBe(500);
|
|
56
|
-
expect(
|
|
57
|
+
expect(res.data).toBe("You need a 'store' to read 'ctx.session.a'");
|
|
57
58
|
});
|
|
58
59
|
|
|
59
60
|
it("cannot write a session without a store", async () => {
|
|
60
|
-
const res = await
|
|
61
|
-
const body = await res.text();
|
|
61
|
+
const res = await api.get("/write");
|
|
62
62
|
expect(res.status).toBe(500);
|
|
63
|
-
expect(
|
|
63
|
+
expect(res.data).toBe("You need a 'store' to write 'ctx.session.a'");
|
|
64
64
|
});
|
|
65
65
|
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { expect } from "@jest/globals";
|
|
2
|
+
|
|
3
|
+
const reset = "\x1b[0m";
|
|
4
|
+
|
|
5
|
+
const spaceOrEnter = (msg) => {
|
|
6
|
+
if (typeof msg === "string") {
|
|
7
|
+
return " ";
|
|
8
|
+
}
|
|
9
|
+
return "\n";
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export default function toSucceed(request, message) {
|
|
13
|
+
const pass = request.status >= 200 && request.status < 300;
|
|
14
|
+
|
|
15
|
+
if (message && JSON.stringify(request.data) !== JSON.stringify(message)) {
|
|
16
|
+
if (pass) {
|
|
17
|
+
return {
|
|
18
|
+
message: () =>
|
|
19
|
+
`${reset}Expected body:${spaceOrEnter(
|
|
20
|
+
message
|
|
21
|
+
)}${this.utils.printExpected(message)}\nReceived body:${spaceOrEnter(
|
|
22
|
+
request.data
|
|
23
|
+
)}${this.utils.printReceived(request.data)}`,
|
|
24
|
+
pass,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
message: () =>
|
|
30
|
+
`${reset}Expected error:${spaceOrEnter(
|
|
31
|
+
message
|
|
32
|
+
)}${this.utils.printExpected(message)}\nReceived error:${spaceOrEnter(
|
|
33
|
+
request.data
|
|
34
|
+
)}${this.utils.printReceived(request.data)}`,
|
|
35
|
+
pass: !pass,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (pass) {
|
|
40
|
+
return {
|
|
41
|
+
message: () =>
|
|
42
|
+
`${reset}Expected ${this.utils.printExpected(
|
|
43
|
+
request.status
|
|
44
|
+
)} to be an error code, received body:\n${this.utils.printExpected(
|
|
45
|
+
request.data
|
|
46
|
+
)}`,
|
|
47
|
+
pass: true,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
message: () =>
|
|
53
|
+
`${reset}Expected ${this.utils.printReceived(
|
|
54
|
+
request.status
|
|
55
|
+
)} to succeed, received body:\n${this.utils.printReceived(
|
|
56
|
+
request.data,
|
|
57
|
+
null,
|
|
58
|
+
2
|
|
59
|
+
)}`,
|
|
60
|
+
pass: false,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
expect.extend({ toSucceed });
|
package/src/url.test.js
CHANGED
|
@@ -2,23 +2,25 @@ import server from "./index.js";
|
|
|
2
2
|
|
|
3
3
|
describe("can match the url", () => {
|
|
4
4
|
it("stops at the first matching route", async () => {
|
|
5
|
-
const
|
|
5
|
+
const api = server()
|
|
6
6
|
.get("/:id", (ctx) => ctx.url.params)
|
|
7
|
-
.get("/*", (ctx) => ctx.url.params)
|
|
7
|
+
.get("/*", (ctx) => ctx.url.params)
|
|
8
|
+
.test();
|
|
8
9
|
|
|
9
|
-
const
|
|
10
|
-
expect(
|
|
10
|
+
const { data, headers } = await api.get("/hello");
|
|
11
|
+
expect(data).toEqual({ id: "hello" });
|
|
12
|
+
expect(headers["content-type"]).toEqual("application/json");
|
|
11
13
|
});
|
|
12
14
|
|
|
13
15
|
it("but it doesn't if it's a use", async () => {
|
|
14
|
-
const
|
|
15
|
-
.use(() => {
|
|
16
|
-
// No-op
|
|
17
|
-
})
|
|
16
|
+
const api = server()
|
|
17
|
+
.use(() => {}) // No-op
|
|
18
18
|
.get("/:id", (ctx) => ctx.url.params)
|
|
19
|
-
.get("/*", (ctx) => ctx.url.params)
|
|
19
|
+
.get("/*", (ctx) => ctx.url.params)
|
|
20
|
+
.test();
|
|
20
21
|
|
|
21
|
-
const
|
|
22
|
-
expect(
|
|
22
|
+
const { data, headers, ...rest } = await api.get("/hello");
|
|
23
|
+
expect(data).toEqual({ id: "hello" });
|
|
24
|
+
expect(headers["content-type"]).toEqual("application/json");
|
|
23
25
|
});
|
|
24
26
|
});
|
package/src/auth.test.js
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import kv from "polystore";
|
|
2
|
-
|
|
3
|
-
import server from "./index.js";
|
|
4
|
-
|
|
5
|
-
const url = (token) =>
|
|
6
|
-
new Request("http://localhost:3000/", {
|
|
7
|
-
headers: { authorization: token },
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
describe("auth", () => {
|
|
11
|
-
const store = kv(new Map());
|
|
12
|
-
const app = server({ store }).get("/", (ctx) => {
|
|
13
|
-
return ctx.headers.authorization;
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it("should be Bearer", async () => {
|
|
17
|
-
const res = await app.fetch(url("Basic REqA2l022l8Q0tuIRtqLOPUy"));
|
|
18
|
-
expect(await res.text()).toBe("Invalid Authorization type, 'Basic'");
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it("should have the proper token", async () => {
|
|
22
|
-
const res = await app.fetch(url("Bearer hola"));
|
|
23
|
-
expect(await res.text()).toBe("Invalid Authorization token");
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it("can get the nested get", async () => {
|
|
27
|
-
const res = await app.fetch(url("Bearer REqA2l022l8Q0tuIRtqLOPUy"));
|
|
28
|
-
expect(await res.text()).toBe("Bearer REqA2l022l8Q0tuIRtqLOPUy");
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
describe("user creation flow", () => {
|
|
32
|
-
// These are obviously mock data
|
|
33
|
-
const EMAIL = "abc@test.com";
|
|
34
|
-
const PASS = "11111111";
|
|
35
|
-
|
|
36
|
-
const url = (path, body = {}, headers = {}) =>
|
|
37
|
-
new Request("http://localhost:3000" + path, {
|
|
38
|
-
headers: {
|
|
39
|
-
cookie: "session=REqA2l022l8Q0tuIRtqLOPUy",
|
|
40
|
-
"content-type": "application/json",
|
|
41
|
-
...headers,
|
|
42
|
-
},
|
|
43
|
-
method: "POST",
|
|
44
|
-
body: JSON.stringify(body),
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
const store = kv(new Map());
|
|
48
|
-
const app = server({ auth: { type: "token", providers: "email" }, store });
|
|
49
|
-
|
|
50
|
-
it("can create a new user", async () => {
|
|
51
|
-
const regReq = await app.fetch(
|
|
52
|
-
url("/auth/register/email", { email: EMAIL, password: PASS })
|
|
53
|
-
);
|
|
54
|
-
const register = await regReq.json();
|
|
55
|
-
expect(regReq.status).toBe(201);
|
|
56
|
-
expect(await store.keys()).toEqual([
|
|
57
|
-
"auth:abc@test.com",
|
|
58
|
-
"session:" + register.token,
|
|
59
|
-
]);
|
|
60
|
-
|
|
61
|
-
const logoutReq = await app.fetch(
|
|
62
|
-
url("/auth/logout", {}, { authorization: "Bearer " + register.token })
|
|
63
|
-
);
|
|
64
|
-
expect(logoutReq.status).toBe(200);
|
|
65
|
-
expect(await store.keys()).toEqual(["auth:abc@test.com"]);
|
|
66
|
-
|
|
67
|
-
const loginReq = await app.fetch(
|
|
68
|
-
url("/auth/login/email", { email: EMAIL, password: PASS })
|
|
69
|
-
);
|
|
70
|
-
const login = await loginReq.json();
|
|
71
|
-
expect(loginReq.status).toBe(200);
|
|
72
|
-
expect(await store.keys()).toEqual([
|
|
73
|
-
"auth:abc@test.com",
|
|
74
|
-
"session:" + login.token,
|
|
75
|
-
]);
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
});
|