|
| 1 | +import * as http from 'http'; |
| 2 | + |
| 3 | +import { |
| 4 | + AsyncQueue, |
| 5 | + SSEItem, |
| 6 | + TestHttpHandlers, |
| 7 | + TestHttpServer, |
| 8 | +} from 'launchdarkly-js-test-helpers'; |
| 9 | + |
| 10 | +import { basicLogger, LDLogger } from '../src'; |
| 11 | +import LDClientNode from '../src/LDClientNode'; |
| 12 | + |
| 13 | +const sdkKey = 'sdkKey'; |
| 14 | +const flagKey = 'flagKey'; |
| 15 | +const expectedFlagValue = 'yes'; |
| 16 | +const flag = { |
| 17 | + key: flagKey, |
| 18 | + version: 1, |
| 19 | + on: false, |
| 20 | + offVariation: 0, |
| 21 | + variations: [expectedFlagValue, 'no'], |
| 22 | +}; |
| 23 | +const allData = { flags: { flagKey: flag }, segments: {} }; |
| 24 | + |
| 25 | +// This proves the proxyAgent option actually reaches the underlying request, through the full |
| 26 | +// LDOptions -> NodePlatform -> NodeRequests wiring, rather than only the NodeRequests constructor. |
| 27 | +describe('When using a custom proxyAgent', () => { |
| 28 | + let logger: LDLogger; |
| 29 | + let closeable: { close: () => void }[]; |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + closeable = []; |
| 33 | + logger = basicLogger({ |
| 34 | + destination: () => {}, |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + closeable.forEach((item) => item.close()); |
| 40 | + }); |
| 41 | + |
| 42 | + it('uses the supplied proxyAgent for polling requests', async () => { |
| 43 | + const server = await TestHttpServer.start(); |
| 44 | + server.forMethodAndPath('get', '/sdk/latest-all', TestHttpHandlers.respondJson(allData)); |
| 45 | + |
| 46 | + const agent = new http.Agent({ keepAlive: false }); |
| 47 | + // addRequest exists at runtime but is not part of the public http.Agent type. |
| 48 | + // @ts-ignore |
| 49 | + const addRequestSpy = jest.spyOn(agent, 'addRequest'); |
| 50 | + |
| 51 | + const client = new LDClientNode(sdkKey, { |
| 52 | + baseUri: server.url, |
| 53 | + proxyAgent: agent, |
| 54 | + stream: false, |
| 55 | + sendEvents: false, |
| 56 | + logger, |
| 57 | + }); |
| 58 | + |
| 59 | + closeable.push(server, client); |
| 60 | + |
| 61 | + await client.waitForInitialization({ timeout: 10 }); |
| 62 | + expect(client.initialized()).toBe(true); |
| 63 | + expect(addRequestSpy).toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('uses the supplied proxyAgent for streaming requests', async () => { |
| 67 | + const server = await TestHttpServer.start(); |
| 68 | + const events = new AsyncQueue<SSEItem>(); |
| 69 | + events.add({ type: 'put', data: JSON.stringify({ data: allData }) }); |
| 70 | + server.forMethodAndPath('get', '/all', TestHttpHandlers.sseStream(events)); |
| 71 | + |
| 72 | + const agent = new http.Agent({ keepAlive: false }); |
| 73 | + // addRequest exists at runtime but is not part of the public http.Agent type. |
| 74 | + // @ts-ignore |
| 75 | + const addRequestSpy = jest.spyOn(agent, 'addRequest'); |
| 76 | + |
| 77 | + const client = new LDClientNode(sdkKey, { |
| 78 | + streamUri: server.url, |
| 79 | + proxyAgent: agent, |
| 80 | + sendEvents: false, |
| 81 | + logger, |
| 82 | + }); |
| 83 | + |
| 84 | + closeable.push(server, events, client); |
| 85 | + |
| 86 | + await client.waitForInitialization({ timeout: 10 }); |
| 87 | + expect(client.initialized()).toBe(true); |
| 88 | + expect(addRequestSpy).toHaveBeenCalled(); |
| 89 | + }); |
| 90 | +}); |
0 commit comments