-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathmockHelpers.ts
More file actions
696 lines (624 loc) · 21.3 KB
/
mockHelpers.ts
File metadata and controls
696 lines (624 loc) · 21.3 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
import type { Mockttp, MockttpServer } from 'mockttp';
import _ from 'lodash';
import { createLogger, LogLevel } from '../../framework/logger.ts';
import type {
MockApiEndpoint,
MockEventsObject,
} from '../../framework/types.ts';
import { sleep } from '../../framework/Utilities.ts';
import { getDecodedProxiedURL } from '../../smoke/notifications/utils/helpers.ts';
import { safeGetBodyText } from '../MockServerE2E.ts';
// Creates a logger with INFO level as the mockServer produces too much noise
// Change this to DEBUG as needed (required to see `waitForProxiedRequestsMatching` dumps)
const logger = createLogger({
name: 'TestSpecificMockHelpers',
level: LogLevel.INFO,
});
interface ResponseParam {
requestMethod: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD';
url: string | RegExp;
response: unknown;
responseCode: number;
}
export interface PostRequestMatchingOptions {
ignoreFields?: string[];
allowPartialMatch?: boolean;
}
export interface PostRequestMatchResult {
matches: boolean;
error?: string;
requestBodyJson?: MockApiEndpoint['requestBody'];
}
/**
* Processes and validates POST request body against expected request body
* This is the unified logic extracted from mock-server.js
*
* @param requestBodyText - Raw request body text
* @param expectedRequestBody - Expected request body object to match against
* @param options - Options for matching behavior
* @returns Match result with validation status
*/
export const processPostRequestBody = (
requestBodyText: string | undefined,
expectedRequestBody: MockApiEndpoint['requestBody'],
options: PostRequestMatchingOptions = {},
): PostRequestMatchResult => {
const { ignoreFields = [], allowPartialMatch = true } = options;
// Handle missing request body
if (!requestBodyText) {
return {
matches: false,
error: 'Missing request body',
};
}
let requestBodyJson: MockApiEndpoint['requestBody'] | undefined;
try {
requestBodyJson = JSON.parse(requestBodyText);
} catch (e) {
return {
matches: false,
error: 'Invalid request body JSON',
};
}
// If no expected body specified, consider it a match
if (!expectedRequestBody) {
return {
matches: true,
requestBodyJson,
};
}
// Clone objects to avoid mutations (using lodash for consistency with mock-server.js)
const requestToCheck = _.cloneDeep(requestBodyJson);
const expectedRequest = _.cloneDeep(expectedRequestBody);
// Remove ignored fields from both objects for comparison
ignoreFields.forEach((field) => {
_.unset(requestToCheck, field);
_.unset(expectedRequest, field);
});
const matches = allowPartialMatch
? typeof requestToCheck === 'object' &&
requestToCheck !== null &&
typeof expectedRequest === 'object' &&
expectedRequest !== null
? _.isMatch(requestToCheck, expectedRequest)
: false
: _.isEqual(requestToCheck, expectedRequest);
if (!matches) {
logger.warn('Request body validation failed:');
logger.info('Expected:', JSON.stringify(expectedRequestBody, null, 2));
logger.info('Received:', JSON.stringify(requestBodyJson, null, 2));
logger.info(
'Differences:',
JSON.stringify(
_.differenceWith([requestBodyJson], [expectedRequestBody], _.isEqual),
null,
2,
),
);
return {
matches: false,
error: 'Request body validation failed',
requestBodyJson,
};
}
return {
matches: true,
requestBodyJson,
};
};
/**
* Finds a matching event from candidate events based on POST request body
* This implements the same logic as mock-server.js for finding the best match
*
* @param candidateEvents - Array of potential matching events
* @param requestBodyJson - Parsed request body JSON
* @returns The best matching event or undefined
*/
export const findMatchingPostEvent = (
candidateEvents: MockApiEndpoint[],
requestBodyJson: MockApiEndpoint['requestBody'],
): MockApiEndpoint | undefined => {
if (!candidateEvents.length) {
return undefined;
}
// Prefer events whose requestBody matches (respecting ignoreFields)
const matchingEvent = candidateEvents.find((event) => {
if (!event.requestBody || !requestBodyJson) return false;
const result = processPostRequestBody(
JSON.stringify(requestBodyJson),
event.requestBody,
{ ignoreFields: event.ignoreFields || [] },
);
return result.matches;
});
// Fallback to an event without a requestBody matcher
if (!matchingEvent) {
return candidateEvents.find((event) => !event.requestBody);
}
return matchingEvent;
};
export async function setupMockRequest(
server: Mockttp,
response: ResponseParam,
priority?: number,
) {
let requestRuleBuilder;
if (response.requestMethod === 'GET') {
requestRuleBuilder = server.forGet('/proxy');
}
if (response.requestMethod === 'POST') {
requestRuleBuilder = server.forPost('/proxy');
}
if (response.requestMethod === 'PUT') {
requestRuleBuilder = server.forPut('/proxy');
}
if (response.requestMethod === 'DELETE') {
requestRuleBuilder = server.forDelete('/proxy');
}
if (response.requestMethod === 'HEAD') {
requestRuleBuilder = server.forHead('/proxy');
}
const ruleBuilder = requestRuleBuilder
?.matching((request) => {
const url = getDecodedProxiedURL(request.url);
if (response.url instanceof RegExp) {
const matches = response.url.test(url);
return matches;
}
const matches = url.includes(String(response.url));
return matches;
})
.asPriority(priority ?? 999); // Adding priority to this mock request helper as we want TestSpecificMocks to always take precedence
// Use thenReply/thenJson instead of thenCallback to avoid mockttp's
// internal body buffering (waitForCompletedRequest → streamToBuffer).
// When clients abort connections (e.g., bridge controller AbortController),
// streamToBuffer rejects with Error('Aborted'). Since CallbackHandler
// eagerly buffers the body BEFORE calling the callback, the rejection
// propagates through mockttp's internals and reaches Jest as a test failure.
// SimpleHandler (used by thenReply/thenJson) never reads the request body,
// eliminating this error source entirely.
if (typeof response.response === 'string') {
await ruleBuilder?.thenReply(
response.responseCode ?? 200,
response.response,
);
} else {
await ruleBuilder?.thenJson(
response.responseCode ?? 200,
response.response as object,
);
}
}
/**
* Sets up a mock for Server-Sent Events (SSE) endpoints through the mobile proxy.
* Unlike {@link setupMockRequest}, this sends the response with
* `Content-Type: text/event-stream` so that SSE clients (e.g. expo/fetch)
* recognise the stream format.
*
* @param server - The mockttp server instance
* @param url - URL pattern to match (string or RegExp)
* @param sseBody - Pre-formatted SSE body (use `toSSEResponse()` to convert quote arrays)
* @param priority - Rule priority (default 999)
*/
export async function setupSSEMockRequest(
server: Mockttp,
url: string | RegExp,
sseBody: string,
priority = 999,
) {
await server
.forGet('/proxy')
.matching((request) => {
const decodedUrl = getDecodedProxiedURL(request.url);
if (url instanceof RegExp) {
return url.test(decodedUrl);
}
return decodedUrl.includes(String(url));
})
.asPriority(priority)
.thenReply(200, sseBody, {
'content-type': 'text/event-stream',
'cache-control': 'no-cache',
connection: 'keep-alive',
});
}
/**
* Helper to mock a POST request with complex body matching through the mobile proxy pattern
*
* @param mockServer - The mock server instance
* @param url - The URL to match - supports string or RegExp
* @param requestBody - Expected request body object to match against
* @param response - The response to return
* @param options - Additional options for matching and response
* @param options.statusCode - HTTP status code to return (default: 200)
* @param options.ignoreFields - Array of field paths to ignore during request body comparison
* @param options.priority - Set the rule priority. Any matching rule with a higher priority will always take precedence over a matching lower-priority rule, unless the higher rule has an explicit completion check (like .once()) that has already been completed. The RulePriority enum defines the standard values useful for most cases, but any positive number may be used for advanced configurations. (default: 999)
*/
export const setupMockPostRequest = async (
mockServer: Mockttp,
url: string | RegExp,
requestBody: unknown,
response: unknown,
options: {
statusCode?: number;
ignoreFields?: string[];
priority?: number;
} = {},
) => {
const { statusCode = 200, ignoreFields = [], priority = 999 } = options;
await mockServer
.forPost('/proxy')
.matching(async (request) => {
const decodedUrl = getDecodedProxiedURL(request.url);
// First check if URL matches
let urlMatches = false;
if (url instanceof RegExp) {
urlMatches = url.test(decodedUrl);
} else {
urlMatches = decodedUrl.includes(String(url));
}
if (!urlMatches) {
return false;
}
// If URL matches, also check if request body matches (ignoring specified fields)
// Use safeGetBodyText to handle abort errors gracefully
const requestBodyText = await safeGetBodyText(request);
// If body read failed/aborted, don't match this mock
if (requestBodyText === undefined) {
return false;
}
const result = processPostRequestBody(requestBodyText, requestBody, {
ignoreFields,
});
if (!result.matches) {
logger.warn('❌ Request body validation failed for', decodedUrl);
logger.debug('Expected:', requestBody);
logger.debug('Received:', result.requestBodyJson);
logger.debug('Ignored fields:', ignoreFields);
logger.debug('Error:', result.error);
}
return result.matches;
})
.asPriority(priority) // Adding priority to this mock request helper as we want TestSpecificMocks to always take precedence
.thenCallback(async (request) => {
const decodedUrl = getDecodedProxiedURL(request.url);
logger.info(`Mocking POST request to: ${decodedUrl}`);
logger.debug(`Returning response:`, response);
return {
statusCode,
json: response,
};
});
};
/**
* Helper to intercept and transform URLs through the mobile proxy pattern
* @param mockServer - The mock server instance
* @param urlMatcher - Function to match URLs that need transformation
* @param urlTransformer - Function to transform the URL
*/
export const interceptProxyUrl = async (
mockServer: Mockttp,
urlMatcher: (url: string) => boolean,
urlTransformer: (url: string) => string,
) => {
await mockServer
.forAnyRequest()
.matching((req) => {
if (!req.path.startsWith('/proxy')) return false;
const urlParam = new URL(req.url).searchParams.get('url');
return urlParam ? urlMatcher(urlParam) : false;
})
.thenCallback(async (request) => {
const urlParam = new URL(request.url).searchParams.get('url');
if (!urlParam) {
return { statusCode: 400, body: 'Missing url parameter' };
}
const transformedUrl = urlTransformer(urlParam);
const headers: Record<string, string> = {};
for (const [key, value] of Object.entries(request.headers)) {
if (typeof value === 'string') {
headers[key] = value;
}
}
// Use safeGetBodyText to handle abort errors gracefully
let bodyText: string | undefined;
if (request.method === 'POST') {
bodyText = await safeGetBodyText(request);
// If body read was aborted, return 499 (client closed request)
if (bodyText === undefined) {
return { statusCode: 499, body: '' };
}
}
try {
const response = await fetch(transformedUrl, {
method: request.method,
headers,
body: bodyText,
});
return {
statusCode: response.status,
body: await response.text(),
};
} catch (error) {
// Handle fetch errors (e.g., network issues)
logger.error('Error forwarding request:', error);
return {
statusCode: 502,
body: JSON.stringify({ error: 'Failed to forward request' }),
};
}
});
};
/**
* Sets up multiple mock events from a structured MockEventsObject definition
*
* @param mockServer - The mock server instance
* @param mockEvents - Object containing mock definitions grouped by method (GET, POST, etc.)
*/
export const setupMockEvents = async (
mockServer: Mockttp,
mockEvents: MockEventsObject,
): Promise<void> => {
for (const [method, mocks] of Object.entries(mockEvents)) {
if (!mocks) continue;
// Cast method to the expected type for setupMockRequest
// setupMockRequest will safely ignore unsupported methods if passed
const requestMethod = method as 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD';
for (const mock of mocks) {
// Special handling for POST requests with body matching
if (requestMethod === 'POST' && mock.requestBody) {
await setupMockPostRequest(
mockServer,
mock.urlEndpoint,
mock.requestBody,
mock.response,
{
statusCode: mock.responseCode,
ignoreFields: mock.ignoreFields,
priority: mock.priority,
},
);
} else {
// Standard handling for all other requests (and POSTs without body matching)
await setupMockRequest(
mockServer,
{
requestMethod,
url: mock.urlEndpoint,
response: mock.response,
responseCode: mock.responseCode,
},
mock.priority,
);
}
}
}
};
/**
* Registers a mock for GET accounts.api.cx.metamask.io/v2/supportedNetworks.
* Called globally from MockServerE2E so all E2E tests get this mock without per-test setup.
*/
export async function setupAccountsV2SupportedNetworksMock(
server: Mockttp,
): Promise<void> {
await setupMockRequest(server, {
requestMethod: 'GET',
url: /^https:\/\/accounts\.api\.cx\.metamask\.io\/v2\/supportedNetworks(\?.*)?$/,
response: {
fullSupport: [1, 137, 56, 59144, 8453, 10, 42161, 534352, 1329],
partialSupport: { balances: [42220, 43114] },
},
responseCode: 200,
});
}
/**
* Registers a default empty history response for Accounts API v4 transactions.
*/
export async function setupAccountsV4TransactionsMock(server: Mockttp) {
await setupMockRequest(server, {
requestMethod: 'GET',
url: /^https:\/\/accounts\.api\.cx\.metamask\.io\/v4\/multiaccount\/transactions(\?.*)?$/,
response: {
data: [],
pageInfo: {
count: 0,
hasNextPage: false,
},
},
responseCode: 200,
});
}
export interface SeenProxiedRequest {
method: string;
proxiedUrl: string;
}
export interface ProxiedRequestMatcher {
method?: string;
urlSubstring?: string;
urlRegex?: RegExp;
}
/**
* Stable string for logs and errors for {@link ProxiedRequestMatcher}.
* Unlike `JSON.stringify(matcher)`, serializes `urlRegex` as `/source/flags` instead of `{}`.
*/
export function formatProxiedRequestMatcher(
matcher: ProxiedRequestMatcher,
): string {
const record: Record<string, string> = {};
if (matcher.method !== undefined) {
record.method = matcher.method;
}
if (matcher.urlSubstring !== undefined) {
record.urlSubstring = matcher.urlSubstring;
}
if (matcher.urlRegex !== undefined) {
record.urlRegex = String(matcher.urlRegex);
}
return JSON.stringify(record);
}
/**
* Completed requests seen by mockttp rules, with the real target URL decoded from `/proxy?url=…`.
* Same pattern as `getEventsPayloads` in tests/helpers/analytics/helpers.ts (without MetaMetrics filtering).
*/
export async function collectSeenProxiedRequests(
mockServer: Mockttp | MockttpServer,
): Promise<SeenProxiedRequest[]> {
const mockedEndpoints = await mockServer.getMockedEndpoints();
const requests = (
await Promise.all(
mockedEndpoints.map((endpoint) => endpoint.getSeenRequests()),
)
).flat();
return requests.map((request) => ({
method: request.method,
proxiedUrl: getDecodedProxiedURL(request.url),
}));
}
export function filterProxiedRequests(
seen: SeenProxiedRequest[],
matcher: ProxiedRequestMatcher,
): SeenProxiedRequest[] {
return seen.filter((r) => {
if (matcher.method !== undefined && r.method !== matcher.method) {
return false;
}
if (
matcher.urlSubstring !== undefined &&
!r.proxiedUrl.includes(matcher.urlSubstring)
) {
return false;
}
if (
matcher.urlRegex !== undefined &&
!matcher.urlRegex.test(r.proxiedUrl)
) {
return false;
}
return true;
});
}
/**
* Count of proxied requests matching `matcher` (for baselines before a flow).
*/
export async function countProxiedRequestsMatching(
mockServer: Mockttp | MockttpServer,
matcher: ProxiedRequestMatcher,
): Promise<number> {
const seen = await collectSeenProxiedRequests(mockServer);
return filterProxiedRequests(seen, matcher).length;
}
export interface WaitForAdditionalProxiedRequestsOptions {
description: string;
/** Matching requests beyond baseline required (default 1). */
additional?: number;
timeout?: number;
interval?: number;
/**
* After a successful wait, logs `label: new count=<delta>` at INFO on `logger`.
* Delta is `matches.length - baselineMatchCount` from the same snapshot as the wait.
*/
successLog?: {
logger: { info: (message: string) => void };
label: string;
};
}
/**
* Waits until at least `baselineMatchCount + additional` proxied requests match,
* then optionally logs how many new matches appeared since baseline.
* Use with {@link countProxiedRequestsMatching} before the flow under test.
*/
export async function waitForAdditionalProxiedRequestsMatching(
mockServer: Mockttp | MockttpServer,
matcher: ProxiedRequestMatcher,
baselineMatchCount: number,
options: WaitForAdditionalProxiedRequestsOptions,
): Promise<SeenProxiedRequest[]> {
const additional = options.additional ?? 1;
const matches = await waitForProxiedRequestsMatching(mockServer, matcher, {
minCount: baselineMatchCount + additional,
description: options.description,
timeout: options.timeout,
interval: options.interval,
});
if (options.successLog) {
// Derive delta from `matches` (same snapshot as wait success) — avoids a second
// collectSeenProxiedRequests pass and TOCTOU inflation from requests after return.
const delta = matches.length - baselineMatchCount;
options.successLog.logger.info(
`${options.successLog.label}: new count=${String(delta)}`,
);
}
return matches;
}
/**
* Polls until at least `minCount` proxied requests match, or throws after timeout.
* Polls every `interval` ms (default 1000) for up to `timeout` ms (default 60000).
* On timeout only: logs the full proxied request list at DEBUG (see module LogLevel).
*/
export async function waitForProxiedRequestsMatching(
mockServer: Mockttp | MockttpServer,
matcher: ProxiedRequestMatcher,
options: {
minCount?: number;
timeout?: number;
interval?: number;
description: string;
},
): Promise<SeenProxiedRequest[]> {
const {
minCount = 1,
timeout = 60000,
interval = 1000,
description,
} = options;
const pollIntervalMs = Math.max(1, interval);
const startMs = Date.now();
logger.info(
`Polling proxied requests for "${description}" (matcher ${formatProxiedRequestMatcher(
matcher,
)}, minCount ${String(minCount)}, every ${String(
pollIntervalMs,
)}ms, up to ${String(timeout)}ms)`,
);
for (;;) {
const seen = await collectSeenProxiedRequests(mockServer);
const matches = filterProxiedRequests(seen, matcher);
if (matches.length >= minCount) {
const matchedRequestsDump = matches
.map(
(request, index) =>
`[${String(index + 1)}] ${request.method} ${request.proxiedUrl}`,
)
.join('\n');
logger.info(
`Matched proxied request(s) for "${description}": ${String(matches.length)} (required: ${String(minCount)})\n${matchedRequestsDump}`,
);
return matches;
}
const elapsedMs = Date.now() - startMs;
if (elapsedMs >= timeout) {
const seenRequestsDump = seen
.map(
(request, index) =>
`[${String(index + 1)}] ${request.method} ${request.proxiedUrl}`,
)
.join('\n');
logger.debug(
`Proxied requests mismatch for "${description}": expected >= ${String(minCount)} matching ${formatProxiedRequestMatcher(
matcher,
)}, got ${String(matches.length)} (total proxied rows: ${String(
seen.length,
)})\nSeen proxied requests:\n${seenRequestsDump || '(none)'}`,
);
throw new Error(
`Expected at least ${String(minCount)} proxied request(s) matching ${formatProxiedRequestMatcher(
matcher,
)}, got ${String(matches.length)} (total proxied rows: ${String(
seen.length,
)})`,
);
}
const remainingMs = timeout - elapsedMs;
await sleep(Math.min(pollIntervalMs, remainingMs));
}
}