Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #157 from launchdarkly/eb/ch56656/http-test-cleanup
Browse files Browse the repository at this point in the history
add more end-to-end tests, improve HTTP test helpers, general cleanup
  • Loading branch information
eli-darkly authored Nov 23, 2019
2 parents dc2755a + e9c869e commit 3da81f7
Show file tree
Hide file tree
Showing 11 changed files with 786 additions and 675 deletions.
123 changes: 6 additions & 117 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"eslint-formatter-pretty": "2.1.1",
"jest": "24.7.1",
"jest-junit": "6.3.0",
"nock": "9.2.3",
"selfsigned": "1.10.4",
"tmp": "0.0.33",
"typescript": "3.0.1"
Expand Down
116 changes: 116 additions & 0 deletions test/LDClient-end-to-end-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const LDClient = require('../index.js');
import { AsyncQueue, sleepAsync, withCloseable } from './async_utils';
import { createServer, respond, respondJson, respondSSE } from './http_server';
import { stubLogger } from './stubs';

async function withAllServers(asyncCallback) {
return await withCloseable(createServer, async pollingServer =>
withCloseable(createServer, async streamingServer =>
withCloseable(createServer, async eventsServer => {
const servers = { polling: pollingServer, streaming: streamingServer, events: eventsServer };
const baseConfig = {
baseUri: pollingServer.url,
streamUri: streamingServer.url,
eventsUri: eventsServer.url,
logger: stubLogger()
};
return await asyncCallback(servers, baseConfig);
})
)
);
}

describe('LDClient end-to-end', () => {
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: {} };

const user = { key: 'userKey' };

it('starts in polling mode', async () => {
await withAllServers(async (servers, config) => {
servers.polling.forMethodAndPath('get', '/sdk/latest-all', respondJson(allData));
servers.events.forMethodAndPath('post', '/bulk', respond(200));

config.stream = false;
await withCloseable(LDClient.init(sdkKey, config), async client => {
await client.waitForInitialization();
expect(client.initialized()).toBe(true);

const value = await client.variation(flag.key, user);
expect(value).toEqual(expectedFlagValue);

await client.flush();
});

expect(servers.polling.requestCount()).toEqual(1);
expect(servers.streaming.requestCount()).toEqual(0);
expect(servers.events.requestCount()).toEqual(1);
});
});

it('fails in polling mode with 401 error', async () => {
await withAllServers(async (servers, config) => {
servers.polling.forMethodAndPath('get', '/sdk/latest-all', respond(401));
servers.events.forMethodAndPath('post', '/bulk', respond(200));

config.stream = false;

await withCloseable(LDClient.init(sdkKey, config), async client => {
await expect(client.waitForInitialization()).rejects.toThrow();
expect(client.initialized()).toBe(false);
});

expect(servers.polling.requestCount()).toEqual(1);
expect(servers.streaming.requestCount()).toEqual(0);
});
});

it('starts in streaming mode', async () => {
await withAllServers(async (servers, config) => {
const streamEvent = { type: 'put', data: { data: allData } };
await withCloseable(AsyncQueue(), async events => {
events.add(streamEvent);
servers.streaming.forMethodAndPath('get', '/all', respondSSE(events));
servers.events.forMethodAndPath('post', '/bulk', respond(200));

await withCloseable(LDClient.init(sdkKey, config), async client => {
await client.waitForInitialization();
expect(client.initialized()).toBe(true);

const value = await client.variation(flag.key, user);
expect(value).toEqual(expectedFlagValue);

await client.flush();
});

expect(servers.polling.requestCount()).toEqual(0);
expect(servers.streaming.requestCount()).toEqual(1);
expect(servers.events.requestCount()).toEqual(1);
});
});
});

it('fails in streaming mode with 401 error', async () => {
await withAllServers(async (servers, config) => {
servers.streaming.forMethodAndPath('get', '/all', respond(401));
servers.events.forMethodAndPath('post', '/bulk', respond(200));

await withCloseable(LDClient.init(sdkKey, config), async client => {
await expect(client.waitForInitialization()).rejects.toThrow();
expect(client.initialized()).toBe(false);
});

expect(servers.polling.requestCount()).toEqual(0);
expect(servers.streaming.requestCount()).toEqual(1);
});
});
});
Loading

0 comments on commit 3da81f7

Please sign in to comment.