chore: use RegExp to check session id requirement (#20947)

* chore: use regext to check session id requirement

* update docs

* add test case

* use match of path-to-regexp

* use another name to avoid conflict

* accept appium prefix for non-session id
This commit is contained in:
Kazuaki Matsuo
2025-02-01 01:03:15 -08:00
committed by GitHub
parent 98da49ff58
commit 7edd3bdc11
2 changed files with 52 additions and 8 deletions
+20 -8
View File
@@ -15,6 +15,7 @@ import ProtocolConverter from './protocol-converter';
import {formatResponseValue, formatStatus} from '../protocol/helpers';
import http from 'http';
import https from 'https';
import { match as pathToRegexMatch } from 'path-to-regexp';
const DEFAULT_LOG = logger.getLogger('WD Proxy');
const DEFAULT_REQUEST_TIMEOUT = 240000;
@@ -110,13 +111,24 @@ class JWProxy {
this._activeRequests = [];
}
/**
* Return true if the given endpoint started with '/session' and
* it could have session id after the path.
* e.g.
* - should return true
* - /session/82a9b7da-faaf-4a1d-8ef3-5e4fb5812200
* - /session/82a9b7da-faaf-4a1d-8ef3-5e4fb5812200/url
* - should return false
* - /session
* - /sessions
* - /session/
* - /status
* - /appium/sessions
* @param {string} endpoint
* @returns {boolean}
*/
endpointRequiresSessionId(endpoint) {
return !_.includes([
'/session',
'/sessions',
'/status',
'/appium/sessions'
], endpoint);
return !!(pathToRegexMatch('/session/:sessionId{/*command}')(endpoint));
}
set downstreamProtocol(value) {
@@ -133,7 +145,7 @@ class JWProxy {
url = '/';
}
const proxyBase = `${this.scheme}://${this.server}:${this.port}${this.base}`;
const endpointRe = '(/(session|status))';
const endpointRe = '(/(session|status|appium))';
let remainingUrl = '';
if (/^http/.test(url)) {
const first = new RegExp(`(https?://.+)${endpointRe}`).exec(url);
@@ -147,7 +159,7 @@ class JWProxy {
throw new Error(`Did not know what to do with url '${url}'`);
}
const stripPrefixRe = new RegExp('^.*?(/(session|status).*)$');
const stripPrefixRe = new RegExp('^.*?(/(session|status|appium).*)$');
if (stripPrefixRe.test(remainingUrl)) {
remainingUrl = /** @type {RegExpExecArray} */ (stripPrefixRe.exec(remainingUrl))[1];
}
@@ -107,6 +107,12 @@ describe('proxy', function () {
let j = mockProxy();
let newUrl = j.getUrlForProxy('/status');
should.exist(newUrl);
});
it('should not throw an error if url does not require a session id with appium vendor prefix and its null', function () {
let j = mockProxy();
let newUrl = j.getUrlForProxy('/appium/something');
should.exist(newUrl);
});
});
@@ -209,4 +215,30 @@ describe('proxy', function () {
res.sentBody.should.eql({value: {message: 'chrome not reachable'}});
});
});
describe('endpointRequiresSessionId', function () {
const j = mockProxy({sessionId: '123'});
[
'/session/82a9b7da-faaf-4a1d-8ef3-5e4fb5812200',
'/session/82a9b7da-faaf-4a1d-8ef3-5e4fb5812200/',
'/session/82a9b7da-faaf-4a1d-8ef3-5e4fb5812200/url',
'/session/82a9b7da-faaf-4a1d-8ef3-5e4fb5812200/element/3d001db2-7987-42a7-975d-8d5d5304083f',
].forEach(function (endpoint) {
it(`should be true with ${endpoint}`, function () {
j.endpointRequiresSessionId(endpoint).should.be.true;
});
});
[
'/session',
'/session/',
'/sessions',
'/appium/sessions',
'/status',
].forEach(function (endpoint) {
it(`should be false with ${endpoint}`, function () {
j.endpointRequiresSessionId(endpoint).should.be.false;
});
});
});
});