Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/__tests__/modules/FSXAApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>) => {
const testForLocalUrl = async (
method: (api: FSXAApi) => Promise<any>,
validate?: (fetch: FetchMock) => void
) => {
fetchMock.enableMocks()
const api = new FSXAApi(FSXAContentMode.PREVIEW, {
mode: 'proxy',
Expand All @@ -193,5 +207,6 @@ const testForLocalUrl = async (method: (api: FSXAApi) => Promise<any>) => {
await method(api)
expect(mockedFetch).toHaveBeenCalledTimes(1)
expect(mockedFetch.mock.calls[0][0]).toMatch(/localhost/g)
validate && validate(fetchMock)
fetchMock.disableMocks()
}
9 changes: 6 additions & 3 deletions src/modules/FSXAApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ export class FSXAApi {

async fetchNavigation(
initialPath: string | null,
defaultLocale: string
defaultLocale: string,
extraHeaders?: Record<string, string>
): Promise<NavigationData | null> {
const encodedInitialPath = initialPath ? encodeURI(initialPath) : null
if (this.params.mode === 'proxy') {
Expand All @@ -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,
Expand Down Expand Up @@ -360,7 +362,8 @@ export class FSXAApi {
})
const response = await fetch(url, {
headers: {
'Accept-Language': '*'
'Accept-Language': '*',
...extraHeaders
}
})

Expand Down