Skip to content

fix: encode serialized JSON string as UTF-8 #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions ibm_cloud_sdk_core/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import json as json_import
import logging
import platform
import sys
from http.cookiejar import CookieJar
from os.path import basename
from typing import Dict, List, Optional, Tuple, Union
Expand Down Expand Up @@ -373,14 +372,13 @@ def prepare_request(self,
params = cleanup_values(params)
request['params'] = params

if sys.version_info >= (3, 0) and isinstance(data, str):
if isinstance(data, str):
Comment on lines -376 to +375
Copy link
Member Author

@pyrooka pyrooka Aug 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't support Python 2 as far as I know, so I removed this version check.

data = data.encode('utf-8')

if data and isinstance(data, dict):
elif isinstance(data, dict) and data:
data = remove_null_values(data)
if headers.get('content-type') is None:
headers.update({'content-type': 'application/json'})
data = json_import.dumps(data)
data = json_import.dumps(data).encode('utf-8')
request['data'] = data

self.authenticator.authenticate(request)
Expand Down
27 changes: 25 additions & 2 deletions test/test_base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ def get_access_token() -> str:
return access_token


def test_invalid_authenticator():
with pytest.raises(ValueError) as err:
AnyServiceV1('2021-08-18')

assert str(err.value) == 'authenticator must be provided'


@responses.activate
def test_url_encoding():
service = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator())
Expand Down Expand Up @@ -480,6 +487,10 @@ def _from_dict(cls, _dict):
res_str = service._convert_list(temp)
assert res_str == 'default,123'

temp2 = 'default123'
res_str2 = service._convert_list(temp2)
assert res_str2 == temp2


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

@responses.activate
def test_ssl_error():
responses.add(
responses.GET,
'https://gateway.watsonplatform.net/test/api',
body=requests.exceptions.SSLError())
service = AnyServiceV1('2021-08-18', authenticator=NoAuthAuthenticator())
with pytest.raises(requests.exceptions.SSLError):
prepped = service.prepare_request('GET', url='')
service.send(prepped)


def test_files_dict():
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())

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


def test_trailing_slash():
Expand Down