diff --git a/src/__test__/fetch-util.spec.js b/src/__test__/fetch-util.spec.js new file mode 100644 index 0000000..509f72f --- /dev/null +++ b/src/__test__/fetch-util.spec.js @@ -0,0 +1,28 @@ +// @flow +/* eslint-env jest */ + +import { copyHeaders } from '../fetch-util' +import { polyfillWindow, polyunfillWindow } from './spec-helpers' + +describe('copyHeaders', () => { + beforeEach(() => polyfillWindow()) + afterEach(() => polyunfillWindow()) + + it('returns an object', () => expect(copyHeaders({})).toEqual({})) + it('returns a copy of whatever is passed in as first param', () => + expect(copyHeaders({ foo: 42 })).toEqual({ foo: 42 })) + it('handles option.headers as object', () => + expect(copyHeaders({ foo: 42 }, { headers: { bar: 1337 } })).toEqual({ + foo: 42, + bar: 1337 + })) + xit('handles options.headers as Headers', () => { + // TODO: Need to have Headers working + const headers = new window.Headers() + headers.append('bar', '1337') + expect(copyHeaders({ foo: '42' }, { headers })).toEqual({ + foo: '42', + bar: '1337' + }) + }) +}) diff --git a/src/__test__/spec-helpers.js b/src/__test__/spec-helpers.js index e1c4527..b1a17d5 100644 --- a/src/__test__/spec-helpers.js +++ b/src/__test__/spec-helpers.js @@ -31,6 +31,23 @@ export const polyfillWindow = () => { value: window.origin }) MessageEvent.prototype.origin = window.location.origin + + // in case Headers is not available + // Object.defineProperty(window, 'Headers', { + // value: class Headers { + // constructor() { + // this.map = {} + // } + // + // append(key, value) { + // this.map[key] = value + // } + // + // forEach(callback) { + // Object.keys(this.map).forEach(key => callback(this.map[key], key)) + // } + // } + // }) } export const polyunfillWindow = () => { diff --git a/src/fetch-util.js b/src/fetch-util.js new file mode 100644 index 0000000..43983e8 --- /dev/null +++ b/src/fetch-util.js @@ -0,0 +1,17 @@ +export function copyHeaders(newHeaders, oldOptions = {}) { + return { + ...normalizeHeaders(oldOptions.headers), + ...normalizeHeaders(newHeaders) + } +} + +function normalizeHeaders(headers = {}) { + if (!headers.forEach) { + return headers + } + const newHeaders = {} + headers.forEach((value, key) => { + newHeaders[key] = value + }) + return newHeaders +} diff --git a/src/webid-oidc.js b/src/webid-oidc.js index a23d25c..75b3a97 100644 --- a/src/webid-oidc.js +++ b/src/webid-oidc.js @@ -9,6 +9,7 @@ import { currentUrl, navigateTo, toUrlString } from './url-util' import type { webIdOidcSession } from './session' import type { AsyncStorage } from './storage' import { defaultStorage, getData, updateStorage } from './storage' +import { copyHeaders } from './fetch-util' export async function login( idp: string, @@ -191,10 +192,12 @@ export async function fetchWithCredentials( const authenticatedOptions = { ...options, credentials: 'include', - headers: { - ...(options && options.headers ? options.headers : {}), - authorization: `Bearer ${popToken}` - } + headers: copyHeaders( + { + authorization: `Bearer ${popToken}` + }, + options + ) } return fetch(input, authenticatedOptions) }