|
| 1 | +import { LDClientImpl } from '../src'; |
| 2 | +import { createBasicPlatform } from './createBasicPlatform'; |
| 3 | +import TestLogger from './Logger'; |
| 4 | +import makeCallbacks from './makeCallbacks'; |
| 5 | + |
| 6 | +// When `internalOptions.instanceId` is supplied, the SDK must attach |
| 7 | +// `X-LaunchDarkly-Instance-Id` to every outbound request using that value. When it is |
| 8 | +// omitted, the header must not be attached. The platform SDK that owns instance-id |
| 9 | +// generation (e.g. the Node server SDK) is responsible for producing the GUID and |
| 10 | +// passing it through `internalOptions`. |
| 11 | +describe('LDClientImpl instance-id header', () => { |
| 12 | + const fixedUuid = 'd3135edb-6531-4874-8a38-f0c9e556e836'; |
| 13 | + |
| 14 | + it('attaches the X-LaunchDarkly-Instance-Id header when internalOptions.instanceId is set', async () => { |
| 15 | + const platform = createBasicPlatform(); |
| 16 | + platform.requests.fetch.mockImplementation(() => |
| 17 | + Promise.resolve({ status: 200, headers: new Headers() }), |
| 18 | + ); |
| 19 | + |
| 20 | + const client = new LDClientImpl( |
| 21 | + 'sdk-key-instance-id-1', |
| 22 | + platform, |
| 23 | + { logger: new TestLogger(), stream: false }, |
| 24 | + makeCallbacks(false), |
| 25 | + { instanceId: fixedUuid }, |
| 26 | + ); |
| 27 | + |
| 28 | + client.identify({ key: 'user' }); |
| 29 | + client.variation('dev-test-flag', { key: 'user' }, false); |
| 30 | + await client.flush(); |
| 31 | + |
| 32 | + expect(platform.requests.fetch).toHaveBeenCalledWith( |
| 33 | + 'https://events.launchdarkly.com/bulk', |
| 34 | + expect.objectContaining({ |
| 35 | + headers: expect.objectContaining({ |
| 36 | + 'x-launchdarkly-instance-id': fixedUuid, |
| 37 | + }), |
| 38 | + }), |
| 39 | + ); |
| 40 | + |
| 41 | + client.close(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('omits the X-LaunchDarkly-Instance-Id header when no instanceId is supplied', async () => { |
| 45 | + const platform = createBasicPlatform(); |
| 46 | + platform.requests.fetch.mockImplementation(() => |
| 47 | + Promise.resolve({ status: 200, headers: new Headers() }), |
| 48 | + ); |
| 49 | + |
| 50 | + const client = new LDClientImpl( |
| 51 | + 'sdk-key-instance-id-2', |
| 52 | + platform, |
| 53 | + { logger: new TestLogger(), stream: false }, |
| 54 | + makeCallbacks(false), |
| 55 | + ); |
| 56 | + |
| 57 | + client.identify({ key: 'user' }); |
| 58 | + client.variation('dev-test-flag', { key: 'user' }, false); |
| 59 | + await client.flush(); |
| 60 | + |
| 61 | + expect(platform.requests.fetch).toHaveBeenCalledWith( |
| 62 | + 'https://events.launchdarkly.com/bulk', |
| 63 | + expect.objectContaining({ |
| 64 | + headers: expect.not.objectContaining({ |
| 65 | + 'x-launchdarkly-instance-id': expect.anything(), |
| 66 | + }), |
| 67 | + }), |
| 68 | + ); |
| 69 | + |
| 70 | + client.close(); |
| 71 | + }); |
| 72 | +}); |
0 commit comments