Skip to content

Commit 4804a40

Browse files
gajusTimothyGu
authored andcommitted
Add a special case for constructing Headers with Headers (node-fetch#253)
Fixes: node-fetch#251.
1 parent a1e76b9 commit 4804a40

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/headers.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ export default class Headers {
3434
constructor(init = undefined) {
3535
this[MAP] = Object.create(null);
3636

37+
if (init instanceof Headers) {
38+
const rawHeaders = init.raw();
39+
const headerNames = Object.keys(rawHeaders);
40+
41+
for (const headerName of headerNames) {
42+
for (const value of rawHeaders[headerName]) {
43+
this.append(headerName, value);
44+
}
45+
}
46+
47+
return;
48+
}
49+
3750
// We don't worry about converting prop to ByteString here as append()
3851
// will handle it.
3952
if (init == null) {

src/response.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class Response {
2323
this.url = opts.url;
2424
this.status = opts.status || 200;
2525
this.statusText = opts.statusText || STATUS_CODES[this.status];
26+
2627
this.headers = new Headers(opts.headers);
2728

2829
Object.defineProperty(this, Symbol.toStringTag, {

test/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,18 @@ describe('node-fetch', () => {
12051205
});
12061206
});
12071207

1208+
it('should return all headers using raw()', function() {
1209+
url = `${base}cookie`;
1210+
return fetch(url).then(res => {
1211+
const expected = [
1212+
'a=1',
1213+
'b=1'
1214+
];
1215+
1216+
expect(res.headers.raw()['set-cookie']).to.deep.equal(expected);
1217+
});
1218+
});
1219+
12081220
it('should allow iterating through all headers with forEach', function() {
12091221
const headers = new Headers([
12101222
['b', '2'],

0 commit comments

Comments
 (0)