This commit is contained in:
Florent Vilmart
2016-03-08 00:15:17 -05:00
parent 982fc72327
commit 6acb5cee80
3 changed files with 25 additions and 26 deletions

View File

@@ -160,13 +160,12 @@ describe("httpRequest", () => {
done(); done();
}) })
}); });
it("should encode a JSON body by default", (done) => { it("should encode a JSON body by default", (done) => {
let options = { let options = {
body: {"foo": "bar"}, body: {"foo": "bar"},
} }
let result = httpRequest.encodeBody(options);
var result = httpRequest.encodeBody(options);
expect(result.body).toEqual('{"foo":"bar"}'); expect(result.body).toEqual('{"foo":"bar"}');
expect(result.headers['Content-Type']).toEqual('application/json'); expect(result.headers['Content-Type']).toEqual('application/json');
done(); done();
@@ -174,13 +173,11 @@ describe("httpRequest", () => {
}) })
it("should encode a JSON body", (done) => { it("should encode a JSON body", (done) => {
let options = { let options = {
body: {"foo": "bar"}, body: {"foo": "bar"},
headers: {'Content-Type': 'application/json'} headers: {'Content-Type': 'application/json'}
} }
let result = httpRequest.encodeBody(options);
var result = httpRequest.encodeBody(options);
expect(result.body).toEqual('{"foo":"bar"}'); expect(result.body).toEqual('{"foo":"bar"}');
done(); done();
@@ -190,7 +187,7 @@ describe("httpRequest", () => {
body: {"foo": "bar", "bar": "baz"}, body: {"foo": "bar", "bar": "baz"},
headers: {'cOntent-tYpe': 'application/x-www-form-urlencoded'} headers: {'cOntent-tYpe': 'application/x-www-form-urlencoded'}
} }
var result = httpRequest.encodeBody(options); let result = httpRequest.encodeBody(options);
expect(result.body).toEqual("foo=bar&bar=baz"); expect(result.body).toEqual("foo=bar&bar=baz");
done(); done();
}); });
@@ -199,7 +196,7 @@ describe("httpRequest", () => {
body:{"foo": "bar", "bar": "baz"}, body:{"foo": "bar", "bar": "baz"},
headers: {'cOntent-tYpe': 'mime/jpeg'} headers: {'cOntent-tYpe': 'mime/jpeg'}
} }
var result = httpRequest.encodeBody(options); let result = httpRequest.encodeBody(options);
expect(result.body).toEqual({"foo": "bar", "bar": "baz"}); expect(result.body).toEqual({"foo": "bar", "bar": "baz"});
done(); done();
}); });

View File

@@ -13,9 +13,9 @@ export default class HTTPResponse {
get data() { get data() {
if (!this._data) { if (!this._data) {
try { try {
this._data = JSON.parse(this.text); this._data = JSON.parse(this.text);
} catch (e) {} } catch (e) {}
} }
return this._data; return this._data;
} }
} }

View File

@@ -1,13 +1,11 @@
var request = require("request"), import request from 'request';
querystring = require('querystring'), import Parse from 'parse/node';
Parse = require('parse/node').Parse; import HTTPResponse from './HTTPResponse';
HTTPResponse = require('./HTTPResponse').HTTPResponse; import querystring from 'querystring';
var encodeBody = function(options = {}) { var encodeBody = function({body, headers = {}}) {
let body = options.body;
let headers = options.headers || {};
if (typeof body !== 'object') { if (typeof body !== 'object') {
return options; return {body, headers};
} }
var contentTypeKeys = Object.keys(headers).filter((key) => { var contentTypeKeys = Object.keys(headers).filter((key) => {
return key.match(/content-type/i) != null; return key.match(/content-type/i) != null;
@@ -16,23 +14,27 @@ var encodeBody = function(options = {}) {
if (contentTypeKeys.length == 0) { if (contentTypeKeys.length == 0) {
// no content type // no content type
try { try {
options.body = JSON.stringify(body); body = JSON.stringify(body);
options.headers = options.headers || {}; headers['Content-Type'] = 'application/json';
options.headers['Content-Type'] = 'application/json';
} catch(e) { } catch(e) {
// do nothing; // do nothing;
} }
} else if (contentTypeKeys.length == 1) { } 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]; var contentType = contentTypeKeys[0];
if (headers[contentType].match(/application\/json/i)) { if (headers[contentType].match(/application\/json/i)) {
options.body = JSON.stringify(body); body = JSON.stringify(body);
} else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) { } else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) {
options.body = Object.keys(body).map(function(key){ body = Object.keys(body).map(function(key){
return `${key}=${encodeURIComponent(body[key])}` return `${key}=${encodeURIComponent(body[key])}`
}).join("&"); }).join("&");
} }
} }
return options; return {body, headers};
} }
module.exports = function(options) { module.exports = function(options) {
@@ -44,7 +46,7 @@ module.exports = function(options) {
delete options.success; delete options.success;
delete options.error; delete options.error;
delete options.uri; // not supported delete options.uri; // not supported
options = encodeBody(options); options = Object.assign(options, encodeBody(options));
// set follow redirects to false by default // set follow redirects to false by default
options.followRedirect = options.followRedirects == true; options.followRedirect = options.followRedirects == true;
// support params options // support params options