From 3aad5a03e9291b11eb86fe68261db84dd87854ea Mon Sep 17 00:00:00 2001 From: Stokes Player Date: Mon, 26 Sep 2022 18:26:33 -0400 Subject: [PATCH] fix: Add missing header and use correct endpoint host (#23982) --- .../src/actions/EventCollectorActions.ts | 12 ++++++++++-- .../test/unit/actions/EventCollectorActions.spec.ts | 2 +- scripts/gulp/gulpConstants.ts | 10 ++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/data-context/src/actions/EventCollectorActions.ts b/packages/data-context/src/actions/EventCollectorActions.ts index 84fc84c8a009..b9f8a901ed12 100644 --- a/packages/data-context/src/actions/EventCollectorActions.ts +++ b/packages/data-context/src/actions/EventCollectorActions.ts @@ -10,7 +10,11 @@ interface CollectableEvent { cohort?: string } -const cloudEnv = (process.env.CYPRESS_INTERNAL_EVENT_COLLECTOR_ENV || 'staging') as 'development' | 'staging' | 'production' +/** + * Defaults to staging when doing development. To override to production for development, + * explicitly set process.env.CYPRESS_INTERNAL_ENV to 'production` + */ +const cloudEnv = (process.env.CYPRESS_INTERNAL_EVENT_COLLECTOR_ENV || 'production') as 'development' | 'staging' | 'production' export class EventCollectorActions { constructor (private ctx: DataContext) { @@ -23,7 +27,11 @@ export class EventCollectorActions { await this.ctx.util.fetch( `${dashboardUrl}/anon-collect`, - { method: 'POST', body: JSON.stringify(event) }, + { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(event), + }, ) debug(`Recorded event: %o`, event) diff --git a/packages/data-context/test/unit/actions/EventCollectorActions.spec.ts b/packages/data-context/test/unit/actions/EventCollectorActions.spec.ts index ae1f3c5c4d2a..b84c302d9cd3 100644 --- a/packages/data-context/test/unit/actions/EventCollectorActions.spec.ts +++ b/packages/data-context/test/unit/actions/EventCollectorActions.spec.ts @@ -32,7 +32,7 @@ describe('EventCollectorActions', () => { expect(ctx.util.fetch).to.have.been.calledOnceWith( sinon.match(/anon-collect$/), // Verify URL ends with expected 'anon-collect' path - { method: 'POST', body: '{"campaign":"abc","medium":"def","messageId":"ghi","cohort":"123"}' }, + { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: '{"campaign":"abc","medium":"def","messageId":"ghi","cohort":"123"}' }, ) }) diff --git a/scripts/gulp/gulpConstants.ts b/scripts/gulp/gulpConstants.ts index 9ff448e62c08..d6afba38590d 100644 --- a/scripts/gulp/gulpConstants.ts +++ b/scripts/gulp/gulpConstants.ts @@ -8,6 +8,16 @@ declare global { } } +/** + * Gulp is only used for running the application during development. At this point of starting the app, + * process.env.CYPRESS_INTERNAL_ENV has not been set yet unless explicitly set on the command line. If not + * set on the command line, it is set to 'development' [here](https://github.com/cypress-io/cypress/blob/a5ec234005fead97f6cfdf611abf8d9f4ad0565d/packages/server/lib/environment.js#L22) + * + * When running in a production build, a file is written out to set CYPRESS_INTERNAL_ENV to 'production' + * [here](https://github.com/cypress-io/cypress/blob/a5ec234005fead97f6cfdf611abf8d9f4ad0565d/scripts/binary/build.ts#L176). + * However, running in production will not use the code in this file. + */ + export const DEFAULT_INTERNAL_CLOUD_ENV = process.env.CYPRESS_INTERNAL_ENV || 'production' export const DEFAULT_INTERNAL_EVENT_COLLECTOR_ENV = process.env.CYPRESS_INTERNAL_ENV || 'staging'