mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-12-11 00:33:47 -06:00
Test api client builder when credentials are forwarded
This commit is contained in:
parent
e12cd68010
commit
1467c8e416
@ -6,8 +6,8 @@ import type { ReachableServer, SelectedServer } from '../../../src/servers/data'
|
||||
describe('ShlinkApiClientBuilder', () => {
|
||||
const server = fromPartial<ReachableServer>;
|
||||
|
||||
const createBuilder = () => {
|
||||
const builder = buildShlinkApiClient(fromPartial({}));
|
||||
const createBuilder = (httpClient: HttpClient = fromPartial({})) => {
|
||||
const builder = buildShlinkApiClient(httpClient);
|
||||
return (selectedServer: SelectedServer) => builder(() => fromPartial({ selectedServer }));
|
||||
};
|
||||
|
||||
@ -17,9 +17,9 @@ describe('ShlinkApiClientBuilder', () => {
|
||||
const secondApiClient = builder(server({ url: 'bar', apiKey: 'bar' }));
|
||||
const thirdApiClient = builder(server({ url: 'bar', apiKey: 'foo' }));
|
||||
|
||||
expect(firstApiClient === secondApiClient).toEqual(false);
|
||||
expect(firstApiClient === thirdApiClient).toEqual(false);
|
||||
expect(secondApiClient === thirdApiClient).toEqual(false);
|
||||
expect(firstApiClient).not.toBe(secondApiClient);
|
||||
expect(firstApiClient).not.toBe(thirdApiClient);
|
||||
expect(secondApiClient).not.toBe(thirdApiClient);
|
||||
});
|
||||
|
||||
it('returns existing instances when provided params are the same', () => {
|
||||
@ -30,21 +30,39 @@ describe('ShlinkApiClientBuilder', () => {
|
||||
const secondApiClient = builder(selectedServer);
|
||||
const thirdApiClient = builder(selectedServer);
|
||||
|
||||
expect(firstApiClient === secondApiClient).toEqual(true);
|
||||
expect(firstApiClient === thirdApiClient).toEqual(true);
|
||||
expect(secondApiClient === thirdApiClient).toEqual(true);
|
||||
expect(firstApiClient).toBe(secondApiClient);
|
||||
expect(firstApiClient).toBe(thirdApiClient);
|
||||
expect(secondApiClient).toBe(thirdApiClient);
|
||||
});
|
||||
|
||||
it('does not fetch from state when provided param is already selected server', async () => {
|
||||
it('does not fetch from state when provided param is already a server', async () => {
|
||||
const url = 'the_url';
|
||||
const apiKey = 'the_api_key';
|
||||
const jsonRequest = vi.fn();
|
||||
const httpClient = fromPartial<HttpClient>({ jsonRequest });
|
||||
const apiClient = buildShlinkApiClient(httpClient)(server({ url, apiKey }));
|
||||
const apiClient = createBuilder(httpClient)(server({ url, apiKey }));
|
||||
|
||||
await apiClient.health();
|
||||
|
||||
expect(jsonRequest).toHaveBeenCalledWith(expect.stringMatching(new RegExp(`^${url}`)), expect.objectContaining({
|
||||
credentials: undefined,
|
||||
headers: {
|
||||
'X-Api-Key': apiKey,
|
||||
},
|
||||
}));
|
||||
});
|
||||
|
||||
it('includes credentials when forwarding is enabled', async () => {
|
||||
const url = 'the_url';
|
||||
const apiKey = 'the_api_key';
|
||||
const jsonRequest = vi.fn();
|
||||
const httpClient = fromPartial<HttpClient>({ jsonRequest });
|
||||
const apiClient = createBuilder(httpClient)(server({ url, apiKey, forwardCredentials: true }));
|
||||
|
||||
await apiClient.health();
|
||||
|
||||
expect(jsonRequest).toHaveBeenCalledWith(expect.stringMatching(new RegExp(`^${url}`)), expect.objectContaining({
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'X-Api-Key': apiKey,
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user