@@ -45,7 +45,10 @@ def test_receive_non_json_response(self, session_mock):
4545
4646 session_mock .return_value = mock .MagicMock (
4747 status_code = 404 ,
48- content = data
48+ content = data ,
49+ headers = {
50+ "Content-Type" : "application/json"
51+ }
4952 )
5053
5154 with self .assertRaises (HyperwalletAPIException ) as exc :
@@ -62,13 +65,18 @@ def test_receive_valid_json_error_response(self, session_mock):
6265 data = {
6366 "errors" : [{
6467 "message" : "Houston, we have a problem" ,
65- "code" : "FORBIDDEN"
68+ "code" : "FORBIDDEN" ,
69+ "relatedResources" : ["trm-f3d38df1-adb7-4127-9858-e72ebe682a79" ,
70+ "trm-601b1401-4464-4f3f-97b3-09079ee7723b" ]
6671 }]
6772 }
6873
6974 session_mock .return_value = mock .MagicMock (
7075 status_code = 400 ,
71- content = json .dumps (data )
76+ content = json .dumps (data ),
77+ headers = {
78+ "Content-Type" : "application/json"
79+ }
7280 )
7381
7482 with self .assertRaises (HyperwalletAPIException ) as exc :
@@ -79,6 +87,16 @@ def test_receive_valid_json_error_response(self, session_mock):
7987 'FORBIDDEN'
8088 )
8189
90+ self .assertEqual (
91+ exc .exception .message .get ('errors' )[0 ].get ('relatedResources' )[0 ],
92+ 'trm-f3d38df1-adb7-4127-9858-e72ebe682a79'
93+ )
94+
95+ self .assertEqual (
96+ exc .exception .message .get ('errors' )[0 ].get ('relatedResources' )[1 ],
97+ 'trm-601b1401-4464-4f3f-97b3-09079ee7723b'
98+ )
99+
82100 @mock .patch ('requests.Session.request' )
83101 def test_receive_valid_json_response (self , session_mock ):
84102
@@ -88,7 +106,10 @@ def test_receive_valid_json_response(self, session_mock):
88106
89107 session_mock .return_value = mock .MagicMock (
90108 status_code = 200 ,
91- content = json .dumps (data )
109+ content = json .dumps (data ),
110+ headers = {
111+ "Content-Type" : "application/json"
112+ }
92113 )
93114
94115 encoded = json .dumps (data )
@@ -100,6 +121,29 @@ def test_receive_valid_json_response(self, session_mock):
100121 json .loads (encoded )
101122 )
102123
124+ @mock .patch ('requests.Session.request' )
125+ def test_receive_json_error_response_when_content_type_is_not_valid (self , session_mock ):
126+
127+ data = {
128+ 'key' : 'value'
129+ }
130+
131+ session_mock .return_value = mock .MagicMock (
132+ status_code = 200 ,
133+ content = json .dumps (data ),
134+ headers = {
135+ "Content-Type" : "wrongContentType"
136+ }
137+ )
138+
139+ with self .assertRaises (HyperwalletAPIException ) as exc :
140+ self .client ._makeRequest ()
141+
142+ self .assertEqual (
143+ exc .exception .message ,
144+ 'Invalid Content-Type specified in Response Header'
145+ )
146+
103147
104148if __name__ == '__main__' :
105149 unittest .main ()
0 commit comments