diff --git a/src/__tests__/decide.js b/src/__tests__/decide.js index 4f8493b23..ca1ab56f3 100644 --- a/src/__tests__/decide.js +++ b/src/__tests__/decide.js @@ -3,7 +3,7 @@ import { Decide } from '../decide' import { PostHogPersistence } from '../posthog-persistence' import { RequestRouter } from '../utils/request-router' -const expectDecodedSendRequest = (send_request, data) => { +const expectDecodedSendRequest = (send_request, data, noCompression) => { const lastCall = send_request.mock.calls[send_request.mock.calls.length - 1] const decoded = lastCall[0].data @@ -15,7 +15,7 @@ const expectDecodedSendRequest = (send_request, data) => { data, method: 'POST', callback: expect.any(Function), - compression: 'base64', + compression: noCompression ? undefined : 'base64', timeout: undefined, }) } @@ -116,6 +116,33 @@ describe('Decide', () => { }) }) + it('should disable compression when config is set', () => { + given('config', () => ({ + api_host: 'https://test.com', + token: 'testtoken', + persistence: 'memory', + disable_compression: true, + })) + given.posthog.register({ + $stored_person_properties: {}, + $stored_group_properties: {}, + }) + given.subject() + + // noCompression is true + expectDecodedSendRequest( + given.posthog._send_request, + { + token: 'testtoken', + distinct_id: 'distinctid', + groups: { organization: '5' }, + person_properties: {}, + group_properties: {}, + }, + true + ) + }) + it('should send disable flags with decide request when config for advanced_disable_feature_flags_on_first_load is set', () => { given('config', () => ({ api_host: 'https://test.com', diff --git a/src/__tests__/featureflags.js b/src/__tests__/featureflags.js index df2ce778a..bba3575fa 100644 --- a/src/__tests__/featureflags.js +++ b/src/__tests__/featureflags.js @@ -209,10 +209,12 @@ describe('featureflags', () => { }, } - expect(given.featureFlags.getFlags()).toEqual(['alpha-feature-2', 'multivariate-flag']) + // should return both true and false flags + expect(given.featureFlags.getFlags()).toEqual(['beta-feature', 'alpha-feature-2', 'multivariate-flag']) expect(given.featureFlags.getFlagVariants()).toEqual({ 'alpha-feature-2': 'as-a-variant', 'multivariate-flag': 'variant-1', + 'beta-feature': false, }) }) @@ -540,6 +542,9 @@ describe('featureflags', () => { second: true, }) + // check right compression is sent + expect(given.instance._send_request.mock.calls[0][0].compression).toEqual('base64') + // check the request sent person properties expect(given.instance._send_request.mock.calls[0][0].data).toEqual({ token: 'random fake token', @@ -585,6 +590,18 @@ describe('featureflags', () => { // check reload request was not sent expect(given.instance._send_request).not.toHaveBeenCalled() }) + + it('on providing config disable_compression', () => { + given.instance.config = { + ...given.instance.config, + disable_compression: true, + } + + given.featureFlags.reloadFeatureFlags() + jest.runAllTimers() + + expect(given.instance._send_request.mock.calls[0][0].compression).toEqual(undefined) + }) }) describe('override person and group properties', () => { diff --git a/src/decide.ts b/src/decide.ts index 75469b320..c85ee3453 100644 --- a/src/decide.ts +++ b/src/decide.ts @@ -37,7 +37,7 @@ export class Decide { method: 'POST', url: this.instance.requestRouter.endpointFor('api', '/decide/?v=3'), data, - compression: Compression.Base64, + compression: this.instance.config.disable_compression ? undefined : Compression.Base64, timeout: this.instance.config.feature_flag_request_timeout_ms, callback: (response) => this.parseDecideResponse(response.json as DecideResponse | undefined), }) diff --git a/src/posthog-featureflags.ts b/src/posthog-featureflags.ts index c123bb263..45272c161 100644 --- a/src/posthog-featureflags.ts +++ b/src/posthog-featureflags.ts @@ -110,11 +110,7 @@ export class PostHogFeatureFlags { const finalFlags = _extend({}, enabledFlags) const overriddenKeys = Object.keys(overriddenFlags) for (let i = 0; i < overriddenKeys.length; i++) { - if (overriddenFlags[overriddenKeys[i]] === false) { - delete finalFlags[overriddenKeys[i]] - } else { - finalFlags[overriddenKeys[i]] = overriddenFlags[overriddenKeys[i]] - } + finalFlags[overriddenKeys[i]] = overriddenFlags[overriddenKeys[i]] } if (!this._override_warning) { logger.warn(' Overriding feature flags!', { @@ -194,7 +190,7 @@ export class PostHogFeatureFlags { method: 'POST', url: this.instance.requestRouter.endpointFor('api', '/decide/?v=3'), data: json_data, - compression: Compression.Base64, + compression: this.instance.config.disable_compression ? undefined : Compression.Base64, timeout: this.instance.config.feature_flag_request_timeout_ms, callback: (response) => { this.setReloadingPaused(false)