|
| 1 | +'use strict'; |
| 2 | +const chai = require('chai'); |
| 3 | +const expect = chai.expect; |
| 4 | +const sinon = require('sinon'); |
| 5 | +const proxyquire = require('proxyquire').noPreserveCache(); |
| 6 | + |
| 7 | +const CONSTANTS_PATH = require.resolve('../../../../bin/testObservability/helper/constants'); |
| 8 | +const API_URL_ENV = 'BROWSERSTACK_TEST_OBSERVABILITY_API_URL'; |
| 9 | +const DEFAULT_API_URL = 'https://collector-observability.browserstack.com'; |
| 10 | + |
| 11 | +// A11Y-13909: the reporter runs on the BrowserStack machine from its own install of this package, |
| 12 | +// so the collector it posts to has to come from the environment the launching cli forwards. These |
| 13 | +// pin the request destination itself, not just the forwarding done by setSystemEnvs. |
| 14 | +describe('observability collector url', () => { |
| 15 | + let savedApiUrl, cachedConstants; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + savedApiUrl = process.env[API_URL_ENV]; |
| 19 | + delete process.env[API_URL_ENV]; |
| 20 | + // constants.js reads the override at load, so it must be re-evaluated per case. |
| 21 | + cachedConstants = require.cache[CONSTANTS_PATH]; |
| 22 | + delete require.cache[CONSTANTS_PATH]; |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + if (savedApiUrl === undefined) { |
| 27 | + delete process.env[API_URL_ENV]; |
| 28 | + } else { |
| 29 | + process.env[API_URL_ENV] = savedApiUrl; |
| 30 | + } |
| 31 | + delete require.cache[CONSTANTS_PATH]; |
| 32 | + if (cachedConstants) { |
| 33 | + require.cache[CONSTANTS_PATH] = cachedConstants; |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + const loadHelperWithAxios = () => { |
| 38 | + const axiosStub = sinon.stub().resolves({ status: 200, data: '{}' }); |
| 39 | + axiosStub['@noCallThru'] = true; |
| 40 | + const helper = proxyquire('../../../../bin/testObservability/helper/helper', { axios: axiosStub }); |
| 41 | + return { helper, axiosStub }; |
| 42 | + }; |
| 43 | + |
| 44 | + it('sends requests to the overridden collector', async () => { |
| 45 | + process.env[API_URL_ENV] = 'https://collector-example.bsstag.com'; |
| 46 | + const { helper, axiosStub } = loadHelperWithAxios(); |
| 47 | + |
| 48 | + await helper.nodeRequest('POST', 'api/v1/batch', {}, { headers: {} }); |
| 49 | + |
| 50 | + expect(axiosStub.firstCall.args[0].url).to.equal('https://collector-example.bsstag.com/api/v1/batch'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('sends requests to the default collector when no override is set', async () => { |
| 54 | + const { helper, axiosStub } = loadHelperWithAxios(); |
| 55 | + |
| 56 | + await helper.nodeRequest('POST', 'api/v1/batch', {}, { headers: {} }); |
| 57 | + |
| 58 | + expect(axiosStub.firstCall.args[0].url).to.equal(`${DEFAULT_API_URL}/api/v1/batch`); |
| 59 | + }); |
| 60 | +}); |
0 commit comments