-
Notifications
You must be signed in to change notification settings - Fork 1
feat: use encoded subdomain for endpoints #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8701dd4
wip
typotter 23f25a1
move event URL into api endpoints
typotter 3784ad6
refactor constants
typotter de58164
test client using subdomain endpoint
typotter fc55a0c
polish tests
typotter 109d083
more tests
typotter 7045e4c
add to PR templates
typotter a85a492
v4.15.0
typotter d2b501d
tweaks
typotter 3541aa1
better docs and helper
typotter 62e8b04
tweak docs
typotter 1259ec7
rename SdkKeyDecoder to SdkTokenDecoder
typotter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,249 @@ | ||
import * as td from 'testdouble'; | ||
|
||
import ApiEndpoints from './api-endpoints'; | ||
import { BASE_URL as DEFAULT_BASE_URL } from './constants'; | ||
import { BASE_URL as DEFAULT_BASE_URL, DEFAULT_EVENT_DOMAIN } from './constants'; | ||
import SdkTokenDecoder from './sdk-token-decoder'; | ||
|
||
describe('ApiEndpoints', () => { | ||
it('should append query parameters to the URL', () => { | ||
const apiEndpoints = new ApiEndpoints({ | ||
baseUrl: 'http://api.example.com', | ||
queryParams: { | ||
apiKey: '12345', | ||
sdkVersion: 'foobar', | ||
sdkName: 'ExampleSDK', | ||
describe('Query parameters', () => { | ||
describe('should correctly handle query parameters in various scenarios', () => { | ||
const testCases = [ | ||
{ | ||
name: 'with custom base URL and query params', | ||
params: { | ||
baseUrl: 'http://api.example.com', | ||
queryParams: { | ||
apiKey: '12345', | ||
sdkVersion: 'foobar', | ||
sdkName: 'ExampleSDK', | ||
}, | ||
}, | ||
expected: | ||
'http://api.example.com/flag-config/v1/config?apiKey=12345&sdkVersion=foobar&sdkName=ExampleSDK', | ||
}, | ||
{ | ||
name: 'with default base URL and query params', | ||
params: { | ||
queryParams: { | ||
apiKey: '12345', | ||
sdkVersion: 'foobar', | ||
sdkName: 'ExampleSDK', | ||
}, | ||
}, | ||
expected: `${DEFAULT_BASE_URL}/flag-config/v1/config?apiKey=12345&sdkVersion=foobar&sdkName=ExampleSDK`, | ||
}, | ||
{ | ||
name: 'without query params', | ||
params: {}, | ||
expected: `${DEFAULT_BASE_URL}/flag-config/v1/config`, | ||
}, | ||
{ | ||
name: 'with special characters in query params', | ||
params: { | ||
queryParams: { | ||
apiKey: 'test-key', | ||
sdkName: 'value with spaces', | ||
sdkVersion: 'a+b=c&d', | ||
}, | ||
}, | ||
expected: | ||
'https://fscdn.eppo.cloud/api/flag-config/v1/config?apiKey=test-key&sdkName=value+with+spaces&sdkVersion=a%2Bb%3Dc%26d', | ||
}, | ||
]; | ||
|
||
testCases.forEach(({ name, params, expected }) => { | ||
it(`${name}`, () => { | ||
const apiEndpoints = new ApiEndpoints(params); | ||
const result = apiEndpoints.ufcEndpoint(); | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
typotter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
describe('Base URL determination', () => { | ||
const testCases = [ | ||
{ | ||
name: 'should use custom baseUrl when provided', | ||
params: { baseUrl: 'https://custom-domain.com' }, | ||
expected: 'https://custom-domain.com/assignments', | ||
}, | ||
{ | ||
name: 'should use subdomain from SDK token when valid', | ||
params: { sdkTokenDecoder: new SdkTokenDecoder('abc.Y3M9dGVzdC1zdWJkb21haW4=') }, | ||
expected: 'https://test-subdomain.fscdn.eppo.cloud/api/assignments', | ||
}, | ||
{ | ||
name: 'should prefer custom baseUrl over SDK token subdomain', | ||
params: { | ||
baseUrl: 'https://custom-domain.com', | ||
sdkTokenDecoder: new SdkTokenDecoder('abc.Y3M9dGVzdC1zdWJkb21haW4='), | ||
}, | ||
expected: 'https://custom-domain.com/assignments', | ||
}, | ||
{ | ||
name: 'should not allow custom baseUrl to be the default base url', | ||
params: { | ||
baseUrl: DEFAULT_BASE_URL, | ||
sdkTokenDecoder: new SdkTokenDecoder('abc.Y3M9dGVzdC1zdWJkb21haW4='), | ||
}, | ||
expected: 'https://test-subdomain.fscdn.eppo.cloud/api/assignments', | ||
}, | ||
{ | ||
name: 'should fallback to DEFAULT_BASE_URL when SDK token has no subdomain', | ||
params: { sdkTokenDecoder: new SdkTokenDecoder('abc.ZWg9ZXZlbnQtaG9zdG5hbWU=') }, | ||
expected: 'https://fscdn.eppo.cloud/api/assignments', | ||
}, | ||
{ | ||
name: 'should fallback to DEFAULT_BASE_URL when SDK token has nothing encoded', | ||
params: { sdkTokenDecoder: new SdkTokenDecoder('invalid-token') }, | ||
expected: 'https://fscdn.eppo.cloud/api/assignments', | ||
}, | ||
]; | ||
|
||
testCases.forEach(({ name, params, expected }) => { | ||
it(name, () => { | ||
const endpoints = new ApiEndpoints(params); | ||
const result = endpoints.precomputedFlagsEndpoint(); | ||
|
||
expect(result).toBe(expected); | ||
}); | ||
}); | ||
expect(apiEndpoints.endpoint('/data').toString()).toEqual( | ||
'http://api.example.com/data?apiKey=12345&sdkVersion=foobar&sdkName=ExampleSDK', | ||
); | ||
expect(apiEndpoints.ufcEndpoint().toString()).toEqual( | ||
'http://api.example.com/flag-config/v1/config?apiKey=12345&sdkVersion=foobar&sdkName=ExampleSDK', | ||
); | ||
}); | ||
|
||
it('should use default base URL if not provided', () => { | ||
const apiEndpoints = new ApiEndpoints({ | ||
queryParams: { | ||
apiKey: '12345', | ||
sdkVersion: 'foobar', | ||
sdkName: 'ExampleSDK', | ||
describe('Endpoint URL construction', () => { | ||
const sdkTokenDecoder = new SdkTokenDecoder('abc.Y3M9dGVzdC1zdWJkb21haW4='); // cs=test-subdomain | ||
|
||
const endpointTestCases = [ | ||
{ | ||
name: 'UFC endpoint with subdomain', | ||
factory: (api: ApiEndpoints) => api.ufcEndpoint(), | ||
expected: 'https://test-subdomain.fscdn.eppo.cloud/api/flag-config/v1/config', | ||
}, | ||
{ | ||
name: 'bandit parameters endpoint with subdomain', | ||
factory: (api: ApiEndpoints) => api.banditParametersEndpoint(), | ||
expected: 'https://test-subdomain.fscdn.eppo.cloud/api/flag-config/v1/bandits', | ||
}, | ||
]; | ||
|
||
endpointTestCases.forEach(({ name, factory, expected }) => { | ||
it(name, () => { | ||
const endpoints = new ApiEndpoints({ sdkTokenDecoder: sdkTokenDecoder }); | ||
const result = factory(endpoints); | ||
expect(result).toBe(expected); | ||
}); | ||
}); | ||
expect(apiEndpoints.endpoint('/data').toString()).toEqual( | ||
`${DEFAULT_BASE_URL}/data?apiKey=12345&sdkVersion=foobar&sdkName=ExampleSDK`, | ||
); | ||
expect(apiEndpoints.ufcEndpoint().toString()).toEqual( | ||
`${DEFAULT_BASE_URL}/flag-config/v1/config?apiKey=12345&sdkVersion=foobar&sdkName=ExampleSDK`, | ||
); | ||
}); | ||
|
||
it('should not append query parameters if not provided', () => { | ||
const apiEndpoints = new ApiEndpoints({}); | ||
expect(apiEndpoints.endpoint('/data').toString()).toEqual(`${DEFAULT_BASE_URL}/data`); | ||
expect(apiEndpoints.ufcEndpoint().toString()).toEqual( | ||
`${DEFAULT_BASE_URL}/flag-config/v1/config`, | ||
describe('Event ingestion URL', () => { | ||
const hostnameToken = new SdkTokenDecoder( | ||
'zCsQuoHJxVPp895.ZWg9MTIzNDU2LmUudGVzdGluZy5lcHBvLmNsb3Vk', | ||
); | ||
let mockedDecoder: SdkTokenDecoder; | ||
|
||
beforeEach(() => { | ||
mockedDecoder = td.object<SdkTokenDecoder>(); | ||
td.when(mockedDecoder.isValid()).thenReturn(true); | ||
}); | ||
|
||
const eventUrlTestCases = [ | ||
{ | ||
name: 'should decode the event ingestion hostname from the SDK key', | ||
setupDecoder: () => hostnameToken, | ||
expected: 'https://123456.e.testing.eppo.cloud/v0/i', | ||
}, | ||
{ | ||
name: 'should decode strings with non URL-safe characters', | ||
setupDecoder: () => { | ||
td.when(mockedDecoder.getEventIngestionHostname()).thenReturn( | ||
'12 3456/.e.testing.eppo.cloud', | ||
); | ||
return mockedDecoder; | ||
}, | ||
expected: 'https://12 3456/.e.testing.eppo.cloud/v0/i', | ||
}, | ||
{ | ||
name: 'should return null if the SDK key is invalid', | ||
setupDecoder: () => { | ||
td.when(mockedDecoder.isValid()).thenReturn(false); | ||
return mockedDecoder; | ||
}, | ||
expected: null, | ||
}, | ||
{ | ||
name: 'should use subdomain with DEFAULT_EVENT_DOMAIN when hostname is not available', | ||
setupDecoder: () => { | ||
td.when(mockedDecoder.getEventIngestionHostname()).thenReturn(null); | ||
td.when(mockedDecoder.getSubdomain()).thenReturn('test-subdomain'); | ||
return mockedDecoder; | ||
}, | ||
expected: `https://test-subdomain.${DEFAULT_EVENT_DOMAIN}/v0/i`, | ||
}, | ||
{ | ||
name: 'should prioritize hostname over subdomain if both are available', | ||
setupDecoder: () => { | ||
td.when(mockedDecoder.getEventIngestionHostname()).thenReturn('event-host.example.com'); | ||
td.when(mockedDecoder.getSubdomain()).thenReturn('test-subdomain'); | ||
return mockedDecoder; | ||
}, | ||
expected: 'https://event-host.example.com/v0/i', | ||
}, | ||
{ | ||
name: 'should return null when token is valid but no hostname or subdomain is available', | ||
setupDecoder: () => { | ||
td.when(mockedDecoder.getEventIngestionHostname()).thenReturn(null); | ||
td.when(mockedDecoder.getSubdomain()).thenReturn(null); | ||
return mockedDecoder; | ||
}, | ||
expected: null, | ||
}, | ||
]; | ||
|
||
eventUrlTestCases.forEach(({ name, setupDecoder, expected }) => { | ||
it(name, () => { | ||
const decoder = setupDecoder(); | ||
const endpoints = new ApiEndpoints({ sdkTokenDecoder: decoder }); | ||
expect(endpoints.eventIngestionEndpoint()).toEqual(expected); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('URL normalization', () => { | ||
const urlNormalizationTestCases = [ | ||
{ | ||
name: 'preserve http:// protocol', | ||
baseUrl: 'http://example.com', | ||
expected: 'http://example.com/flag-config/v1/config', | ||
}, | ||
{ | ||
name: 'preserve https:// protocol', | ||
baseUrl: 'https://example.com', | ||
expected: 'https://example.com/flag-config/v1/config', | ||
}, | ||
{ | ||
name: 'preserve // protocol', | ||
baseUrl: '//example.com', | ||
expected: '//example.com/flag-config/v1/config', | ||
}, | ||
{ | ||
name: 'add https:// to URLs without protocols', | ||
baseUrl: 'example.com', | ||
expected: 'https://example.com/flag-config/v1/config', | ||
}, | ||
{ | ||
name: 'handle multiple slashes', | ||
baseUrl: 'example.com/', | ||
expected: 'https://example.com/flag-config/v1/config', | ||
}, | ||
]; | ||
|
||
urlNormalizationTestCases.forEach(({ name, baseUrl, expected }) => { | ||
it(`should ${name}`, () => { | ||
const endpoints = new ApiEndpoints({ baseUrl }); | ||
expect(endpoints.ufcEndpoint()).toEqual(expected); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.