diff --git a/spec/HTTPRequest.spec.js b/spec/HTTPRequest.spec.js index e4096f5b9b..cb34fe70bb 100644 --- a/spec/HTTPRequest.spec.js +++ b/spec/HTTPRequest.spec.js @@ -1,6 +1,7 @@ 'use strict'; var httpRequest = require("../src/cloud-code/httpRequest"), + HTTPResponse = require('../src/cloud-code/HTTPResponse').default, bodyParser = require('body-parser'), express = require("express"); @@ -245,4 +246,81 @@ describe("httpRequest", () => { }) }); + it('should not crash with undefined body', () => { + let httpResponse = new HTTPResponse({}); + expect(httpResponse.body).toBeUndefined(); + expect(httpResponse.data).toBeUndefined(); + expect(httpResponse.text).toBeUndefined(); + expect(httpResponse.buffer).toBeUndefined(); + }); + + it('serialized httpResponse correctly with body string', () => { + let httpResponse = new HTTPResponse({}, 'hello'); + expect(httpResponse.text).toBe('hello'); + expect(httpResponse.data).toBe(undefined); + expect(httpResponse.body).toBe('hello'); + + let serialized = JSON.stringify(httpResponse); + let result = JSON.parse(serialized); + expect(result.text).toBe('hello'); + expect(result.data).toBe(undefined); + expect(result.body).toBe(undefined); + }); + + it('serialized httpResponse correctly with body object', () => { + let httpResponse = new HTTPResponse({}, {foo: "bar"}); + let encodedResponse = Parse._encode(httpResponse); + let serialized = JSON.stringify(httpResponse); + let result = JSON.parse(serialized); + + expect(httpResponse.text).toEqual('{"foo":"bar"}'); + expect(httpResponse.data).toEqual({foo: 'bar'}); + expect(httpResponse.body).toEqual({foo: 'bar'}); + + expect(result.text).toEqual('{"foo":"bar"}'); + expect(result.data).toEqual({foo: 'bar'}); + expect(result.body).toEqual(undefined); + }); + + it('serialized httpResponse correctly with body buffer string', () => { + let httpResponse = new HTTPResponse({}, new Buffer('hello')); + expect(httpResponse.text).toBe('hello'); + expect(httpResponse.data).toBe(undefined); + + let serialized = JSON.stringify(httpResponse); + let result = JSON.parse(serialized); + expect(result.text).toBe('hello'); + expect(result.data).toBe(undefined); + }); + + it('serialized httpResponse correctly with body buffer JSON Object', () => { + let json = '{"foo":"bar"}'; + let httpResponse = new HTTPResponse({}, new Buffer(json)); + let serialized = JSON.stringify(httpResponse); + let result = JSON.parse(serialized); + expect(result.text).toEqual('{"foo":"bar"}'); + expect(result.data).toEqual({foo: 'bar'}); + }); + + it('serialized httpResponse with Parse._encode should be allright', () => { + let json = '{"foo":"bar"}'; + let httpResponse = new HTTPResponse({}, new Buffer(json)); + let encoded = Parse._encode(httpResponse); + let foundData, foundText, foundBody = false; + for(var key in encoded) { + if (key == 'data') { + foundData = true; + } + if (key == 'text') { + foundText = true; + } + if (key == 'body') { + foundBody = true; + } + } + expect(foundData).toBe(true); + expect(foundText).toBe(true); + expect(foundBody).toBe(false); + }); + }); diff --git a/src/cloud-code/HTTPResponse.js b/src/cloud-code/HTTPResponse.js index f234f332db..8359860ee6 100644 --- a/src/cloud-code/HTTPResponse.js +++ b/src/cloud-code/HTTPResponse.js @@ -1,21 +1,49 @@ export default class HTTPResponse { - constructor(response) { + constructor(response, body) { + let _text, _data; this.status = response.statusCode; - this.headers = response.headers; - this.buffer = response.body; - this.cookies = response.headers["set-cookie"]; - } - - get text() { - return this.buffer.toString('utf-8'); - } - get data() { - if (!this._data) { - try { - this._data = JSON.parse(this.text); - } catch (e) {} + this.headers = response.headers || {}; + this.cookies = this.headers["set-cookie"]; + + if (typeof body == 'string') { + _text = body; + } else if (Buffer.isBuffer(body)) { + this.buffer = body; + } else if (typeof body == 'object') { + _data = body; + } + + let getText = () => { + if (!_text && this.buffer) { + _text = this.buffer.toString('utf-8'); + } else if (!_text && _data) { + _text = JSON.stringify(_data); + } + return _text; } - return this._data; + + let getData = () => { + if (!_data) { + try { + _data = JSON.parse(getText()); + } catch (e) {} + } + return _data; + } + + Object.defineProperty(this, 'body', { + get: () => { return body } + }); + + Object.defineProperty(this, 'text', { + enumerable: true, + get: getText + }); + + Object.defineProperty(this, 'data', { + enumerable: true, + get: getData + }); } } diff --git a/src/cloud-code/httpRequest.js b/src/cloud-code/httpRequest.js index ccea11c80d..820500733d 100644 --- a/src/cloud-code/httpRequest.js +++ b/src/cloud-code/httpRequest.js @@ -62,7 +62,7 @@ module.exports = function(options) { } return promise.reject(error); } - let httpResponse = new HTTPResponse(response); + let httpResponse = new HTTPResponse(response, body); // Consider <200 && >= 400 as errors if (httpResponse.status < 200 || httpResponse.status >= 400) {