提交 e1f8935d 编写于 作者: C Cleber Rosa

selftests/functional/test_thirdparty_bugs.py: feed string to json

As it was done in d2b4edc3, Python 3.4 needs a string to be passed to
json.loads() (and, indirectly, to json.load()).  Different from
d2b4edc3, 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: NCleber Rosa <crosa@redhat.com>
上级 766a55ce
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 '
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册