Skip to content

Commit

Permalink
Add delay option to api requests
Browse files Browse the repository at this point in the history
  • Loading branch information
sudharsan-selvaraj committed Jan 25, 2024
1 parent 1a2cc01 commit 5c79393
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "appium-interceptor",
"version": "1.0.0-beta.6",
"version": "1.0.0-beta.7",
"description": "Appium 2.0 plugin to mock api calls for android apps",
"main": "./lib/index.js",
"scripts": {
Expand Down Expand Up @@ -29,6 +29,10 @@
"email": "[email protected]"
}
],
"bugs": {
"url": "https://github.com/AppiumTestDistribution/appium-interceptor-plugin/issues"
},
"homepage": "https://github.com/AppiumTestDistribution/appium-interceptor-plugin#readme",
"license": "ISC",
"peerDependencies": {
"appium": "^2.4.1"
Expand Down
2 changes: 2 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { configureWifiProxy, isRealDevice } from './utils/adb';
import { cleanUpProxyServer, sanitizeMockConfig, setupProxyServer } from './utils/proxy';
import proxyCache from './proxy-cache';
import logger from './logger';
import log from './logger';

export class AppiumInterceptorPlugin extends BasePlugin {
static executeMethodMap = {
Expand Down Expand Up @@ -145,6 +146,7 @@ export class AppiumInterceptorPlugin extends BasePlugin {
throw new Error('Proxy is not active for current session');
}

log.info(`Adding listener with config ${config}`);
return proxy?.addSniffer(config);
}

Expand Down
15 changes: 10 additions & 5 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
modifyRequestHeaders,
modifyRequestUrl,
modifyResponseBody,
parseJson,
sleep,
} from './utils/proxy';
import ResponseDecoder from './response-decoder';
import { Mock } from './mock';
Expand Down Expand Up @@ -111,7 +113,8 @@ export class Proxy {

public addSniffer(sniffConfg: SniffConfig): string {
const id = uuid();
this.sniffers.set(id, new ApiSniffer(id, sniffConfg));
const parsedConfig = !sniffConfg ? {} : parseJson(sniffConfg);
this.sniffers.set(id, new ApiSniffer(id, parsedConfig));
return id;
}

Expand All @@ -132,7 +135,7 @@ export class Proxy {
const matchedMocks = await this.findMatchingMocks(ctx);
if (matchedMocks.length) {
const compiledMock = compileMockConfig(matchedMocks);
this.performMockResponse(ctx, compiledMock, next);
this.applyMockToRequest(ctx, compiledMock, next);
} else {
next();
}
Expand Down Expand Up @@ -165,9 +168,11 @@ export class Proxy {
return matchedMocks;
}

private performMockResponse(ctx: IContext, mockConfig: MockConfig, next: () => void): void {
private async applyMockToRequest(ctx: IContext, mockConfig: MockConfig, next: () => void) {
ctx.use(ResponseDecoder);

if (mockConfig.delay) {
await sleep(mockConfig.delay);
}
this.modifyClientRequest(ctx, mockConfig);
this.modifyClientResponse(ctx, mockConfig, next);
}
Expand All @@ -178,7 +183,7 @@ export class Proxy {
modifyRequestBody(ctx, mockConfig);
}

private modifyClientResponse(ctx: IContext, mockConfig: MockConfig, next: () => void): void {
private async modifyClientResponse(ctx: IContext, mockConfig: MockConfig, next: () => void) {
if (mockConfig.statusCode && mockConfig.responseBody) {
ctx.proxyToClientResponse.writeHead(mockConfig.statusCode);
ctx.proxyToClientResponse.end(mockConfig.responseBody);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type MockConfig = {
responseHeaders?: HttpHeader;
responseBody?: string;
updateResponseBody?: UpdateBodySpec[];
delay?: number;
};

export type SniffConfig = {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,7 @@ export function sanitizeMockConfig(config: MockConfig) {

return config;
}

export function sleep(timeMs: number) {
return new Promise((resolve) => setTimeout(resolve, timeMs));
}

0 comments on commit 5c79393

Please sign in to comment.