@j3r3mcdev/oast-server 1.1.5 → 1.1.6
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/.github/workflows/ci.yml +29 -29
- package/.github/workflows/publish.yml +31 -31
- package/README.md +192 -192
- package/dist/index.d.ts +0 -1
- package/dist/index.js +1 -16
- package/jest.config.js +14 -14
- package/package.json +45 -45
- package/sadmin list shadows +9 -9
- package/src/api/controllers/__tests__/tasks.controller.test.ts +74 -74
- package/src/api/controllers/events.controller.ts +10 -10
- package/src/api/controllers/health.controller.ts +7 -7
- package/src/api/controllers/tasks.controller.ts +41 -41
- package/src/api/dto/__tests__/create-task.dto.test.ts +41 -41
- package/src/api/dto/__tests__/filter-tasks.dto.test.ts +35 -35
- package/src/api/dto/create-task.dto.ts +33 -33
- package/src/api/dto/filter-tasks.dto.ts +33 -33
- package/src/api/services/__tests__/events.service.test.ts +41 -41
- package/src/api/services/__tests__/tasks.service.test.ts +41 -41
- package/src/api/services/events.service.ts +17 -17
- package/src/api/services/tasks.service.ts +79 -79
- package/src/api/sse/events.stream.ts +90 -90
- package/src/bootstrap.ts +89 -89
- package/src/core/__tests__/core-router.test.ts +30 -30
- package/src/core/__tests__/core-server.test.ts +44 -44
- package/src/core/__tests__/event.normalizer.test.ts +56 -56
- package/src/core/__tests__/event.router.test.ts +89 -89
- package/src/core/__tests__/logger.test.ts +32 -32
- package/src/core/__tests__/storage-manager.test.ts +74 -74
- package/src/core/event.normalizer.ts +147 -147
- package/src/core/event.router.ts +13 -13
- package/src/core/http/__tests__/adapter-node.test.ts +52 -52
- package/src/core/http/__tests__/body-parser-multipart.test.ts +41 -41
- package/src/core/http/__tests__/body-parser-raw.test.ts +28 -28
- package/src/core/http/__tests__/body-parser-text.test.ts +28 -28
- package/src/core/http/__tests__/compile-path.test.ts +39 -39
- package/src/core/http/__tests__/middleware-pipeline.test.ts +51 -51
- package/src/core/http/__tests__/request.test.ts +34 -34
- package/src/core/http/__tests__/response.test.ts +35 -35
- package/src/core/http/__tests__/router-match.test.ts +171 -171
- package/src/core/http/adapter-node.ts +51 -51
- package/src/core/http/buildRequest.ts +18 -18
- package/src/core/http/compile-path.ts +32 -32
- package/src/core/http/errors.ts +37 -37
- package/src/core/http/http-server.ts +52 -52
- package/src/core/http/middleware.ts +160 -160
- package/src/core/http/request.ts +55 -55
- package/src/core/http/response.ts +93 -93
- package/src/core/http/router.ts +138 -138
- package/src/core/id-generator.ts +8 -8
- package/src/core/logger.ts +113 -113
- package/src/core/router.ts +44 -44
- package/src/core/server.ts +85 -85
- package/src/core/storage.ts +64 -64
- package/src/index.ts +14 -14
- package/src/listeners/api/__tests__/api.controller.test.ts +116 -116
- package/src/listeners/api/__tests__/api.extractor.test.ts +46 -46
- package/src/listeners/api/__tests__/api.listener.test.ts +82 -82
- package/src/listeners/api/__tests__/api.routes.test.ts +155 -155
- package/src/listeners/api/__tests__/api.sse.test.ts +105 -105
- package/src/listeners/api/api.controllers.ts +67 -67
- package/src/listeners/api/api.extractor.ts +43 -43
- package/src/listeners/api/api.listener.ts +50 -50
- package/src/listeners/api/api.routes.ts +76 -76
- package/src/listeners/api/api.sse.ts +38 -38
- package/src/listeners/dns/__tests__/dns.test.ts +118 -118
- package/src/listeners/dns/dns.extractor.ts +14 -14
- package/src/listeners/dns/dns.listener.ts +61 -61
- package/src/listeners/http/__tests__/http.extractor.test.ts +59 -59
- package/src/listeners/http/__tests__/http.listener.test.ts +133 -133
- package/src/listeners/http/http.extractor.ts +15 -15
- package/src/listeners/http/http.listener.ts +110 -110
- package/src/listeners/listener.interface.ts +4 -4
- package/src/listeners/smtp/__tests__/smtp.extractor.test.ts +69 -69
- package/src/listeners/smtp/__tests__/smtp.listener.test.ts +150 -150
- package/src/listeners/smtp/smtp.extractor.ts +18 -18
- package/src/listeners/smtp/smtp.listener.ts +60 -60
- package/src/listeners/ssrf/__tests__/ssrf.extractor.test.ts +41 -41
- package/src/listeners/ssrf/__tests__/ssrf.listener.test.ts +87 -87
- package/src/listeners/ssrf/ssrf.extractor.ts +14 -14
- package/src/listeners/ssrf/ssrf.listener.ts +37 -37
- package/src/listeners/tcp/tcp.extractor.ts +16 -16
- package/src/listeners/tcp/tcp.listener.ts +61 -61
- package/src/listeners/webhook/__tests__/webhook.extractor.test.ts +35 -35
- package/src/listeners/webhook/__tests__/webhook.listener.test.ts +122 -122
- package/src/listeners/webhook/webhook.extractor.ts +12 -12
- package/src/listeners/webhook/webhook.listener.ts +58 -58
- package/src/listeners/websocket/__tests__/websocket.extractor.test.ts +33 -33
- package/src/listeners/websocket/__tests__/websocket.listener.test.ts +90 -90
- package/src/listeners/websocket/websocket.extractor.ts +11 -11
- package/src/listeners/websocket/websocket.listener.ts +40 -40
- package/src/storage-adapters/adapters/__tests__/memory.storage.test.ts +75 -75
- package/src/storage-adapters/adapters/memory.storage.ts +64 -64
- package/src/storage-adapters/storage.interface.ts +26 -26
- package/src/types/event.types.ts +147 -147
- package/tsconfig.json +21 -21
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import { compilePath } from "../compile-path";
|
|
2
|
-
import { describe, it, expect } from "@jest/globals";
|
|
3
|
-
|
|
4
|
-
describe("compilePath", () => {
|
|
5
|
-
it("compile un chemin simple avec segment statique", () => {
|
|
6
|
-
const result = compilePath("/tasks");
|
|
7
|
-
|
|
8
|
-
expect(result).toEqual([{ type: "static", value: "tasks" }]);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it("compile un chemin avec paramètre", () => {
|
|
12
|
-
const result = compilePath("/tasks/:id");
|
|
13
|
-
|
|
14
|
-
expect(result).toEqual([
|
|
15
|
-
{ type: "static", value: "tasks" },
|
|
16
|
-
{ type: "param", name: "id" },
|
|
17
|
-
]);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it("compile un chemin avec paramètre", () => {
|
|
21
|
-
const result = compilePath("/tasks/:id");
|
|
22
|
-
|
|
23
|
-
expect(result).toEqual([
|
|
24
|
-
{ type: "static", value: "tasks" },
|
|
25
|
-
{ type: "param", name: "id" },
|
|
26
|
-
]);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it("compile un chemin complexe avec static, param et wildcard", () => {
|
|
30
|
-
const result = compilePath("/a/:b/*/c");
|
|
31
|
-
|
|
32
|
-
expect(result).toEqual([
|
|
33
|
-
{ type: "static", value: "a" },
|
|
34
|
-
{ type: "param", name: "b" },
|
|
35
|
-
{ type: "wildcard" },
|
|
36
|
-
{ type: "static", value: "c" },
|
|
37
|
-
]);
|
|
38
|
-
});
|
|
39
|
-
});
|
|
1
|
+
import { compilePath } from "../compile-path";
|
|
2
|
+
import { describe, it, expect } from "@jest/globals";
|
|
3
|
+
|
|
4
|
+
describe("compilePath", () => {
|
|
5
|
+
it("compile un chemin simple avec segment statique", () => {
|
|
6
|
+
const result = compilePath("/tasks");
|
|
7
|
+
|
|
8
|
+
expect(result).toEqual([{ type: "static", value: "tasks" }]);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("compile un chemin avec paramètre", () => {
|
|
12
|
+
const result = compilePath("/tasks/:id");
|
|
13
|
+
|
|
14
|
+
expect(result).toEqual([
|
|
15
|
+
{ type: "static", value: "tasks" },
|
|
16
|
+
{ type: "param", name: "id" },
|
|
17
|
+
]);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("compile un chemin avec paramètre", () => {
|
|
21
|
+
const result = compilePath("/tasks/:id");
|
|
22
|
+
|
|
23
|
+
expect(result).toEqual([
|
|
24
|
+
{ type: "static", value: "tasks" },
|
|
25
|
+
{ type: "param", name: "id" },
|
|
26
|
+
]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("compile un chemin complexe avec static, param et wildcard", () => {
|
|
30
|
+
const result = compilePath("/a/:b/*/c");
|
|
31
|
+
|
|
32
|
+
expect(result).toEqual([
|
|
33
|
+
{ type: "static", value: "a" },
|
|
34
|
+
{ type: "param", name: "b" },
|
|
35
|
+
{ type: "wildcard" },
|
|
36
|
+
{ type: "static", value: "c" },
|
|
37
|
+
]);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
import { MiddlewarePipeline } from "../../http/middleware";
|
|
2
|
-
import { Request } from "../../http/request";
|
|
3
|
-
import { Response } from "../../http/response";
|
|
4
|
-
import { describe, it, expect, jest } from "@jest/globals";
|
|
5
|
-
|
|
6
|
-
describe("MiddlewarePipeline", () => {
|
|
7
|
-
it("exécute les middlewares dans l'ordre", async () => {
|
|
8
|
-
const pipeline = new MiddlewarePipeline();
|
|
9
|
-
|
|
10
|
-
const calls: string[] = [];
|
|
11
|
-
|
|
12
|
-
pipeline.use(async (req, res, next) => {
|
|
13
|
-
calls.push("mw1");
|
|
14
|
-
await next();
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
pipeline.use(async (req, res, next) => {
|
|
18
|
-
calls.push("mw2");
|
|
19
|
-
await next();
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
const req = new Request({ method: "GET", path: "/", headers: {} });
|
|
23
|
-
const res = new Response({ setHeader: jest.fn(), end: jest.fn() });
|
|
24
|
-
|
|
25
|
-
await pipeline.run(req, res);
|
|
26
|
-
|
|
27
|
-
expect(calls).toEqual(["mw1", "mw2"]);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it("arrête la chaîne si next() n'est pas appelé", async () => {
|
|
31
|
-
const pipeline = new MiddlewarePipeline();
|
|
32
|
-
|
|
33
|
-
const calls: string[] = [];
|
|
34
|
-
|
|
35
|
-
pipeline.use(async () => {
|
|
36
|
-
calls.push("mw1");
|
|
37
|
-
// pas de next()
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
pipeline.use(async () => {
|
|
41
|
-
calls.push("mw2");
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
const req = new Request({ method: "GET", path: "/", headers: {} });
|
|
45
|
-
const res = new Response({ setHeader: jest.fn(), end: jest.fn() });
|
|
46
|
-
|
|
47
|
-
await pipeline.run(req, res);
|
|
48
|
-
|
|
49
|
-
expect(calls).toEqual(["mw1"]);
|
|
50
|
-
});
|
|
51
|
-
});
|
|
1
|
+
import { MiddlewarePipeline } from "../../http/middleware";
|
|
2
|
+
import { Request } from "../../http/request";
|
|
3
|
+
import { Response } from "../../http/response";
|
|
4
|
+
import { describe, it, expect, jest } from "@jest/globals";
|
|
5
|
+
|
|
6
|
+
describe("MiddlewarePipeline", () => {
|
|
7
|
+
it("exécute les middlewares dans l'ordre", async () => {
|
|
8
|
+
const pipeline = new MiddlewarePipeline();
|
|
9
|
+
|
|
10
|
+
const calls: string[] = [];
|
|
11
|
+
|
|
12
|
+
pipeline.use(async (req, res, next) => {
|
|
13
|
+
calls.push("mw1");
|
|
14
|
+
await next();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
pipeline.use(async (req, res, next) => {
|
|
18
|
+
calls.push("mw2");
|
|
19
|
+
await next();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const req = new Request({ method: "GET", path: "/", headers: {} });
|
|
23
|
+
const res = new Response({ setHeader: jest.fn(), end: jest.fn() });
|
|
24
|
+
|
|
25
|
+
await pipeline.run(req, res);
|
|
26
|
+
|
|
27
|
+
expect(calls).toEqual(["mw1", "mw2"]);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("arrête la chaîne si next() n'est pas appelé", async () => {
|
|
31
|
+
const pipeline = new MiddlewarePipeline();
|
|
32
|
+
|
|
33
|
+
const calls: string[] = [];
|
|
34
|
+
|
|
35
|
+
pipeline.use(async () => {
|
|
36
|
+
calls.push("mw1");
|
|
37
|
+
// pas de next()
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
pipeline.use(async () => {
|
|
41
|
+
calls.push("mw2");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const req = new Request({ method: "GET", path: "/", headers: {} });
|
|
45
|
+
const res = new Response({ setHeader: jest.fn(), end: jest.fn() });
|
|
46
|
+
|
|
47
|
+
await pipeline.run(req, res);
|
|
48
|
+
|
|
49
|
+
expect(calls).toEqual(["mw1"]);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import { Request } from "../request";
|
|
2
|
-
import { describe, it, expect } from "@jest/globals";
|
|
3
|
-
|
|
4
|
-
describe("Request", () => {
|
|
5
|
-
it("crée une requête immuable avec les bonnes propriétés", () => {
|
|
6
|
-
const req = new Request({
|
|
7
|
-
method: "GET",
|
|
8
|
-
path: "/test",
|
|
9
|
-
headers: { "content-type": "application/json" },
|
|
10
|
-
query: { a: "1" },
|
|
11
|
-
params: { id: "123" },
|
|
12
|
-
body: { x: 1 },
|
|
13
|
-
ip: "127.0.0.1",
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
expect(req.method).toBe("GET");
|
|
17
|
-
expect(req.path).toBe("/test");
|
|
18
|
-
expect(req.headers["content-type"]).toBe("application/json");
|
|
19
|
-
expect(req.query.a).toBe("1");
|
|
20
|
-
expect(req.params.id).toBe("123");
|
|
21
|
-
expect(req.body.x).toBe(1);
|
|
22
|
-
expect(req.ip).toBe("127.0.0.1");
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it("retourne un header via header()", () => {
|
|
26
|
-
const req = new Request({
|
|
27
|
-
method: "GET",
|
|
28
|
-
path: "/",
|
|
29
|
-
headers: { "x-test": "ok" },
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
expect(req.header("x-test")).toBe("ok");
|
|
33
|
-
});
|
|
34
|
-
});
|
|
1
|
+
import { Request } from "../request";
|
|
2
|
+
import { describe, it, expect } from "@jest/globals";
|
|
3
|
+
|
|
4
|
+
describe("Request", () => {
|
|
5
|
+
it("crée une requête immuable avec les bonnes propriétés", () => {
|
|
6
|
+
const req = new Request({
|
|
7
|
+
method: "GET",
|
|
8
|
+
path: "/test",
|
|
9
|
+
headers: { "content-type": "application/json" },
|
|
10
|
+
query: { a: "1" },
|
|
11
|
+
params: { id: "123" },
|
|
12
|
+
body: { x: 1 },
|
|
13
|
+
ip: "127.0.0.1",
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
expect(req.method).toBe("GET");
|
|
17
|
+
expect(req.path).toBe("/test");
|
|
18
|
+
expect(req.headers["content-type"]).toBe("application/json");
|
|
19
|
+
expect(req.query.a).toBe("1");
|
|
20
|
+
expect(req.params.id).toBe("123");
|
|
21
|
+
expect(req.body.x).toBe(1);
|
|
22
|
+
expect(req.ip).toBe("127.0.0.1");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("retourne un header via header()", () => {
|
|
26
|
+
const req = new Request({
|
|
27
|
+
method: "GET",
|
|
28
|
+
path: "/",
|
|
29
|
+
headers: { "x-test": "ok" },
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
expect(req.header("x-test")).toBe("ok");
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
import { Response } from "../../http/response";
|
|
2
|
-
import { describe, it, expect, jest } from "@jest/globals";
|
|
3
|
-
|
|
4
|
-
describe("Response", () => {
|
|
5
|
-
it("enregistre le status et les headers", () => {
|
|
6
|
-
const raw = {
|
|
7
|
-
setHeader: jest.fn(),
|
|
8
|
-
end: jest.fn(),
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
const res = new Response(raw);
|
|
12
|
-
|
|
13
|
-
res.status(201).header("x-test", "ok").send("hello");
|
|
14
|
-
|
|
15
|
-
expect(raw.setHeader).toHaveBeenCalledWith("x-test", "ok");
|
|
16
|
-
expect(raw.end).toHaveBeenCalledWith("hello");
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it("envoie du JSON", () => {
|
|
20
|
-
const raw = {
|
|
21
|
-
setHeader: jest.fn(),
|
|
22
|
-
end: jest.fn(),
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const res = new Response(raw);
|
|
26
|
-
|
|
27
|
-
res.json({ a: 1 });
|
|
28
|
-
|
|
29
|
-
expect(raw.setHeader).toHaveBeenCalledWith(
|
|
30
|
-
"content-type",
|
|
31
|
-
"application/json",
|
|
32
|
-
);
|
|
33
|
-
expect(raw.end).toHaveBeenCalledWith(JSON.stringify({ a: 1 }));
|
|
34
|
-
});
|
|
35
|
-
});
|
|
1
|
+
import { Response } from "../../http/response";
|
|
2
|
+
import { describe, it, expect, jest } from "@jest/globals";
|
|
3
|
+
|
|
4
|
+
describe("Response", () => {
|
|
5
|
+
it("enregistre le status et les headers", () => {
|
|
6
|
+
const raw = {
|
|
7
|
+
setHeader: jest.fn(),
|
|
8
|
+
end: jest.fn(),
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const res = new Response(raw);
|
|
12
|
+
|
|
13
|
+
res.status(201).header("x-test", "ok").send("hello");
|
|
14
|
+
|
|
15
|
+
expect(raw.setHeader).toHaveBeenCalledWith("x-test", "ok");
|
|
16
|
+
expect(raw.end).toHaveBeenCalledWith("hello");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("envoie du JSON", () => {
|
|
20
|
+
const raw = {
|
|
21
|
+
setHeader: jest.fn(),
|
|
22
|
+
end: jest.fn(),
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const res = new Response(raw);
|
|
26
|
+
|
|
27
|
+
res.json({ a: 1 });
|
|
28
|
+
|
|
29
|
+
expect(raw.setHeader).toHaveBeenCalledWith(
|
|
30
|
+
"content-type",
|
|
31
|
+
"application/json",
|
|
32
|
+
);
|
|
33
|
+
expect(raw.end).toHaveBeenCalledWith(JSON.stringify({ a: 1 }));
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -1,171 +1,171 @@
|
|
|
1
|
-
import { Router } from "../router";
|
|
2
|
-
import { describe, it, expect } from "@jest/globals";
|
|
3
|
-
|
|
4
|
-
describe("Router.match", () => {
|
|
5
|
-
it("match une route statique", () => {
|
|
6
|
-
const router = new Router();
|
|
7
|
-
|
|
8
|
-
router.register("GET", "/tasks", () => "ok");
|
|
9
|
-
|
|
10
|
-
const result = router.match("GET", "/tasks");
|
|
11
|
-
|
|
12
|
-
expect(result).not.toBeNull();
|
|
13
|
-
expect(result?.handler()).toBe("ok");
|
|
14
|
-
expect(result?.params).toEqual({});
|
|
15
|
-
expect(result?.middlewares).toEqual([]);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it("match une route avec paramètre", () => {
|
|
19
|
-
const router = new Router();
|
|
20
|
-
|
|
21
|
-
router.register("GET", "/tasks/:id", () => "task");
|
|
22
|
-
|
|
23
|
-
const result = router.match("GET", "/tasks/42");
|
|
24
|
-
|
|
25
|
-
expect(result).not.toBeNull();
|
|
26
|
-
expect(result?.handler()).toBe("task");
|
|
27
|
-
expect(result?.params).toEqual({ id: "42" });
|
|
28
|
-
expect(result?.middlewares).toEqual([]);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("match une route avec wildcard", () => {
|
|
32
|
-
const router = new Router();
|
|
33
|
-
|
|
34
|
-
router.register("GET", "/files/*", () => "wild");
|
|
35
|
-
|
|
36
|
-
const result = router.match("GET", "/files/any/path/here");
|
|
37
|
-
|
|
38
|
-
expect(result).not.toBeNull();
|
|
39
|
-
expect(result?.handler()).toBe("wild");
|
|
40
|
-
expect(result?.params).toEqual({});
|
|
41
|
-
expect(result?.middlewares).toEqual([]);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it("match un chemin complexe avec static, param et wildcard", () => {
|
|
45
|
-
const router = new Router();
|
|
46
|
-
|
|
47
|
-
router.register("GET", "/a/:b/*/c", () => "complex");
|
|
48
|
-
|
|
49
|
-
const result = router.match("GET", "/a/hello/anything/here/c");
|
|
50
|
-
|
|
51
|
-
expect(result).not.toBeNull();
|
|
52
|
-
expect(result?.handler()).toBe("complex");
|
|
53
|
-
expect(result?.params).toEqual({ b: "hello" });
|
|
54
|
-
expect(result?.middlewares).toEqual([]);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it("ne match pas quand le chemin ne correspond pas", () => {
|
|
58
|
-
const router = new Router();
|
|
59
|
-
|
|
60
|
-
router.register("GET", "/tasks/:id", () => "ok");
|
|
61
|
-
|
|
62
|
-
const result = router.match("GET", "/tasks");
|
|
63
|
-
|
|
64
|
-
expect(result).toBeNull();
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("ne match pas quand le chemin est trop long", () => {
|
|
68
|
-
const router = new Router();
|
|
69
|
-
|
|
70
|
-
router.register("GET", "/tasks/:id", () => "ok");
|
|
71
|
-
|
|
72
|
-
const result = router.match("GET", "/tasks/42/extra");
|
|
73
|
-
|
|
74
|
-
expect(result).toBeNull();
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("ne match pas quand la méthode HTTP ne correspond pas", () => {
|
|
78
|
-
const router = new Router();
|
|
79
|
-
|
|
80
|
-
router.register("GET", "/tasks", () => "ok");
|
|
81
|
-
|
|
82
|
-
const result = router.match("POST", "/tasks");
|
|
83
|
-
|
|
84
|
-
expect(result).toBeNull();
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it("match une route avec plusieurs paramètres", () => {
|
|
88
|
-
const router = new Router();
|
|
89
|
-
|
|
90
|
-
router.register("GET", "/users/:userId/tasks/:taskId", () => "multi");
|
|
91
|
-
|
|
92
|
-
const result = router.match("GET", "/users/12/tasks/99");
|
|
93
|
-
|
|
94
|
-
expect(result).not.toBeNull();
|
|
95
|
-
expect(result?.handler()).toBe("multi");
|
|
96
|
-
expect(result?.params).toEqual({
|
|
97
|
-
userId: "12",
|
|
98
|
-
taskId: "99",
|
|
99
|
-
});
|
|
100
|
-
expect(result?.middlewares).toEqual([]);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it("match une route complexe avec plusieurs paramètres et un wildcard", () => {
|
|
104
|
-
const router = new Router();
|
|
105
|
-
|
|
106
|
-
router.register("GET", "/users/:userId/*/tasks/:taskId", () => "mw");
|
|
107
|
-
|
|
108
|
-
const result = router.match("GET", "/users/12/any/path/here/tasks/99");
|
|
109
|
-
|
|
110
|
-
expect(result).not.toBeNull();
|
|
111
|
-
expect(result?.handler()).toBe("mw");
|
|
112
|
-
expect(result?.params).toEqual({
|
|
113
|
-
userId: "12",
|
|
114
|
-
taskId: "99",
|
|
115
|
-
});
|
|
116
|
-
expect(result?.middlewares).toEqual([]);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it("ne match pas un chemin complexe si la fin ne correspond pas", () => {
|
|
120
|
-
const router = new Router();
|
|
121
|
-
|
|
122
|
-
router.register("GET", "/users/:userId/*/tasks/:taskId", () => "mw");
|
|
123
|
-
|
|
124
|
-
const result = router.match("GET", "/users/12/any/path/here/taskz/99");
|
|
125
|
-
|
|
126
|
-
expect(result).toBeNull();
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it("ne match pas si le paramètre final est manquant", () => {
|
|
130
|
-
const router = new Router();
|
|
131
|
-
|
|
132
|
-
router.register("GET", "/users/:userId/*/tasks/:taskId", () => "mw");
|
|
133
|
-
|
|
134
|
-
const result = router.match("GET", "/users/12/any/path/here/tasks/");
|
|
135
|
-
|
|
136
|
-
expect(result).toBeNull();
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it("ne match pas si le paramètre du début est manquant", () => {
|
|
140
|
-
const router = new Router();
|
|
141
|
-
|
|
142
|
-
router.register("GET", "/users/:userId/*/tasks/:taskId", () => "mw");
|
|
143
|
-
|
|
144
|
-
const result = router.match("GET", "/users//any/path/tasks/99");
|
|
145
|
-
|
|
146
|
-
expect(result).toBeNull();
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it("ne match pas si le wildcard est vide", () => {
|
|
150
|
-
const router = new Router();
|
|
151
|
-
|
|
152
|
-
router.register("GET", "/users/:userId/*/tasks/:taskId", () => "mw");
|
|
153
|
-
|
|
154
|
-
const result = router.match("GET", "/users/12//tasks/99");
|
|
155
|
-
|
|
156
|
-
expect(result).toBeNull();
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
it("match une route avec wildcard en fin de chemin", () => {
|
|
160
|
-
const router = new Router();
|
|
161
|
-
|
|
162
|
-
router.register("GET", "/files/*", () => "end");
|
|
163
|
-
|
|
164
|
-
const result = router.match("GET", "/files/path/to/something");
|
|
165
|
-
|
|
166
|
-
expect(result).not.toBeNull();
|
|
167
|
-
expect(result?.handler()).toBe("end");
|
|
168
|
-
expect(result?.params).toEqual({});
|
|
169
|
-
expect(result?.middlewares).toEqual([]);
|
|
170
|
-
});
|
|
171
|
-
});
|
|
1
|
+
import { Router } from "../router";
|
|
2
|
+
import { describe, it, expect } from "@jest/globals";
|
|
3
|
+
|
|
4
|
+
describe("Router.match", () => {
|
|
5
|
+
it("match une route statique", () => {
|
|
6
|
+
const router = new Router();
|
|
7
|
+
|
|
8
|
+
router.register("GET", "/tasks", () => "ok");
|
|
9
|
+
|
|
10
|
+
const result = router.match("GET", "/tasks");
|
|
11
|
+
|
|
12
|
+
expect(result).not.toBeNull();
|
|
13
|
+
expect(result?.handler()).toBe("ok");
|
|
14
|
+
expect(result?.params).toEqual({});
|
|
15
|
+
expect(result?.middlewares).toEqual([]);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("match une route avec paramètre", () => {
|
|
19
|
+
const router = new Router();
|
|
20
|
+
|
|
21
|
+
router.register("GET", "/tasks/:id", () => "task");
|
|
22
|
+
|
|
23
|
+
const result = router.match("GET", "/tasks/42");
|
|
24
|
+
|
|
25
|
+
expect(result).not.toBeNull();
|
|
26
|
+
expect(result?.handler()).toBe("task");
|
|
27
|
+
expect(result?.params).toEqual({ id: "42" });
|
|
28
|
+
expect(result?.middlewares).toEqual([]);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("match une route avec wildcard", () => {
|
|
32
|
+
const router = new Router();
|
|
33
|
+
|
|
34
|
+
router.register("GET", "/files/*", () => "wild");
|
|
35
|
+
|
|
36
|
+
const result = router.match("GET", "/files/any/path/here");
|
|
37
|
+
|
|
38
|
+
expect(result).not.toBeNull();
|
|
39
|
+
expect(result?.handler()).toBe("wild");
|
|
40
|
+
expect(result?.params).toEqual({});
|
|
41
|
+
expect(result?.middlewares).toEqual([]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("match un chemin complexe avec static, param et wildcard", () => {
|
|
45
|
+
const router = new Router();
|
|
46
|
+
|
|
47
|
+
router.register("GET", "/a/:b/*/c", () => "complex");
|
|
48
|
+
|
|
49
|
+
const result = router.match("GET", "/a/hello/anything/here/c");
|
|
50
|
+
|
|
51
|
+
expect(result).not.toBeNull();
|
|
52
|
+
expect(result?.handler()).toBe("complex");
|
|
53
|
+
expect(result?.params).toEqual({ b: "hello" });
|
|
54
|
+
expect(result?.middlewares).toEqual([]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("ne match pas quand le chemin ne correspond pas", () => {
|
|
58
|
+
const router = new Router();
|
|
59
|
+
|
|
60
|
+
router.register("GET", "/tasks/:id", () => "ok");
|
|
61
|
+
|
|
62
|
+
const result = router.match("GET", "/tasks");
|
|
63
|
+
|
|
64
|
+
expect(result).toBeNull();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("ne match pas quand le chemin est trop long", () => {
|
|
68
|
+
const router = new Router();
|
|
69
|
+
|
|
70
|
+
router.register("GET", "/tasks/:id", () => "ok");
|
|
71
|
+
|
|
72
|
+
const result = router.match("GET", "/tasks/42/extra");
|
|
73
|
+
|
|
74
|
+
expect(result).toBeNull();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("ne match pas quand la méthode HTTP ne correspond pas", () => {
|
|
78
|
+
const router = new Router();
|
|
79
|
+
|
|
80
|
+
router.register("GET", "/tasks", () => "ok");
|
|
81
|
+
|
|
82
|
+
const result = router.match("POST", "/tasks");
|
|
83
|
+
|
|
84
|
+
expect(result).toBeNull();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("match une route avec plusieurs paramètres", () => {
|
|
88
|
+
const router = new Router();
|
|
89
|
+
|
|
90
|
+
router.register("GET", "/users/:userId/tasks/:taskId", () => "multi");
|
|
91
|
+
|
|
92
|
+
const result = router.match("GET", "/users/12/tasks/99");
|
|
93
|
+
|
|
94
|
+
expect(result).not.toBeNull();
|
|
95
|
+
expect(result?.handler()).toBe("multi");
|
|
96
|
+
expect(result?.params).toEqual({
|
|
97
|
+
userId: "12",
|
|
98
|
+
taskId: "99",
|
|
99
|
+
});
|
|
100
|
+
expect(result?.middlewares).toEqual([]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("match une route complexe avec plusieurs paramètres et un wildcard", () => {
|
|
104
|
+
const router = new Router();
|
|
105
|
+
|
|
106
|
+
router.register("GET", "/users/:userId/*/tasks/:taskId", () => "mw");
|
|
107
|
+
|
|
108
|
+
const result = router.match("GET", "/users/12/any/path/here/tasks/99");
|
|
109
|
+
|
|
110
|
+
expect(result).not.toBeNull();
|
|
111
|
+
expect(result?.handler()).toBe("mw");
|
|
112
|
+
expect(result?.params).toEqual({
|
|
113
|
+
userId: "12",
|
|
114
|
+
taskId: "99",
|
|
115
|
+
});
|
|
116
|
+
expect(result?.middlewares).toEqual([]);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("ne match pas un chemin complexe si la fin ne correspond pas", () => {
|
|
120
|
+
const router = new Router();
|
|
121
|
+
|
|
122
|
+
router.register("GET", "/users/:userId/*/tasks/:taskId", () => "mw");
|
|
123
|
+
|
|
124
|
+
const result = router.match("GET", "/users/12/any/path/here/taskz/99");
|
|
125
|
+
|
|
126
|
+
expect(result).toBeNull();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("ne match pas si le paramètre final est manquant", () => {
|
|
130
|
+
const router = new Router();
|
|
131
|
+
|
|
132
|
+
router.register("GET", "/users/:userId/*/tasks/:taskId", () => "mw");
|
|
133
|
+
|
|
134
|
+
const result = router.match("GET", "/users/12/any/path/here/tasks/");
|
|
135
|
+
|
|
136
|
+
expect(result).toBeNull();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("ne match pas si le paramètre du début est manquant", () => {
|
|
140
|
+
const router = new Router();
|
|
141
|
+
|
|
142
|
+
router.register("GET", "/users/:userId/*/tasks/:taskId", () => "mw");
|
|
143
|
+
|
|
144
|
+
const result = router.match("GET", "/users//any/path/tasks/99");
|
|
145
|
+
|
|
146
|
+
expect(result).toBeNull();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("ne match pas si le wildcard est vide", () => {
|
|
150
|
+
const router = new Router();
|
|
151
|
+
|
|
152
|
+
router.register("GET", "/users/:userId/*/tasks/:taskId", () => "mw");
|
|
153
|
+
|
|
154
|
+
const result = router.match("GET", "/users/12//tasks/99");
|
|
155
|
+
|
|
156
|
+
expect(result).toBeNull();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("match une route avec wildcard en fin de chemin", () => {
|
|
160
|
+
const router = new Router();
|
|
161
|
+
|
|
162
|
+
router.register("GET", "/files/*", () => "end");
|
|
163
|
+
|
|
164
|
+
const result = router.match("GET", "/files/path/to/something");
|
|
165
|
+
|
|
166
|
+
expect(result).not.toBeNull();
|
|
167
|
+
expect(result?.handler()).toBe("end");
|
|
168
|
+
expect(result?.params).toEqual({});
|
|
169
|
+
expect(result?.middlewares).toEqual([]);
|
|
170
|
+
});
|
|
171
|
+
});
|