From b4005eac5fc5f23a1ebd9910d891d195c89275be Mon Sep 17 00:00:00 2001 From: Vincent DANTI Date: Thu, 18 Jun 2026 11:57:52 +0200 Subject: [PATCH] feat: Add interceptor:removeAllMocks command --- docs/commands.md | 26 +++++++++++++++++++++ src/plugin.ts | 9 ++++++++ src/proxy.ts | 8 +++++++ test/plugin.spec.js | 37 ++++++++++++++++++++++++++++++ test/unit.spec.ts | 55 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 135 insertions(+) create mode 100644 test/unit.spec.ts diff --git a/docs/commands.md b/docs/commands.md index efd79b6..f9a7cb7 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -134,6 +134,32 @@ Given a mockId return during addMock command, will remove the mock configuration // authorizationMock will not be active after this point and the test will proceed with normal flow ``` +### interceptor: removeAllMocks +Removes all registered mock configurations from the proxy server at once. + +#### Example: + +```javascript + await driver.execute("interceptor: addMock", { + config: { + url: "**/api/users/**", + statusCode: 400 + } + }); + + await driver.execute("interceptor: addMock", { + config: { + url: "**/api/login/**", + statusCode: 401 + } + }); + + // Perform actions under mock state... + + // Remove all mocks at once to return to normal flow + await driver.execute("interceptor: removeAllMocks"); +``` + ### interceptor: startListening Start listening for all network traffic (API calls) made by the device during a session diff --git a/src/plugin.ts b/src/plugin.ts index cfcfc7d..17805b7 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -37,6 +37,9 @@ export class AppiumInterceptorPlugin extends BasePlugin { command: 'removeMock', params: { required: ['id'] }, }, + 'interceptor: removeAllMocks': { + command: 'removeAllMocks', + }, 'interceptor: disableMock': { command: 'disableMock', params: { required: ['id'] }, @@ -178,6 +181,12 @@ export class AppiumInterceptorPlugin extends BasePlugin { proxy.removeMock(id); } + async removeAllMocks(_next: any, driver: any) { + const proxy = this.getSessionProxy(driver.sessionId); + log.debug(`[${driver.sessionId}] Removing all mock rules`); + proxy.removeAllMocks(); + } + async disableMock(_next: any, driver: any, id: string) { const proxy = this.getSessionProxy(driver.sessionId); log.debug(`[${driver.sessionId}] Disabling mock rule with ID: ${id}`); diff --git a/src/proxy.ts b/src/proxy.ts index 9613991..07d0e9f 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -201,6 +201,14 @@ export class Proxy { this.mocks.delete(id); } + public removeAllMocks(): void { + this.mocks.clear(); + } + + public getMockCount(): number { + return this.mocks.size; + } + public enableMock(id: string): void { this.mocks.get(id)?.setEnableStatus(true); } diff --git a/test/plugin.spec.js b/test/plugin.spec.js index f14e8ed..729cacc 100644 --- a/test/plugin.spec.js +++ b/test/plugin.spec.js @@ -127,6 +127,43 @@ describe('Plugin Test', () => { expect(page.includes('Error')).to.be.true; }); + it('Should be able to remove all mocks at once', async () => { + const mockId = await driver.execute('interceptor: addMock', { + config: { + url: '/api/users?.*', + responseBody: JSON.stringify({ + page: 1, + per_page: 1, + total: 1, + total_pages: 1, + data: [ + { + id: 1, + email: 'allmocksremoved.test@reqres.in', + first_name: 'Removed', + last_name: 'Mocks', + }, + ], + }), + }, + }); + expect(mockId).to.not.be.null; + + // Verify mock is active + const el1 = await driver.$('xpath://android.widget.TextView[@text="Get User List"]'); + await el1.click(); + let page = await driver.getPageSource(); + expect(page.includes('allmocksremoved.test@reqres.in')).to.be.true; + + // Now remove all mocks + await driver.execute('interceptor: removeAllMocks'); + + // Click again and verify mock is no longer applied + await el1.click(); + page = await driver.getPageSource(); + expect(page.includes('allmocksremoved.test@reqres.in')).to.be.false; + }); + afterEach(async () => { await driver.deleteSession(); }); diff --git a/test/unit.spec.ts b/test/unit.spec.ts new file mode 100644 index 0000000..ba0af55 --- /dev/null +++ b/test/unit.spec.ts @@ -0,0 +1,55 @@ +import { expect } from 'chai'; +import { Proxy } from '../src/proxy'; +import { AppiumInterceptorPlugin } from '../src/plugin'; +import proxyCache from '../src/proxy-cache'; + +describe('Unit Tests - removeAllMocks', () => { + it('should successfully add and remove all mocks from Proxy', () => { + const proxy = new Proxy({ + deviceUDID: 'test-udid', + sessionId: 'test-session', + certificatePath: 'test-cert-path', + port: 12345, + ip: '127.0.0.1', + }); + + expect(proxy.getMockCount()).to.equal(0); + + const mockId1 = proxy.addMock({ url: '/api/test1' }); + const mockId2 = proxy.addMock({ url: '/api/test2' }); + + expect(proxy.getMockCount()).to.equal(2); + + proxy.removeAllMocks(); + + expect(proxy.getMockCount()).to.equal(0); + }); + + it('should successfully call removeAllMocks from AppiumInterceptorPlugin command', async () => { + const plugin = new AppiumInterceptorPlugin('interceptor', {}); + const proxy = new Proxy({ + deviceUDID: 'test-udid', + sessionId: 'test-session', + certificatePath: 'test-cert-path', + port: 12345, + ip: '127.0.0.1', + }); + + proxyCache.add('test-session', proxy); + + const driver = { + sessionId: 'test-session', + }; + + await plugin.addMock(null, driver, { url: '/api/test1' }); + await plugin.addMock(null, driver, { url: '/api/test2' }); + + expect(proxy.getMockCount()).to.equal(2); + + await plugin.removeAllMocks(null, driver); + + expect(proxy.getMockCount()).to.equal(0); + + proxyCache.remove('test-session'); + }); +});