Skip to content

Commit 5c79393

Browse files
Add delay option to api requests
1 parent 1a2cc01 commit 5c79393

File tree

6 files changed

+24
-8
lines changed

6 files changed

+24
-8
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "appium-interceptor",
3-
"version": "1.0.0-beta.6",
3+
"version": "1.0.0-beta.7",
44
"description": "Appium 2.0 plugin to mock api calls for android apps",
55
"main": "./lib/index.js",
66
"scripts": {
@@ -29,6 +29,10 @@
2929
"email": "sudharsanselvaraj.c0@gmail.com"
3030
}
3131
],
32+
"bugs": {
33+
"url": "https://github.com/AppiumTestDistribution/appium-interceptor-plugin/issues"
34+
},
35+
"homepage": "https://github.com/AppiumTestDistribution/appium-interceptor-plugin#readme",
3236
"license": "ISC",
3337
"peerDependencies": {
3438
"appium": "^2.4.1"

src/plugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { configureWifiProxy, isRealDevice } from './utils/adb';
77
import { cleanUpProxyServer, sanitizeMockConfig, setupProxyServer } from './utils/proxy';
88
import proxyCache from './proxy-cache';
99
import logger from './logger';
10+
import log from './logger';
1011

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

149+
log.info(`Adding listener with config ${config}`);
148150
return proxy?.addSniffer(config);
149151
}
150152

src/proxy.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
modifyRequestHeaders,
1212
modifyRequestUrl,
1313
modifyResponseBody,
14+
parseJson,
15+
sleep,
1416
} from './utils/proxy';
1517
import ResponseDecoder from './response-decoder';
1618
import { Mock } from './mock';
@@ -111,7 +113,8 @@ export class Proxy {
111113

112114
public addSniffer(sniffConfg: SniffConfig): string {
113115
const id = uuid();
114-
this.sniffers.set(id, new ApiSniffer(id, sniffConfg));
116+
const parsedConfig = !sniffConfg ? {} : parseJson(sniffConfg);
117+
this.sniffers.set(id, new ApiSniffer(id, parsedConfig));
115118
return id;
116119
}
117120

@@ -132,7 +135,7 @@ export class Proxy {
132135
const matchedMocks = await this.findMatchingMocks(ctx);
133136
if (matchedMocks.length) {
134137
const compiledMock = compileMockConfig(matchedMocks);
135-
this.performMockResponse(ctx, compiledMock, next);
138+
this.applyMockToRequest(ctx, compiledMock, next);
136139
} else {
137140
next();
138141
}
@@ -165,9 +168,11 @@ export class Proxy {
165168
return matchedMocks;
166169
}
167170

168-
private performMockResponse(ctx: IContext, mockConfig: MockConfig, next: () => void): void {
171+
private async applyMockToRequest(ctx: IContext, mockConfig: MockConfig, next: () => void) {
169172
ctx.use(ResponseDecoder);
170-
173+
if (mockConfig.delay) {
174+
await sleep(mockConfig.delay);
175+
}
171176
this.modifyClientRequest(ctx, mockConfig);
172177
this.modifyClientResponse(ctx, mockConfig, next);
173178
}
@@ -178,7 +183,7 @@ export class Proxy {
178183
modifyRequestBody(ctx, mockConfig);
179184
}
180185

181-
private modifyClientResponse(ctx: IContext, mockConfig: MockConfig, next: () => void): void {
186+
private async modifyClientResponse(ctx: IContext, mockConfig: MockConfig, next: () => void) {
182187
if (mockConfig.statusCode && mockConfig.responseBody) {
183188
ctx.proxyToClientResponse.writeHead(mockConfig.statusCode);
184189
ctx.proxyToClientResponse.end(mockConfig.responseBody);

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export type MockConfig = {
3636
responseHeaders?: HttpHeader;
3737
responseBody?: string;
3838
updateResponseBody?: UpdateBodySpec[];
39+
delay?: number;
3940
};
4041

4142
export type SniffConfig = {

src/utils/proxy.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,7 @@ export function sanitizeMockConfig(config: MockConfig) {
304304

305305
return config;
306306
}
307+
308+
export function sleep(timeMs: number) {
309+
return new Promise((resolve) => setTimeout(resolve, timeMs));
310+
}

0 commit comments

Comments
 (0)