-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtest-api.js
More file actions
102 lines (93 loc) · 3.64 KB
/
test-api.js
File metadata and controls
102 lines (93 loc) · 3.64 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
const http = require("http");
const test = (method, path, body = null, validate, timeout = 5000) => {
return new Promise((resolve) => {
const options = {
hostname: "localhost",
port: 3000,
path,
method,
headers: body ? { "Content-Type": "application/json" } : {},
};
let resolved = false;
const timer = setTimeout(() => {
if (!resolved) {
resolved = true;
console.log(`✗ ${method} ${path} - Timeout`);
resolve(false);
}
}, timeout);
const req = http.request(options, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
if (path === "/events" && data.includes("data:")) {
clearTimeout(timer);
if (!resolved) {
resolved = true;
const valid = validate(data, res.headers["content-type"]);
console.log(`${valid ? "✓" : "✗"} ${method} ${path}`);
req.destroy();
resolve(valid);
}
}
});
res.on("end", () => {
clearTimeout(timer);
if (!resolved) {
resolved = true;
try {
const valid = validate(data, res.headers["content-type"]);
console.log(`${valid ? "✓" : "✗"} ${method} ${path}`);
resolve(valid);
} catch (e) {
console.log(`✗ ${method} ${path} - ${e.message}`);
resolve(false);
}
}
});
});
req.on("error", (e) => {
clearTimeout(timer);
if (!resolved) {
resolved = true;
console.log(`✗ ${method} ${path} - ${e.message}`);
resolve(false);
}
});
if (body) req.write(JSON.stringify(body));
req.end();
});
};
(async () => {
console.log("Testing API endpoints...\n");
const results = await Promise.all([
test("GET", "/api/stats", null, (data) => {
const json = JSON.parse(data);
return json.count !== undefined && json.id && json.screenname && json.diagnostics;
}),
test("GET", "/api/github/latest-release", null, (data) => {
const json = JSON.parse(data);
return json.tag_name && json.html_url;
}),
test("POST", "/api/chat", { content: "test", scope: "LOCAL" }, (data) => {
const json = JSON.parse(data);
return json.success === true;
}),
test("GET", "/events", null, (data, contentType) => {
return contentType.includes("text/event-stream") && data.includes("data:");
}, 2000),
test("GET", "/js/lists.js", null, (data, contentType) => {
return contentType.includes("javascript") && data.includes("ADJECTIVES") && data.includes("NOUNS");
}),
test("GET", "/js/screenname.js", null, (data, contentType) => {
return contentType.includes("javascript") && data.includes("generateScreenname");
}),
test("GET", "/", null, (data, contentType) => {
return contentType.includes("text/html") && data.includes("Hypermind");
}),
]);
const passed = results.filter(Boolean).length;
const total = results.length;
console.log(`\n${passed}/${total} tests passed`);
process.exit(passed === total ? 0 : 1);
})();