Skip to content

Commit 504903a

Browse files
authored
Merge pull request #67 from patrickhulce/fix_tests
fix: handle 0-length header values and trim whitespace
2 parents 13a7577 + d0e5543 commit 504903a

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default typeof fetch=='function' ? fetch.bind() : function(url, options)
2525
headers = {},
2626
header;
2727

28-
request.getAllResponseHeaders().replace(/^(.*?):\s*?([\s\S]*?)$/gm, (m, key, value) => {
28+
request.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm, (m, key, value) => {
2929
keys.push(key = key.toLowerCase());
3030
all.push([key, value]);
3131
header = headers[key];

test/index.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ describe('unfetch', () => {
2727
});
2828

2929
describe('fetch()', () => {
30-
it('sanity test', () => {
31-
let xhr = {
30+
let xhr;
31+
32+
beforeEach(() => {
33+
xhr = {
3234
setRequestHeader: spy(),
3335
getAllResponseHeaders: stub().returns('X-Foo: bar\nX-Foo:baz'),
3436
open: spy(),
@@ -40,7 +42,13 @@ describe('unfetch', () => {
4042
};
4143

4244
global.XMLHttpRequest = stub().returns(xhr);
45+
});
4346

47+
afterEach(() => {
48+
delete global.XMLHttpRequest;
49+
});
50+
51+
it('sanity test', () => {
4452
let p = fetch('/foo', { headers: { a: 'b' } })
4553
.then( r => {
4654
expect(r).to.have.property('text').that.is.a('function');
@@ -60,8 +68,6 @@ describe('unfetch', () => {
6068
expect(xhr.setRequestHeader).to.have.been.calledOnce.and.calledWith('a', 'b');
6169
expect(xhr.open).to.have.been.calledOnce.and.calledWith('get', '/foo');
6270
expect(xhr.send).to.have.been.calledOnce.and.calledWith();
63-
64-
delete global.XMLHttpRequest;
6571
});
6672

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

7278
return p;
7379
});
80+
81+
it('handles empty header values', () => {
82+
xhr.getAllResponseHeaders = stub().returns('Server: \nX-Foo:baz');
83+
let p = fetch('/foo')
84+
.then(r => {
85+
expect(r.headers.get('server')).to.equal('');
86+
expect(r.headers.get('X-foo')).to.equal('baz');
87+
});
88+
89+
xhr.onload();
90+
91+
return p;
92+
});
7493
});
7594
});

0 commit comments

Comments
 (0)