|
| 1 | +import { getLDHeaders, transformHeaders } from '../headers'; |
| 2 | +import { getLDUserAgentString } from '../utils'; |
| 3 | +import * as stubPlatform from './stubPlatform'; |
| 4 | + |
| 5 | +describe('getLDHeaders', () => { |
| 6 | + it('sends no headers unless sendLDHeaders is true', () => { |
| 7 | + const platform = stubPlatform.defaults(); |
| 8 | + const headers = getLDHeaders(platform, {}); |
| 9 | + expect(headers).toEqual({}); |
| 10 | + }); |
| 11 | + |
| 12 | + it('adds user-agent header', () => { |
| 13 | + const platform = stubPlatform.defaults(); |
| 14 | + const headers = getLDHeaders(platform, { sendLDHeaders: true }); |
| 15 | + expect(headers).toMatchObject({ 'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform) }); |
| 16 | + }); |
| 17 | + |
| 18 | + it('adds wrapper info if specified, without version', () => { |
| 19 | + const platform = stubPlatform.defaults(); |
| 20 | + const headers = getLDHeaders(platform, { sendLDHeaders: true, wrapperName: 'FakeSDK' }); |
| 21 | + expect(headers).toMatchObject({ |
| 22 | + 'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform), |
| 23 | + 'X-LaunchDarkly-Wrapper': 'FakeSDK', |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + it('adds wrapper info if specified, with version', () => { |
| 28 | + const platform = stubPlatform.defaults(); |
| 29 | + const headers = getLDHeaders(platform, { sendLDHeaders: true, wrapperName: 'FakeSDK', wrapperVersion: '9.9' }); |
| 30 | + expect(headers).toMatchObject({ |
| 31 | + 'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform), |
| 32 | + 'X-LaunchDarkly-Wrapper': 'FakeSDK/9.9', |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('sets the X-LaunchDarkly-Tags header with valid id and version.', () => { |
| 37 | + const platform = stubPlatform.defaults(); |
| 38 | + const headers = getLDHeaders(platform, { |
| 39 | + sendLDHeaders: true, |
| 40 | + application: { |
| 41 | + id: 'test-application', |
| 42 | + version: 'test-version', |
| 43 | + }, |
| 44 | + }); |
| 45 | + expect(headers).toMatchObject({ |
| 46 | + 'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform), |
| 47 | + 'x-launchdarkly-tags': 'application-id/test-application application-version/test-version', |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('sets the X-LaunchDarkly-Tags header with just application id', () => { |
| 52 | + const platform = stubPlatform.defaults(); |
| 53 | + const headers = getLDHeaders(platform, { |
| 54 | + sendLDHeaders: true, |
| 55 | + application: { |
| 56 | + id: 'test-application', |
| 57 | + }, |
| 58 | + }); |
| 59 | + expect(headers).toMatchObject({ |
| 60 | + 'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform), |
| 61 | + 'x-launchdarkly-tags': 'application-id/test-application', |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('sets the X-LaunchDarkly-Tags header with just application version.', () => { |
| 66 | + const platform = stubPlatform.defaults(); |
| 67 | + const headers = getLDHeaders(platform, { |
| 68 | + sendLDHeaders: true, |
| 69 | + application: { |
| 70 | + version: 'test-version', |
| 71 | + }, |
| 72 | + }); |
| 73 | + expect(headers).toMatchObject({ |
| 74 | + 'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform), |
| 75 | + 'x-launchdarkly-tags': 'application-version/test-version', |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe('transformHeaders', () => { |
| 81 | + it('does not modify the headers if the option is not available', () => { |
| 82 | + const inputHeaders = { a: '1', b: '2' }; |
| 83 | + const headers = transformHeaders(inputHeaders, {}); |
| 84 | + expect(headers).toEqual(inputHeaders); |
| 85 | + }); |
| 86 | + |
| 87 | + it('modifies the headers if the option has a transform', () => { |
| 88 | + const inputHeaders = { c: '3', d: '4' }; |
| 89 | + const outputHeaders = { c: '9', d: '4', e: '5' }; |
| 90 | + const headerTransform = input => { |
| 91 | + const output = { ...input }; |
| 92 | + output['c'] = '9'; |
| 93 | + output['e'] = '5'; |
| 94 | + return output; |
| 95 | + }; |
| 96 | + const headers = transformHeaders(inputHeaders, { requestHeaderTransform: headerTransform }); |
| 97 | + expect(headers).toEqual(outputHeaders); |
| 98 | + }); |
| 99 | + |
| 100 | + it('cannot mutate the input header object', () => { |
| 101 | + const inputHeaders = { f: '6' }; |
| 102 | + const expectedInputHeaders = { f: '6' }; |
| 103 | + const headerMutate = input => { |
| 104 | + input['f'] = '7'; // eslint-disable-line no-param-reassign |
| 105 | + return input; |
| 106 | + }; |
| 107 | + transformHeaders(inputHeaders, { requestHeaderTransform: headerMutate }); |
| 108 | + expect(inputHeaders).toEqual(expectedInputHeaders); |
| 109 | + }); |
| 110 | +}); |
0 commit comments