@j3r3mcdev/oast-server 1.1.6 → 1.1.8
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/core/router.d.ts +1 -0
- package/dist/core/router.js +3 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.js +16 -1
- 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 +20 -21
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
import { ApiController } from "../api.controllers";
|
|
2
|
-
import { StorageManager } from "../../../core/storage";
|
|
3
|
-
import { ServerResponse, IncomingMessage } from "http";
|
|
4
|
-
import { Socket } from "net";
|
|
5
|
-
import { describe, it, expect, beforeEach, jest } from "@jest/globals";
|
|
6
|
-
|
|
7
|
-
// Mock ServerResponse
|
|
8
|
-
function mockRes(): ServerResponse {
|
|
9
|
-
const socket = new Socket();
|
|
10
|
-
const req = new IncomingMessage(socket);
|
|
11
|
-
const res = new ServerResponse(req);
|
|
12
|
-
|
|
13
|
-
jest.spyOn(res, "writeHead");
|
|
14
|
-
jest.spyOn(res, "end");
|
|
15
|
-
|
|
16
|
-
return res;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
describe("ApiController", () => {
|
|
20
|
-
let storage: StorageManager;
|
|
21
|
-
|
|
22
|
-
beforeEach(() => {
|
|
23
|
-
// On utilise un vrai StorageManager en mémoire
|
|
24
|
-
storage = new StorageManager();
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("listEvents renvoie la liste des events (vide par défaut)", async () => {
|
|
28
|
-
const res = mockRes();
|
|
29
|
-
const url = new URL("http://localhost/events?page=2&type=http");
|
|
30
|
-
|
|
31
|
-
await ApiController.listEvents(url, res, storage);
|
|
32
|
-
|
|
33
|
-
expect(storage.listEvents).toBeDefined();
|
|
34
|
-
|
|
35
|
-
expect(res.writeHead).toHaveBeenCalledWith(200, {
|
|
36
|
-
"Content-Type": "application/json",
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
const body = (res.end as jest.Mock).mock.calls[0][0] as string;
|
|
40
|
-
const payload = JSON.parse(body);
|
|
41
|
-
expect(payload.success).toBe(true);
|
|
42
|
-
expect(payload.events).toEqual([]);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it("getEvent renvoie 404 si event absent", async () => {
|
|
46
|
-
const res = mockRes();
|
|
47
|
-
|
|
48
|
-
await ApiController.getEvent("unknown", res, storage);
|
|
49
|
-
|
|
50
|
-
expect(res.writeHead).toHaveBeenCalledWith(404, {
|
|
51
|
-
"Content-Type": "application/json",
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
const body = (res.end as jest.Mock).mock.calls[0][0] as string;
|
|
55
|
-
const payload = JSON.parse(body);
|
|
56
|
-
expect(payload.success).toBe(false);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it("getEvent renvoie l'event si présent", async () => {
|
|
60
|
-
// On mocke juste getEvent pour ce test
|
|
61
|
-
jest.spyOn(storage, "getEvent").mockResolvedValue({ id: "abc" } as any);
|
|
62
|
-
|
|
63
|
-
const res = mockRes();
|
|
64
|
-
|
|
65
|
-
await ApiController.getEvent("abc", res, storage);
|
|
66
|
-
|
|
67
|
-
expect(res.writeHead).toHaveBeenCalledWith(200, {
|
|
68
|
-
"Content-Type": "application/json",
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
const body = (res.end as jest.Mock).mock.calls[0][0] as string;
|
|
72
|
-
const payload = JSON.parse(body);
|
|
73
|
-
expect(payload.success).toBe(true);
|
|
74
|
-
expect(payload.event.id).toBe("abc");
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("deleteAll supprime tous les events", async () => {
|
|
78
|
-
const res = mockRes();
|
|
79
|
-
|
|
80
|
-
await ApiController.deleteAll(res, storage);
|
|
81
|
-
|
|
82
|
-
const body = (res.end as jest.Mock).mock.calls[0][0] as string;
|
|
83
|
-
const payload = JSON.parse(body);
|
|
84
|
-
expect(payload.success).toBe(true);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it("deleteOne supprime un event", async () => {
|
|
88
|
-
const res = mockRes();
|
|
89
|
-
|
|
90
|
-
await storage.save({
|
|
91
|
-
id: "xyz",
|
|
92
|
-
type: "http",
|
|
93
|
-
timestamp: Date.now(),
|
|
94
|
-
sourceIp: "127.0.0.1",
|
|
95
|
-
request: { method: "GET", path: "/", headers: {}, query: {}, body: "" },
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
await ApiController.deleteOne("xyz", res, storage);
|
|
99
|
-
|
|
100
|
-
const raw = String((res.end as jest.Mock).mock.calls[0][0]);
|
|
101
|
-
const payload = JSON.parse(raw);
|
|
102
|
-
|
|
103
|
-
expect(payload.success).toBe(true);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it("stats renvoie les stats", async () => {
|
|
107
|
-
const res = mockRes();
|
|
108
|
-
|
|
109
|
-
await ApiController.stats(res, storage);
|
|
110
|
-
|
|
111
|
-
const body = (res.end as jest.Mock).mock.calls[0][0] as string;
|
|
112
|
-
const payload = JSON.parse(body);
|
|
113
|
-
expect(payload.success).toBe(true);
|
|
114
|
-
expect(payload.stats).toEqual({ total: 0, byType: {} });
|
|
115
|
-
});
|
|
116
|
-
});
|
|
1
|
+
import { ApiController } from "../api.controllers";
|
|
2
|
+
import { StorageManager } from "../../../core/storage";
|
|
3
|
+
import { ServerResponse, IncomingMessage } from "http";
|
|
4
|
+
import { Socket } from "net";
|
|
5
|
+
import { describe, it, expect, beforeEach, jest } from "@jest/globals";
|
|
6
|
+
|
|
7
|
+
// Mock ServerResponse
|
|
8
|
+
function mockRes(): ServerResponse {
|
|
9
|
+
const socket = new Socket();
|
|
10
|
+
const req = new IncomingMessage(socket);
|
|
11
|
+
const res = new ServerResponse(req);
|
|
12
|
+
|
|
13
|
+
jest.spyOn(res, "writeHead");
|
|
14
|
+
jest.spyOn(res, "end");
|
|
15
|
+
|
|
16
|
+
return res;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe("ApiController", () => {
|
|
20
|
+
let storage: StorageManager;
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
// On utilise un vrai StorageManager en mémoire
|
|
24
|
+
storage = new StorageManager();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("listEvents renvoie la liste des events (vide par défaut)", async () => {
|
|
28
|
+
const res = mockRes();
|
|
29
|
+
const url = new URL("http://localhost/events?page=2&type=http");
|
|
30
|
+
|
|
31
|
+
await ApiController.listEvents(url, res, storage);
|
|
32
|
+
|
|
33
|
+
expect(storage.listEvents).toBeDefined();
|
|
34
|
+
|
|
35
|
+
expect(res.writeHead).toHaveBeenCalledWith(200, {
|
|
36
|
+
"Content-Type": "application/json",
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const body = (res.end as jest.Mock).mock.calls[0][0] as string;
|
|
40
|
+
const payload = JSON.parse(body);
|
|
41
|
+
expect(payload.success).toBe(true);
|
|
42
|
+
expect(payload.events).toEqual([]);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("getEvent renvoie 404 si event absent", async () => {
|
|
46
|
+
const res = mockRes();
|
|
47
|
+
|
|
48
|
+
await ApiController.getEvent("unknown", res, storage);
|
|
49
|
+
|
|
50
|
+
expect(res.writeHead).toHaveBeenCalledWith(404, {
|
|
51
|
+
"Content-Type": "application/json",
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const body = (res.end as jest.Mock).mock.calls[0][0] as string;
|
|
55
|
+
const payload = JSON.parse(body);
|
|
56
|
+
expect(payload.success).toBe(false);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("getEvent renvoie l'event si présent", async () => {
|
|
60
|
+
// On mocke juste getEvent pour ce test
|
|
61
|
+
jest.spyOn(storage, "getEvent").mockResolvedValue({ id: "abc" } as any);
|
|
62
|
+
|
|
63
|
+
const res = mockRes();
|
|
64
|
+
|
|
65
|
+
await ApiController.getEvent("abc", res, storage);
|
|
66
|
+
|
|
67
|
+
expect(res.writeHead).toHaveBeenCalledWith(200, {
|
|
68
|
+
"Content-Type": "application/json",
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const body = (res.end as jest.Mock).mock.calls[0][0] as string;
|
|
72
|
+
const payload = JSON.parse(body);
|
|
73
|
+
expect(payload.success).toBe(true);
|
|
74
|
+
expect(payload.event.id).toBe("abc");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("deleteAll supprime tous les events", async () => {
|
|
78
|
+
const res = mockRes();
|
|
79
|
+
|
|
80
|
+
await ApiController.deleteAll(res, storage);
|
|
81
|
+
|
|
82
|
+
const body = (res.end as jest.Mock).mock.calls[0][0] as string;
|
|
83
|
+
const payload = JSON.parse(body);
|
|
84
|
+
expect(payload.success).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("deleteOne supprime un event", async () => {
|
|
88
|
+
const res = mockRes();
|
|
89
|
+
|
|
90
|
+
await storage.save({
|
|
91
|
+
id: "xyz",
|
|
92
|
+
type: "http",
|
|
93
|
+
timestamp: Date.now(),
|
|
94
|
+
sourceIp: "127.0.0.1",
|
|
95
|
+
request: { method: "GET", path: "/", headers: {}, query: {}, body: "" },
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
await ApiController.deleteOne("xyz", res, storage);
|
|
99
|
+
|
|
100
|
+
const raw = String((res.end as jest.Mock).mock.calls[0][0]);
|
|
101
|
+
const payload = JSON.parse(raw);
|
|
102
|
+
|
|
103
|
+
expect(payload.success).toBe(true);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("stats renvoie les stats", async () => {
|
|
107
|
+
const res = mockRes();
|
|
108
|
+
|
|
109
|
+
await ApiController.stats(res, storage);
|
|
110
|
+
|
|
111
|
+
const body = (res.end as jest.Mock).mock.calls[0][0] as string;
|
|
112
|
+
const payload = JSON.parse(body);
|
|
113
|
+
expect(payload.success).toBe(true);
|
|
114
|
+
expect(payload.stats).toEqual({ total: 0, byType: {} });
|
|
115
|
+
});
|
|
116
|
+
});
|
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
import { ApiExtractor } from "../api.extractor";
|
|
2
|
-
import { IncomingMessage } from "http";
|
|
3
|
-
import { Socket } from "net";
|
|
4
|
-
import { describe, it, expect } from "@jest/globals";
|
|
5
|
-
|
|
6
|
-
function mockReq(url: string, method = "GET", body?: any): IncomingMessage {
|
|
7
|
-
const socket = new Socket();
|
|
8
|
-
const req = new IncomingMessage(socket);
|
|
9
|
-
|
|
10
|
-
req.url = url;
|
|
11
|
-
req.method = method;
|
|
12
|
-
req.headers = { host: "localhost" };
|
|
13
|
-
|
|
14
|
-
if (body !== undefined) {
|
|
15
|
-
const json = JSON.stringify(body);
|
|
16
|
-
process.nextTick(() => {
|
|
17
|
-
req.emit("data", json);
|
|
18
|
-
req.emit("end");
|
|
19
|
-
});
|
|
20
|
-
} else {
|
|
21
|
-
process.nextTick(() => req.emit("end"));
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return req;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
describe("ApiExtractor", () => {
|
|
28
|
-
it("extrait un RawEvent GET", async () => {
|
|
29
|
-
const req = mockReq("/events?page=2&type=http");
|
|
30
|
-
|
|
31
|
-
const event = await ApiExtractor.extract(req);
|
|
32
|
-
|
|
33
|
-
expect(event.method).toBe("GET");
|
|
34
|
-
expect(event.path).toBe("/events");
|
|
35
|
-
expect(event.query).toEqual({ page: "2", type: "http" });
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it("extrait un RawEvent POST avec body JSON", async () => {
|
|
39
|
-
const req = mockReq("/events", "POST", { hello: "world" });
|
|
40
|
-
|
|
41
|
-
const event = await ApiExtractor.extract(req);
|
|
42
|
-
|
|
43
|
-
expect(event.method).toBe("POST");
|
|
44
|
-
expect(event.body).toEqual({ hello: "world" });
|
|
45
|
-
});
|
|
46
|
-
});
|
|
1
|
+
import { ApiExtractor } from "../api.extractor";
|
|
2
|
+
import { IncomingMessage } from "http";
|
|
3
|
+
import { Socket } from "net";
|
|
4
|
+
import { describe, it, expect } from "@jest/globals";
|
|
5
|
+
|
|
6
|
+
function mockReq(url: string, method = "GET", body?: any): IncomingMessage {
|
|
7
|
+
const socket = new Socket();
|
|
8
|
+
const req = new IncomingMessage(socket);
|
|
9
|
+
|
|
10
|
+
req.url = url;
|
|
11
|
+
req.method = method;
|
|
12
|
+
req.headers = { host: "localhost" };
|
|
13
|
+
|
|
14
|
+
if (body !== undefined) {
|
|
15
|
+
const json = JSON.stringify(body);
|
|
16
|
+
process.nextTick(() => {
|
|
17
|
+
req.emit("data", json);
|
|
18
|
+
req.emit("end");
|
|
19
|
+
});
|
|
20
|
+
} else {
|
|
21
|
+
process.nextTick(() => req.emit("end"));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return req;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe("ApiExtractor", () => {
|
|
28
|
+
it("extrait un RawEvent GET", async () => {
|
|
29
|
+
const req = mockReq("/events?page=2&type=http");
|
|
30
|
+
|
|
31
|
+
const event = await ApiExtractor.extract(req);
|
|
32
|
+
|
|
33
|
+
expect(event.method).toBe("GET");
|
|
34
|
+
expect(event.path).toBe("/events");
|
|
35
|
+
expect(event.query).toEqual({ page: "2", type: "http" });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("extrait un RawEvent POST avec body JSON", async () => {
|
|
39
|
+
const req = mockReq("/events", "POST", { hello: "world" });
|
|
40
|
+
|
|
41
|
+
const event = await ApiExtractor.extract(req);
|
|
42
|
+
|
|
43
|
+
expect(event.method).toBe("POST");
|
|
44
|
+
expect(event.body).toEqual({ hello: "world" });
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
import http from "http";
|
|
2
|
-
import { ApiListener } from "../api.listener";
|
|
3
|
-
import { StorageManager } from "../../../core/storage";
|
|
4
|
-
import { Logger } from "../../../core/logger";
|
|
5
|
-
import { describe, test, beforeEach, afterEach, expect } from "@jest/globals";
|
|
6
|
-
import { ApiController } from "../api.controllers";
|
|
7
|
-
|
|
8
|
-
describe("ApiListener", () => {
|
|
9
|
-
let storage: StorageManager;
|
|
10
|
-
let api: ApiListener;
|
|
11
|
-
|
|
12
|
-
beforeEach(async () => {
|
|
13
|
-
storage = new StorageManager();
|
|
14
|
-
|
|
15
|
-
api = new ApiListener(storage, {
|
|
16
|
-
port: 9999,
|
|
17
|
-
logger: new Logger({ context: "ApiTest" }),
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
await api.start();
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
afterEach(async () => {
|
|
24
|
-
await api.stop();
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test("GET /events returns empty list", async () => {
|
|
28
|
-
const res = await fetch("http://localhost:9999/events");
|
|
29
|
-
const json = await res.json();
|
|
30
|
-
|
|
31
|
-
expect(json.success).toBe(true);
|
|
32
|
-
expect(json.events).toEqual([]);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test("GET /stats returns correct structure", async () => {
|
|
36
|
-
const res = await fetch("http://localhost:9999/stats");
|
|
37
|
-
const json = await res.json();
|
|
38
|
-
|
|
39
|
-
expect(json.success).toBe(true);
|
|
40
|
-
expect(json.stats.total).toBe(0);
|
|
41
|
-
expect(json.stats.byType).toEqual({});
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test("GET /events/:id returns 404 when not found", async () => {
|
|
45
|
-
const res = await fetch("http://localhost:9999/events/unknown");
|
|
46
|
-
const json = await res.json();
|
|
47
|
-
|
|
48
|
-
expect(res.status).toBe(404);
|
|
49
|
-
expect(json.success).toBe(false);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
test("DELETE /events clears all events", async () => {
|
|
53
|
-
// On ajoute un event dans le storage
|
|
54
|
-
await storage.save({
|
|
55
|
-
id: "1",
|
|
56
|
-
type: "http",
|
|
57
|
-
timestamp: Date.now(),
|
|
58
|
-
sourceIp: "127.0.0.1",
|
|
59
|
-
request: {
|
|
60
|
-
method: "GET",
|
|
61
|
-
path: "/",
|
|
62
|
-
headers: {},
|
|
63
|
-
query: {},
|
|
64
|
-
body: "",
|
|
65
|
-
},
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
let events = await storage.listEvents({});
|
|
69
|
-
expect(events.length).toBe(1);
|
|
70
|
-
|
|
71
|
-
// On appelle l'API réelle
|
|
72
|
-
const res = await fetch("http://localhost:9999/events", {
|
|
73
|
-
method: "DELETE",
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
const json = await res.json();
|
|
77
|
-
expect(json.success).toBe(true);
|
|
78
|
-
|
|
79
|
-
events = await storage.listEvents({});
|
|
80
|
-
expect(events.length).toBe(0);
|
|
81
|
-
});
|
|
82
|
-
});
|
|
1
|
+
import http from "http";
|
|
2
|
+
import { ApiListener } from "../api.listener";
|
|
3
|
+
import { StorageManager } from "../../../core/storage";
|
|
4
|
+
import { Logger } from "../../../core/logger";
|
|
5
|
+
import { describe, test, beforeEach, afterEach, expect } from "@jest/globals";
|
|
6
|
+
import { ApiController } from "../api.controllers";
|
|
7
|
+
|
|
8
|
+
describe("ApiListener", () => {
|
|
9
|
+
let storage: StorageManager;
|
|
10
|
+
let api: ApiListener;
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
storage = new StorageManager();
|
|
14
|
+
|
|
15
|
+
api = new ApiListener(storage, {
|
|
16
|
+
port: 9999,
|
|
17
|
+
logger: new Logger({ context: "ApiTest" }),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
await api.start();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
afterEach(async () => {
|
|
24
|
+
await api.stop();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("GET /events returns empty list", async () => {
|
|
28
|
+
const res = await fetch("http://localhost:9999/events");
|
|
29
|
+
const json = await res.json();
|
|
30
|
+
|
|
31
|
+
expect(json.success).toBe(true);
|
|
32
|
+
expect(json.events).toEqual([]);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("GET /stats returns correct structure", async () => {
|
|
36
|
+
const res = await fetch("http://localhost:9999/stats");
|
|
37
|
+
const json = await res.json();
|
|
38
|
+
|
|
39
|
+
expect(json.success).toBe(true);
|
|
40
|
+
expect(json.stats.total).toBe(0);
|
|
41
|
+
expect(json.stats.byType).toEqual({});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("GET /events/:id returns 404 when not found", async () => {
|
|
45
|
+
const res = await fetch("http://localhost:9999/events/unknown");
|
|
46
|
+
const json = await res.json();
|
|
47
|
+
|
|
48
|
+
expect(res.status).toBe(404);
|
|
49
|
+
expect(json.success).toBe(false);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("DELETE /events clears all events", async () => {
|
|
53
|
+
// On ajoute un event dans le storage
|
|
54
|
+
await storage.save({
|
|
55
|
+
id: "1",
|
|
56
|
+
type: "http",
|
|
57
|
+
timestamp: Date.now(),
|
|
58
|
+
sourceIp: "127.0.0.1",
|
|
59
|
+
request: {
|
|
60
|
+
method: "GET",
|
|
61
|
+
path: "/",
|
|
62
|
+
headers: {},
|
|
63
|
+
query: {},
|
|
64
|
+
body: "",
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
let events = await storage.listEvents({});
|
|
69
|
+
expect(events.length).toBe(1);
|
|
70
|
+
|
|
71
|
+
// On appelle l'API réelle
|
|
72
|
+
const res = await fetch("http://localhost:9999/events", {
|
|
73
|
+
method: "DELETE",
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const json = await res.json();
|
|
77
|
+
expect(json.success).toBe(true);
|
|
78
|
+
|
|
79
|
+
events = await storage.listEvents({});
|
|
80
|
+
expect(events.length).toBe(0);
|
|
81
|
+
});
|
|
82
|
+
});
|