Improve Body spec compliance when body is null

This commit is contained in:
Timothy Gu 2016-10-08 19:40:56 -07:00
parent 9d3cc52601
commit 64e239b092
3 changed files with 14 additions and 1 deletions

View File

@ -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));

View File

@ -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;

View File

@ -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);