test_classes: Include more detail in incorrect JSON responses.

If the status code is wrong, we show the actual error message now,
which often saves a bit of time when debugging.
This commit is contained in:
Tim Abbott
2017-08-28 21:33:10 -07:00
parent 0686567e46
commit a8b9ffc020

View File

@@ -414,18 +414,25 @@ class ZulipTestCase(TestCase):
Successful POSTs return a 200 and JSON of the form {"result": "success", Successful POSTs return a 200 and JSON of the form {"result": "success",
"msg": ""}. "msg": ""}.
""" """
self.assertEqual(result.status_code, 200, result) try:
json = ujson.loads(result.content) json = ujson.loads(result.content)
except Exception: # nocoverage
json = {'msg': "Error parsing JSON in response!"}
self.assertEqual(result.status_code, 200, json['msg'])
self.assertEqual(json.get("result"), "success") self.assertEqual(json.get("result"), "success")
# We have a msg key for consistency with errors, but it typically has an # We have a msg key for consistency with errors, but it typically has an
# empty value. # empty value.
self.assertIn("msg", json) self.assertIn("msg", json)
self.assertNotEqual(json["msg"], "Error parsing JSON in response!")
return json return json
def get_json_error(self, result, status_code=400): def get_json_error(self, result, status_code=400):
# type: (HttpResponse, int) -> Dict[str, Any] # type: (HttpResponse, int) -> Dict[str, Any]
self.assertEqual(result.status_code, status_code) try:
json = ujson.loads(result.content) json = ujson.loads(result.content)
except Exception: # nocoverage
json = {'msg': "Error parsing JSON in response!"}
self.assertEqual(result.status_code, status_code, msg=json.get('msg'))
self.assertEqual(json.get("result"), "error") self.assertEqual(json.get("result"), "error")
return json['msg'] return json['msg']