2024-08-03 09:47:13 +01:00
|
|
|
import { normalizeRequest } from "../processing/request.js";
|
|
|
|
import match from "../processing/match.js";
|
|
|
|
import { extract } from "../processing/url.js";
|
2024-07-09 19:30:24 +01:00
|
|
|
|
|
|
|
export async function runTest(url, params, expect) {
|
2024-08-08 17:34:54 +01:00
|
|
|
const { success, data: normalized } = await normalizeRequest({ url, ...params });
|
|
|
|
if (!success) {
|
2024-07-09 19:30:24 +01:00
|
|
|
throw "invalid request";
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsed = extract(normalized.url);
|
|
|
|
if (parsed === null) {
|
|
|
|
throw `invalid url: ${normalized.url}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await match(
|
2024-08-03 18:33:53 +01:00
|
|
|
parsed.host, parsed.patternMatch, normalized
|
2024-07-09 19:30:24 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
let error = [];
|
|
|
|
if (expect.status !== result.body.status) {
|
|
|
|
const detail = `${expect.status} (expected) != ${result.body.status} (actual)`;
|
|
|
|
error.push(`status mismatch: ${detail}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expect.code !== result.status) {
|
|
|
|
const detail = `${expect.code} (expected) != ${result.status} (actual)`;
|
|
|
|
error.push(`status code mismatch: ${detail}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.length) {
|
|
|
|
if (result.body.text) {
|
|
|
|
error.push(`error message: ${result.body.text}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error.join('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.body.status === 'stream') {
|
|
|
|
// TODO: stream testing
|
|
|
|
}
|
|
|
|
}
|