Skip to content
Permalink
Newer
Older
100644 53 lines (43 sloc) 1.58 KB
September 13, 2024 22:55
1
import { normalizeRequest } from "../processing/request.js";
2
import match from "../processing/match.js";
3
import { extract } from "../processing/url.js";
4
5
export async function runTest(url, params, expect) {
6
const { success, data: normalized } = await normalizeRequest({ url, ...params });
7
if (!success) {
8
throw "invalid request";
9
}
10
11
const parsed = extract(normalized.url);
12
if (parsed === null) {
13
throw `invalid url: ${normalized.url}`;
14
}
15
16
const result = await match({
17
host: parsed.host,
18
patternMatch: parsed.patternMatch,
19
params: normalized,
20
});
21
22
let error = [];
23
if (expect.status !== result.body.status) {
24
const detail = `${expect.status} (expected) != ${result.body.status} (actual)`;
25
error.push(`status mismatch: ${detail}`);
March 10, 2025 19:56
26
27
if (result.body.status === 'error') {
28
error.push(`error code: ${result.body?.error?.code}`);
29
}
30
}
31
32
if (expect.errorCode && expect.errorCode !== result.body?.error?.code) {
33
const detail = `${expect.errorCode} (expected) != ${result.body.error.code} (actual)`
34
error.push(`error mismatch: ${detail}`);
September 13, 2024 22:55
35
}
36
37
if (expect.code !== result.status) {
38
const detail = `${expect.code} (expected) != ${result.status} (actual)`;
39
error.push(`status code mismatch: ${detail}`);
40
}
41
42
if (error.length) {
43
if (result.body.text) {
44
error.push(`error message: ${result.body.text}`);
45
}
46
47
throw error.join('\n');
48
}
49
50
if (result.body.status === 'tunnel') {
51
// TODO: stream testing
52
}
January 5, 2025 20:35
53
}