Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'] },
Expand Down Expand Up @@ -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}`);
Expand Down
8 changes: 8 additions & 0 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
37 changes: 37 additions & 0 deletions test/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
55 changes: 55 additions & 0 deletions test/unit.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});