From 64e239b09258c9433ecd57b755967f6cccafa5e2 Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Sat, 8 Oct 2016 19:40:56 -0700 Subject: [PATCH] Improve Body spec compliance when body is null --- src/body.js | 5 +++++ src/response.js | 2 +- test/test.js | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/body.js b/src/body.js index 962232f..349657e 100644 --- a/src/body.js +++ b/src/body.js @@ -79,6 +79,11 @@ export default class Body { this[DISTURBED] = true; + // body is null + if (!this.body) { + return Body.Promise.resolve(new Buffer(0)); + } + // body is string if (typeof this.body === 'string') { return Body.Promise.resolve(convertBody([new Buffer(this.body)], this.headers)); diff --git a/src/response.js b/src/response.js index fb53a78..bc3175b 100644 --- a/src/response.js +++ b/src/response.js @@ -17,7 +17,7 @@ import Body, { clone } from './body'; * @return Void */ export default class Response extends Body { - constructor(body, opts = {}) { + constructor(body = null, opts = {}) { super(body, opts); this.url = opts.url; diff --git a/test/test.js b/test/test.js index 180953d..34880c2 100644 --- a/test/test.js +++ b/test/test.js @@ -1427,6 +1427,14 @@ describe('node-fetch', () => { }); }); + it('should default to null as body', function() { + const res = new Response(); + expect(res.body).to.equal(null); + return res.text().then(result => { + expect(result).to.equal(''); + }); + }); + it('should default to 200 as status code', function() { const res = new Response(null); expect(res.status).to.equal(200);