Skip to content

Commit 64e239b

Browse files
committed
Improve Body spec compliance when body is null
1 parent 9d3cc52 commit 64e239b

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src/body.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ export default class Body {
7979

8080
this[DISTURBED] = true;
8181

82+
// body is null
83+
if (!this.body) {
84+
return Body.Promise.resolve(new Buffer(0));
85+
}
86+
8287
// body is string
8388
if (typeof this.body === 'string') {
8489
return Body.Promise.resolve(convertBody([new Buffer(this.body)], this.headers));

src/response.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Body, { clone } from './body';
1717
* @return Void
1818
*/
1919
export default class Response extends Body {
20-
constructor(body, opts = {}) {
20+
constructor(body = null, opts = {}) {
2121
super(body, opts);
2222

2323
this.url = opts.url;

test/test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,14 @@ describe('node-fetch', () => {
14271427
});
14281428
});
14291429

1430+
it('should default to null as body', function() {
1431+
const res = new Response();
1432+
expect(res.body).to.equal(null);
1433+
return res.text().then(result => {
1434+
expect(result).to.equal('');
1435+
});
1436+
});
1437+
14301438
it('should default to 200 as status code', function() {
14311439
const res = new Response(null);
14321440
expect(res.status).to.equal(200);

0 commit comments

Comments
 (0)