Repository: wix/Detox
Pull request #2691 (2021-03-16 14:28:42)

Passthrough or set user-agent to gmsaas
## Description

As per request from Genymobile, adding `detox` as the (default) user-agent extra-data for requests sent out via Detox.
The actual value can be overridden by a user by setting `GMSAAS_USER_AGENT_EXTRA_DATA` themselves, before invoking detox.

Relates to #2655.

noomorph: Won't it make sense to also include Detox version number and Node.js version as well as a user-agent? 
d4vidi: No - this won't be visible for us, just Genymobile. All they want is to be able to get a sense of usage through detox. Probably not much any time soon.

Patch:
diff --git a/detox/src/devices/drivers/android/genycloud/exec/GenyCloudExec.js b/detox/src/devices/drivers/android/genycloud/exec/GenyCloudExec.js
index 4a164f0aa..b04202a05 100644
--- a/detox/src/devices/drivers/android/genycloud/exec/GenyCloudExec.js
+++ b/detox/src/devices/drivers/android/genycloud/exec/GenyCloudExec.js
@@ -3,6 +3,7 @@ const exec = require('../../../../../utils/exec').execWithRetriesAndLogs;
 class GenyCloudExec {
   constructor(binaryPath) {
     this.binaryExec = `"${binaryPath}" --format compactjson`;
+    process.env.GMSAAS_USER_AGENT_EXTRA_DATA = process.env.GMSAAS_USER_AGENT_EXTRA_DATA || 'detox';
   }
 
   getVersion() {


Test patch:
diff --git a/detox/src/devices/drivers/android/genycloud/exec/GenyCloudExec.test.js b/detox/src/devices/drivers/android/genycloud/exec/GenyCloudExec.test.js
index ddb1f5c7d..e911d9e2b 100644
--- a/detox/src/devices/drivers/android/genycloud/exec/GenyCloudExec.test.js
+++ b/detox/src/devices/drivers/android/genycloud/exec/GenyCloudExec.test.js
@@ -38,6 +38,10 @@ describe('Genymotion-cloud executable', () => {
     uut = new GenyCloudExec('mock/path/to/gmsaas');
   });
 
+  afterEach(() => {
+    delete process.env.GMSAAS_USER_AGENT_EXTRA_DATA;
+  });
+
   [
     {
       commandName: 'version',
@@ -118,4 +122,25 @@ describe('Genymotion-cloud executable', () => {
       });
     });
   });
+
+  describe('User-agent bundling into gmsaas requests', () => {
+    it('should set up \'detox\' as the default user-agent', () => {
+      delete process.env.GMSAAS_USER_AGENT_EXTRA_DATA;
+
+      const GenyCloudExec = require('./GenyCloudExec');
+      new GenyCloudExec('mock/path/to/gmsaas');
+
+      expect(process.env.GMSAAS_USER_AGENT_EXTRA_DATA).toEqual('detox');
+    });
+
+    it('should retain any value prespecified for user-agent', () => {
+      delete process.env.GMSAAS_USER_AGENT_EXTRA_DATA;
+      process.env.GMSAAS_USER_AGENT_EXTRA_DATA = 'mockUserAgent';
+
+      const GenyCloudExec = require('./GenyCloudExec');
+      new GenyCloudExec('mock/path/to/gmsaas');
+
+      expect(process.env.GMSAAS_USER_AGENT_EXTRA_DATA).toEqual('mockUserAgent');
+    });
+  });
 });
diff --git a/detox/src/utils/exec.test.js b/detox/src/utils/exec.test.js
index 2d86ea795..292f82556 100644
--- a/detox/src/utils/exec.test.js
+++ b/detox/src/utils/exec.test.js
@@ -24,12 +24,6 @@ describe('exec', () => {
     expect(cpp.exec).toHaveBeenCalledWith(`bin`, { timeout: 0 });
   });
 
-  it(`exec command with no arguments successfully`, async () => {
-    mockCppSuccessful(cpp);
-    await exec.execWithRetriesAndLogs('bin');
-    expect(cpp.exec).toHaveBeenCalledWith(`bin`, { timeout: 0 });
-  });
-
   it(`exec command with arguments successfully`, async () => {
     mockCppSuccessful(cpp);
 
@@ -39,6 +33,14 @@ describe('exec', () => {
     expect(cpp.exec).toHaveBeenCalledWith(`bin --argument 123`, { timeout: 0 });
   });
 
+  it(`exec command with env-vars pass-through (i.e. no custom env-vars specification`, async () => {
+    mockCppSuccessful(cpp);
+    await exec.execWithRetriesAndLogs('bin');
+    const usedOptions = cpp.exec.mock.calls[0][1];
+    expect(usedOptions).not.toHaveProperty('env');
+    expect(cpp.exec).toHaveBeenCalledTimes(1);
+  });
+
   it(`exec command with arguments and prefix successfully`, async () => {
     mockCppSuccessful(cpp);