Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/project_config/config_manager_factory.browser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -78,6 +78,7 @@ describe('createPollingConfigManager', () => {
autoUpdate: true,
urlTemplate: 'urlTemplate',
datafileAccessToken: 'datafileAccessToken',
customHeaders: { 'X-Test-Header': 'test-value' },
cache: getMockSyncCache<string>(),
};

Expand Down
5 changes: 2 additions & 3 deletions lib/project_config/config_manager_factory.browser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,9 +14,8 @@
* limitations under the License.
*/

import { getOpaquePollingConfigManager, OpaqueConfigManager, PollingConfigManagerConfig } from './config_manager_factory';
import { BrowserRequestHandler } from '../utils/http_request_handler/request_handler.browser';
import { ProjectConfigManager } from './project_config_manager';
import { getOpaquePollingConfigManager, OpaqueConfigManager, PollingConfigManagerConfig } from './config_manager_factory';

export const createPollingProjectConfigManager = (config: PollingConfigManagerConfig): OpaqueConfigManager => {
const defaultConfig = {
Expand Down
3 changes: 2 additions & 1 deletion lib/project_config/config_manager_factory.node.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -78,6 +78,7 @@ describe('createPollingConfigManager', () => {
autoUpdate: false,
urlTemplate: 'urlTemplate',
datafileAccessToken: 'datafileAccessToken',
customHeaders: { 'X-Test-Header': 'test-value' },
cache: getMockSyncCache(),
};

Expand Down
6 changes: 2 additions & 4 deletions lib/project_config/config_manager_factory.node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,10 +14,8 @@
* limitations under the License.
*/

import { getOpaquePollingConfigManager, OpaqueConfigManager, PollingConfigManagerConfig } from "./config_manager_factory";
import { NodeRequestHandler } from "../utils/http_request_handler/request_handler.node";
import { ProjectConfigManager } from "./project_config_manager";
import { DEFAULT_URL_TEMPLATE, DEFAULT_AUTHENTICATED_URL_TEMPLATE } from './constant';
import { getOpaquePollingConfigManager, OpaqueConfigManager, PollingConfigManagerConfig } from "./config_manager_factory";

export const createPollingProjectConfigManager = (config: PollingConfigManagerConfig): OpaqueConfigManager => {
const defaultConfig = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -129,6 +129,7 @@ describe('createPollingConfigManager', () => {
autoUpdate: false,
urlTemplate: 'urlTemplate',
datafileAccessToken: 'datafileAccessToken',
customHeaders: { 'X-Test-Header': 'test-value' },
cache: getMockSyncCache(),
};

Expand Down
9 changes: 3 additions & 6 deletions lib/project_config/config_manager_factory.react_native.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,12 +14,9 @@
* limitations under the License.
*/

import { getOpaquePollingConfigManager, PollingConfigManagerConfig } from "./config_manager_factory";
import { BrowserRequestHandler } from "../utils/http_request_handler/request_handler.browser";
import { ProjectConfigManager } from "./project_config_manager";
import { AsyncStorageCache } from "../utils/cache/async_storage_cache.react_native";

import { OpaqueConfigManager } from "./config_manager_factory";
import { BrowserRequestHandler } from "../utils/http_request_handler/request_handler.browser";
import { getOpaquePollingConfigManager, PollingConfigManagerConfig, OpaqueConfigManager } from "./config_manager_factory";

export const createPollingProjectConfigManager = (config: PollingConfigManagerConfig): OpaqueConfigManager => {
const defaultConfig = {
Expand Down
2 changes: 2 additions & 0 deletions lib/project_config/config_manager_factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ describe('getPollingConfigManager', () => {
autoUpdate: true,
urlTemplate: 'urlTemplate',
datafileAccessToken: 'datafileAccessToken',
customHeaders: { 'X-Custom-Header': 'custom-value' },
cache: getMockSyncCache<string>(),
};

Expand All @@ -171,6 +172,7 @@ describe('getPollingConfigManager', () => {
autoUpdate: config.autoUpdate,
urlTemplate: config.urlTemplate,
datafileAccessToken: config.datafileAccessToken,
customHeaders: config.customHeaders,
requestHandler: config.requestHandler,
repeater: MockIntervalRepeater.mock.instances[0],
cache: config.cache,
Expand Down
2 changes: 2 additions & 0 deletions lib/project_config/config_manager_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export type PollingConfigManagerConfig = {
updateInterval?: number;
urlTemplate?: string;
datafileAccessToken?: string;
customHeaders?: Record<string, string>;
cache?: Store<string>;
};

Expand Down Expand Up @@ -88,6 +89,7 @@ export const getPollingConfigManager = (
autoUpdate: opt.autoUpdate,
urlTemplate: opt.urlTemplate,
datafileAccessToken: opt.datafileAccessToken,
customHeaders: opt.customHeaders,
requestHandler: opt.requestHandler,
cache: opt.cache,
repeater,
Expand Down
1 change: 1 addition & 0 deletions lib/project_config/datafile_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type DatafileManagerConfig = {
urlTemplate?: string;
cache?: Store<string>;
datafileAccessToken?: string;
customHeaders?: Record<string, string>;
initRetry?: number;
repeater: Repeater;
logger?: LoggerFacade;
Expand Down
79 changes: 79 additions & 0 deletions lib/project_config/polling_datafile_manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,85 @@ describe('PollingDatafileManager', () => {
expect(requestHandler.makeRequest.mock.calls[0][1].Authorization).toBe('Bearer token123');
});

it('sends customHeaders in the request headers', async () => {
const repeater = getMockRepeater();
const requestHandler = getMockRequestHandler();
const mockResponse = getMockAbortableRequest(Promise.resolve({ statusCode: 200, body: '{"foo": "bar"}', headers: {} }));
requestHandler.makeRequest.mockReturnValueOnce(mockResponse);

const customHeaders = {
'X-Custom-Header': 'custom-value',
'X-Another-Header': 'another-value',
};

const manager = new PollingDatafileManager({
repeater,
requestHandler,
sdkKey: 'keyThatExists',
customHeaders,
});

manager.start();
repeater.execute(0);

await expect(manager.onRunning()).resolves.not.toThrow();
expect(requestHandler.makeRequest).toHaveBeenCalledOnce();
const sentHeaders = requestHandler.makeRequest.mock.calls[0][1];
expect(sentHeaders['X-Custom-Header']).toBe('custom-value');
expect(sentHeaders['X-Another-Header']).toBe('another-value');
});

it('merges customHeaders with other headers (access token and if-modified-since)', async () => {
const repeater = getMockRepeater();
const requestHandler = getMockRequestHandler();

// First request to set up last-modified header
const mockResponse1 = getMockAbortableRequest(Promise.resolve({
statusCode: 200,
body: '{"foo": "bar"}',
headers: { 'last-modified': 'Fri, 08 Mar 2019 18:57:17 GMT' }
}));

// Second request to test all headers together
const mockResponse2 = getMockAbortableRequest(Promise.resolve({
statusCode: 304,
body: '',
headers: {}
}));

requestHandler.makeRequest.mockReturnValueOnce(mockResponse1)
.mockReturnValueOnce(mockResponse2);

const customHeaders = {
'X-Custom-Header': 'custom-value',
};

const manager = new PollingDatafileManager({
repeater,
requestHandler,
sdkKey: 'keyThatExists',
datafileAccessToken: 'token123',
customHeaders,
autoUpdate: true,
});

manager.start();

// First request
await repeater.execute(0);

// Second request should have all headers
await repeater.execute(0);

expect(requestHandler.makeRequest).toHaveBeenCalledTimes(2);

// Check second request headers include custom, auth, and if-modified-since
const secondRequestHeaders = requestHandler.makeRequest.mock.calls[1][1];
expect(secondRequestHeaders['X-Custom-Header']).toBe('custom-value');
expect(secondRequestHeaders['Authorization']).toBe('Bearer token123');
expect(secondRequestHeaders['if-modified-since']).toBe('Fri, 08 Mar 2019 18:57:17 GMT');
});

it('uses the provided urlTemplate', async () => {
const repeater = getMockRepeater();
const requestHandler = getMockRequestHandler();
Expand Down
8 changes: 7 additions & 1 deletion lib/project_config/polling_datafile_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ export class PollingDatafileManager extends BaseService implements DatafileManag
private cache?: Store<string>;
private sdkKey: string;
private datafileAccessToken?: string;
private customHeaders?: Record<string, string>;

constructor(config: DatafileManagerConfig) {
super(config.startupLogs);
const {
autoUpdate = false,
sdkKey,
datafileAccessToken,
customHeaders,
urlTemplate,
cache,
initRetry,
Expand All @@ -74,6 +76,7 @@ export class PollingDatafileManager extends BaseService implements DatafileManag
this.cacheKey = 'opt-datafile-' + sdkKey;
this.sdkKey = sdkKey;
this.datafileAccessToken = datafileAccessToken;
this.customHeaders = customHeaders;
this.requestHandler = requestHandler;
this.emitter = new EventEmitter();
this.autoUpdate = autoUpdate;
Expand Down Expand Up @@ -194,7 +197,10 @@ export class PollingDatafileManager extends BaseService implements DatafileManag
}

private makeDatafileRequest(): AbortableRequest {
const headers: Headers = {};
const headers: Headers = {
...this.customHeaders,
};

if (this.lastResponseLastModified) {
headers['if-modified-since'] = this.lastResponseLastModified;
}
Expand Down
Loading