@@ -8,9 +8,30 @@ import { mocked } from "jest-mock";
88
99describe ( "JsonService" , ( ) => {
1010 let subject : JsonService ;
11+ let customStaticHeaderSubject : JsonService ;
12+ let customDynamicHeaderSubject : JsonService ;
13+
14+ const staticCustomHeaders = {
15+ "Custom-Header-1" : "this-is-header-1" ,
16+ "Custom-Header-2" : "this-is-header-2" ,
17+ "acCept" : "application/fake" ,
18+ "AuthoriZation" : "not good" ,
19+ "Content-Type" : "application/fail" ,
20+ } ;
21+ const dynamicCustomHeaders = {
22+ "Custom-Header-1" : ( ) => "my-name-is-header-1" ,
23+ "Custom-Header-2" : ( ) => {
24+ return "my-name-is-header-2" ;
25+ } ,
26+ "acCept" : ( ) => "nothing" ,
27+ "AuthoriZation" : ( ) => "not good" ,
28+ "Content-Type" : "application/fail" ,
29+ } ;
1130
1231 beforeEach ( ( ) => {
1332 subject = new JsonService ( ) ;
33+ customStaticHeaderSubject = new JsonService ( undefined , null , staticCustomHeaders ) ;
34+ customDynamicHeaderSubject = new JsonService ( undefined , null , dynamicCustomHeaders ) ;
1435 } ) ;
1536
1637 describe ( "getJson" , ( ) => {
@@ -28,6 +49,42 @@ describe("JsonService", () => {
2849 ) ;
2950 } ) ;
3051
52+ it ( "should make GET request to url with static custom headers" , async ( ) => {
53+ // act
54+ await expect ( customStaticHeaderSubject . getJson ( "http://test" ) ) . rejects . toThrow ( ) ;
55+
56+ // assert
57+ expect ( fetch ) . toBeCalledWith (
58+ "http://test" ,
59+ expect . objectContaining ( {
60+ headers : {
61+ Accept : "application/json" ,
62+ "Custom-Header-1" : "this-is-header-1" ,
63+ "Custom-Header-2" : "this-is-header-2" ,
64+ } ,
65+ method : "GET" ,
66+ } ) ,
67+ ) ;
68+ } ) ;
69+
70+ it ( "should make GET request to url with dynamic custom headers" , async ( ) => {
71+ // act
72+ await expect ( customDynamicHeaderSubject . getJson ( "http://test" ) ) . rejects . toThrow ( ) ;
73+
74+ // assert
75+ expect ( fetch ) . toBeCalledWith (
76+ "http://test" ,
77+ expect . objectContaining ( {
78+ headers : {
79+ Accept : "application/json" ,
80+ "Custom-Header-1" : "my-name-is-header-1" ,
81+ "Custom-Header-2" : "my-name-is-header-2" ,
82+ } ,
83+ method : "GET" ,
84+ } ) ,
85+ ) ;
86+ } ) ;
87+
3188 it ( "should set token as authorization header" , async ( ) => {
3289 // act
3390 await expect ( subject . getJson ( "http://test" , { token : "token" } ) ) . rejects . toThrow ( ) ;
@@ -42,6 +99,44 @@ describe("JsonService", () => {
4299 ) ;
43100 } ) ;
44101
102+ it ( "should set token as authorization header with static custom headers" , async ( ) => {
103+ // act
104+ await expect ( customStaticHeaderSubject . getJson ( "http://test" , { token : "token" } ) ) . rejects . toThrow ( ) ;
105+
106+ // assert
107+ expect ( fetch ) . toBeCalledWith (
108+ "http://test" ,
109+ expect . objectContaining ( {
110+ headers : {
111+ Accept : "application/json" ,
112+ Authorization : "Bearer token" ,
113+ "Custom-Header-1" : "this-is-header-1" ,
114+ "Custom-Header-2" : "this-is-header-2" ,
115+ } ,
116+ method : "GET" ,
117+ } ) ,
118+ ) ;
119+ } ) ;
120+
121+ it ( "should set token as authorization header with dynamic custom headers" , async ( ) => {
122+ // act
123+ await expect ( customDynamicHeaderSubject . getJson ( "http://test" , { token : "token" } ) ) . rejects . toThrow ( ) ;
124+
125+ // assert
126+ expect ( fetch ) . toBeCalledWith (
127+ "http://test" ,
128+ expect . objectContaining ( {
129+ headers : {
130+ Accept : "application/json" ,
131+ Authorization : "Bearer token" ,
132+ "Custom-Header-1" : "my-name-is-header-1" ,
133+ "Custom-Header-2" : "my-name-is-header-2" ,
134+ } ,
135+ method : "GET" ,
136+ } ) ,
137+ ) ;
138+ } ) ;
139+
45140 it ( "should fulfill promise when http response is 200" , async ( ) => {
46141 // arrange
47142 const json = { foo : 1 , bar : "test" } ;
@@ -185,6 +280,46 @@ describe("JsonService", () => {
185280 ) ;
186281 } ) ;
187282
283+ it ( "should make POST request to url with custom static headers" , async ( ) => {
284+ // act
285+ await expect ( customStaticHeaderSubject . postForm ( "http://test" , { body : new URLSearchParams ( "a=b" ) } ) ) . rejects . toThrow ( ) ;
286+
287+ // assert
288+ expect ( fetch ) . toBeCalledWith (
289+ "http://test" ,
290+ expect . objectContaining ( {
291+ headers : {
292+ Accept : "application/json" ,
293+ "Content-Type" : "application/x-www-form-urlencoded" ,
294+ "Custom-Header-1" : "this-is-header-1" ,
295+ "Custom-Header-2" : "this-is-header-2" ,
296+ } ,
297+ method : "POST" ,
298+ body : new URLSearchParams ( ) ,
299+ } ) ,
300+ ) ;
301+ } ) ;
302+
303+ it ( "should make POST request to url with custom dynamic headers" , async ( ) => {
304+ // act
305+ await expect ( customDynamicHeaderSubject . postForm ( "http://test" , { body : new URLSearchParams ( "a=b" ) } ) ) . rejects . toThrow ( ) ;
306+
307+ // assert
308+ expect ( fetch ) . toBeCalledWith (
309+ "http://test" ,
310+ expect . objectContaining ( {
311+ headers : {
312+ Accept : "application/json" ,
313+ "Content-Type" : "application/x-www-form-urlencoded" ,
314+ "Custom-Header-1" : "my-name-is-header-1" ,
315+ "Custom-Header-2" : "my-name-is-header-2" ,
316+ } ,
317+ method : "POST" ,
318+ body : new URLSearchParams ( ) ,
319+ } ) ,
320+ ) ;
321+ } ) ;
322+
188323 it ( "should set basicAuth as authorization header" , async ( ) => {
189324 // act
190325 await expect ( subject . postForm ( "http://test" , { body : new URLSearchParams ( "payload=dummy" ) , basicAuth : "basicAuth" } ) ) . rejects . toThrow ( ) ;
0 commit comments