-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathLDClientNode.proxyAgent.test.ts
More file actions
94 lines (78 loc) · 2.95 KB
/
Copy pathLDClientNode.proxyAgent.test.ts
File metadata and controls
94 lines (78 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import * as http from 'http';
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: {} };
// This proves the proxyAgent option actually reaches the underlying request, through the full
// LDOptions -> NodePlatform -> NodeRequests wiring, rather than only the NodeRequests constructor.
describe('When using a custom proxyAgent', () => {
let logger: LDLogger;
let closeable: { close: () => void }[];
beforeEach(() => {
closeable = [];
logger = basicLogger({
destination: () => {},
});
});
afterEach(() => {
closeable.forEach((item) => item.close());
});
it('uses the supplied proxyAgent for polling requests', async () => {
const server = await TestHttpServer.start();
server.forMethodAndPath('get', '/sdk/latest-all', TestHttpHandlers.respondJson(allData));
const agent = new http.Agent({ keepAlive: false });
// addRequest exists at runtime but is not part of the public http.Agent type.
// @ts-ignore
const addRequestSpy = jest.spyOn(agent, 'addRequest');
const warnSpy = jest.spyOn(logger, 'warn');
const client = new LDClientNode(sdkKey, {
baseUri: server.url,
proxyAgent: agent,
stream: false,
sendEvents: false,
logger,
});
closeable.push(server, client);
await client.waitForInitialization({ timeout: 10 });
expect(client.initialized()).toBe(true);
expect(addRequestSpy).toHaveBeenCalled();
// proxyAgent is a real, known LDOptions member; the shared Configuration validator should
// not flag it as unrecognized just because it isn't part of the common LDOptions.
expect(warnSpy).not.toHaveBeenCalledWith(expect.stringContaining('unknown config option'));
});
it('uses the supplied proxyAgent for streaming requests', async () => {
const server = await TestHttpServer.start();
const events = new AsyncQueue<SSEItem>();
events.add({ type: 'put', data: JSON.stringify({ data: allData }) });
server.forMethodAndPath('get', '/all', TestHttpHandlers.sseStream(events));
const agent = new http.Agent({ keepAlive: false });
// addRequest exists at runtime but is not part of the public http.Agent type.
// @ts-ignore
const addRequestSpy = jest.spyOn(agent, 'addRequest');
const client = new LDClientNode(sdkKey, {
streamUri: server.url,
proxyAgent: agent,
sendEvents: false,
logger,
});
closeable.push(server, events, client);
await client.waitForInitialization({ timeout: 10 });
expect(client.initialized()).toBe(true);
expect(addRequestSpy).toHaveBeenCalled();
});
});