Encodes the body by default to application/json if is object

This commit is contained in:
Florent Vilmart
2016-03-07 11:06:56 -05:00
parent 0c446b9731
commit 8300dd8a47
2 changed files with 53 additions and 21 deletions

View File

@@ -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",