Handle setValue API call conversion between protocols
MODIFY
lib/jsonwp-proxy/protocol-converter.js
lib/jsonwp-proxy/protocol-converter.js
@@ -129,6 +129,30 @@ class ProtocolConverter {
return await this.proxyFunc(url, method, body);
}
+ async proxySetValue (url, method, body) {
+ const bodyObj = util.safeJsonParse(body);
+ if (_.isPlainObject(bodyObj) && (util.hasValue(bodyObj.text) || util.hasValue(bodyObj.value))) {
+ let {text, value} = bodyObj;
+ if (util.hasValue(text) && !util.hasValue(value)) {
+ value = _.isString(text)
+ ? [...text]
+ : (_.isArray(text) ? text : []);
+ log.debug(`Added 'value' property ${JSON.stringify(value)} to 'setValue' request body`);
+ } else if (!util.hasValue(text) && util.hasValue(value)) {
+ text = _.isArray(value)
+ ? value.join('')
+ : (_.isString(value) ? value : '');
+ log.debug(`Added 'text' property ${JSON.stringify(text)} to 'setValue' request body`);
+ }
+ return await this.proxyFunc(url, method, {
+ text,
+ value,
+ });
+ }
+
+ return await this.proxyFunc(url, method, body);
+ }
+
/**
* Handle "crossing" endpoints for the case
* when upstream and downstream drivers operate different protocols
@@ -152,6 +176,8 @@ class ProtocolConverter {
return await this.proxySetTimeouts(url, method, body);
case 'setWindow':
return await this.proxySetWindow(url, method, body);
+ case 'setValue':
+ return await this.proxySetValue(url, method, body);
default:
break;
}

MODIFY
test/jsonwp-proxy/protocol-converter-specs.js
test/jsonwp-proxy/protocol-converter-specs.js
@@ -69,4 +69,47 @@ describe('Protocol Converter', function () {
timeoutObjects[0].should.eql({type: 'implicit', ms: 300});
});
});
+
+ describe('setValue', function () {
+ let converter;
+ let responseBody;
+ before(function () {
+ responseBody = null;
+ converter = new ProtocolConverter((url, method, body) => {
+ responseBody = body;
+ });
+ });
+ beforeEach(function () {
+ responseBody = {};
+ });
+
+ it('should calculate value if not present', async function () {
+ await converter.proxySetValue('', '', {
+ text: 'bla',
+ });
+ responseBody.should.eql({
+ text: 'bla',
+ value: ['b', 'l', 'a'],
+ });
+ });
+ it('should calculate text if not present', async function () {
+ await converter.proxySetValue('', '', {
+ value: ['b', 'l', 'a'],
+ });
+ responseBody.should.eql({
+ text: 'bla',
+ value: ['b', 'l', 'a'],
+ });
+ });
+ it('should keep the response body unchanged if both value and text are present', async function () {
+ await converter.proxySetValue('', '', {
+ text: 'bla',
+ value: ['b', 'l', 'a'],
+ });
+ responseBody.should.eql({
+ text: 'bla',
+ value: ['b', 'l', 'a'],
+ });
+ });
+ });
});

Handle setValue API call conversion between protocols (#297)