fix: Adjust the validation of proxied requests
MODIFY
lib/jsonwp-proxy/proxy.js
lib/jsonwp-proxy/proxy.js
@@ -145,7 +145,6 @@ class JWProxy {
'user-agent': 'appium',
accept: 'application/json, */*',
},
- validateStatus: null,
timeout: this.timeout,
httpAgent: new http.Agent({ keepAlive: this.keepAlive }),
httpsAgent: new https.Agent({ keepAlive: this.keepAlive }),
@@ -195,7 +194,7 @@ class JWProxy {
this.downstreamProtocol = this.getProtocolFromResBody(data);
log.info(`Determined the downstream protocol as '${this.downstreamProtocol}'`);
}
- if (status < 400 && _.has(data, 'status') && parseInt(data.status, 10) !== 0) {
+ if (_.has(data, 'status') && parseInt(data.status, 10) !== 0) {
// Some servers, like chromedriver may return response code 200 for non-zero JSONWP statuses
throwProxyError(data);
}
@@ -205,6 +204,7 @@ class JWProxy {
// We only consider an error unexpected if this was not
// an async request module error or if the response cannot be cast to
// a valid JSON
+ let proxyErrorMsg = e.message;
if (util.hasValue(e.response)) {
if (!isResponseLogged) {
const error = truncateBody(e.response.data);
@@ -213,11 +213,11 @@ class JWProxy {
: `Got response with unknown status: ${error}`);
}
} else {
+ proxyErrorMsg = `Could not proxy command to the remote server. Original error: ${e.message}`;
log.warn(e.message);
log.debug(e.stack);
}
- throw new errors.ProxyRequestError(`Could not proxy command to the remote server. ` +
- `Original error: ${e.message}`, e.response?.data, e.response?.status);
+ throw new errors.ProxyRequestError(proxyErrorMsg, e.response?.data, e.response?.status);
}
}

MODIFY
lib/protocol/protocol.js
lib/protocol/protocol.js
@@ -382,10 +382,11 @@ function buildHandler (app, method, path, spec, driver, isSessCmd) {
// is the first part of the stack trace
errMsg = `${err.message}${errMsg ? ('\n' + errMsg) : ''}`;
}
- SESSIONS_CACHE.getLogger(req.params.sessionId || newSessionId, currentProtocol).debug(`Encountered ` +
- `internal error running command: ${errMsg}`);
if (isErrorType(err, errors.ProxyRequestError)) {
actualErr = err.getActualError();
+ } else {
+ SESSIONS_CACHE.getLogger(req.params.sessionId || newSessionId, currentProtocol)
+ .debug(`Encountered internal error running command: ${errMsg}`);
}
if (currentProtocol === PROTOCOLS.W3C) {

MODIFY
test/jsonwp-proxy/proxy-specs.js
test/jsonwp-proxy/proxy-specs.js
@@ -4,6 +4,7 @@ import { JWProxy } from '../..';
import request from './mock-request';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
+import { isErrorType, errors } from '../../lib/protocol/errors';
const should = chai.should();
@@ -107,9 +108,11 @@ describe('proxy', function () {
});
it('should proxy error responses and codes', async function () {
let j = mockProxy({sessionId: '123'});
- let [res, body] = await j.proxy('/element/bad/text', 'GET');
- res.statusCode.should.equal(500);
- body.should.eql({status: 11, value: {message: 'Invisible element'}});
+ try {
+ await j.proxy('/element/bad/text', 'GET');
+ } catch (e) {
+ isErrorType(e.getActualError(), errors.ElementNotVisibleError).should.be.true;
+ }
});
});
describe('command proxy', function () {

fix: Adjust the validation of proxied requests (#410)