automation_model 1.0.842-dev → 1.0.844-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/route.d.ts +64 -2
- package/lib/route.js +360 -261
- package/lib/route.js.map +1 -1
- package/lib/stable_browser.d.ts +1 -1
- package/lib/stable_browser.js +6 -1
- package/lib/stable_browser.js.map +1 -1
- package/lib/utils.d.ts +4 -2
- package/lib/utils.js +2 -4
- package/lib/utils.js.map +1 -1
- package/package.json +5 -2
package/lib/route.js
CHANGED
|
@@ -47,15 +47,311 @@ async function loadRoutes(context, template) {
|
|
|
47
47
|
}
|
|
48
48
|
return context.loadedRoutes.get(template) || [];
|
|
49
49
|
}
|
|
50
|
+
export function pathFilter(savedPath, actualPath) {
|
|
51
|
+
if (typeof savedPath !== "string")
|
|
52
|
+
return false;
|
|
53
|
+
if (savedPath.includes("*")) {
|
|
54
|
+
// Escape regex special characters in savedPath
|
|
55
|
+
const escapedPath = savedPath.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
|
|
56
|
+
// Treat it as a wildcard
|
|
57
|
+
const regex = new RegExp(escapedPath.replace(/\*/g, ".*"));
|
|
58
|
+
return regex.test(actualPath);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
return savedPath === actualPath;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export function queryParamsFilter(savedQueryParams, actualQueryParams) {
|
|
65
|
+
if (!savedQueryParams)
|
|
66
|
+
return true;
|
|
67
|
+
for (const [key, value] of Object.entries(savedQueryParams)) {
|
|
68
|
+
if (value === "*") {
|
|
69
|
+
// If the saved query param is a wildcard, it matches anything
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (actualQueryParams.get(key) !== value) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
export function methodFilter(savedMethod, actualMethod) {
|
|
79
|
+
if (!savedMethod)
|
|
80
|
+
return true;
|
|
81
|
+
if (savedMethod === "*") {
|
|
82
|
+
const httpMethodRegex = /^(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD)$/;
|
|
83
|
+
return httpMethodRegex.test(actualMethod);
|
|
84
|
+
}
|
|
85
|
+
return savedMethod === actualMethod;
|
|
86
|
+
}
|
|
50
87
|
function matchRoute(routeItem, req) {
|
|
51
88
|
const url = new URL(req.request().url());
|
|
52
|
-
const methodMatch = !routeItem.filters.method || routeItem.filters.method === req.request().method();
|
|
53
|
-
const pathMatch = routeItem.filters.path === url.pathname;
|
|
54
89
|
const queryParams = routeItem.filters.queryParams;
|
|
55
|
-
|
|
56
|
-
|
|
90
|
+
return (methodFilter(routeItem.filters.method, req.request().method()) &&
|
|
91
|
+
pathFilter(routeItem.filters.path, url.pathname) &&
|
|
92
|
+
queryParamsFilter(queryParams, url.searchParams));
|
|
93
|
+
}
|
|
94
|
+
function handleAbortRequest(action, context) {
|
|
95
|
+
if (context.tracking.timer)
|
|
96
|
+
clearTimeout(context.tracking.timer);
|
|
97
|
+
const errorCode = action.config?.errorCode ?? "failed";
|
|
98
|
+
console.log(`[abort_request] Aborting with error code: ${errorCode}`);
|
|
99
|
+
context.route.abort(errorCode);
|
|
100
|
+
context.abortActionPerformed = true;
|
|
101
|
+
context.tracking.completed = true;
|
|
102
|
+
return {
|
|
103
|
+
type: action.type,
|
|
104
|
+
description: JSON.stringify(action.config),
|
|
105
|
+
status: "success",
|
|
106
|
+
message: `Request aborted with code: ${errorCode}`,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function handleStatusCodeVerification(action, context) {
|
|
110
|
+
const isSuccess = String(context.status) === String(action.config);
|
|
111
|
+
return {
|
|
112
|
+
type: action.type,
|
|
113
|
+
description: JSON.stringify(action.config),
|
|
114
|
+
status: isSuccess ? "success" : "fail",
|
|
115
|
+
message: `Status code verification ${isSuccess ? "passed" : "failed"}. Expected ${action.config}, got ${context.status}`,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function handleJsonModify(action, context) {
|
|
119
|
+
if (!context.json) {
|
|
120
|
+
return {
|
|
121
|
+
type: action.type,
|
|
122
|
+
description: JSON.stringify(action.config),
|
|
123
|
+
status: "fail",
|
|
124
|
+
message: "JSON modification failed. Response is not JSON",
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
objectPath.set(context.json, action.config.path, action.config.modifyValue);
|
|
128
|
+
context.finalBody = JSON.parse(JSON.stringify(context.json));
|
|
129
|
+
return {
|
|
130
|
+
type: action.type,
|
|
131
|
+
description: JSON.stringify(action.config),
|
|
132
|
+
status: "success",
|
|
133
|
+
message: `JSON modified at path '${action.config.path}'`,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function handleJsonWholeModify(action, context) {
|
|
137
|
+
if (!context.json) {
|
|
138
|
+
return {
|
|
139
|
+
type: action.type,
|
|
140
|
+
description: JSON.stringify(action.config),
|
|
141
|
+
status: "fail",
|
|
142
|
+
message: "JSON modification failed. Response is not JSON",
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
const parsedConfig = typeof action.config === "string" ? JSON.parse(action.config) : action.config;
|
|
147
|
+
context.json = parsedConfig;
|
|
148
|
+
context.finalBody = JSON.parse(JSON.stringify(context.json));
|
|
149
|
+
return {
|
|
150
|
+
type: action.type,
|
|
151
|
+
description: JSON.stringify(action.config),
|
|
152
|
+
status: "success",
|
|
153
|
+
message: "Whole JSON body was replaced.",
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
catch (e) {
|
|
157
|
+
const message = `JSON modification failed. Invalid JSON in config: ${e instanceof Error ? e.message : String(e)}`;
|
|
158
|
+
return { type: action.type, description: JSON.stringify(action.config), status: "fail", message };
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function handleStatusCodeChange(action, context) {
|
|
162
|
+
context.status = Number(action.config);
|
|
163
|
+
return {
|
|
164
|
+
type: action.type,
|
|
165
|
+
description: JSON.stringify(action.config),
|
|
166
|
+
status: "success",
|
|
167
|
+
message: `Status code changed to ${context.status}`,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
function handleChangeText(action, context) {
|
|
171
|
+
if (context.isBinary) {
|
|
172
|
+
return {
|
|
173
|
+
type: action.type,
|
|
174
|
+
description: JSON.stringify(action.config),
|
|
175
|
+
status: "fail",
|
|
176
|
+
message: "Change text action failed. Body is not text.",
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
context.body = action.config;
|
|
180
|
+
context.finalBody = context.body;
|
|
181
|
+
return {
|
|
182
|
+
type: action.type,
|
|
183
|
+
description: JSON.stringify(action.config),
|
|
184
|
+
status: "success",
|
|
185
|
+
message: "Response body text was replaced.",
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function handleAssertJson(action, context) {
|
|
189
|
+
if (!context.json) {
|
|
190
|
+
return {
|
|
191
|
+
type: action.type,
|
|
192
|
+
description: JSON.stringify(action.config),
|
|
193
|
+
status: "fail",
|
|
194
|
+
message: "JSON assertion failed. Response is not JSON.",
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
const actual = objectPath.get(context.json, action.config.path);
|
|
198
|
+
const expected = action.config.expectedValue;
|
|
199
|
+
const isSuccess = JSON.stringify(actual) === JSON.stringify(expected);
|
|
200
|
+
return {
|
|
201
|
+
type: action.type,
|
|
202
|
+
description: JSON.stringify(action.config),
|
|
203
|
+
status: isSuccess ? "success" : "fail",
|
|
204
|
+
message: isSuccess
|
|
205
|
+
? `JSON assertion passed for path '${action.config.path}'.`
|
|
206
|
+
: `JSON assertion failed for path '${action.config.path}': expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
function handleAssertWholeJson(action, context) {
|
|
210
|
+
if (!context.json) {
|
|
211
|
+
return {
|
|
212
|
+
type: action.type,
|
|
213
|
+
description: JSON.stringify(action.config),
|
|
214
|
+
status: "fail",
|
|
215
|
+
message: "Whole JSON assertion failed. Response is not JSON.",
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
const originalJSON = JSON.stringify(context.json, null, 2);
|
|
219
|
+
let isSuccess = false;
|
|
220
|
+
let message = "";
|
|
221
|
+
if ("contains" in action.config) {
|
|
222
|
+
isSuccess = originalJSON.includes(action.config.contains);
|
|
223
|
+
message = isSuccess
|
|
224
|
+
? "Whole JSON assertion passed."
|
|
225
|
+
: `Whole JSON assertion failed. Expected to contain: "${action.config.contains}".`;
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
isSuccess = originalJSON === action.config.equals;
|
|
229
|
+
message = isSuccess
|
|
230
|
+
? "Whole JSON assertion passed."
|
|
231
|
+
: `Whole JSON assertion failed. Expected exact match: "${action.config.equals}".`;
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
type: action.type,
|
|
235
|
+
description: JSON.stringify(action.config),
|
|
236
|
+
status: isSuccess ? "success" : "fail",
|
|
237
|
+
message,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function handleAssertText(action, context) {
|
|
241
|
+
if (typeof context.body !== "string") {
|
|
242
|
+
return {
|
|
243
|
+
type: action.type,
|
|
244
|
+
description: JSON.stringify(action.config),
|
|
245
|
+
status: "fail",
|
|
246
|
+
message: "Text assertion failed. Body is not text.",
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
let isSuccess = false;
|
|
250
|
+
let message = "";
|
|
251
|
+
if ("contains" in action.config) {
|
|
252
|
+
isSuccess = context.body.includes(action.config.contains);
|
|
253
|
+
message = isSuccess
|
|
254
|
+
? "Text assertion passed."
|
|
255
|
+
: `Text assertion failed. Expected to contain: "${action.config.contains}".`;
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
isSuccess = context.body === action.config.equals;
|
|
259
|
+
message = isSuccess
|
|
260
|
+
? "Text assertion passed."
|
|
261
|
+
: `Text assertion failed. Expected exact match: "${action.config.equals}".`;
|
|
262
|
+
}
|
|
263
|
+
return {
|
|
264
|
+
type: action.type,
|
|
265
|
+
description: JSON.stringify(action.config),
|
|
266
|
+
status: isSuccess ? "success" : "fail",
|
|
267
|
+
message,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
function handleStubAction(stubAction, route, tracking) {
|
|
271
|
+
let actionStatus = "success";
|
|
272
|
+
const description = JSON.stringify(stubAction.config);
|
|
273
|
+
const request = route.request();
|
|
274
|
+
let stubActionPerformed = false;
|
|
275
|
+
debug(`Stub action found for ${request.url()}. Skipping fetch.`);
|
|
276
|
+
if (tracking.timer)
|
|
277
|
+
clearTimeout(tracking.timer);
|
|
278
|
+
const fullFillConfig = {};
|
|
279
|
+
if (!tracking.actionResults)
|
|
280
|
+
tracking.actionResults = [];
|
|
281
|
+
if (stubAction.config.path) {
|
|
282
|
+
const filePath = path.join(process.cwd(), "data", "fixtures", stubAction.config.path);
|
|
283
|
+
debug(`Stub action file path: ${filePath}`);
|
|
284
|
+
if (existsSync(filePath)) {
|
|
285
|
+
fullFillConfig.path = filePath;
|
|
286
|
+
debug(`Stub action fulfilled with file: ${filePath}`);
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
actionStatus = "fail";
|
|
290
|
+
tracking.actionResults.push({
|
|
291
|
+
type: "stub_request",
|
|
292
|
+
description,
|
|
293
|
+
status: actionStatus,
|
|
294
|
+
message: `Stub action failed for ${tracking.url}: File not found at ${filePath}`,
|
|
295
|
+
});
|
|
296
|
+
stubActionPerformed = true;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
if (!fullFillConfig.path) {
|
|
300
|
+
if (stubAction.config.statusCode) {
|
|
301
|
+
fullFillConfig.status = Number(stubAction.config.statusCode);
|
|
302
|
+
}
|
|
303
|
+
if (stubAction.config.contentType) {
|
|
304
|
+
if (stubAction.config.contentType === "application/json") {
|
|
305
|
+
fullFillConfig.contentType = "application/json";
|
|
306
|
+
if (stubAction.config.body) {
|
|
307
|
+
try {
|
|
308
|
+
fullFillConfig.json = JSON.parse(stubAction.config.body);
|
|
309
|
+
}
|
|
310
|
+
catch (e) {
|
|
311
|
+
debug(`Invalid JSON in stub action body: ${stubAction.config.body}, `, e instanceof Error ? e.message : String(e));
|
|
312
|
+
debug("Invalid JSON, defaulting to empty object");
|
|
313
|
+
fullFillConfig.json = {};
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
fullFillConfig.contentType = stubAction.config.contentType;
|
|
319
|
+
fullFillConfig.body = stubAction.config.body || "";
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (!fullFillConfig.json && !fullFillConfig.body) {
|
|
323
|
+
if (stubAction.config.body) {
|
|
324
|
+
fullFillConfig.body = stubAction.config.body;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
if (actionStatus === "success") {
|
|
329
|
+
try {
|
|
330
|
+
route.fulfill(fullFillConfig);
|
|
331
|
+
stubActionPerformed = true;
|
|
332
|
+
tracking.completed = true;
|
|
333
|
+
tracking.actionResults.push({
|
|
334
|
+
type: "stub_request",
|
|
335
|
+
description,
|
|
336
|
+
status: actionStatus,
|
|
337
|
+
message: `Stub action executed for ${request.url()}`,
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
catch (e) {
|
|
341
|
+
actionStatus = "fail";
|
|
342
|
+
debug(`Failed to fulfill stub request for ${request.url()}`, e);
|
|
343
|
+
tracking.actionResults.push({
|
|
344
|
+
type: "stub_request",
|
|
345
|
+
description,
|
|
346
|
+
status: actionStatus,
|
|
347
|
+
message: `Stub action failed for ${request.url()}: ${e instanceof Error ? e.message : String(e)}`,
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return stubActionPerformed;
|
|
57
352
|
}
|
|
58
353
|
export async function registerBeforeStepRoutes(context, stepName, world) {
|
|
354
|
+
const debug = createDebug("automation_model:route:registerBeforeStepRoutes");
|
|
59
355
|
const page = context.web.page;
|
|
60
356
|
if (!page)
|
|
61
357
|
throw new Error("context.web.page is missing");
|
|
@@ -67,22 +363,35 @@ export async function registerBeforeStepRoutes(context, stepName, world) {
|
|
|
67
363
|
}
|
|
68
364
|
for (let i = 0; i < allRouteItems.length; i++) {
|
|
69
365
|
const item = allRouteItems[i];
|
|
366
|
+
debug(`Setting up mandatory route with timeout ${item.timeout}ms: ${JSON.stringify(item.filters)}`);
|
|
367
|
+
let content = JSON.stringify(item);
|
|
368
|
+
try {
|
|
369
|
+
content = await replaceWithLocalTestData(content, context.web.world, true, false, content, context.web, false);
|
|
370
|
+
allRouteItems[i] = JSON.parse(content); // Modify the original array
|
|
371
|
+
debug(`After replacing test data: ${JSON.stringify(allRouteItems[i])}`);
|
|
372
|
+
}
|
|
373
|
+
catch (error) {
|
|
374
|
+
debug("Error replacing test data:", error);
|
|
375
|
+
}
|
|
70
376
|
if (item.mandatory) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
377
|
+
const path = item.filters.path;
|
|
378
|
+
const queryParams = Object.entries(item.filters.queryParams || {})
|
|
379
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
380
|
+
.join("&");
|
|
381
|
+
const tracking = {
|
|
382
|
+
routeItem: item,
|
|
383
|
+
url: `${path}${queryParams ? `?${queryParams}` : ""}`,
|
|
384
|
+
completed: false,
|
|
385
|
+
startedAt: Date.now(),
|
|
386
|
+
actionResults: [],
|
|
387
|
+
};
|
|
388
|
+
context.__routeState.matched.push(tracking);
|
|
81
389
|
}
|
|
82
390
|
}
|
|
83
391
|
debug("New allrouteItems", JSON.stringify(allRouteItems));
|
|
84
392
|
let message = null;
|
|
85
393
|
page.route("**/*", async (route) => {
|
|
394
|
+
const debug = createDebug("automation_model:route:intercept");
|
|
86
395
|
const request = route.request();
|
|
87
396
|
debug(`Intercepting request: ${request.method()} ${request.url()}`);
|
|
88
397
|
const matchedItem = allRouteItems.find((item) => matchRoute(item, route));
|
|
@@ -90,7 +399,7 @@ export async function registerBeforeStepRoutes(context, stepName, world) {
|
|
|
90
399
|
return route.continue();
|
|
91
400
|
debug(`Matched route item: ${JSON.stringify(matchedItem)}`);
|
|
92
401
|
debug("Initial context route state", context.__routeState);
|
|
93
|
-
let tracking = context.__routeState.matched.find((t) => t.routeItem === matchedItem && !t.completed);
|
|
402
|
+
let tracking = context.__routeState.matched.find((t) => JSON.stringify(t.routeItem) === JSON.stringify(matchedItem) && !t.completed);
|
|
94
403
|
debug("Tracking", tracking);
|
|
95
404
|
let stubActionPerformed = false;
|
|
96
405
|
if (!tracking) {
|
|
@@ -112,88 +421,12 @@ export async function registerBeforeStepRoutes(context, stepName, world) {
|
|
|
112
421
|
}
|
|
113
422
|
const stubAction = matchedItem.actions.find((a) => a.type === "stub_request");
|
|
114
423
|
if (stubAction) {
|
|
115
|
-
|
|
116
|
-
const description = JSON.stringify(stubAction.config);
|
|
117
|
-
debug(`Stub action found for ${request.url()}. Skipping fetch.`);
|
|
118
|
-
if (tracking.timer)
|
|
119
|
-
clearTimeout(tracking.timer);
|
|
120
|
-
const fullFillConfig = {};
|
|
121
|
-
if (stubAction.config.path) {
|
|
122
|
-
const filePath = path.join(process.cwd(), "data", "fixtures", stubAction.config.path);
|
|
123
|
-
debug(`Stub action file path: ${filePath}`);
|
|
124
|
-
if (existsSync(filePath)) {
|
|
125
|
-
fullFillConfig.path = filePath;
|
|
126
|
-
debug(`Stub action fulfilled with file: ${filePath}`);
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
actionStatus = "fail";
|
|
130
|
-
tracking.actionResults.push({
|
|
131
|
-
type: "stub_request",
|
|
132
|
-
description,
|
|
133
|
-
status: actionStatus,
|
|
134
|
-
message: `Stub action failed for ${tracking.url}: File not found at ${filePath}`,
|
|
135
|
-
});
|
|
136
|
-
stubActionPerformed = true;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
if (!fullFillConfig.path) {
|
|
140
|
-
if (stubAction.config.statusCode) {
|
|
141
|
-
fullFillConfig.status = Number(stubAction.config.statusCode);
|
|
142
|
-
}
|
|
143
|
-
if (stubAction.config.contentType) {
|
|
144
|
-
if (stubAction.config.contentType === "application/json") {
|
|
145
|
-
fullFillConfig.contentType = "application/json";
|
|
146
|
-
if (stubAction.config.body) {
|
|
147
|
-
try {
|
|
148
|
-
fullFillConfig.json = JSON.parse(stubAction.config.body);
|
|
149
|
-
}
|
|
150
|
-
catch (e) {
|
|
151
|
-
debug(`Invalid JSON in stub action body: ${stubAction.config.body}, `, e instanceof Error ? e.message : String(e));
|
|
152
|
-
debug("Invalid JSON, defaulting to empty object");
|
|
153
|
-
fullFillConfig.json = {};
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
fullFillConfig.contentType = stubAction.config.contentType;
|
|
159
|
-
fullFillConfig.body = stubAction.config.body || "";
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
if (!fullFillConfig.json && !fullFillConfig.body) {
|
|
163
|
-
if (stubAction.config.body) {
|
|
164
|
-
fullFillConfig.body = stubAction.config.body;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
if (actionStatus === "success") {
|
|
169
|
-
try {
|
|
170
|
-
route.fulfill(fullFillConfig);
|
|
171
|
-
stubActionPerformed = true;
|
|
172
|
-
tracking.completed = true;
|
|
173
|
-
tracking.actionResults.push({
|
|
174
|
-
type: "stub_request",
|
|
175
|
-
description,
|
|
176
|
-
status: actionStatus,
|
|
177
|
-
message: `Stub action executed for ${request.url()}`,
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
catch (e) {
|
|
181
|
-
actionStatus = "fail";
|
|
182
|
-
debug(`Failed to fulfill stub request for ${request.url()}`, e);
|
|
183
|
-
tracking.actionResults.push({
|
|
184
|
-
type: "stub_request",
|
|
185
|
-
description,
|
|
186
|
-
status: actionStatus,
|
|
187
|
-
message: `Stub action failed for ${request.url()}: ${e instanceof Error ? e.message : String(e)}`,
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
}
|
|
424
|
+
stubActionPerformed = handleStubAction(stubAction, route, tracking);
|
|
191
425
|
}
|
|
192
426
|
if (!stubActionPerformed) {
|
|
193
427
|
let response;
|
|
194
428
|
try {
|
|
195
429
|
response = await route.fetch();
|
|
196
|
-
// debug("Matched item response", response);
|
|
197
430
|
}
|
|
198
431
|
catch (e) {
|
|
199
432
|
console.error("Fetch failed for", request.url(), e);
|
|
@@ -201,213 +434,79 @@ export async function registerBeforeStepRoutes(context, stepName, world) {
|
|
|
201
434
|
clearTimeout(tracking.timer);
|
|
202
435
|
return route.abort();
|
|
203
436
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
const
|
|
207
|
-
!headers["content-type"]?.includes("text") &&
|
|
208
|
-
!headers["content-type"]?.includes("application/csv");
|
|
209
|
-
// debug("Matched item isBinary", isBinary);
|
|
210
|
-
const isJSON = headers["content-type"]?.includes("application/json") || headers["content-type"]?.includes("json")
|
|
211
|
-
? true
|
|
212
|
-
: false;
|
|
213
|
-
// debug("Matched item isJSON", isJSON);
|
|
214
|
-
let body;
|
|
215
|
-
if (isBinary) {
|
|
216
|
-
body = await response.body(); // returns a Buffer
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
body = await response.text();
|
|
220
|
-
}
|
|
437
|
+
const headers = response.headers();
|
|
438
|
+
const isBinary = !headers["content-type"]?.includes("application/json") && !headers["content-type"]?.includes("text");
|
|
439
|
+
const body = isBinary ? await response.body() : await response.text();
|
|
221
440
|
let json;
|
|
222
441
|
try {
|
|
223
|
-
|
|
224
|
-
if (typeof body === "string") {
|
|
442
|
+
if (typeof body === "string")
|
|
225
443
|
json = JSON.parse(body);
|
|
226
|
-
}
|
|
227
444
|
}
|
|
228
445
|
catch (_) { }
|
|
446
|
+
const actionHandlerContext = {
|
|
447
|
+
route,
|
|
448
|
+
tracking,
|
|
449
|
+
status: response.status(),
|
|
450
|
+
body,
|
|
451
|
+
json,
|
|
452
|
+
isBinary,
|
|
453
|
+
finalBody: json ?? body,
|
|
454
|
+
abortActionPerformed: false,
|
|
455
|
+
};
|
|
229
456
|
const actionResults = [];
|
|
230
|
-
let abortActionPerformed = false;
|
|
231
|
-
let finalBody = isJSON && json ? json : body;
|
|
232
|
-
// debug("Matched item actions", matchedItem.actions);
|
|
233
457
|
for (const action of matchedItem.actions) {
|
|
234
|
-
let
|
|
235
|
-
const description = JSON.stringify(action.config);
|
|
458
|
+
let result;
|
|
236
459
|
switch (action.type) {
|
|
237
460
|
case "abort_request":
|
|
238
|
-
|
|
239
|
-
clearTimeout(tracking.timer);
|
|
240
|
-
const errorCode = action.config?.errorCode ?? "failed";
|
|
241
|
-
console.log(`[abort_request] Aborting with error code: ${errorCode}`);
|
|
242
|
-
await route.abort(errorCode);
|
|
243
|
-
abortActionPerformed = true;
|
|
244
|
-
tracking.completed = true;
|
|
461
|
+
result = handleAbortRequest(action, actionHandlerContext);
|
|
245
462
|
break;
|
|
246
463
|
case "status_code_verification":
|
|
247
|
-
|
|
248
|
-
actionStatus = "fail";
|
|
249
|
-
message = `Status code verification failed. Expected ${action.config}, got ${status}`;
|
|
250
|
-
debug(`[status_code_verification] Failed: ${message}`);
|
|
251
|
-
}
|
|
252
|
-
else {
|
|
253
|
-
console.log(`[status_code_verification] Passed`);
|
|
254
|
-
message = `Status code verification passed. Expected ${action.config}, got ${status}`;
|
|
255
|
-
}
|
|
464
|
+
result = handleStatusCodeVerification(action, actionHandlerContext);
|
|
256
465
|
break;
|
|
257
466
|
case "json_modify":
|
|
258
|
-
|
|
259
|
-
actionStatus = "fail";
|
|
260
|
-
message = "JSON modification failed. Response is not JSON";
|
|
261
|
-
debug(`[json_modify] Failed: ${message}`);
|
|
262
|
-
}
|
|
263
|
-
else {
|
|
264
|
-
if (action.config && action.config.path && action.config.modifyValue) {
|
|
265
|
-
objectPath.set(json, action.config.path, action.config.modifyValue);
|
|
266
|
-
console.log(`[json_modify] Modified path ${action.config.path} to ${action.config.modifyValue}`);
|
|
267
|
-
console.log(`[json_modify] Modified JSON`);
|
|
268
|
-
message = `JSON modified successfully`;
|
|
269
|
-
finalBody = JSON.parse(JSON.stringify(json));
|
|
270
|
-
}
|
|
271
|
-
}
|
|
467
|
+
result = handleJsonModify(action, actionHandlerContext);
|
|
272
468
|
break;
|
|
273
469
|
case "json_whole_modify":
|
|
274
|
-
|
|
275
|
-
actionStatus = "fail";
|
|
276
|
-
message = "JSON modification failed. Response is not JSON";
|
|
277
|
-
debug(`[json_whole_modify] Failed: ${message}`);
|
|
278
|
-
}
|
|
279
|
-
else {
|
|
280
|
-
try {
|
|
281
|
-
const parsedConfig = JSON.parse(action.config);
|
|
282
|
-
json = parsedConfig;
|
|
283
|
-
finalBody = JSON.parse(JSON.stringify(json));
|
|
284
|
-
}
|
|
285
|
-
catch (e) {
|
|
286
|
-
actionStatus = "fail";
|
|
287
|
-
message = `JSON modification failed. Invalid JSON: ${e instanceof Error ? e.message : String(e)}`;
|
|
288
|
-
debug(`[json_whole_modify] Failed: ${message}`);
|
|
289
|
-
break;
|
|
290
|
-
}
|
|
291
|
-
console.log(`[json_whole_modify] Whole JSON replaced`);
|
|
292
|
-
message = `JSON replaced successfully`;
|
|
293
|
-
}
|
|
470
|
+
result = handleJsonWholeModify(action, actionHandlerContext);
|
|
294
471
|
break;
|
|
295
472
|
case "status_code_change":
|
|
296
|
-
|
|
297
|
-
console.log(`[status_code_change] Status changed to ${status}`);
|
|
298
|
-
message = `Status code changed to ${status}`;
|
|
473
|
+
result = handleStatusCodeChange(action, actionHandlerContext);
|
|
299
474
|
break;
|
|
300
475
|
case "change_text":
|
|
301
|
-
|
|
302
|
-
actionStatus = "fail";
|
|
303
|
-
message = "Change text action failed. Body is not a text";
|
|
304
|
-
debug(`[change_text] Failed: ${message}`);
|
|
305
|
-
}
|
|
306
|
-
else {
|
|
307
|
-
body = action.config;
|
|
308
|
-
console.log(`[change_text] HTML body replaced`);
|
|
309
|
-
message = `HTML body replaced successfully`;
|
|
310
|
-
finalBody = body;
|
|
311
|
-
}
|
|
476
|
+
result = handleChangeText(action, actionHandlerContext);
|
|
312
477
|
break;
|
|
313
478
|
case "assert_json":
|
|
314
|
-
|
|
315
|
-
actionStatus = "fail";
|
|
316
|
-
message = "JSON assertion failed. Response is not JSON";
|
|
317
|
-
debug(`[assert_json] Failed: ${message}`);
|
|
318
|
-
}
|
|
319
|
-
else {
|
|
320
|
-
const actual = objectPath.get(json, action.config.path);
|
|
321
|
-
if (typeof actual !== "object") {
|
|
322
|
-
if (JSON.stringify(actual) !== JSON.stringify(action.config.expectedValue)) {
|
|
323
|
-
actionStatus = "fail";
|
|
324
|
-
message = `JSON assertion failed for path ${action.config.path}: expected ${JSON.stringify(action.config.expectedValue)}, got ${JSON.stringify(actual)}`;
|
|
325
|
-
debug(`[assert_json] Failed: ${message}`);
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
else if (JSON.stringify(actual) !== action.config.expectedValue) {
|
|
329
|
-
actionStatus = "fail";
|
|
330
|
-
message = `JSON assertion failed for path ${action.config.path}: expected ${action.config.expectedValue}, got ${JSON.stringify(actual)}`;
|
|
331
|
-
debug(`[assert_json] Failed: ${message}`);
|
|
332
|
-
}
|
|
333
|
-
else {
|
|
334
|
-
console.log(`[assert_json] Assertion passed for path ${action.config.path}`);
|
|
335
|
-
message = `JSON assertion passed for path ${action.config.path}`;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
479
|
+
result = handleAssertJson(action, actionHandlerContext);
|
|
338
480
|
break;
|
|
339
481
|
case "assert_whole_json":
|
|
340
|
-
|
|
341
|
-
actionStatus = "fail";
|
|
342
|
-
message = "Whole JSON assertion failed. Response is not JSON";
|
|
343
|
-
debug(`[assert_whole_json] Failed: ${message}`);
|
|
344
|
-
}
|
|
345
|
-
else {
|
|
346
|
-
if (action.config.contains) {
|
|
347
|
-
const originalJSON = JSON.stringify(json, null, 2);
|
|
348
|
-
if (!originalJSON.includes(action.config.contains)) {
|
|
349
|
-
actionStatus = "fail";
|
|
350
|
-
message = `Whole JSON assertion failed. Expected to contain: "${action.config.contains}", actual: "${body}"`;
|
|
351
|
-
debug(`[assert_whole_json] Failed: ${message}`);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
else if (action.config.equals) {
|
|
355
|
-
const originalJSON = JSON.stringify(json, null, 2);
|
|
356
|
-
if (originalJSON !== action.config.equals) {
|
|
357
|
-
actionStatus = "fail";
|
|
358
|
-
message = `Whole JSON assertion failed. Expected exact match: "${action.config.equals}", actual: "${body}"`;
|
|
359
|
-
debug(`[assert_whole_json] Failed: ${message}`);
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
else {
|
|
363
|
-
console.log(`[assert_whole_json] Assertion passed`);
|
|
364
|
-
message = `Whole JSON assertion passed.`;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
482
|
+
result = handleAssertWholeJson(action, actionHandlerContext);
|
|
367
483
|
break;
|
|
368
484
|
case "assert_text":
|
|
369
|
-
|
|
370
|
-
console.error(`[assert_text] Body is not text`);
|
|
371
|
-
actionStatus = "fail";
|
|
372
|
-
message = "Text assertion failed. Body is not text";
|
|
373
|
-
debug(`[assert_text] Failed: ${message}`);
|
|
374
|
-
}
|
|
375
|
-
else {
|
|
376
|
-
if (action.config.contains && !body.includes(action.config.contains)) {
|
|
377
|
-
actionStatus = "fail";
|
|
378
|
-
message = `Text assertion failed. Expected to contain: "${action.config.contains}", actual: "${body}"`;
|
|
379
|
-
debug(`[assert_text] Failed: ${message}`);
|
|
380
|
-
}
|
|
381
|
-
else if (action.config.equals && body !== action.config.equals) {
|
|
382
|
-
actionStatus = "fail";
|
|
383
|
-
message = `Text assertion failed. Expected exact match: "${action.config.equals}", actual: "${body}"`;
|
|
384
|
-
debug(`[assert_text] Failed: ${message}`);
|
|
385
|
-
}
|
|
386
|
-
else {
|
|
387
|
-
console.log(`[assert_text] Assertion passed`);
|
|
388
|
-
message = `Text assertion passed.`;
|
|
389
|
-
}
|
|
390
|
-
}
|
|
485
|
+
result = handleAssertText(action, actionHandlerContext);
|
|
391
486
|
break;
|
|
392
487
|
default:
|
|
393
|
-
console.warn(`Unknown action type
|
|
488
|
+
console.warn(`Unknown action type`);
|
|
394
489
|
}
|
|
395
|
-
|
|
396
|
-
|
|
490
|
+
if (result)
|
|
491
|
+
actionResults.push(result);
|
|
397
492
|
}
|
|
398
493
|
tracking.completed = true;
|
|
399
494
|
tracking.actionResults = actionResults;
|
|
400
495
|
if (tracking.timer)
|
|
401
496
|
clearTimeout(tracking.timer);
|
|
402
|
-
if (!abortActionPerformed) {
|
|
497
|
+
if (!actionHandlerContext.abortActionPerformed) {
|
|
403
498
|
try {
|
|
499
|
+
const isJSON = headers["content-type"]?.includes("application/json");
|
|
404
500
|
if (isJSON) {
|
|
405
|
-
await route.fulfill({ status, json: finalBody, headers });
|
|
501
|
+
await route.fulfill({ status: actionHandlerContext.status, json: actionHandlerContext.finalBody, headers });
|
|
406
502
|
}
|
|
407
503
|
else {
|
|
408
|
-
await route.fulfill({
|
|
504
|
+
await route.fulfill({
|
|
505
|
+
status: actionHandlerContext.status,
|
|
506
|
+
body: actionHandlerContext.finalBody,
|
|
507
|
+
headers,
|
|
508
|
+
});
|
|
409
509
|
}
|
|
410
|
-
// await route.fulfill({ status, body: finalBody, headers });
|
|
411
510
|
}
|
|
412
511
|
catch (e) {
|
|
413
512
|
console.error("Failed to fulfill route:", e);
|