Skip to content

Commit 6c1ddac

Browse files
authored
fix: encode serialized JSON string as UTF-8 (#121)
1 parent 5395f65 commit 6c1ddac

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

ibm_cloud_sdk_core/base_service.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import json as json_import
1919
import logging
2020
import platform
21-
import sys
2221
from http.cookiejar import CookieJar
2322
from os.path import basename
2423
from typing import Dict, List, Optional, Tuple, Union
@@ -373,14 +372,13 @@ def prepare_request(self,
373372
params = cleanup_values(params)
374373
request['params'] = params
375374

376-
if sys.version_info >= (3, 0) and isinstance(data, str):
375+
if isinstance(data, str):
377376
data = data.encode('utf-8')
378-
379-
if data and isinstance(data, dict):
377+
elif isinstance(data, dict) and data:
380378
data = remove_null_values(data)
381379
if headers.get('content-type') is None:
382380
headers.update({'content-type': 'application/json'})
383-
data = json_import.dumps(data)
381+
data = json_import.dumps(data).encode('utf-8')
384382
request['data'] = data
385383

386384
self.authenticator.authenticate(request)

test/test_base_service.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ def get_access_token() -> str:
119119
return access_token
120120

121121

122+
def test_invalid_authenticator():
123+
with pytest.raises(ValueError) as err:
124+
AnyServiceV1('2021-08-18')
125+
126+
assert str(err.value) == 'authenticator must be provided'
127+
128+
122129
@responses.activate
123130
def test_url_encoding():
124131
service = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator())
@@ -480,6 +487,10 @@ def _from_dict(cls, _dict):
480487
res_str = service._convert_list(temp)
481488
assert res_str == 'default,123'
482489

490+
temp2 = 'default123'
491+
res_str2 = service._convert_list(temp2)
492+
assert res_str2 == temp2
493+
483494

484495
def test_default_headers():
485496
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
@@ -712,6 +723,18 @@ def test_reserved_keys(caplog):
712723
assert caplog.record_tuples[2][2] == '"headers" has been removed from the request'
713724
assert caplog.record_tuples[3][2] == '"cookies" has been removed from the request'
714725

726+
@responses.activate
727+
def test_ssl_error():
728+
responses.add(
729+
responses.GET,
730+
'https://gateway.watsonplatform.net/test/api',
731+
body=requests.exceptions.SSLError())
732+
service = AnyServiceV1('2021-08-18', authenticator=NoAuthAuthenticator())
733+
with pytest.raises(requests.exceptions.SSLError):
734+
prepped = service.prepare_request('GET', url='')
735+
service.send(prepped)
736+
737+
715738
def test_files_dict():
716739
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
717740

@@ -786,8 +809,8 @@ def test_files_duplicate_parts():
786809
def test_json():
787810
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
788811
req = service.prepare_request('POST', url='', headers={
789-
'X-opt-out': True}, data={'hello': 'world'})
790-
assert req.get('data') == "{\"hello\": \"world\"}"
812+
'X-opt-out': True}, data={'hello': 'world', 'fóó': 'bår'})
813+
assert req.get('data') == b'{"hello": "world", "f\\u00f3\\u00f3": "b\\u00e5r"}'
791814

792815

793816
def test_trailing_slash():

0 commit comments

Comments
 (0)