|
| 1 | +'use strict'; |
| 2 | + |
1 | 3 | var httpRequest = require("../src/cloud-code/httpRequest"),
|
2 | 4 | bodyParser = require('body-parser'),
|
3 | 5 | express = require("express");
|
@@ -154,31 +156,50 @@ describe("httpRequest", () => {
|
154 | 156 | done();
|
155 | 157 | })
|
156 | 158 | });
|
157 |
| - it("should encode a JSON body", (done) => { |
| 159 | + it("should encode a JSON body by default", (done) => { |
158 | 160 |
|
159 |
| - var result = httpRequest.encodeBody({"foo": "bar"}, {'Content-Type': 'application/json'}); |
160 |
| - expect(result).toEqual('{"foo":"bar"}'); |
| 161 | + let options = { |
| 162 | + body: {"foo": "bar"}, |
| 163 | + } |
| 164 | + |
| 165 | + var result = httpRequest.encodeBody(options); |
| 166 | + expect(result.body).toEqual('{"foo":"bar"}'); |
| 167 | + expect(result.headers['Content-Type']).toEqual('application/json'); |
161 | 168 | done();
|
162 | 169 |
|
163 | 170 | })
|
164 |
| - it("should encode a www-form body", (done) => { |
| 171 | + |
| 172 | + it("should encode a JSON body", (done) => { |
| 173 | + |
| 174 | + let options = { |
| 175 | + body: {"foo": "bar"}, |
| 176 | + headers: {'Content-Type': 'application/json'} |
| 177 | + } |
165 | 178 |
|
166 |
| - var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'application/x-www-form-urlencoded'}); |
167 |
| - expect(result).toEqual("foo=bar&bar=baz"); |
| 179 | + var result = httpRequest.encodeBody(options); |
| 180 | + expect(result.body).toEqual('{"foo":"bar"}'); |
168 | 181 | done();
|
169 |
| - }); |
170 |
| - it("should not encode a wrong content type", (done) => { |
171 | 182 |
|
172 |
| - var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'mime/jpeg'}); |
173 |
| - expect(result).toEqual({"foo": "bar", "bar": "baz"}); |
| 183 | + }) |
| 184 | + it("should encode a www-form body", (done) => { |
| 185 | + let options = { |
| 186 | + body: {"foo": "bar", "bar": "baz"}, |
| 187 | + headers: {'cOntent-tYpe': 'application/x-www-form-urlencoded'} |
| 188 | + } |
| 189 | + var result = httpRequest.encodeBody(options); |
| 190 | + expect(result.body).toEqual("foo=bar&bar=baz"); |
174 | 191 | done();
|
175 | 192 | });
|
176 |
| - it("should not encode when missing content type", (done) => { |
177 |
| - var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'X-Custom-Header': 'my-header'}); |
178 |
| - expect(result).toEqual({"foo": "bar", "bar": "baz"}); |
| 193 | + it("should not encode a wrong content type", (done) => { |
| 194 | + let options = { |
| 195 | + body:{"foo": "bar", "bar": "baz"}, |
| 196 | + headers: {'cOntent-tYpe': 'mime/jpeg'} |
| 197 | + } |
| 198 | + var result = httpRequest.encodeBody(options); |
| 199 | + expect(result.body).toEqual({"foo": "bar", "bar": "baz"}); |
179 | 200 | done();
|
180 | 201 | });
|
181 |
| - |
| 202 | + |
182 | 203 | it("should fail gracefully", (done) => {
|
183 | 204 | httpRequest({
|
184 | 205 | url: "http://not a good url",
|
|
0 commit comments