Limit the count of request agents in the proxy pool
MODIFY
lib/jsonwp-proxy/proxy.js
lib/jsonwp-proxy/proxy.js
@@ -118,16 +118,16 @@ class JWProxy {
method = method.toUpperCase();
const newUrl = this.getUrlForProxy(url);
const reqOpts = {
+ agent: false,
url: newUrl,
method,
headers: {
'content-type': 'application/json; charset=utf-8',
'user-agent': 'appium',
- accept: '*/*',
+ accept: 'application/json, */*',
},
resolveWithFullResponse: true,
timeout: this.timeout,
- forever: true,
};
if (body !== null) {
if (typeof body !== 'object') {
@@ -170,14 +170,12 @@ class JWProxy {
throw err;
}
} catch (e) {
- let responseError;
+ let responseError = e.error;
try {
- responseError = JSON.parse(e.error);
+ responseError = JSON.parse(responseError);
} catch (e1) {
- if (!_.isEmpty(e.error) && _.isString(e.error)) {
- log.warn(`Got an unexpected response: ${_.truncate(e.error, {length: 300})}`);
- }
- responseError = _.isPlainObject(e.error) ? e.error : null;
+ log.warn(`Got an unexpected response: ` +
+ _.truncate(_.isString(responseError) ? responseError : JSON.stringify(responseError), {length: 300}));
}
throw new errors.ProxyRequestError(`Could not proxy command to remote server. ` +
`Original error: ${e.message}`, responseError, e.statusCode);

MODIFY
test/protocol/helpers.js
test/protocol/helpers.js
@@ -8,18 +8,3 @@ export function createProxyServer (sessionId, port) {
let server = app.listen(port);
return {app, server};
}
-
-let handlers = {
- post: {},
- get: {},
- delete: {},
- put: {},
-};
-
-export function addHandler (app, method, url, handler) {
- method = method.toLowerCase();
- if (!handlers[method][url]) {
- app[method](url, (req, res) => handlers[method][url].call(this, req, res));
- }
- handlers[method][url] = handler;
-}
\ No newline at end of file

MODIFY
test/protocol/protocol-e2e-specs.js
test/protocol/protocol-e2e-specs.js
@@ -8,7 +8,7 @@ import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
import HTTPStatusCodes from 'http-status-codes';
-import { createProxyServer, addHandler } from './helpers';
+import { createProxyServer } from './helpers';
import { MJSONWP_ELEMENT_KEY, W3C_ELEMENT_KEY } from '../../lib/protocol/protocol';
@@ -606,7 +606,7 @@ describe('Protocol', async function () {
});
it('should work if a proxied request returns a response with status 200', async function () {
- addHandler(app, 'post', '/wd/hub/session/:sessionId/perform-actions', (req, res) => {
+ app.post('/wd/hub/session/:sessionId/perform-actions', (req, res) => {
res.json({
sessionId: req.params.sessionId,
value: req.body,
@@ -625,7 +625,7 @@ describe('Protocol', async function () {
});
it('should return error if a proxied request returns a MJSONWP error response', async function () {
- addHandler(app, 'post', '/wd/hub/session/:sessionId/perform-actions', (req, res) => {
+ app.post('/wd/hub/session/:sessionId/perform-actions', (req, res) => {
res.status(500).json({
sessionId,
status: 6,
@@ -663,7 +663,7 @@ describe('Protocol', async function () {
});
it('should return error if a proxied request returns a MJSONWP error response but HTTP status code is 200', async function () {
- addHandler(app, 'post', '/wd/hub/session/:sessionId/perform-actions', (req, res) => {
+ app.post('/wd/hub/session/:sessionId/perform-actions', (req, res) => {
res.status(200).json({
sessionId: 'Fake Session Id',
status: 7,
@@ -684,7 +684,7 @@ describe('Protocol', async function () {
});
it('should return error if a proxied request returns a W3C error response', async function () {
- addHandler(app, 'post', '/wd/hub/session/:sessionId/perform-actions', (req, res) => {
+ app.post('/wd/hub/session/:sessionId/perform-actions', (req, res) => {
res.status(404).json({
value: {
error: 'no such element',
@@ -706,7 +706,7 @@ describe('Protocol', async function () {
});
it('should return an error if a proxied request returns a W3C error response', async function () {
- addHandler(app, 'post', '/wd/hub/session/:sessionId/perform-actions', (req, res) => {
+ app.post('/wd/hub/session/:sessionId/perform-actions', (req, res) => {
res.set('Connection', 'close');
res.status(444).json({
value: {

Limit the count of request agents in the proxy pool (#260)