mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-15 12:03:46 +00:00
Test api client builder when credentials are forwarded
This commit is contained in:
@@ -6,8 +6,8 @@ import type { ReachableServer, SelectedServer } from '../../../src/servers/data'
|
|||||||
describe('ShlinkApiClientBuilder', () => {
|
describe('ShlinkApiClientBuilder', () => {
|
||||||
const server = fromPartial<ReachableServer>;
|
const server = fromPartial<ReachableServer>;
|
||||||
|
|
||||||
const createBuilder = () => {
|
const createBuilder = (httpClient: HttpClient = fromPartial({})) => {
|
||||||
const builder = buildShlinkApiClient(fromPartial({}));
|
const builder = buildShlinkApiClient(httpClient);
|
||||||
return (selectedServer: SelectedServer) => builder(() => fromPartial({ selectedServer }));
|
return (selectedServer: SelectedServer) => builder(() => fromPartial({ selectedServer }));
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -17,9 +17,9 @@ describe('ShlinkApiClientBuilder', () => {
|
|||||||
const secondApiClient = builder(server({ url: 'bar', apiKey: 'bar' }));
|
const secondApiClient = builder(server({ url: 'bar', apiKey: 'bar' }));
|
||||||
const thirdApiClient = builder(server({ url: 'bar', apiKey: 'foo' }));
|
const thirdApiClient = builder(server({ url: 'bar', apiKey: 'foo' }));
|
||||||
|
|
||||||
expect(firstApiClient === secondApiClient).toEqual(false);
|
expect(firstApiClient).not.toBe(secondApiClient);
|
||||||
expect(firstApiClient === thirdApiClient).toEqual(false);
|
expect(firstApiClient).not.toBe(thirdApiClient);
|
||||||
expect(secondApiClient === thirdApiClient).toEqual(false);
|
expect(secondApiClient).not.toBe(thirdApiClient);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns existing instances when provided params are the same', () => {
|
it('returns existing instances when provided params are the same', () => {
|
||||||
@@ -30,21 +30,39 @@ describe('ShlinkApiClientBuilder', () => {
|
|||||||
const secondApiClient = builder(selectedServer);
|
const secondApiClient = builder(selectedServer);
|
||||||
const thirdApiClient = builder(selectedServer);
|
const thirdApiClient = builder(selectedServer);
|
||||||
|
|
||||||
expect(firstApiClient === secondApiClient).toEqual(true);
|
expect(firstApiClient).toBe(secondApiClient);
|
||||||
expect(firstApiClient === thirdApiClient).toEqual(true);
|
expect(firstApiClient).toBe(thirdApiClient);
|
||||||
expect(secondApiClient === thirdApiClient).toEqual(true);
|
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 url = 'the_url';
|
||||||
const apiKey = 'the_api_key';
|
const apiKey = 'the_api_key';
|
||||||
const jsonRequest = vi.fn();
|
const jsonRequest = vi.fn();
|
||||||
const httpClient = fromPartial<HttpClient>({ jsonRequest });
|
const httpClient = fromPartial<HttpClient>({ jsonRequest });
|
||||||
const apiClient = buildShlinkApiClient(httpClient)(server({ url, apiKey }));
|
const apiClient = createBuilder(httpClient)(server({ url, apiKey }));
|
||||||
|
|
||||||
await apiClient.health();
|
await apiClient.health();
|
||||||
|
|
||||||
expect(jsonRequest).toHaveBeenCalledWith(expect.stringMatching(new RegExp(`^${url}`)), expect.objectContaining({
|
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: {
|
headers: {
|
||||||
'X-Api-Key': apiKey,
|
'X-Api-Key': apiKey,
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user