diff --git a/src/__tests__/modules/FSXAApi.spec.ts b/src/__tests__/modules/FSXAApi.spec.ts index 5efcaed..3775431 100644 --- a/src/__tests__/modules/FSXAApi.spec.ts +++ b/src/__tests__/modules/FSXAApi.spec.ts @@ -170,18 +170,32 @@ describe('FSXAApi', () => { describe('fetchPage', async () => { it('should call local url in proxy-mode', async () => { - testForLocalUrl(api => api.fetchElement('foobar', 'de_DE')) + await testForLocalUrl(api => api.fetchElement('foobar', 'de_DE')) }) }) describe('fetchNavigation', async () => { it('should call local url in proxy-mode', async () => { - testForLocalUrl(api => api.fetchNavigation(null, 'de_DE')) + await testForLocalUrl(api => api.fetchNavigation(null, 'de_DE')) + }) + + it('should pass extraHeaders', async () => { + await testForLocalUrl( + api => api.fetchNavigation(null, 'de_DE', { 'x-test': 'foobar' }), + fetch => { + const request = fetch.mock.calls[0][1] + const headers = request && request['headers'] + expect(headers && 'x-test' in headers && headers['x-test']).toBe('foobar') + } + ) }) }) }) -const testForLocalUrl = async (method: (api: FSXAApi) => Promise) => { +const testForLocalUrl = async ( + method: (api: FSXAApi) => Promise, + validate?: (fetch: FetchMock) => void +) => { fetchMock.enableMocks() const api = new FSXAApi(FSXAContentMode.PREVIEW, { mode: 'proxy', @@ -193,5 +207,6 @@ const testForLocalUrl = async (method: (api: FSXAApi) => Promise) => { await method(api) expect(mockedFetch).toHaveBeenCalledTimes(1) expect(mockedFetch.mock.calls[0][0]).toMatch(/localhost/g) + validate && validate(fetchMock) fetchMock.disableMocks() } diff --git a/src/modules/FSXAApi.ts b/src/modules/FSXAApi.ts index f7642d3..62be236 100644 --- a/src/modules/FSXAApi.ts +++ b/src/modules/FSXAApi.ts @@ -315,7 +315,8 @@ export class FSXAApi { async fetchNavigation( initialPath: string | null, - defaultLocale: string + defaultLocale: string, + extraHeaders?: Record ): Promise { const encodedInitialPath = initialPath ? encodeURI(initialPath) : null if (this.params.mode === 'proxy') { @@ -327,7 +328,8 @@ export class FSXAApi { const response = await fetch(url, { method: 'POST', headers: { - 'Content-Type': 'application/json' + 'Content-Type': 'application/json', + ...extraHeaders }, body: JSON.stringify({ initialPath, @@ -360,7 +362,8 @@ export class FSXAApi { }) const response = await fetch(url, { headers: { - 'Accept-Language': '*' + 'Accept-Language': '*', + ...extraHeaders } })