Skip to content

Commit 2aba4f6

Browse files
committed
Split out the response data decoding chunk.
1 parent 3313281 commit 2aba4f6

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

tests/test_mig_services_coreapi.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@
3131
from urllib.request import urlopen, Request
3232

3333

34+
def attempt_to_decode_response_data(data, response_encoding=None):
35+
if response_encoding == 'textual':
36+
data = codecs.decode(data, 'utf8')
37+
38+
try:
39+
return json.loads(data)
40+
except Exception as e:
41+
return data
42+
elif response_encoding == 'binary':
43+
return data
44+
else:
45+
raise AssertionError(
46+
'issue_POST: unknown response_encoding "%s"' % (response_encoding,))
47+
48+
3449
class MigServerGrid_openid(MigTestCase, HtmlAssertMixin):
3550
def before_each(self):
3651
self.server_addr = None
@@ -108,18 +123,8 @@ def issue_POST(self, request_path, request_data=None, request_json=None, respons
108123
status = httpexc.code
109124
data = httpexc.file.read()
110125

111-
if response_encoding == 'textual':
112-
data = codecs.decode(data, 'utf8')
113-
114-
try:
115-
data = json.loads(data)
116-
except Exception as e:
117-
pass
118-
elif response_encoding != 'binary':
119-
raise AssertionError(
120-
'issue_POST: unknown response_encoding "%s"' % (response_encoding,))
121-
122-
return (status, data)
126+
content = attempt_to_decode_response_data(data, response_encoding)
127+
return (status, content)
123128

124129
@unittest.skipIf(PY2, "Python 3 only")
125130
def test__GET_returns_not_found_for_missing_path(self):

0 commit comments

Comments
 (0)