diff --git a/platform-node/__tests__/LDClientNode.proxy.test.ts b/platform-node/__tests__/LDClientNode.proxy.test.ts new file mode 100644 index 0000000000..bd764d5901 --- /dev/null +++ b/platform-node/__tests__/LDClientNode.proxy.test.ts @@ -0,0 +1,127 @@ +import { + AsyncQueue, + SSEItem, + TestHttpHandlers, + TestHttpServer, +} from 'launchdarkly-js-test-helpers'; +import { basicLogger, LDLogger } from '../src'; + +import LDClientNode from '../src/LDClientNode'; + +const sdkKey = 'sdkKey'; +const flagKey = 'flagKey'; +const expectedFlagValue = 'yes'; +const flag = { + key: flagKey, + version: 1, + on: false, + offVariation: 0, + variations: [expectedFlagValue, 'no'], +}; +const allData = { flags: { flagKey: flag }, segments: {} }; + +describe('When using a proxy', () => { + let logger: LDLogger; + let closeable: { close: () => void }[]; + + beforeEach(() => { + closeable = []; + logger = basicLogger({ + destination: () => { }, + }); + }); + + afterEach(() => { + closeable.forEach((item) => item.close()); + }); + + it('can use proxy in polling mode', async () => { + const proxy = await TestHttpServer.startProxy(); + const server = await TestHttpServer.start(); + server.forMethodAndPath('get', '/sdk/latest-all', TestHttpHandlers.respondJson(allData)); + + const client = new LDClientNode(sdkKey, { + baseUri: server.url, + proxyOptions: { + host: proxy.hostname, + port: proxy.port, + }, + stream: false, + sendEvents: false, + logger, + }); + + closeable.push(proxy, server, client); + + await client.waitForInitialization(); + expect(client.initialized()).toBe(true); + + // If the proxy server did not log a request then the SDK did not actually use the proxy + expect(proxy.requestCount()).toEqual(1); + const req = await proxy.nextRequest(); + expect(req.path).toEqual(server.url); + }); + + it('can use proxy in streaming mode', async () => { + const proxy = await TestHttpServer.startProxy(); + const server = await TestHttpServer.start(); + const events = new AsyncQueue(); + events.add({ type: 'put', data: JSON.stringify({ data: allData }) }); + server.forMethodAndPath('get', '/all', TestHttpHandlers.sseStream(events)); + + const client = new LDClientNode(sdkKey, { + streamUri: server.url, + proxyOptions: { + host: proxy.hostname, + port: proxy.port, + }, + sendEvents: false, + logger, + }); + + closeable.push(proxy, server, events, client); + + await client.waitForInitialization(); + expect(client.initialized()).toBe(true); + + // If the proxy server did not log a request then the SDK did not actually use the proxy + expect(proxy.requestCount()).toEqual(1); + const req = await proxy.nextRequest(); + expect(req.path).toEqual(server.url); + }); + + it('can use proxy for events', async () => { + const proxy = await TestHttpServer.startProxy(); + const pollingServer = await TestHttpServer.start(); + const eventsServer = await TestHttpServer.start(); + pollingServer.forMethodAndPath('get', '/sdk/latest-all', TestHttpHandlers.respondJson(allData)); + eventsServer.forMethodAndPath('post', '/diagnostic', TestHttpHandlers.respond(200)); + + const client = new LDClientNode(sdkKey, { + baseUri: pollingServer.url, + eventsUri: eventsServer.url, + proxyOptions: { + host: proxy.hostname, + port: proxy.port, + }, + stream: false, + logger, + }); + + closeable.push(proxy, pollingServer, eventsServer, client); + + await client.waitForInitialization(); + expect(client.initialized()).toBe(true); + + // If the proxy server did not log a request then the SDK did not actually use the proxy + expect(proxy.requestCount()).toEqual(2); + const req0 = await proxy.nextRequest(); + const req1 = await proxy.nextRequest(); + if (req0.path === pollingServer.url) { + expect(req1.path).toEqual(eventsServer.url); + } else { + expect(req0.path).toEqual(eventsServer.url); + expect(req1.path).toEqual(pollingServer.url); + } + }); +}); diff --git a/platform-node/src/platform/NodeRequests.ts b/platform-node/src/platform/NodeRequests.ts index 33d15bfc05..6ee0e2dbd8 100644 --- a/platform-node/src/platform/NodeRequests.ts +++ b/platform-node/src/platform/NodeRequests.ts @@ -1,4 +1,5 @@ -import createHttpsProxyAgent, { HttpsProxyAgentOptions } from 'https-proxy-agent'; +import * as createHttpsProxyAgent from 'https-proxy-agent'; +import { HttpsProxyAgentOptions } from 'https-proxy-agent'; import { platform, LDTLSOptions, LDProxyOptions } from '@launchdarkly/js-server-sdk-common'; diff --git a/server-sdk-common/__tests__/LDClientImpl.listeners.test.ts b/server-sdk-common/__tests__/LDClientImpl.listeners.test.ts index 875c603c65..94581ed609 100644 --- a/server-sdk-common/__tests__/LDClientImpl.listeners.test.ts +++ b/server-sdk-common/__tests__/LDClientImpl.listeners.test.ts @@ -22,14 +22,6 @@ describe('given an LDClient with test data', () => { logger: new TestLogger(), }, { ...makeCallbacks(true), onUpdate: (key: string) => queue.push(key) }, - // () => { }, - // () => { }, - // () => { }, - // (key) => { - // queue.push(key); - // }, - // // Always listen to events. - // () => true, ); }); diff --git a/server-sdk-common/__tests__/LDClientImpl.test.ts b/server-sdk-common/__tests__/LDClientImpl.test.ts index 8a78258df2..ab9cdf5c53 100644 --- a/server-sdk-common/__tests__/LDClientImpl.test.ts +++ b/server-sdk-common/__tests__/LDClientImpl.test.ts @@ -47,14 +47,6 @@ it('isOffline returns true in offline mode', (done) => { done(); }, }, - // (_err) => { }, - // (_err) => { }, - // () => { - // expect(client.isOffline()).toEqual(true); - // done(); - // }, - // (_key) => { }, - // () => false, ); client.close();