Skip to content

Commit b921512

Browse files
test(e2e): harden android proxy cleanup and allowlist probes
Co-authored-by: javiergarciavera <javiergarciavera@users.noreply.github.com>
1 parent 8a66047 commit b921512

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

tests/api-mocking/MockServerE2E.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,43 @@ const translateFallbackPortToActual = (url: string): string => {
123123
}
124124
};
125125

126+
/**
127+
* Some app code probes the command queue debug endpoint even when the command queue
128+
* server is not enabled for the current test. When device proxying is active, those
129+
* probes are routed through MockServer and would fail with fetch errors.
130+
*
131+
* Treat this endpoint as optional and return an empty JSON response when no
132+
* command queue server port is allocated for the test.
133+
*/
134+
const isOptionalCommandQueueDebugEndpoint = (url: string): boolean => {
135+
try {
136+
const parsedUrl = new URL(url);
137+
const isLocalhost =
138+
parsedUrl.hostname === 'localhost' ||
139+
parsedUrl.hostname === '127.0.0.1' ||
140+
parsedUrl.hostname === '10.0.2.2';
141+
142+
if (!isLocalhost) {
143+
return false;
144+
}
145+
146+
if (parsedUrl.port !== FALLBACK_COMMAND_QUEUE_SERVER_PORT.toString()) {
147+
return false;
148+
}
149+
150+
if (parsedUrl.pathname !== '/debug.json') {
151+
return false;
152+
}
153+
154+
const commandQueuePort = PortManager.getInstance().getPort(
155+
ResourceType.COMMAND_QUEUE_SERVER,
156+
);
157+
return commandQueuePort === undefined;
158+
} catch {
159+
return false;
160+
}
161+
};
162+
126163
const isUrlAllowed = (url: string): boolean => {
127164
try {
128165
if (ALLOWLISTED_URLS.includes(url)) {
@@ -400,6 +437,9 @@ export default class MockServerE2E implements Resource {
400437
}
401438

402439
try {
440+
if (isOptionalCommandQueueDebugEndpoint(updatedUrl)) {
441+
return { statusCode: 200, body: '{}' };
442+
}
403443
return await handleDirectFetch(
404444
updatedUrl,
405445
method,
@@ -492,6 +532,9 @@ export default class MockServerE2E implements Resource {
492532
}
493533

494534
try {
535+
if (isOptionalCommandQueueDebugEndpoint(translatedUrl)) {
536+
return { statusCode: 200, body: '{}' };
537+
}
495538
// Read body safely before passing to handleDirectFetch to catch abort errors
496539
const bodyText = await safeGetBodyText(request);
497540
// If body read was aborted, return 499 (client closed request)

tests/api-mocking/mock-e2e-allowlist.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export const ALLOWLISTED_HOSTS = [
3232
export const ALLOWLISTED_URLS = [
3333
// Temporarily allow existing live requests during migration
3434
'https://clients3.google.com/generate_204',
35+
'http://connectivitycheck.gstatic.com/generate_204',
36+
'http://play.googleapis.com/generate_204',
37+
'http://www.google.com/gen_204',
3538
'https://api.avax.network/ext/bc/C/rpc',
3639
// Token SVGs in notifications list
3740
'https://raw.githubusercontent.com/MetaMask/contract-metadata/master/images/usdc.svg',

tests/framework/fixtures/FixtureHelper.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,17 @@ export async function withFixtures(
690690
}
691691
}
692692

693+
// Disable device proxy before RN reload to avoid interfering with Detox transport
694+
// during teardown/reconnect.
695+
if (shouldEnableDeviceNetworkProxy) {
696+
try {
697+
await disableDeviceTrafficProxy();
698+
} catch (cleanupError) {
699+
logger.error('Error disabling device network proxy:', cleanupError);
700+
cleanupErrors.push(cleanupError as Error);
701+
}
702+
}
703+
693704
// skipReactNativeReload needs to happen before killing the mock server to avoid race conditions
694705
if (!skipReactNativeReload) {
695706
try {
@@ -754,15 +765,6 @@ export async function withFixtures(
754765
}
755766
}
756767

757-
if (shouldEnableDeviceNetworkProxy) {
758-
try {
759-
await disableDeviceTrafficProxy();
760-
} catch (cleanupError) {
761-
logger.error('Error disabling device network proxy:', cleanupError);
762-
cleanupErrors.push(cleanupError as Error);
763-
}
764-
}
765-
766768
// Remove the abort filter AFTER all cleanup is complete so late async
767769
// "Aborted" rejections from destroyed sockets are still caught.
768770
if (mockServerInstance) {

0 commit comments

Comments
 (0)