From f2569cd2723cd7646b3b615884ef092e61e2a898 Mon Sep 17 00:00:00 2001 From: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com> Date: Wed, 31 Jul 2024 09:43:33 -0700 Subject: [PATCH 1/3] feat: Remove mock fetch from mocks. --- packages/shared/mocks/src/mockFetch.ts | 32 -------------------------- 1 file changed, 32 deletions(-) delete mode 100644 packages/shared/mocks/src/mockFetch.ts diff --git a/packages/shared/mocks/src/mockFetch.ts b/packages/shared/mocks/src/mockFetch.ts deleted file mode 100644 index aa0013f14b..0000000000 --- a/packages/shared/mocks/src/mockFetch.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { Response } from '@common'; - -import { basicPlatform } from './platform'; - -const createMockResponse = (remoteJson: any, statusCode: number) => { - const response: Response = { - headers: { - get: jest.fn(), - keys: jest.fn(), - values: jest.fn(), - entries: jest.fn(), - has: jest.fn(), - }, - status: statusCode, - text: jest.fn(), - json: () => Promise.resolve(remoteJson), - }; - return Promise.resolve(response); -}; - -/** - * Mocks basicPlatform fetch. Returns the fetch jest.Mock object. - * @param remoteJson - * @param statusCode - */ -const mockFetch = (remoteJson: any, statusCode: number = 200): jest.Mock => { - const f = basicPlatform.requests.fetch as jest.Mock; - f.mockResolvedValue(createMockResponse(remoteJson, statusCode)); - return f; -}; - -export default mockFetch; From 72bb2132928f23e559a39e91bbf44bb508525c5e Mon Sep 17 00:00:00 2001 From: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com> Date: Wed, 31 Jul 2024 09:48:17 -0700 Subject: [PATCH 2/3] Don't export mock fetch. --- packages/shared/mocks/src/index.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/shared/mocks/src/index.ts b/packages/shared/mocks/src/index.ts index c299841036..a58f70badf 100644 --- a/packages/shared/mocks/src/index.ts +++ b/packages/shared/mocks/src/index.ts @@ -3,7 +3,6 @@ import ContextDeduplicator from './contextDeduplicator'; import { hasher } from './crypto'; import { MockEventProcessor, setupMockEventProcessor } from './eventProcessor'; import logger from './logger'; -import mockFetch from './mockFetch'; import { basicPlatform } from './platform'; import { MockStreamingProcessor, setupMockStreamingProcessor } from './streamingProcessor'; @@ -11,7 +10,6 @@ export { basicPlatform, clientContext, hasher, - mockFetch, logger, ContextDeduplicator, MockEventProcessor, From 3be63ebc5ba8db33b2077c8494fa0546d19341f3 Mon Sep 17 00:00:00 2001 From: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com> Date: Wed, 31 Jul 2024 09:51:37 -0700 Subject: [PATCH 3/3] Remove fetch flags. --- .../src/evaluation/fetchFlags.test.ts | 73 ----------------- .../sdk-client/src/evaluation/fetchFlags.ts | 21 ----- .../src/evaluation/fetchUtils.test.ts | 4 - .../sdk-client/src/evaluation/fetchUtils.ts | 81 ------------------- .../evaluation/mockResponseWithReasons.json | 66 --------------- 5 files changed, 245 deletions(-) delete mode 100644 packages/shared/sdk-client/src/evaluation/fetchFlags.test.ts delete mode 100644 packages/shared/sdk-client/src/evaluation/fetchFlags.ts delete mode 100644 packages/shared/sdk-client/src/evaluation/fetchUtils.test.ts delete mode 100644 packages/shared/sdk-client/src/evaluation/fetchUtils.ts delete mode 100644 packages/shared/sdk-client/src/evaluation/mockResponseWithReasons.json diff --git a/packages/shared/sdk-client/src/evaluation/fetchFlags.test.ts b/packages/shared/sdk-client/src/evaluation/fetchFlags.test.ts deleted file mode 100644 index 07a6b98467..0000000000 --- a/packages/shared/sdk-client/src/evaluation/fetchFlags.test.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { LDContext } from '@launchdarkly/js-sdk-common'; -import { basicPlatform, mockFetch } from '@launchdarkly/private-js-mocks'; - -import Configuration from '../configuration'; -import fetchFlags from './fetchFlags'; -import * as mockResponse from './mockResponse.json'; -import * as mockResponseWithReasons from './mockResponseWithReasons.json'; - -describe('fetchFeatures', () => { - const sdkKey = 'testSdkKey1'; - const context: LDContext = { kind: 'user', key: 'test-user-key-1' }; - const getHeaders = { - authorization: 'testSdkKey1', - 'user-agent': 'TestUserAgent/2.0.2', - 'x-launchdarkly-wrapper': 'Rapper/1.2.3', - }; - - let config: Configuration; - let platformFetch: jest.Mock; - - beforeEach(() => { - platformFetch = basicPlatform.requests.fetch as jest.Mock; - mockFetch(mockResponse); - config = new Configuration(); - }); - - afterEach(() => { - jest.resetAllMocks(); - }); - - test('get', async () => { - const json = await fetchFlags(sdkKey, context, config, basicPlatform); - - expect(platformFetch).toHaveBeenCalledWith( - 'https://clientsdk.launchdarkly.com/sdk/evalx/testSdkKey1/contexts/eyJraW5kIjoidXNlciIsImtleSI6InRlc3QtdXNlci1rZXktMSJ9', - { - method: 'GET', - headers: getHeaders, - }, - ); - expect(json).toEqual(mockResponse); - }); - - test('withReasons', async () => { - mockFetch(mockResponseWithReasons); - config = new Configuration({ withReasons: true }); - const json = await fetchFlags(sdkKey, context, config, basicPlatform); - - expect(platformFetch).toHaveBeenCalledWith( - 'https://clientsdk.launchdarkly.com/sdk/evalx/testSdkKey1/contexts/eyJraW5kIjoidXNlciIsImtleSI6InRlc3QtdXNlci1rZXktMSJ9?withReasons=true', - { - method: 'GET', - headers: getHeaders, - }, - ); - expect(json).toEqual(mockResponseWithReasons); - }); - - // TODO: test fetchFlags with hash - // test('hash', async () => { - // config = new Configuration({ hash: 'test-hash', withReasons: false }); - // const json = await fetchFlags(sdkKey, context, config, basicPlatform); - // - // expect(platformFetch).toHaveBeenCalledWith( - // 'https://clientsdk.launchdarkly.com/sdk/evalx/testSdkKey1/contexts/eyJraW5kIjoidXNlciIsImtleSI6InRlc3QtdXNlci1rZXktMSJ9?h=test-hash', - // { - // method: 'GET', - // headers: getHeaders, - // }, - // ); - // expect(json).toEqual(mockResponse); - // }); -}); diff --git a/packages/shared/sdk-client/src/evaluation/fetchFlags.ts b/packages/shared/sdk-client/src/evaluation/fetchFlags.ts deleted file mode 100644 index 755de56b8c..0000000000 --- a/packages/shared/sdk-client/src/evaluation/fetchFlags.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { LDContext, Platform } from '@launchdarkly/js-sdk-common'; - -import Configuration from '../configuration'; -import { Flags } from '../types'; -import { createFetchOptions, createFetchUrl } from './fetchUtils'; - -const fetchFlags = async ( - sdkKey: string, - context: LDContext, - config: Configuration, - { encoding, info, requests }: Platform, -): Promise => { - const fetchUrl = createFetchUrl(sdkKey, context, config, encoding!); - const fetchOptions = createFetchOptions(sdkKey, context, config, info); - - // TODO: add error handling, retry and timeout - const response = await requests.fetch(fetchUrl, fetchOptions); - return response.json(); -}; - -export default fetchFlags; diff --git a/packages/shared/sdk-client/src/evaluation/fetchUtils.test.ts b/packages/shared/sdk-client/src/evaluation/fetchUtils.test.ts deleted file mode 100644 index be06225fbd..0000000000 --- a/packages/shared/sdk-client/src/evaluation/fetchUtils.test.ts +++ /dev/null @@ -1,4 +0,0 @@ -// TODO: add fetchUtils tests -describe('fetchUtils', () => { - test('sucesss', () => {}); -}); diff --git a/packages/shared/sdk-client/src/evaluation/fetchUtils.ts b/packages/shared/sdk-client/src/evaluation/fetchUtils.ts deleted file mode 100644 index ea415ee645..0000000000 --- a/packages/shared/sdk-client/src/evaluation/fetchUtils.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { - base64UrlEncode, - defaultHeaders, - Encoding, - Info, - LDContext, - Options, -} from '@launchdarkly/js-sdk-common'; - -import Configuration from '../configuration'; - -export const createFetchPath = ( - sdkKey: string, - context: LDContext, - baseUrlPolling: string, - useReport: boolean, - encoding: Encoding, -) => - useReport - ? `${baseUrlPolling}/sdk/evalx/${sdkKey}/context` - : `${baseUrlPolling}/sdk/evalx/${sdkKey}/contexts/${base64UrlEncode( - JSON.stringify(context), - encoding, - )}`; - -export const createQueryString = (hash: string | undefined, withReasons: boolean) => { - const qs = { - h: hash, - withReasons, - }; - - const qsArray: string[] = []; - Object.entries(qs).forEach(([key, value]) => { - if (value) { - qsArray.push(`${key}=${value}`); - } - }); - - return qsArray.join('&'); -}; - -export const createFetchUrl = ( - sdkKey: string, - context: LDContext, - config: Configuration, - encoding: Encoding, -) => { - const { - withReasons, - hash, - serviceEndpoints: { polling }, - useReport, - } = config; - const path = createFetchPath(sdkKey, context, polling, useReport, encoding); - const qs = createQueryString(hash, withReasons); - - return qs ? `${path}?${qs}` : path; -}; - -export const createFetchOptions = ( - sdkKey: string, - context: LDContext, - config: Configuration, - info: Info, -): Options => { - const { useReport, tags } = config; - const headers = defaultHeaders(sdkKey, info, tags); - - if (useReport) { - return { - method: 'REPORT', - headers: { ...headers, 'content-type': 'application/json' }, - body: JSON.stringify(context), - }; - } - - return { - method: 'GET', - headers, - }; -}; diff --git a/packages/shared/sdk-client/src/evaluation/mockResponseWithReasons.json b/packages/shared/sdk-client/src/evaluation/mockResponseWithReasons.json deleted file mode 100644 index 0e198ad32b..0000000000 --- a/packages/shared/sdk-client/src/evaluation/mockResponseWithReasons.json +++ /dev/null @@ -1,66 +0,0 @@ -{ - "fdsafdsafdsafdsa": { - "version": 827, - "flagVersion": 3, - "value": true, - "variation": 0, - "trackEvents": false, - "reason": { "kind": "FALLTHROUGH" } - }, - "this-is-a-test": { - "version": 827, - "flagVersion": 5, - "value": true, - "variation": 0, - "trackEvents": false, - "reason": { "kind": "FALLTHROUGH" } - }, - "dev-test-flag": { - "version": 827, - "flagVersion": 555, - "value": true, - "variation": 0, - "trackEvents": true, - "reason": { "kind": "FALLTHROUGH" } - }, - "easter-specials": { - "version": 827, - "flagVersion": 37, - "value": "no specials", - "variation": 3, - "trackEvents": false, - "reason": { "kind": "FALLTHROUGH" } - }, - "moonshot-demo": { - "version": 827, - "flagVersion": 91, - "value": true, - "variation": 0, - "trackEvents": true, - "reason": { "kind": "FALLTHROUGH" } - }, - "test1": { - "version": 827, - "flagVersion": 5, - "value": "s1", - "variation": 0, - "trackEvents": false, - "reason": { "kind": "FALLTHROUGH" } - }, - "easter-i-tunes-special": { - "version": 827, - "flagVersion": 15, - "value": false, - "variation": 1, - "trackEvents": false, - "reason": { "kind": "FALLTHROUGH" } - }, - "log-level": { - "version": 827, - "flagVersion": 14, - "value": "warn", - "variation": 3, - "trackEvents": false, - "reason": { "kind": "OFF" } - } -}