From a85f3bf02141dd656bce64438617970605f17144 Mon Sep 17 00:00:00 2001 From: Kegan Myers Date: Mon, 3 Mar 2014 14:32:45 -0600 Subject: [PATCH] Add test for JsonResponse --- test/Typertext/Json/JsonResponse.test.js | 58 +++++++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/test/Typertext/Json/JsonResponse.test.js b/test/Typertext/Json/JsonResponse.test.js index f9f93dd..cd97f16 100644 --- a/test/Typertext/Json/JsonResponse.test.js +++ b/test/Typertext/Json/JsonResponse.test.js @@ -1,5 +1,59 @@ -describe("Typertext.Json.JsonResponse", function() { - it("exists", function() { +describe("Typertext.Json.JsonResponse", function () { + it("exists", function () { expect(typeof Typertext.Json.JsonResponse).toBe("function"); }); + + describe("fromHttpResponse", function () { + it("exists", function () { + expect(typeof Typertext.Json.JsonResponse.fromHttpResponse).toBe("function"); + }); + + it("handles an empty json object response", function () { + function hf() { + return ""; + } + + var inputBody = "{}", + input = new Typertext.Http.HttpResponse(Typertext.Http.HttpResponseStatus.success, hf, 200, inputBody), + expectedBody = {}, + expectedOutput = new Typertext.Json.JsonResponse(Typertext.Http.HttpResponseStatus.success, hf, 200, expectedBody), + actualOutput = Typertext.Json.JsonResponse.fromHttpResponse(input); + expect(window["JSON"].parse(inputBody)).toEqual(expectedBody); + expect(window["JSON"].stringify(actualOutput)).toEqual(window["JSON"].stringify(expectedOutput)); + }); + + it("handles an empty string", function () { + function hf() { + return ""; + } + + var inputBody = "", + input = new Typertext.Http.HttpResponse(Typertext.Http.HttpResponseStatus.success, hf, 200, inputBody); + expect(function () { + Typertext.Json.JsonResponse.fromHttpResponse(input); + }).toThrow(); + }); + + it("handles an example server response", function () { + function hf() { + return ""; + } + + var inputBody = "{\"access_token\":\"0d95289cb2f54831dc435ce9274b1d1bdf8f5949\",\"expires_in\":86400," + + "\"token_type\":\"Bearer\",\"scope\":null," + + "\"refresh_token\":\"8a4431470af2edc3fdf747eca5f71451a3ad2d98\"}", + input = new Typertext.Http.HttpResponse(Typertext.Http.HttpResponseStatus.success, hf, 200, inputBody), + expectedBody = { + "access_token": "0d95289cb2f54831dc435ce9274b1d1bdf8f5949", + "expires_in": 86400, + "token_type": "Bearer", + "scope": null, + "refresh_token": "8a4431470af2edc3fdf747eca5f71451a3ad2d98" + }, + expectedOutput = new Typertext.Json.JsonResponse(Typertext.Http.HttpResponseStatus.success, hf, 200, expectedBody), + actualOutput = Typertext.Json.JsonResponse.fromHttpResponse(input); + expect(window["JSON"].parse(inputBody)).toEqual(expectedBody); + expect(window["JSON"].stringify(actualOutput)).toEqual(window["JSON"].stringify(expectedOutput)); + }); + }); }); \ No newline at end of file