Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handling case where options.headers is of type window.Headers
- Loading branch information
Showing
3 changed files
with
49 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// @flow | ||
/* eslint-env jest */ | ||
/* global Headers */ | ||
|
||
import { copyHeaders } from '../fetch-util' | ||
|
||
describe('copyHeaders', () => { | ||
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', () => { | ||
// Enable this when there is support for window.Headers | ||
const headers = new Headers() | ||
headers.append('bar', '1337') | ||
expect(copyHeaders({ foo: '42' }, { headers })).toEqual({ | ||
foo: '42', | ||
bar: '1337' | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters