Encodes the body by default to application/json if is object
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
var httpRequest = require("../src/cloud-code/httpRequest"),
|
||||
bodyParser = require('body-parser'),
|
||||
express = require("express");
|
||||
@@ -158,31 +160,50 @@ describe("httpRequest", () => {
|
||||
done();
|
||||
})
|
||||
});
|
||||
it("should encode a JSON body by default", (done) => {
|
||||
|
||||
let options = {
|
||||
body: {"foo": "bar"},
|
||||
}
|
||||
|
||||
var result = httpRequest.encodeBody(options);
|
||||
expect(result.body).toEqual('{"foo":"bar"}');
|
||||
expect(result.headers['Content-Type']).toEqual('application/json');
|
||||
done();
|
||||
|
||||
})
|
||||
|
||||
it("should encode a JSON body", (done) => {
|
||||
|
||||
var result = httpRequest.encodeBody({"foo": "bar"}, {'Content-Type': 'application/json'});
|
||||
expect(result).toEqual('{"foo":"bar"}');
|
||||
let options = {
|
||||
body: {"foo": "bar"},
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
}
|
||||
|
||||
var 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'}
|
||||
}
|
||||
var 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'}
|
||||
}
|
||||
var 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",
|
||||
|
||||
@@ -2,25 +2,36 @@ var request = require("request"),
|
||||
querystring = require('querystring'),
|
||||
Parse = require('parse/node').Parse;
|
||||
|
||||
var encodeBody = function(body, headers = {}) {
|
||||
var encodeBody = function(options = {}) {
|
||||
let body = options.body;
|
||||
let headers = options.headers || {};
|
||||
if (typeof body !== 'object') {
|
||||
return body;
|
||||
return options;
|
||||
}
|
||||
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
|
||||
try {
|
||||
options.body = JSON.stringify(body);
|
||||
options.headers = options.headers || {};
|
||||
options.headers['Content-Type'] = 'application/json';
|
||||
} catch(e) {
|
||||
// do nothing;
|
||||
}
|
||||
} else if (contentTypeKeys.length == 1) {
|
||||
var contentType = contentTypeKeys[0];
|
||||
if (headers[contentType].match(/application\/json/i)) {
|
||||
body = JSON.stringify(body);
|
||||
options.body = JSON.stringify(body);
|
||||
} else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) {
|
||||
body = Object.keys(body).map(function(key){
|
||||
options.body = Object.keys(body).map(function(key){
|
||||
return `${key}=${encodeURIComponent(body[key])}`
|
||||
}).join("&");
|
||||
}
|
||||
}
|
||||
return body;
|
||||
return options;
|
||||
}
|
||||
|
||||
module.exports = function(options) {
|
||||
@@ -32,7 +43,7 @@ module.exports = function(options) {
|
||||
delete options.success;
|
||||
delete options.error;
|
||||
delete options.uri; // not supported
|
||||
options.body = encodeBody(options.body, options.headers);
|
||||
options = encodeBody(options);
|
||||
// set follow redirects to false by default
|
||||
options.followRedirect = options.followRedirects == true;
|
||||
// support params options
|
||||
|
||||
Reference in New Issue
Block a user