Skip to content

Commit

Permalink
fix(decide): Respect disable_compression for flags
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar committed Mar 29, 2024
1 parent 174f83b commit 16a5d62
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
31 changes: 29 additions & 2 deletions src/__tests__/decide.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,7 +15,7 @@ const expectDecodedSendRequest = (send_request, data) => {
data,
method: 'POST',
callback: expect.any(Function),
compression: 'base64',
compression: noCompression ? undefined : 'base64',
timeout: undefined,
})
}
Expand Down Expand Up @@ -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',
Expand Down
19 changes: 18 additions & 1 deletion src/__tests__/featureflags.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
})

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/decide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand Down
8 changes: 2 additions & 6 deletions src/posthog-featureflags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!', {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 16a5d62

Please sign in to comment.