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:
committed by
Tyler Brock
parent
35b4c063fb
commit
3e413683f7
@@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var httpRequest = require("../src/cloud-code/httpRequest"),
|
var httpRequest = require("../src/cloud-code/httpRequest"),
|
||||||
|
HTTPResponse = require('../src/cloud-code/HTTPResponse').default,
|
||||||
bodyParser = require('body-parser'),
|
bodyParser = require('body-parser'),
|
||||||
express = require("express");
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,21 +1,49 @@
|
|||||||
|
|
||||||
export default class HTTPResponse {
|
export default class HTTPResponse {
|
||||||
constructor(response) {
|
constructor(response, body) {
|
||||||
|
let _text, _data;
|
||||||
this.status = response.statusCode;
|
this.status = response.statusCode;
|
||||||
this.headers = response.headers;
|
this.headers = response.headers || {};
|
||||||
this.buffer = response.body;
|
this.cookies = this.headers["set-cookie"];
|
||||||
this.cookies = response.headers["set-cookie"];
|
|
||||||
}
|
|
||||||
|
|
||||||
get text() {
|
if (typeof body == 'string') {
|
||||||
return this.buffer.toString('utf-8');
|
_text = body;
|
||||||
}
|
} else if (Buffer.isBuffer(body)) {
|
||||||
get data() {
|
this.buffer = body;
|
||||||
if (!this._data) {
|
} else if (typeof body == 'object') {
|
||||||
try {
|
_data = body;
|
||||||
this._data = JSON.parse(this.text);
|
|
||||||
} catch (e) {}
|
|
||||||
}
|
}
|
||||||
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
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ module.exports = function(options) {
|
|||||||
}
|
}
|
||||||
return promise.reject(error);
|
return promise.reject(error);
|
||||||
}
|
}
|
||||||
let httpResponse = new HTTPResponse(response);
|
let httpResponse = new HTTPResponse(response, body);
|
||||||
|
|
||||||
// Consider <200 && >= 400 as errors
|
// Consider <200 && >= 400 as errors
|
||||||
if (httpResponse.status < 200 || httpResponse.status >= 400) {
|
if (httpResponse.status < 200 || httpResponse.status >= 400) {
|
||||||
|
|||||||
Reference in New Issue
Block a user