From c0343207eac7d99f200bdae5a2360c1030eed060 Mon Sep 17 00:00:00 2001 From: Marcus Huewe Date: Wed, 3 Jun 2020 17:46:07 +0200 Subject: [PATCH] Make tests.common.MyHTTPHandler.__mock_PUT bytes aware For now, we assume that if the "exp" keyword argument is specified, then it is a str. In this case, we simply encode it (using the utf-8 encoding). Also, simplify the code a bit (get rid of the if-statement that is always executed). --- tests/common.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/common.py b/tests/common.py index 211cc63f..353b7398 100644 --- a/tests/common.py +++ b/tests/common.py @@ -90,16 +90,18 @@ class MyHTTPHandler(HTTPHandler): if exp is not None and 'expfile' in kwargs: raise RuntimeError('either specify exp or expfile') elif 'expfile' in kwargs: - exp = open(os.path.join(self.__fixtures_dir, kwargs['expfile']), 'r').read() + exp = open(os.path.join(self.__fixtures_dir, kwargs['expfile']), 'rb').read() elif exp is None: raise RuntimeError('exp or expfile required') - if exp is not None: - # use req.data instead of req.get_data() for python3 compatiblity - data = req.data - if hasattr(data, 'read'): - data = data.read() - if data != bytes(exp, "utf-8"): - raise RequestDataMismatch(req.get_full_url(), repr(data), repr(exp)) + else: + # for now, assume exp is a str + exp = exp.encode('utf-8') + # use req.data instead of req.get_data() for python3 compatiblity + data = req.data + if hasattr(data, 'read'): + data = data.read() + if data != exp: + raise RequestDataMismatch(req.get_full_url(), repr(data), repr(exp)) return self.__get_response(req.get_full_url(), **kwargs) def __get_response(self, url, **kwargs):