diff --git a/package-lock.json b/package-lock.json index 357a827..7200670 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "appium-interceptor", - "version": "1.0.0-beta.6", + "version": "1.0.0-beta.7", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "appium-interceptor", - "version": "1.0.0-beta.6", + "version": "1.0.0-beta.7", "license": "ISC", "dependencies": { "@appium/support": "^4.1.11", diff --git a/package.json b/package.json index 37c08bf..1083e5d 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -29,6 +29,10 @@ "email": "sudharsanselvaraj.c0@gmail.com" } ], + "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" diff --git a/src/plugin.ts b/src/plugin.ts index 4500823..4437674 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -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 = { @@ -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); } diff --git a/src/proxy.ts b/src/proxy.ts index 33a2e34..39efdb7 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -11,6 +11,8 @@ import { modifyRequestHeaders, modifyRequestUrl, modifyResponseBody, + parseJson, + sleep, } from './utils/proxy'; import ResponseDecoder from './response-decoder'; import { Mock } from './mock'; @@ -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; } @@ -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(); } @@ -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); } @@ -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); diff --git a/src/types.ts b/src/types.ts index 8554ae1..2d75668 100644 --- a/src/types.ts +++ b/src/types.ts @@ -36,6 +36,7 @@ export type MockConfig = { responseHeaders?: HttpHeader; responseBody?: string; updateResponseBody?: UpdateBodySpec[]; + delay?: number; }; export type SniffConfig = { diff --git a/src/utils/proxy.ts b/src/utils/proxy.ts index 9fd589e..5feb1e3 100644 --- a/src/utils/proxy.ts +++ b/src/utils/proxy.ts @@ -304,3 +304,7 @@ export function sanitizeMockConfig(config: MockConfig) { return config; } + +export function sleep(timeMs: number) { + return new Promise((resolve) => setTimeout(resolve, timeMs)); +}