Skip to content

Commit dbd6cca

Browse files
author
Peter Joseph Olamit
authored
Merge pull request #11 from akreisman-epam/content-type-header-check
Check hyperwallet client response content type
2 parents 3d44ccb + ed5c2f3 commit dbd6cca

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

hyperwallet/tests/test_client.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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:
@@ -70,7 +73,10 @@ def test_receive_valid_json_error_response(self, session_mock):
7073

7174
session_mock.return_value = mock.MagicMock(
7275
status_code=400,
73-
content=json.dumps(data)
76+
content=json.dumps(data),
77+
headers={
78+
"Content-Type": "application/json"
79+
}
7480
)
7581

7682
with self.assertRaises(HyperwalletAPIException) as exc:
@@ -100,7 +106,10 @@ def test_receive_valid_json_response(self, session_mock):
100106

101107
session_mock.return_value = mock.MagicMock(
102108
status_code=200,
103-
content=json.dumps(data)
109+
content=json.dumps(data),
110+
headers={
111+
"Content-Type": "application/json"
112+
}
104113
)
105114

106115
encoded = json.dumps(data)
@@ -112,6 +121,29 @@ def test_receive_valid_json_response(self, session_mock):
112121
json.loads(encoded)
113122
)
114123

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+
115147

116148
if __name__ == '__main__':
117149
unittest.main()

hyperwallet/utils/apiclient.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __init__(self, username, password, server, encryptionData=None):
4141
# Hyperwallet SDK.
4242
self.baseHeaders = {
4343
'User-Agent': 'Hyperwallet Python SDK v{}'.format(__version__),
44-
'Accept': 'application/json',
44+
'Accept': 'application/jose+json' if self.encrypted else 'application/json',
4545
'Content-Type': 'application/jose+json' if self.encrypted else 'application/json'
4646
}
4747

@@ -113,6 +113,8 @@ def _makeRequest(self,
113113
if response.status_code is 204:
114114
return {}
115115

116+
self.__checkResponseHeaderContentType(response)
117+
116118
content = response.content
117119
if hasattr(content, 'decode'): # Python 2
118120
content = content.decode('utf-8')
@@ -193,6 +195,20 @@ def doPut(self, partialUrl, data):
193195
data=json.dumps(data).encode('utf-8')
194196
)
195197

198+
def __checkResponseHeaderContentType(self, response):
199+
'''
200+
Check response header Content-Type.
201+
202+
:param response:
203+
Response to be checked. **REQUIRED**
204+
'''
205+
206+
if response is None:
207+
return
208+
contentType = response.headers['Content-Type']
209+
if (not self.encrypted and contentType != 'application/json') or (self.encrypted and contentType != 'application/jose+json'):
210+
raise HyperwalletAPIException('Invalid Content-Type specified in Response Header')
211+
196212
def __getRequestData(self, data):
197213
'''
198214
If encryption is enabled try to encrypt request data, otherwise no action required.

0 commit comments

Comments
 (0)