Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default typeof fetch=='function' ? fetch.bind() : function(url, options)
headers = {},
header;

request.getAllResponseHeaders().replace(/^(.*?):\s*?([\s\S]*?)$/gm, (m, key, value) => {
request.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm, (m, key, value) => {
keys.push(key = key.toLowerCase());
all.push([key, value]);
header = headers[key];
Expand Down
27 changes: 23 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ describe('unfetch', () => {
});

describe('fetch()', () => {
it('sanity test', () => {
let xhr = {
let xhr;

beforeEach(() => {
xhr = {
setRequestHeader: spy(),
getAllResponseHeaders: stub().returns('X-Foo: bar\nX-Foo:baz'),
open: spy(),
Expand All @@ -40,7 +42,13 @@ describe('unfetch', () => {
};

global.XMLHttpRequest = stub().returns(xhr);
});

afterEach(() => {
delete global.XMLHttpRequest;
});

it('sanity test', () => {
let p = fetch('/foo', { headers: { a: 'b' } })
.then( r => {
expect(r).to.have.property('text').that.is.a('function');
Expand All @@ -60,8 +68,6 @@ describe('unfetch', () => {
expect(xhr.setRequestHeader).to.have.been.calledOnce.and.calledWith('a', 'b');
expect(xhr.open).to.have.been.calledOnce.and.calledWith('get', '/foo');
expect(xhr.send).to.have.been.calledOnce.and.calledWith();

delete global.XMLHttpRequest;
});

expect(xhr.onload).to.be.a('function');
Expand All @@ -71,5 +77,18 @@ describe('unfetch', () => {

return p;
});

it('handles empty header values', () => {
xhr.getAllResponseHeaders = stub().returns('Server: \nX-Foo:baz');
let p = fetch('/foo')
.then(r => {
expect(r.headers.get('server')).to.equal('');
expect(r.headers.get('X-foo')).to.equal('baz');
});

xhr.onload();

return p;
});
});
});