|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | var httpRequest = require("../src/cloud-code/httpRequest"),
|
| 4 | + HTTPResponse = require('../src/cloud-code/HTTPResponse').default, |
4 | 5 | bodyParser = require('body-parser'),
|
5 | 6 | express = require("express");
|
6 | 7 |
|
@@ -245,4 +246,81 @@ describe("httpRequest", () => {
|
245 | 246 | })
|
246 | 247 | });
|
247 | 248 |
|
| 249 | + it('should not crash with undefined body', () => { |
| 250 | + let httpResponse = new HTTPResponse({}); |
| 251 | + expect(httpResponse.body).toBeUndefined(); |
| 252 | + expect(httpResponse.data).toBeUndefined(); |
| 253 | + expect(httpResponse.text).toBeUndefined(); |
| 254 | + expect(httpResponse.buffer).toBeUndefined(); |
| 255 | + }); |
| 256 | + |
| 257 | + it('serialized httpResponse correctly with body string', () => { |
| 258 | + let httpResponse = new HTTPResponse({}, 'hello'); |
| 259 | + expect(httpResponse.text).toBe('hello'); |
| 260 | + expect(httpResponse.data).toBe(undefined); |
| 261 | + expect(httpResponse.body).toBe('hello'); |
| 262 | + |
| 263 | + let serialized = JSON.stringify(httpResponse); |
| 264 | + let result = JSON.parse(serialized); |
| 265 | + expect(result.text).toBe('hello'); |
| 266 | + expect(result.data).toBe(undefined); |
| 267 | + expect(result.body).toBe(undefined); |
| 268 | + }); |
| 269 | + |
| 270 | + it('serialized httpResponse correctly with body object', () => { |
| 271 | + let httpResponse = new HTTPResponse({}, {foo: "bar"}); |
| 272 | + let encodedResponse = Parse._encode(httpResponse); |
| 273 | + let serialized = JSON.stringify(httpResponse); |
| 274 | + let result = JSON.parse(serialized); |
| 275 | + |
| 276 | + expect(httpResponse.text).toEqual('{"foo":"bar"}'); |
| 277 | + expect(httpResponse.data).toEqual({foo: 'bar'}); |
| 278 | + expect(httpResponse.body).toEqual({foo: 'bar'}); |
| 279 | + |
| 280 | + expect(result.text).toEqual('{"foo":"bar"}'); |
| 281 | + expect(result.data).toEqual({foo: 'bar'}); |
| 282 | + expect(result.body).toEqual(undefined); |
| 283 | + }); |
| 284 | + |
| 285 | + it('serialized httpResponse correctly with body buffer string', () => { |
| 286 | + let httpResponse = new HTTPResponse({}, new Buffer('hello')); |
| 287 | + expect(httpResponse.text).toBe('hello'); |
| 288 | + expect(httpResponse.data).toBe(undefined); |
| 289 | + |
| 290 | + let serialized = JSON.stringify(httpResponse); |
| 291 | + let result = JSON.parse(serialized); |
| 292 | + expect(result.text).toBe('hello'); |
| 293 | + expect(result.data).toBe(undefined); |
| 294 | + }); |
| 295 | + |
| 296 | + it('serialized httpResponse correctly with body buffer JSON Object', () => { |
| 297 | + let json = '{"foo":"bar"}'; |
| 298 | + let httpResponse = new HTTPResponse({}, new Buffer(json)); |
| 299 | + let serialized = JSON.stringify(httpResponse); |
| 300 | + let result = JSON.parse(serialized); |
| 301 | + expect(result.text).toEqual('{"foo":"bar"}'); |
| 302 | + expect(result.data).toEqual({foo: 'bar'}); |
| 303 | + }); |
| 304 | + |
| 305 | + it('serialized httpResponse with Parse._encode should be allright', () => { |
| 306 | + let json = '{"foo":"bar"}'; |
| 307 | + let httpResponse = new HTTPResponse({}, new Buffer(json)); |
| 308 | + let encoded = Parse._encode(httpResponse); |
| 309 | + let foundData, foundText, foundBody = false; |
| 310 | + for(var key in encoded) { |
| 311 | + if (key == 'data') { |
| 312 | + foundData = true; |
| 313 | + } |
| 314 | + if (key == 'text') { |
| 315 | + foundText = true; |
| 316 | + } |
| 317 | + if (key == 'body') { |
| 318 | + foundBody = true; |
| 319 | + } |
| 320 | + } |
| 321 | + expect(foundData).toBe(true); |
| 322 | + expect(foundText).toBe(true); |
| 323 | + expect(foundBody).toBe(false); |
| 324 | + }); |
| 325 | + |
248 | 326 | });
|
0 commit comments