diff --git a/spec/HTTPRequest.spec.js b/spec/HTTPRequest.spec.js index abc5decf62..4bacd0fe2c 100644 --- a/spec/HTTPRequest.spec.js +++ b/spec/HTTPRequest.spec.js @@ -1,3 +1,5 @@ +'use strict'; + var httpRequest = require("../src/cloud-code/httpRequest"), bodyParser = require('body-parser'), express = require("express"); @@ -158,31 +160,47 @@ describe("httpRequest", () => { done(); }) }); - it("should encode a JSON body", (done) => { + + it("should encode a query string body by default", (done) => { + let options = { + body: {"foo": "bar"}, + } + let result = httpRequest.encodeBody(options); + expect(result.body).toEqual('foo=bar'); + expect(result.headers['Content-Type']).toEqual('application/x-www-form-urlencoded'); + done(); - var result = httpRequest.encodeBody({"foo": "bar"}, {'Content-Type': 'application/json'}); - expect(result).toEqual('{"foo":"bar"}'); + }) + + it("should encode a JSON body", (done) => { + let options = { + body: {"foo": "bar"}, + headers: {'Content-Type': 'application/json'} + } + let result = httpRequest.encodeBody(options); + expect(result.body).toEqual('{"foo":"bar"}'); done(); }) it("should encode a www-form body", (done) => { - - var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'application/x-www-form-urlencoded'}); - expect(result).toEqual("foo=bar&bar=baz"); + let options = { + body: {"foo": "bar", "bar": "baz"}, + headers: {'cOntent-tYpe': 'application/x-www-form-urlencoded'} + } + let result = httpRequest.encodeBody(options); + expect(result.body).toEqual("foo=bar&bar=baz"); done(); }); it("should not encode a wrong content type", (done) => { - - var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'mime/jpeg'}); - expect(result).toEqual({"foo": "bar", "bar": "baz"}); + let options = { + body:{"foo": "bar", "bar": "baz"}, + headers: {'cOntent-tYpe': 'mime/jpeg'} + } + let result = httpRequest.encodeBody(options); + expect(result.body).toEqual({"foo": "bar", "bar": "baz"}); done(); }); - it("should not encode when missing content type", (done) => { - var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'X-Custom-Header': 'my-header'}); - expect(result).toEqual({"foo": "bar", "bar": "baz"}); - done(); - }); - + it("should fail gracefully", (done) => { httpRequest({ url: "http://not a good url", @@ -196,6 +214,17 @@ describe("httpRequest", () => { done(); } }); + }); + + it('should get a cat image', (done) => { + httpRequest({ + url: 'http://thecatapi.com/api/images/get?format=src&type=jpg', + followRedirects: true + }).then((res) => { + expect(res.buffer).not.toBe(null); + expect(res.text).not.toBe(null); + done(); + }) }) it("should params object to query string", (done) => { diff --git a/src/cloud-code/HTTPResponse.js b/src/cloud-code/HTTPResponse.js new file mode 100644 index 0000000000..f234f332db --- /dev/null +++ b/src/cloud-code/HTTPResponse.js @@ -0,0 +1,21 @@ + +export default class HTTPResponse { + constructor(response) { + 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) {} + } + return this._data; + } +} diff --git a/src/cloud-code/httpRequest.js b/src/cloud-code/httpRequest.js index 35af79ca3e..d78e67350f 100644 --- a/src/cloud-code/httpRequest.js +++ b/src/cloud-code/httpRequest.js @@ -1,26 +1,36 @@ -var request = require("request"), - querystring = require('querystring'), - Parse = require('parse/node').Parse; +import request from 'request'; +import Parse from 'parse/node'; +import HTTPResponse from './HTTPResponse'; +import querystring from 'querystring'; -var encodeBody = function(body, headers = {}) { +var encodeBody = function({body, headers = {}}) { if (typeof body !== 'object') { - return body; + return {body, headers}; } var contentTypeKeys = Object.keys(headers).filter((key) => { return key.match(/content-type/i) != null; }); - if (contentTypeKeys.length == 1) { + if (contentTypeKeys.length == 0) { + // no content type + // As per https://parse.com/docs/cloudcode/guide#cloud-code-advanced-sending-a-post-request the default encoding is supposedly x-www-form-urlencoded + + body = querystring.stringify(body); + headers['Content-Type'] = 'application/x-www-form-urlencoded'; + } else { + /* istanbul ignore next */ + if (contentTypeKeys.length > 1) { + console.error('multiple content-type headers are set.'); + } + // There maybe many, we'll just take the 1st one var contentType = contentTypeKeys[0]; if (headers[contentType].match(/application\/json/i)) { body = JSON.stringify(body); } else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) { - body = Object.keys(body).map(function(key){ - return `${key}=${encodeURIComponent(body[key])}` - }).join("&"); + body = querystring.stringify(body); } } - return body; + return {body, headers}; } module.exports = function(options) { @@ -32,7 +42,7 @@ module.exports = function(options) { delete options.success; delete options.error; delete options.uri; // not supported - options.body = encodeBody(options.body, options.headers); + options = Object.assign(options, encodeBody(options)); // set follow redirects to false by default options.followRedirect = options.followRedirects == true; // support params options @@ -41,6 +51,8 @@ module.exports = function(options) { } else if (typeof options.params === 'string') { options.qs = querystring.parse(options.params); } + // force the response as a buffer + options.encoding = null; request(options, (error, response, body) => { if (error) { @@ -49,15 +61,8 @@ module.exports = function(options) { } return promise.reject(error); } - var httpResponse = {}; - httpResponse.status = response.statusCode; - httpResponse.headers = response.headers; - httpResponse.buffer = new Buffer(response.body); - httpResponse.cookies = response.headers["set-cookie"]; - httpResponse.text = response.body; - try { - httpResponse.data = JSON.parse(response.body); - } catch (e) {} + let httpResponse = new HTTPResponse(response); + // Consider <200 && >= 400 as errors if (httpResponse.status < 200 || httpResponse.status >= 400) { if (callbacks.error) {