-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_helper.js
More file actions
117 lines (106 loc) · 3.27 KB
/
test_helper.js
File metadata and controls
117 lines (106 loc) · 3.27 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
// Copyright Titanium I.T. LLC.
import * as ensure from "util/ensure.js";
import http from "node:http";
import childProcess from "node:child_process";
/**
* Perform an HTTP request in a way that's convenient for tests. Deletes certain headers tests don't care about.
* @param port localhost port
* @param url path
* @param method GET, POST, etc.
* @param headers optional; headers to send
* @param body optional; body to send
* @returns { status, headers, body } status, headers, and body returned by server
*/
export async function requestAsync({ port, url, method, headers, body = [] } = {}) {
return await new Promise((resolve, reject) => {
ensure.signature(arguments, [[ undefined, {
port: [ Number, String ],
url: [ undefined, String ],
method: [ undefined, String ],
headers: [ undefined, Object ],
body: [ undefined, Array ],
}]]);
if (method === undefined && body.length !== 0) method = "POST";
const request = http.request({ port, path: url, method, headers });
body.forEach((chunk) => request.write(chunk));
request.end();
request.on("response", (response) => {
let body = "";
response.on("data", (chunk) => {
body += chunk;
});
response.on("error", (err) => reject(err));
response.on("end", () => {
const headers = response.headers;
delete headers.connection;
delete headers["content-length"];
delete headers.date;
resolve({
status: response.statusCode,
headers: response.headers,
body,
});
});
});
});
}
/**
* Run specified module in a separate process.
* @param absolutePath path to module (relative to working directory)
* @param args optional; process arguments (default none)
* @param failOnStderr optional; if true, throws an error if anything is written to stderr (default true)
* @returns { stdout, stderr } data written to stdout and stderr
*/
export function runModuleAsync(absolutePath, { args = [], failOnStderr = true } = {}) {
return new Promise((resolve, reject) => {
ensure.signature(arguments, [ String, [ undefined, {
args: [ undefined, Array ],
failOnStderr: [ undefined, Boolean ],
}]], [ "absolutePath", "options" ]);
const child = forkModule(absolutePath, { args });
let stdout = "";
let stderr = "";
child.stdout.on("data", (data) => {
stdout += data;
});
child.stderr.on("data", (data) => {
stderr += data;
});
child.on("exit", (code) => {
if (code !== 0 || (failOnStderr && stderr !== "")) {
return reject(new Error(`Runner failed.\nstdout: ${stdout}\nstderr: ${stderr}`));
}
else {
return resolve({ stdout, stderr });
}
});
});
}
/**
* Runs a Node.js module in a separate process.
* @param absolutePath path to module
* @param args optional; process arguments (default none)
* @returns { stdout, stderr } data written to stdout and stderr
*/
export function forkModule(absolutePath, {
args = []
} = {}) {
ensure.signature(arguments, [ String, [ undefined, {
args: [ undefined, Array ],
}]], [ "absolutePath", "options" ]);
const options = {
stdio: "pipe",
};
return childProcess.fork(absolutePath, args, options);
}
/**
* Resolves a promise and ignores any errors it throws.
* @param promise the promise to ignore
*/
export async function ignorePromiseErrorAsync(promise) {
try {
await promise;
}
catch (err) {
}
}