feat(fake-driver): add a new 'PROXY' context that does 'proxying' for use in testing
MODIFY
packages/base-driver/test/basedriver/driver-tests.js
packages/base-driver/test/basedriver/driver-tests.js
@@ -335,8 +335,8 @@ function baseDriverUnitTests (DriverClass, defaultCaps = {}) {
it('should have a #canProxy method', function () {
d.canProxy.should.be.an.instanceof(Function);
});
- it('should return false from #canProxy', function () {
- d.canProxy(sessId).should.be.false;
+ it('should return a boolean from #canProxy', function () {
+ [true, false].should.include(d.canProxy(sessId));
});
it('should throw an error when sessionId is wrong', function () {
(() => { d.canProxy(); }).should.throw;

MODIFY
packages/fake-driver/lib/commands/contexts.js
packages/fake-driver/lib/commands/contexts.js
@@ -4,7 +4,7 @@ import { errors } from '@appium/base-driver';
let commands = {}, helpers = {}, extensions = {};
helpers.getRawContexts = function getRawContexts () {
- let contexts = {'NATIVE_APP': null};
+ let contexts = {NATIVE_APP: null, PROXY: null};
let wvs = this.appModel.getWebviews();
for (let i = 1; i < wvs.length + 1; i++) {
contexts[`WEBVIEW_${i}`] = wvs[i - 1];
@@ -32,8 +32,12 @@ commands.setContext = async function setContext (context) {
this.curContext = context;
if (context === 'NATIVE_APP') {
this.appModel.deactivateWebview();
+ this._proxyActive = false;
+ } else if (context === 'PROXY') {
+ this._proxyActive = true;
} else {
this.appModel.activateWebview(contexts[context]);
+ this._proxyActive = false;
}
} else {
throw new errors.NoSuchContextError();

MODIFY
packages/fake-driver/lib/driver.js
packages/fake-driver/lib/driver.js
@@ -15,6 +15,7 @@ class FakeDriver extends BaseDriver {
this.caps = {};
this.fakeThing = null;
this.cliArgs = {};
+ this._proxyActive = false;
this.desiredCapConstraints = {
'app': {
@@ -24,6 +25,27 @@ class FakeDriver extends BaseDriver {
};
}
+ proxyActive () {
+ return this._proxyActive;
+ }
+
+ canProxy () {
+ return true;
+ }
+
+ proxyReqRes (req, res) {
+ // fake implementation of jwp proxy req res
+ res.set('content-type', 'application/json');
+ const resBodyObj = {value: 'proxied via proxyReqRes'};
+ const match = req.originalUrl.match(/\/session\/([^/]+)/);
+ resBodyObj.sessionId = match ? match[1] : null;
+ res.status(200).send(JSON.stringify(resBodyObj));
+ }
+
+ proxyCommand (/*url, method, body*/) {
+ return 'proxied via proxyCommand';
+ }
+
async createSession (jsonwpDesiredCapabilities, jsonwpRequiredCaps, w3cCapabilities, otherSessionData = []) {
// TODO add validation on caps.app that we will get for free from

MODIFY
packages/fake-driver/test/context-tests.js
packages/fake-driver/test/context-tests.js
@@ -15,7 +15,7 @@ function contextTests () {
});
it('should get contexts', async function () {
await driver.getContexts()
- .should.eventually.become(['NATIVE_APP', 'WEBVIEW_1']);
+ .should.eventually.become(['NATIVE_APP', 'PROXY', 'WEBVIEW_1']);
});
it('should not set context that is not there', async function () {
await driver.switchContext('WEBVIEW_FOO')

feat(fake-driver): add a new 'PROXY' context that does 'proxying' for use in testing