Skip to content

Commit

Permalink
fix(go-feature-flag-web): Set API Key in headers (#1030)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org>
  • Loading branch information
thomaspoignant authored Aug 5, 2024
1 parent 533e217 commit 31a8adb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ describe('GoFeatureFlagWebProvider', () => {
});

describe('flag evaluation', () => {
/**
* TODO: reactivate this test when the issue "web-sdk: onContextChange not called for named provider" is solved.\
* Issue link: https://github.com/open-feature/js-sdk/issues/488
*/
it('should change evaluation value if context has changed', async () => {
await OpenFeature.setContext(defaultContext);
OpenFeature.setProvider('test-provider', defaultProvider);
Expand Down Expand Up @@ -322,6 +318,34 @@ describe('GoFeatureFlagWebProvider', () => {
};
expect(got).toEqual(want);
});

it('should have apiKey as header if set in the provider', async () => {
const apiKeyProvider = new GoFeatureFlagWebProvider(
{
endpoint: endpoint,
apiTimeout: 1000,
maxRetries: 1,
apiKey: 'my-api-key',
},
logger,
);

const flagKey = 'bool-flag';
await OpenFeature.setContext(defaultContext);
await OpenFeature.setProviderAndWait('test-provider', apiKeyProvider);
const client = OpenFeature.getClient('test-provider');
await websocketMockServer.connected;
client.getBooleanDetails(flagKey, false);
const lastCall = fetchMock.lastCall(allFlagsEndpoint);
expect(lastCall).not.toBeUndefined();
if (lastCall) {
const headers = lastCall[1]?.headers as never;
expect(headers).not.toBeUndefined();
expect(headers['Authorization']).toBe('Bearer my-api-key');
return;
}
expect(true).toBe(false);
});
});

describe('eventing', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,19 +270,13 @@ export class GoFeatureFlagWebProvider implements Provider {
: endpointURL.pathname + '/' + path;

const request: GoFeatureFlagAllFlagRequest = { evaluationContext: transformContext(context) };
const headers = new Headers({
'Content-Type': 'application/json',
Accept: 'application/json',
});
if (this._apiKey) {
headers.set('Authorization', `Bearer ${this._apiKey}`);
}

const init: RequestInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
// we had the authorization header only if we have an API Key
...(this._apiKey ? { Authorization: `Bearer ${this._apiKey}` } : {}),
},
body: JSON.stringify(request),
};
Expand Down

0 comments on commit 31a8adb

Please sign in to comment.