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"),
|
var httpRequest = require("../src/cloud-code/httpRequest"),
|
||||||
bodyParser = require('body-parser'),
|
bodyParser = require('body-parser'),
|
||||||
express = require("express");
|
express = require("express");
|
||||||
@@ -158,31 +160,50 @@ describe("httpRequest", () => {
|
|||||||
done();
|
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) => {
|
it("should encode a JSON body", (done) => {
|
||||||
|
|
||||||
var result = httpRequest.encodeBody({"foo": "bar"}, {'Content-Type': 'application/json'});
|
let options = {
|
||||||
expect(result).toEqual('{"foo":"bar"}');
|
body: {"foo": "bar"},
|
||||||
|
headers: {'Content-Type': 'application/json'}
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = httpRequest.encodeBody(options);
|
||||||
|
expect(result.body).toEqual('{"foo":"bar"}');
|
||||||
done();
|
done();
|
||||||
|
|
||||||
})
|
})
|
||||||
it("should encode a www-form body", (done) => {
|
it("should encode a www-form body", (done) => {
|
||||||
|
let options = {
|
||||||
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'application/x-www-form-urlencoded'});
|
body: {"foo": "bar", "bar": "baz"},
|
||||||
expect(result).toEqual("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();
|
done();
|
||||||
});
|
});
|
||||||
it("should not encode a wrong content type", (done) => {
|
it("should not encode a wrong content type", (done) => {
|
||||||
|
let options = {
|
||||||
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'mime/jpeg'});
|
body:{"foo": "bar", "bar": "baz"},
|
||||||
expect(result).toEqual({"foo": "bar", "bar": "baz"});
|
headers: {'cOntent-tYpe': 'mime/jpeg'}
|
||||||
|
}
|
||||||
|
var result = httpRequest.encodeBody(options);
|
||||||
|
expect(result.body).toEqual({"foo": "bar", "bar": "baz"});
|
||||||
done();
|
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) => {
|
it("should fail gracefully", (done) => {
|
||||||
httpRequest({
|
httpRequest({
|
||||||
url: "http://not a good url",
|
url: "http://not a good url",
|
||||||
|
|||||||
@@ -2,25 +2,36 @@ var request = require("request"),
|
|||||||
querystring = require('querystring'),
|
querystring = require('querystring'),
|
||||||
Parse = require('parse/node').Parse;
|
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') {
|
if (typeof body !== 'object') {
|
||||||
return body;
|
return options;
|
||||||
}
|
}
|
||||||
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;
|
||||||
});
|
});
|
||||||
|
|
||||||
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];
|
var contentType = contentTypeKeys[0];
|
||||||
if (headers[contentType].match(/application\/json/i)) {
|
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)) {
|
} 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])}`
|
return `${key}=${encodeURIComponent(body[key])}`
|
||||||
}).join("&");
|
}).join("&");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return body;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function(options) {
|
module.exports = function(options) {
|
||||||
@@ -32,7 +43,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.body = encodeBody(options.body, options.headers);
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user