Skip to content

Commit bdf5f2a

Browse files
committed
Fix response content type header check when content type header contains charset substring
1 parent 4393964 commit bdf5f2a

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

hyperwallet/tests/test_client.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,54 @@ def test_receive_valid_json_response(self, session_mock):
120120
json.loads(encoded)
121121
)
122122

123+
@mock.patch('requests.Session.request')
124+
def test_receive_valid_json_response_when_content_type_contains_charset(self, session_mock):
125+
126+
data = {
127+
'key': 'value'
128+
}
129+
130+
session_mock.return_value = mock.MagicMock(
131+
status_code=200,
132+
content=json.dumps(data),
133+
headers={
134+
"Content-Type": "application/json;charset=utf-8"
135+
}
136+
)
137+
138+
encoded = json.dumps(data)
139+
if hasattr(encoded, 'decode'): # Python 2
140+
encoded = encoded.decode('utf-8')
141+
142+
self.assertEqual(
143+
self.client._makeRequest(),
144+
json.loads(encoded)
145+
)
146+
147+
@mock.patch('requests.Session.request')
148+
def test_receive_valid_json_response_when_content_type_starts_with_charset(self, session_mock):
149+
150+
data = {
151+
'key': 'value'
152+
}
153+
154+
session_mock.return_value = mock.MagicMock(
155+
status_code=200,
156+
content=json.dumps(data),
157+
headers={
158+
"Content-Type": "charset=utf-8;application/json"
159+
}
160+
)
161+
162+
encoded = json.dumps(data)
163+
if hasattr(encoded, 'decode'): # Python 2
164+
encoded = encoded.decode('utf-8')
165+
166+
self.assertEqual(
167+
self.client._makeRequest(),
168+
json.loads(encoded)
169+
)
170+
123171
@mock.patch('requests.Session.request')
124172
def test_receive_json_error_response_when_content_type_is_not_valid(self, session_mock):
125173

hyperwallet/utils/apiclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def __checkResponseHeaderContentType(self, response):
204204
'''
205205

206206
contentType = response.headers['Content-Type']
207-
if (not self.encrypted and contentType != 'application/json') or (self.encrypted and contentType != 'application/jose+json'):
207+
if (not self.encrypted and 'application/json' not in contentType) or (self.encrypted and 'application/jose+json' not in contentType):
208208
raise HyperwalletAPIException('Invalid Content-Type specified in Response Header')
209209

210210
def __getRequestData(self, data):

0 commit comments

Comments
 (0)