Repository navigation
Expand file tree
/
Copy pathhttp_server.test.js
More file actions
237 lines (234 loc) · 9.89 KB
/
Copy pathhttp_server.test.js
File metadata and controls
237 lines (234 loc) · 9.89 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { jest } from "@jest/globals";
import * as http from "http";
import * as path from "path";
import * as fs from "fs";
import * as os from "os";
import { fileURLToPath } from "url";
import { runHTTPServer, runHTTPServerCLI } from "../index.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
describe("HTTP Server SAPI Tests", () => {
let server;
const testPort = 8888;
const docRoot = path.join(__dirname, "http_server_test_dir");
beforeAll(async () => {
jest.setTimeout(120000);
if (!fs.existsSync(docRoot)) {
fs.mkdirSync(docRoot, { recursive: true });
}
// Create static index.html
fs.writeFileSync(path.join(docRoot, "index.html"), "<!DOCTYPE html><html><body><h1>Static Test</h1></body></html>");
// Create general_test.php in docRoot
fs.writeFileSync(path.join(docRoot, "general_test.php"), `<?php
function calculate_sum($a, $b) {
return $a + $b;
}
class TestCalculator {
public function multiply($x, $y) {
return $x * $y;
}
}
$calc = new TestCalculator();
$sumResult = calculate_sum(15, 25);
$multResult = $calc->multiply(6, 7);
echo "METHOD: " . ($_SERVER['REQUEST_METHOD'] ?? 'UNKNOWN') . "\\n";
echo "SUM: " . $sumResult . "\\n";
echo "MULT: " . $multResult . "\\n";
echo "QUERY_A: " . ($_GET['a'] ?? 'NONE') . "\\n";
echo "STATUS: SUCCESS\\n";
`);
// Create fatal_error_test.php in docRoot
fs.writeFileSync(path.join(docRoot, "fatal_error_test.php"), `<?php
function cause_error() {
non_existent_function_call();
}
cause_error();
`);
fs.writeFileSync(path.join(docRoot, "unexpected_error.php"), "<?php throw new \\Error('Unexpected runtime failure');");
fs.writeFileSync(path.join(docRoot, "ext_test.php"), `<?php
echo "NODEJS: " . (function_exists('njs_import') || function_exists('nodejs_require') ? 'YES' : 'NO') . "\\n";
echo "MYSQLI: " . (function_exists('mysqli_connect') ? 'YES' : 'NO') . "\\n";
echo "PCRE: " . (function_exists('preg_match') ? 'YES' : 'NO') . "\\n";
`);
server = await runHTTPServer(testPort, docRoot);
});
afterAll((done) => {
const cleanup = () => {
if (fs.existsSync(docRoot)) {
try {
fs.rmSync(docRoot, { recursive: true, force: true });
}
catch (e) { }
}
const testCacheDir = path.join(__dirname, "../.test_cache");
if (fs.existsSync(testCacheDir)) {
try {
fs.rmSync(testCacheDir, { recursive: true, force: true });
}
catch (e) { }
}
const defaultCacheDir = process.env.JSPHP_CACHE || path.join(os.tmpdir(), "jsphp_cache");
if (fs.existsSync(defaultCacheDir)) {
try {
fs.rmSync(defaultCacheDir, { recursive: true, force: true });
}
catch (e) { }
}
done();
};
if (server) {
server.close(cleanup);
}
else {
cleanup();
}
});
function makeRequest(urlPath, method = "GET", postData = "") {
return new Promise((resolve, reject) => {
const options = {
hostname: "127.0.0.1",
port: testPort,
path: urlPath,
method: method,
headers: method === "POST" ? { "Content-Type": "application/x-www-form-urlencoded" } : {},
};
const req = http.request(options, (res) => {
let body = "";
res.on("data", (chunk) => { body += chunk; });
res.on("end", () => {
resolve({ status: res.statusCode || 0, headers: res.headers, body });
});
});
req.on("error", reject);
if (postData) {
req.write(postData);
}
req.end();
});
}
test("Serves static html files", async () => {
const res = await makeRequest("/index.html");
expect(res.status).toBe(200);
expect(res.body).toContain("Static Test");
});
test("Executes general_test.php with classes, functions and query params", async () => {
const res = await makeRequest("/general_test.php?a=100");
expect(res.status).toBe(200);
expect(res.body).toContain("METHOD: GET");
expect(res.body).toContain("SUM: 40");
expect(res.body).toContain("MULT: 42");
expect(res.body).toContain("QUERY_A: 100");
expect(res.body).toContain("STATUS: SUCCESS");
});
test("Returns HTTP 500 and PHP Stack Trace on fatal errors", async () => {
const res = await makeRequest("/fatal_error_test.php");
expect(res.status).toBe(500);
expect(res.headers["content-type"]).toContain("text/html");
expect(res.body).toContain("PHP Fatal Error");
expect(res.body).toContain("PHP Stack Trace");
expect(res.body).toContain("undefined function");
});
test("Virtualizes unexpected runtime errors instead of exposing JavaScript frames", async () => {
const res = await makeRequest("/unexpected_error.php");
expect(res.status).toBe(500);
expect(res.body).toContain("PHP Stack Trace");
expect(res.body).not.toContain("eval at compileCode");
expect(res.body).not.toContain("PHPContext.js");
expect(res.body).not.toContain("PHPContext.ts");
expect(res.body).not.toContain("node:internal");
});
test("Default server includes nodejs and mysqli extensions", async () => {
const res = await makeRequest("/ext_test.php");
expect(res.status).toBe(200);
expect(res.body).toContain("NODEJS: YES");
expect(res.body).toContain("MYSQLI: YES");
expect(res.body).toContain("PCRE: YES");
});
test("Disables specified extensions using disabledExtensions option", async () => {
const customServer = await runHTTPServer(8890, docRoot, { disabledExtensions: ["nodejs", "mysqli"] });
try {
const res = await new Promise((resolve, reject) => {
http.get("http://127.0.0.1:8890/ext_test.php", (r) => {
let b = "";
r.on("data", (chunk) => { b += chunk; });
r.on("end", () => resolve({ status: r.statusCode || 0, body: b }));
}).on("error", reject);
});
expect(res.status).toBe(200);
expect(res.body).toContain("NODEJS: NO");
expect(res.body).toContain("MYSQLI: NO");
expect(res.body).toContain("PCRE: YES");
}
finally {
customServer.close();
}
});
test("CLI --disable-extension flag disables specified extensions", async () => {
const cliServer = await runHTTPServerCLI(["-p", "8891", "-d", docRoot, "--disable-extension", "nodejs", "--disable-extension", "mysqli", "--no-cluster"]);
try {
const res = await new Promise((resolve, reject) => {
http.get("http://127.0.0.1:8891/ext_test.php", (r) => {
let b = "";
r.on("data", (chunk) => { b += chunk; });
r.on("end", () => resolve({ status: r.statusCode || 0, body: b }));
}).on("error", reject);
});
expect(res.status).toBe(200);
expect(res.body).toContain("NODEJS: NO");
expect(res.body).toContain("MYSQLI: NO");
expect(res.body).toContain("PCRE: YES");
}
finally {
if (cliServer && "close" in cliServer) {
await new Promise((resolve) => cliServer.close(() => resolve()));
}
}
});
test("Serves WordPress setup-config.php with rendered text and stylesheet", async () => {
const wpDir = path.join(__dirname, "../wordpress-test");
const wpConfigPath = path.join(wpDir, "wp-config.php");
if (fs.existsSync(wpConfigPath)) {
try {
fs.unlinkSync(wpConfigPath);
}
catch (e) { }
}
const wpServer = await runHTTPServer(8889, wpDir, { cacheDir: path.join(__dirname, "../.test_cache") });
try {
const res = await new Promise((resolve, reject) => {
http.get("http://127.0.0.1:8889/wp-admin/setup-config.php", (r) => {
let b = "";
r.on("data", (chunk) => { b += chunk; });
r.on("end", () => resolve({ status: r.statusCode || 0, body: b }));
}).on("error", reject);
});
expect(res.status).toBe(200);
expect(res.body).toContain("wp-core-ui");
expect(res.body).toMatch(/<link\s+rel=['"]stylesheet['"].*?install/i);
const resStep1 = await new Promise((resolve, reject) => {
http.get("http://127.0.0.1:8889/wp-admin/setup-config.php?step=1", (r) => {
let b = "";
r.on("data", (chunk) => { b += chunk; });
r.on("end", () => resolve({ status: r.statusCode || 0, body: b }));
}).on("error", reject);
});
console.log("STEP 1 HEAD:\n", resStep1.body.slice(0, resStep1.body.indexOf("</head>") + 7));
expect(resStep1.status).toBe(200);
expect(resStep1.body).toContain("Database Name");
expect(resStep1.body).toContain("Username");
expect(resStep1.body).toContain("Password");
expect(resStep1.body).toContain("wp-core-ui");
expect(resStep1.body).toMatch(/<link\s+rel=['"]stylesheet['"].*?install/i);
}
finally {
wpServer.close();
if (fs.existsSync(wpConfigPath)) {
try {
fs.unlinkSync(wpConfigPath);
}
catch (e) { }
}
}
}, 180000);
});
//# sourceMappingURL=http_server.test.js.map