From adfe87e081182d34f16264a4a80f9134f9ee2fc4 Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Thu, 13 Feb 2020 15:42:40 +0100
Subject: [PATCH] TMSS-142: only interpret/compare response content when
 statuscode is succesfull

---
 .../tmss_test_environment_unittest_setup.py   | 30 ++++++++++++-------
 1 file changed, 20 insertions(+), 10 deletions(-)

diff --git a/SAS/TMSS/test/tmss_test_environment_unittest_setup.py b/SAS/TMSS/test/tmss_test_environment_unittest_setup.py
index dc5e1c5316a..128cda77d2a 100644
--- a/SAS/TMSS/test/tmss_test_environment_unittest_setup.py
+++ b/SAS/TMSS/test/tmss_test_environment_unittest_setup.py
@@ -65,16 +65,26 @@ def _call_API_and_assert_expected_response(test_instance, url, call, data, expec
     if response.status_code != expected_code:
         print("!!! Unexpected: [%s] - %s %s: %s" % (test_instance.id(), call, url, response.content.decode('utf-8').strip()))
     test_instance.assertEqual(response.status_code, expected_code)
-    r_dict = json.loads(response.content.decode('utf-8'))
-    for key, value in expected_content.items():
-        if key not in r_dict.keys():
-            print('!!! Missing key: %s in %s' % (key, r_dict.keys()))
-        test_instance.assertTrue(key in r_dict.keys())
-        if type(value) is list:
-            test_instance.assertEqual(sorted(value), sorted(r_dict[key])) # compare lists independent of ordering
-        else:
-            test_instance.assertEqual(value, r_dict[key])
-    return r_dict
+
+    content = response.content.decode('utf-8')
+
+    if response.status_code in range(200, 100):
+        r_dict = json.loads(content)
+        for key, value in expected_content.items():
+            if key not in r_dict.keys():
+                print('!!! Missing key: %s in %s' % (key, r_dict.keys()))
+            test_instance.assertTrue(key in r_dict.keys())
+            if type(value) is list:
+                test_instance.assertEqual(sorted(value), sorted(r_dict[key])) # compare lists independent of ordering
+            else:
+                test_instance.assertEqual(value, r_dict[key])
+        return r_dict
+
+    try:
+        return json.loads(content)
+    except:
+        return content
+
 
 def PUT_and_assert_expected_response(test_instance, url, data, expected_code, expected_content):
     """
-- 
GitLab