From ee272697d3e69e429dcc8bab0ee979b88af66d10 Mon Sep 17 00:00:00 2001
From: Norbert Biczo <pyrooka@users.noreply.github.com>
Date: Tue, 17 Aug 2021 16:21:24 +0200
Subject: [PATCH 1/2] fix: encode serialized JSON string as UTF-8

---
 ibm_cloud_sdk_core/base_service.py | 8 +++-----
 test/test_base_service.py          | 4 ++--
 2 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/ibm_cloud_sdk_core/base_service.py b/ibm_cloud_sdk_core/base_service.py
index bbc6dda6..1bf690c3 100644
--- a/ibm_cloud_sdk_core/base_service.py
+++ b/ibm_cloud_sdk_core/base_service.py
@@ -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
@@ -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):
             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)
diff --git a/test/test_base_service.py b/test/test_base_service.py
index 0ea68c00..a7a5bf67 100644
--- a/test/test_base_service.py
+++ b/test/test_base_service.py
@@ -786,8 +786,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():

From f5dfa5c601ba7452f34bdd892355b183d8c629e7 Mon Sep 17 00:00:00 2001
From: Norbert Biczo <pyrooka@users.noreply.github.com>
Date: Wed, 18 Aug 2021 18:28:13 +0200
Subject: [PATCH 2/2] test: improve coverage

---
 test/test_base_service.py | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/test/test_base_service.py b/test/test_base_service.py
index a7a5bf67..e01bf8ac 100644
--- a/test/test_base_service.py
+++ b/test/test_base_service.py
@@ -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())
@@ -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())
@@ -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())
 
