Makes HTTPResponse serializable (#2143)

* Use the callback body instead of response.body that may not be set

* Adds test to handle undefined responses

* Adds toJSON method to properly serialize HTTPResponse

* Use ES5 defineProperty to make keys enumerable

* removes body key from serialization

* Indent nits
This commit is contained in:
Florent Vilmart
2016-06-25 13:56:02 -04:00
committed by Tyler Brock
parent 35b4c063fb
commit 3e413683f7
3 changed files with 122 additions and 16 deletions

View File

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

View File

@@ -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;
}
return this._data;
let getText = () => {
if (!_text && this.buffer) {
_text = this.buffer.toString('utf-8');
} else if (!_text && _data) {
_text = JSON.stringify(_data);
}
return _text;
}
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
});
}
}

View File

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