Skip to content

Commit 8de4d9c

Browse files
committed
added executeScript
1 parent 009b9b7 commit 8de4d9c

4 files changed

Lines changed: 140 additions & 43 deletions

File tree

src/plugin.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import _ from 'lodash';
66
import { configureWifiProxy, isRealDevice } from './utils/adb';
77
import { cleanUpProxyServer, setupProxyServer } from './utils/proxy';
88
import proxyCache from './proxy-cache';
9+
import logger from './logger';
910

1011
export class AppiumInterceptorPlugin extends BasePlugin {
1112
constructor(name: string, cliArgs: CliArg) {
@@ -63,4 +64,39 @@ export class AppiumInterceptorPlugin extends BasePlugin {
6364
}
6465
}
6566
}
67+
68+
static executeMethodMap = {
69+
'interceptor: addMock': {
70+
command: 'addMock',
71+
params: { required: ['values'] },
72+
},
73+
74+
'interceptor: removeMock': {
75+
command: 'removeMock',
76+
params: { required: ['id'] },
77+
},
78+
79+
};
80+
81+
async addMock(next: any, driver: any, values: any) {
82+
const proxy = proxyCache.get(driver.sessionId);
83+
if (!proxy) {
84+
logger.error('Proxy is not running');
85+
}
86+
// @ts-ignore
87+
return proxy.addMock(values);
88+
}
89+
90+
async removeMock(next: any, driver: any, id: any) {
91+
const proxy = proxyCache.get(driver.sessionId);
92+
if (!proxy) {
93+
logger.error('Proxy is not running');
94+
}
95+
// @ts-ignore
96+
proxy.removeMock(id);
97+
}
98+
99+
async execute(next: any, driver: any, script: any, args: any) {
100+
return await this.executeMethod(next, driver, script, args);
101+
}
66102
}

src/proxy.js

Lines changed: 89 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,115 @@ const Proxy = require('http-mitm-proxy').Proxy;
33
// import { Proxy } from "http-mitm-proxy";
44
const proxy = new Proxy();
55

6-
proxy.onError(function(ctx, err) {
6+
proxy.onError(function (ctx, err) {
77
console.error('proxy error:', err);
88
});
99

10-
proxy.onRequest(function(ctx, callback) {
10+
proxy.onRequest(function (ctx, callback) {
1111
if (ctx.clientToProxyRequest.headers.host == 'reqres.in') {
12-
console.log('proxying request for:', ctx.clientToProxyRequest.headers.host);
13-
const chunks = [];
12+
console.log('proxying request for:', ctx.clientToProxyRequest.headers.host);
13+
const chunks = [];
1414
ctx.use(Proxy.gunzip);
1515

1616
/* Replace url */
17-
if(ctx.proxyToServerRequestOptions?.path) {
18-
ctx.proxyToServerRequestOptions.path = ctx.proxyToServerRequestOptions.path.replace("page=1", "page=2")
17+
if (ctx.proxyToServerRequestOptions?.path) {
18+
ctx.proxyToServerRequestOptions.path = ctx.proxyToServerRequestOptions.path.replace(
19+
'page=1',
20+
'page=2'
21+
);
1922
}
2023

21-
if(ctx.proxyToServerRequestOptions?.path) {
22-
ctx.proxyToServerRequestOptions.path = ctx.proxyToServerRequestOptions.path.replace("page=1", "page=2")
24+
if (ctx.proxyToServerRequestOptions?.path) {
25+
ctx.proxyToServerRequestOptions.path = ctx.proxyToServerRequestOptions.path.replace(
26+
'page=1',
27+
'page=2'
28+
);
2329
}
24-
30+
2531
/* Replace request body */
26-
const requestChunks: any = []
27-
ctx.onRequestData(function(ctx, chunk, callback) {
32+
const requestChunks: any = [];
33+
ctx.onRequestData(function (ctx, chunk, callback) {
2834
// console.log(chunk.toString("utf-8").replace(/Dashboard/g, '<h3>Pwned!</h3>'))
29-
//chunk = Buffer.from(chunk.toString().replace(/Dashboard/g, '<h3>Pwned!</h3>'));
30-
requestChunks.push(chunk);
31-
return callback(null, undefined);
32-
});
35+
//chunk = Buffer.from(chunk.toString().replace(/Dashboard/g, '<h3>Pwned!</h3>'));
36+
requestChunks.push(chunk);
37+
return callback(null, undefined);
38+
});
3339

34-
ctx.onRequestEnd(function(ctx, callback) {
40+
ctx.onRequestEnd(function (ctx, callback) {
3541
let bodyOld = Buffer.concat(requestChunks).toString('utf8');
36-
bodyOld = bodyOld.replace("login=abc", "login=someusername")
37-
bodyOld = bodyOld.replace("password=123", "password=somepassword")
38-
ctx.proxyToServerRequest?.write(bodyOld)
42+
bodyOld = bodyOld.replace('login=abc', 'login=someusername');
43+
bodyOld = bodyOld.replace('password=123', 'password=somepassword');
44+
ctx.proxyToServerRequest?.write(bodyOld);
3945
callback();
40-
});
46+
});
4147

42-
ctx.onResponseData(function(ctx, chunk, callback) {
48+
ctx.onResponseData(function (ctx, chunk, callback) {
4349
chunks.push(chunk);
44-
chunk = Buffer.from(JSON.stringify({"page":1,"per_page":6,"total":12,"total_pages":2,"data":[{"id":1,"email":"saikrishna.bluth@reqres.in","first_name":"George","last_name":"Bluth","avatar":"https://reqres.in/img/faces/1-image.jpg"},{"id":2,"email":"janet.weaver@reqres.in","first_name":"Janet","last_name":"Weaver","avatar":"https://reqres.in/img/faces/2-image.jpg"},{"id":3,"email":"emma.wong@reqres.in","first_name":"Emma","last_name":"Wong","avatar":"https://reqres.in/img/faces/3-image.jpg"},{"id":4,"email":"eve.holt@reqres.in","first_name":"Eve","last_name":"Holt","avatar":"https://reqres.in/img/faces/4-image.jpg"},{"id":5,"email":"charles.morris@reqres.in","first_name":"Charles","last_name":"Morris","avatar":"https://reqres.in/img/faces/5-image.jpg"},{"id":6,"email":"tracey.ramos@reqres.in","first_name":"Tracey","last_name":"Ramos","avatar":"https://reqres.in/img/faces/6-image.jpg"}],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}));
50+
chunk = Buffer.from(
51+
JSON.stringify({
52+
page: 1,
53+
per_page: 6,
54+
total: 12,
55+
total_pages: 2,
56+
data: [
57+
{
58+
id: 1,
59+
email: 'saikrishna.bluth@reqres.in',
60+
first_name: 'George',
61+
last_name: 'Bluth',
62+
avatar: 'https://reqres.in/img/faces/1-image.jpg',
63+
},
64+
{
65+
id: 2,
66+
email: 'janet.weaver@reqres.in',
67+
first_name: 'Janet',
68+
last_name: 'Weaver',
69+
avatar: 'https://reqres.in/img/faces/2-image.jpg',
70+
},
71+
{
72+
id: 3,
73+
email: 'emma.wong@reqres.in',
74+
first_name: 'Emma',
75+
last_name: 'Wong',
76+
avatar: 'https://reqres.in/img/faces/3-image.jpg',
77+
},
78+
{
79+
id: 4,
80+
email: 'eve.holt@reqres.in',
81+
first_name: 'Eve',
82+
last_name: 'Holt',
83+
avatar: 'https://reqres.in/img/faces/4-image.jpg',
84+
},
85+
{
86+
id: 5,
87+
email: 'charles.morris@reqres.in',
88+
first_name: 'Charles',
89+
last_name: 'Morris',
90+
avatar: 'https://reqres.in/img/faces/5-image.jpg',
91+
},
92+
{
93+
id: 6,
94+
email: 'tracey.ramos@reqres.in',
95+
first_name: 'Tracey',
96+
last_name: 'Ramos',
97+
avatar: 'https://reqres.in/img/faces/6-image.jpg',
98+
},
99+
],
100+
support: {
101+
url: 'https://reqres.in/#support-heading',
102+
text: 'To keep ReqRes free, contributions towards server costs are appreciated!',
103+
},
104+
})
105+
);
45106
return callback(null, chunk);
46107
});
47-
ctx.onResponseEnd(function(ctx, callback) {
48-
const response = Buffer.concat(chunks).toString('utf8');
49-
callback(null, null)
50-
})
108+
ctx.onResponseEnd(function (ctx, callback) {
109+
const response = Buffer.concat(chunks).toString('utf8');
110+
callback(null, null);
111+
});
51112
}
52113
return callback();
53114
});
54115

55-
console.log('begin listening on 8082')
56-
proxy.listen({port: 8084, host: "::"});
116+
console.log('begin listening on 8082');
117+
proxy.listen({ port: 8084, host: '::' });

src/utils/proxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ export function updateRequestHeaders(ctx: IContext, mockConfig: MockConfig) {
6262
}
6363

6464
const { headers } = ctx.proxyToServerRequestOptions;
65-
if (mockConfig.headers.add) {
65+
if (mockConfig.headers?.add) {
6666
Object.assign(headers, mockConfig.headers.add);
6767
}
68-
if (mockConfig.headers.remove && Array.isArray(mockConfig.headers.remove)) {
68+
if (mockConfig.headers?.remove && Array.isArray(mockConfig.headers?.remove)) {
6969
mockConfig.headers.remove.forEach((header: string) => delete headers[header]);
7070
}
7171
}

test/plugin.spec.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
import { remote } from "webdriverio";
2-
import { API_DEMOS_APK_PATH as apidemosApp } from "android-apidemos";
1+
import { remote } from 'webdriverio';
2+
import { API_DEMOS_APK_PATH as apidemosApp } from 'android-apidemos';
33

4-
const APPIUM_HOST = "127.0.0.1";
4+
const APPIUM_HOST = '127.0.0.1';
55
const APPIUM_PORT = 4723;
66
const WDIO_PARAMS = {
77
connectionRetryCount: 0,
88
hostname: APPIUM_HOST,
99
port: APPIUM_PORT,
10-
path: "/wd/hub/",
11-
logLevel: "info",
10+
path: '/wd/hub/',
11+
logLevel: 'info',
1212
};
1313
const capabilities = {
14-
platformName: "Android",
15-
"appium:automationName": "espresso",
16-
"appium:app": apidemosApp,
17-
"appium:forceEspressoRebuild": false,
18-
"appium:showGradleLog": true,
14+
platformName: 'Android',
15+
'appium:automationName': 'espresso',
16+
'appium:app': apidemosApp,
17+
'appium:forceEspressoRebuild': false,
18+
'appium:showGradleLog': true,
1919
};
2020
let driver;
21-
describe("Plugin Test", () => {
21+
describe('Plugin Test', () => {
2222
beforeEach(async () => {
2323
driver = await remote({ ...WDIO_PARAMS, capabilities });
2424
});
2525

26-
it("Vertical swipe test", async () => {
27-
await driver.$("~Animation");
26+
it('Vertical swipe test', async () => {
27+
await driver.$('~Animation');
2828
});
2929
afterEach(async () => {
3030
await driver.quit();

0 commit comments

Comments
 (0)