Skip to content

Commit 62b749d

Browse files
feat(navigation / pass extra headers): allow passing of extra headers to navigation service (#22)
#21 Co-authored-by: Kolja Markwardt <kolja.markwardt@edeka.de>
1 parent 9208eb5 commit 62b749d

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/__tests__/modules/FSXAApi.spec.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,32 @@ describe('FSXAApi', () => {
170170

171171
describe('fetchPage', async () => {
172172
it('should call local url in proxy-mode', async () => {
173-
testForLocalUrl(api => api.fetchElement('foobar', 'de_DE'))
173+
await testForLocalUrl(api => api.fetchElement('foobar', 'de_DE'))
174174
})
175175
})
176176

177177
describe('fetchNavigation', async () => {
178178
it('should call local url in proxy-mode', async () => {
179-
testForLocalUrl(api => api.fetchNavigation(null, 'de_DE'))
179+
await testForLocalUrl(api => api.fetchNavigation(null, 'de_DE'))
180+
})
181+
182+
it('should pass extraHeaders', async () => {
183+
await testForLocalUrl(
184+
api => api.fetchNavigation(null, 'de_DE', { 'x-test': 'foobar' }),
185+
fetch => {
186+
const request = fetch.mock.calls[0][1]
187+
const headers = request && request['headers']
188+
expect(headers && 'x-test' in headers && headers['x-test']).toBe('foobar')
189+
}
190+
)
180191
})
181192
})
182193
})
183194

184-
const testForLocalUrl = async (method: (api: FSXAApi) => Promise<any>) => {
195+
const testForLocalUrl = async (
196+
method: (api: FSXAApi) => Promise<any>,
197+
validate?: (fetch: FetchMock) => void
198+
) => {
185199
fetchMock.enableMocks()
186200
const api = new FSXAApi(FSXAContentMode.PREVIEW, {
187201
mode: 'proxy',
@@ -193,5 +207,6 @@ const testForLocalUrl = async (method: (api: FSXAApi) => Promise<any>) => {
193207
await method(api)
194208
expect(mockedFetch).toHaveBeenCalledTimes(1)
195209
expect(mockedFetch.mock.calls[0][0]).toMatch(/localhost/g)
210+
validate && validate(fetchMock)
196211
fetchMock.disableMocks()
197212
}

src/modules/FSXAApi.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ export class FSXAApi {
315315

316316
async fetchNavigation(
317317
initialPath: string | null,
318-
defaultLocale: string
318+
defaultLocale: string,
319+
extraHeaders?: Record<string, string>
319320
): Promise<NavigationData | null> {
320321
const encodedInitialPath = initialPath ? encodeURI(initialPath) : null
321322
if (this.params.mode === 'proxy') {
@@ -327,7 +328,8 @@ export class FSXAApi {
327328
const response = await fetch(url, {
328329
method: 'POST',
329330
headers: {
330-
'Content-Type': 'application/json'
331+
'Content-Type': 'application/json',
332+
...extraHeaders
331333
},
332334
body: JSON.stringify({
333335
initialPath,
@@ -360,7 +362,8 @@ export class FSXAApi {
360362
})
361363
const response = await fetch(url, {
362364
headers: {
363-
'Accept-Language': '*'
365+
'Accept-Language': '*',
366+
...extraHeaders
364367
}
365368
})
366369

0 commit comments

Comments
 (0)