Skip to content

Commit d051525

Browse files
authored
fix: attach requestBody, add error listener & export types (#20)
1 parent 6bb0aa8 commit d051525

File tree

7 files changed

+29
-9
lines changed

7 files changed

+29
-9
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
lib/
33
.idea
4-
.history
4+
.history
5+
.DS_Store

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.0.0-beta.7",
44
"description": "Appium 2.0 plugin to mock api calls for android apps",
55
"main": "./lib/index.js",
6+
"types": "./lib/types/index.d.ts",
67
"scripts": {
78
"build": "npx tsc",
89
"test": "mocha --require ts-node/register -p test/plugin.spec.js --exit --timeout 260000",

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import { AppiumInterceptorPlugin } from './plugin';
22
export default AppiumInterceptorPlugin;
33
export { AppiumInterceptorPlugin };
4+
5+
export { type MockConfig, type SniffConfig, type RequestInfo } from './types';

src/interceptor.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,19 @@ function RequestInterceptor(requestCompletionCallback: (value: any) => void) {
2929
return function (ctx: IContext, callback: ErrorCallback) {
3030
ctx.use(responseDecoder);
3131
const requestData = {} as any;
32+
const requestBodyDataChunks: Buffer[] = [];
33+
34+
// Get request body from http-mitm-proxy
35+
// more info: https://github.com/joeferner/node-http-mitm-proxy/blob/master/README.md#proxyonrequestendfn-or-ctxonrequestendfn
36+
ctx.onRequestData(function (ctx, chunk, callback) {
37+
requestBodyDataChunks.push(chunk);
38+
return callback(null, chunk);
39+
});
40+
3241
ctx.onRequestEnd((ctx, callback) => {
3342
readBodyFromStream(ctx.proxyToServerRequest, (requestBody) => {
34-
requestData['requestBody'] = requestBody;
43+
const requestBodyString = Buffer.concat(requestBodyDataChunks).toString('utf8');
44+
requestData['requestBody'] = requestBody ? requestBody : requestBodyString;
3545
requestData['requestHeaders'] = ctx.proxyToServerRequest?.getHeaders();
3646
});
3747
callback();

src/plugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BasePlugin } from 'appium/plugin';
22
import http from 'http';
33
import { Application } from 'express';
4-
import { CliArg, ISessionCapability, MockConfig, SniffConfig } from './types';
4+
import { CliArg, ISessionCapability, MockConfig, RequestInfo, SniffConfig } from './types';
55
import _ from 'lodash';
66
import { configureWifiProxy, isRealDevice } from './utils/adb';
77
import { cleanUpProxyServer, sanitizeMockConfig, setupProxyServer } from './utils/proxy';
@@ -139,7 +139,7 @@ export class AppiumInterceptorPlugin extends BasePlugin {
139139
proxy.enableMock(id);
140140
}
141141

142-
async startListening(next: any, driver: any, config: SniffConfig) {
142+
async startListening(next: any, driver: any, config: SniffConfig): Promise<string> {
143143
const proxy = proxyCache.get(driver.sessionId);
144144
if (!proxy) {
145145
logger.error('Proxy is not running');
@@ -150,7 +150,7 @@ export class AppiumInterceptorPlugin extends BasePlugin {
150150
return proxy?.addSniffer(config);
151151
}
152152

153-
async stopListening(next: any, driver: any, id: any) {
153+
async stopListening(next: any, driver: any, id: any): Promise<RequestInfo[]> {
154154
const proxy = proxyCache.get(driver.sessionId);
155155
if (!proxy) {
156156
logger.error('Proxy is not running');

src/proxy.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { Mock } from './mock';
1919
import { RequestInterceptor } from './interceptor';
2020
import { ApiSniffer } from './api-sniffer';
2121
import _ from 'lodash';
22+
import logger from './logger';
2223

2324
export interface ProxyOptions {
2425
deviceUDID: string;
@@ -79,6 +80,10 @@ export class Proxy {
7980
);
8081
this.httpProxy.onRequest(this.handleMockApiRequest.bind(this));
8182

83+
this.httpProxy.onError((context, error, errorType) => {
84+
logger.error(`${errorType}: ${error}`);
85+
});
86+
8287
await new Promise((resolve) => {
8388
this.httpProxy.listen(proxyOptions, () => {
8489
this._started = true;
@@ -111,9 +116,9 @@ export class Proxy {
111116
this.mocks.get(id)?.setEnableStatus(false);
112117
}
113118

114-
public addSniffer(sniffConfg: SniffConfig): string {
119+
public addSniffer(sniffConfig: SniffConfig): string {
115120
const id = uuid();
116-
const parsedConfig = !sniffConfg ? {} : parseJson(sniffConfg);
121+
const parsedConfig = !sniffConfig ? {} : parseJson(sniffConfig);
117122
this.sniffers.set(id, new ApiSniffer(id, parsedConfig));
118123
return id;
119124
}

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
"allowJs": true, /* Allow javascript files to be compiled. */
1111
// "checkJs": true, /* Report errors in .js files. */
1212
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
13-
// "declaration": true, /* Generates corresponding '.d.ts' file. */
14-
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
13+
"declaration": true, /* Generates corresponding '.d.ts' file. */
14+
"declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
15+
"declarationDir": "./lib/types", /* Redirect declarations output structure to the directory. */
1516
"sourceMap": true, /* Generates corresponding '.map' file. */
1617
// "outFile": "./", /* Concatenate and emit output to single file. */
1718
"outDir": "./lib", /* Redirect output structure to the directory. */

0 commit comments

Comments
 (0)