77
88import { assert } from "chai" ;
99
10- import { GraphResponseHandler } from "../../src/GraphResponseHandler" ;
10+ import { DocumentType , GraphResponseHandler } from "../../src/GraphResponseHandler" ;
1111import { ResponseType } from "../../src/ResponseType" ;
1212
1313describe ( "GraphResponseHandler.ts" , ( ) => {
@@ -33,11 +33,43 @@ describe("GraphResponseHandler.ts", () => {
3333 status : 500 ,
3434 statusText : "Internal Server Error" ,
3535 } ;
36+ const status202 = {
37+ status : 202 ,
38+ statusText : "OK" ,
39+ } ;
40+ const status200Text = {
41+ status : 200 ,
42+ stautsText : "OK" ,
43+ headers : {
44+ "Content-Type" : "text/plain" ,
45+ } ,
46+ } ;
47+ const status200Json = {
48+ status : 200 ,
49+ stautsText : "OK" ,
50+ headers : {
51+ "Content-Type" : "application/json" ,
52+ } ,
53+ } ;
54+ const status200Image = {
55+ status : 200 ,
56+ stautsText : "OK" ,
57+ headers : {
58+ "Content-Type" : "image/jpeg" ,
59+ } ,
60+ } ;
61+ const status200Unknown = {
62+ status : 200 ,
63+ statusText : "OK" ,
64+ headers : {
65+ "Content-Type" : "dummy/unknown" ,
66+ } ,
67+ } ;
3668 /* tslint:disable: no-string-literal */
3769 describe ( "parseDocumentResponse" , ( ) => {
3870 it ( "Should return the html string" , async ( ) => {
3971 const response = new Response ( htmlString , status200 ) ;
40- const dom = await GraphResponseHandler [ "parseDocumentResponse" ] ( response , GraphResponseHandler [ "DocumentTypes" ] [ " TEXT_HTML" ] ) ;
72+ const dom = await GraphResponseHandler [ "parseDocumentResponse" ] ( response , DocumentType . TEXT_HTML ) ;
4173 assert . isDefined ( dom ) ;
4274 assert . equal ( typeof dom , "string" ) ;
4375 } ) ;
@@ -50,6 +82,35 @@ describe("GraphResponseHandler.ts", () => {
5082 assert . isUndefined ( responseValue ) ;
5183 } ) ;
5284
85+ it ( "Should return empty text value for empty response" , async ( ) => {
86+ const response = new Response ( undefined , status202 ) ;
87+ const responseValue = await GraphResponseHandler [ "convertResponse" ] ( response ) ;
88+ assert . isUndefined ( responseValue ) ;
89+ } ) ;
90+
91+ it ( "Should return text data for text/plain content-type" , async ( ) => {
92+ const data = "text data" ;
93+ const response = new Response ( data , status200Text ) ;
94+ const responseValue = await GraphResponseHandler [ "convertResponse" ] ( response ) ;
95+ assert . equal ( responseValue , data ) ;
96+ } ) ;
97+
98+ it ( "Should return json data for application/json content-type" , async ( ) => {
99+ const data = {
100+ test : "test" ,
101+ } ;
102+ const response = new Response ( JSON . stringify ( data ) , status200Json ) ;
103+ const responseValue = await GraphResponseHandler [ "convertResponse" ] ( response ) ;
104+ assert . equal ( responseValue . test , data . test ) ;
105+ } ) ;
106+
107+ it ( "Should return raw response incase of unknown content-type" , async ( ) => {
108+ const data = "test data" ;
109+ const response = new Response ( data , status200Unknown ) ;
110+ const responseValue = await GraphResponseHandler [ "convertResponse" ] ( response ) ;
111+ assert . equal ( responseValue , data ) ;
112+ } ) ;
113+
53114 it ( "Should return response value as text" , async ( ) => {
54115 const response = new Response ( htmlString , status200 ) ;
55116 const responseValue = await GraphResponseHandler [ "convertResponse" ] ( response , ResponseType . TEXT ) ;
0 commit comments