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",
|
||||
|
||||
Reference in New Issue
Block a user