From e1f8935d45a6b6eb6f79d471f94cc4a39244bb66 Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Wed, 4 Apr 2018 14:54:17 -0400 Subject: [PATCH] selftests/functional/test_thirdparty_bugs.py: feed string to json As it was done in d2b4edc, Python 3.4 needs a string to be passed to json.loads() (and, indirectly, to json.load()). Different from d2b4edc, the content we fetch externally may be able to tell us the encoding that is used. Let's use that information, if available, to decode the stream of bytes into a string that is acceptable to json.loads(). Signed-off-by: Cleber Rosa --- selftests/functional/test_thirdparty_bugs.py | 34 ++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/selftests/functional/test_thirdparty_bugs.py b/selftests/functional/test_thirdparty_bugs.py index f7b98da4..fec9ba5e 100644 --- a/selftests/functional/test_thirdparty_bugs.py +++ b/selftests/functional/test_thirdparty_bugs.py @@ -1,3 +1,4 @@ +import re import json import unittest @@ -9,6 +10,33 @@ except ImportError: from avocado.utils import download +def get_content_by_encoding(url): + """ + Returns the content of the given URL, attempting to use server provided + encoding. + + :rtype: str + """ + http_response = download.url_open(url) + content_type = None + encoding = None + if hasattr(http_response, 'headers'): + content_type = http_response.headers['Content-Type'] + elif hasattr(http_response, 'getheader'): + content_type = http_response.getheader('Content-Type') + if content_type is not None: + match = re.match(r'^[az\\].*\; charset\=(.*)$', content_type) + if match is not None: + encoding = match.group(1) + content = http_response.read() + if hasattr(content, 'decode'): + if encoding is not None: + content = content.decode(encoding) + else: + content = content.decode() # Python default encoding + return content + + class TestThirdPartyBugs(unittest.TestCase): """ Class created to verify third-party known issues @@ -20,7 +48,8 @@ class TestThirdPartyBugs(unittest.TestCase): # accepts RSA or DSS keys try: issue_url = 'https://api.github.com/repos/paramiko/paramiko/issues/243' - issue = json.load(download.url_open(issue_url)) + content = get_content_by_encoding(issue_url) + issue = json.loads(content) self.assertEqual(issue['state'], 'open', 'The issue %s is not open ' 'anymore. Please double check and, if already fixed, ' 'change the avocado.conf option ' @@ -37,7 +66,8 @@ class TestThirdPartyBugs(unittest.TestCase): # on that file try: issue_url = 'https://api.github.com/repos/avocado-framework/inspektor/issues/31' - issue = json.load(download.url_open(issue_url)) + content = get_content_by_encoding(issue_url) + issue = json.loads(content) self.assertEqual(issue['state'], 'open', 'The issue %s is not open ' 'anymore. Please double check and, if already fixed, ' 'remove the selftests/unit/test_utils_cpu.py from ' -- GitLab