Skip to content

Commit

Permalink
Multple mock events
Browse files Browse the repository at this point in the history
  • Loading branch information
Andepande committed Oct 15, 2024
1 parent 933d577 commit bc17d66
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 21 deletions.
32 changes: 17 additions & 15 deletions e2e/mockServer/mockServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@ import portfinder from 'portfinder';

const mockServer = getLocal();

export const startMockServer = async ({
mockUrl,
responseCode = 500,
responseBody = {},
port,
}) => {
if (!mockUrl) throw new Error('The mockUrl parameter is required');

export const startMockServer = async (events, port) => {
// Find an available port
port = port || await portfinder.getPortPromise();

await mockServer.start(port);
console.log(`Mockttp server running at http://localhost:${port}`);

Check failure on line 11 in e2e/mockServer/mockServer.js

View workflow job for this annotation

GitHub Actions / scripts (lint)

Unexpected console statement

await mockServer
.forGet('/health-check')
.thenReply(200, 'Mock server is running');
// Health check endpoint
await mockServer.forGet('/health-check').thenReply(200, 'Mock server is running');

// Set up mock events
for (const event of events) {
const { mockUrl, responseCode, responseBodyFile } = event;
let responseBody;

// Dynamically import the response body from the file
if (responseBodyFile) {
responseBody = (await import(responseBodyFile)).default; // Adjust based on your exports
} else {
responseBody = {};
}

await mockServer
.forGet('/proxy')
.withQuery({ url: mockUrl })
.thenReply(responseCode, JSON.stringify(responseBody));
await mockServer.forGet(mockUrl).thenReply(responseCode, JSON.stringify(responseBody));
}

await mockServer.forUnmatchedRequest().thenPassThrough({
beforeRequest: async ({ url, method }) => {
Expand Down
20 changes: 20 additions & 0 deletions e2e/specs/mock-data/mock-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const mockEvents = {
suggestedGasApiSuccessResponse: {
mockUrl: 'https://gas.api.cx.metamask.io/networks/1/suggestedGasFees',
responseCode: 200,
responseBodyFile: '../responseBodies/suggestedGasApi',
responseType: 'success',
},
suggestedGasApiErrorResponse: {
mockUrl: 'https://gas.api.cx.metamask.io/networks/1/suggestedGasFees',
responseCode: 500,
responseBodyFile: '../responseBodies/suggestedGasApi',
responseType: 'error',
},
suggestedGasApiSlowResponse: {
mockUrl: 'https://gas.api.cx.metamask.io/networks/1/suggestedGasFees',
responseCode: 200,
responseBodyFile: '../responseBodies/suggestedGasApi',
responseType: 'slowResponse',
},
};
18 changes: 18 additions & 0 deletions e2e/specs/mock-data/mock-responses/suggestGasApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const success = {
success: true,
data: {
gasFee: '100 Gwei',
},
};
export const error = {
error: 'Internal server error',
message: 'Unable to fetch suggested gas fees.',
};
export const slowResponse = {
success: true,
data: {
gasFee: '150 Gwei',
},
delay: 5000, // Simulate delay
};

File renamed without changes.
11 changes: 5 additions & 6 deletions e2e/specs/mock-specs/suggestedGasApi.mock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@ import Accounts from '../../../wdio/helpers/Accounts.js';
import { withFixtures } from '../../fixtures/fixture-helper.js';
import FixtureBuilder from '../../fixtures/fixture-builder.js';
import TestHelpers from '../../helpers.js';
import { urls } from '../../mockServer/mockUrlCollection.json';
import { urls } from '../mock-data/mockUrlCollection.json';
import SuccessImportAccountView from '../../pages/importAccount/SuccessImportAccountView.js';
import { mockEvents } from '../mock-data/mock-events.js';

describe(SmokeCore('Mock suggestedGasApi fallback to legacy gas endpoint when EIP1559 endpoint is down'), () => {
let mockServer;
beforeAll(async () => {
jest.setTimeout(150000);
await TestHelpers.reverseServerPort();

mockServer = await startMockServer({ // Configure mock server
mockUrl: urls.suggestedGasApiMainnet,
responseCode: 500,
responseBody: { error: 'Internal server error' },
});
mockServer = await startMockServer([
mockEvents.suggestedGasApiSuccessResponse // Multiple events can be passed through this
]);
});

// Because we stop the server within the test, a try catch block here would stop the server if the test fails midway
Expand Down

0 comments on commit bc17d66

Please sign in to comment.