Skip to content

Makes HTTPResponse serializable #2143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions spec/HTTPRequest.spec.js
Original file line number Diff line number Diff line change
@@ -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");

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

});
58 changes: 43 additions & 15 deletions src/cloud-code/HTTPResponse.js
Original file line number Diff line number Diff line change
@@ -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
});
}
}
2 changes: 1 addition & 1 deletion src/cloud-code/httpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down