Browse Source

Move out basic response body checks

Zane Kansil 10 years ago
parent
commit
18a02e1e4d

+ 4 - 13
toolset/benchmark/test_types/db_type.py

@@ -1,5 +1,5 @@
 from benchmark.test_types.framework_test_type import FrameworkTestType
 from benchmark.test_types.framework_test_type import FrameworkTestType
-from benchmark.test_types.verifications import verify_headers, verify_randomnumber_object
+from benchmark.test_types.verifications import basic_body_verification, verify_headers, verify_randomnumber_object
 
 
 import json
 import json
 
 
@@ -26,19 +26,10 @@ class DBTestType(FrameworkTestType):
         url = base_url + self.db_url
         url = base_url + self.db_url
         headers, body = self.request_headers_and_body(url)
         headers, body = self.request_headers_and_body(url)
 
 
-        # Empty response
-        if body is None:
-            return [('fail', 'No response', url)]
-        elif len(body) == 0:
-            return [('fail', 'Empty Response', url)]
+        response, problems = basic_body_verification(body)
 
 
-        # Valid JSON?
-        try:
-            response = json.loads(body)
-        except ValueError as ve:
-            return [('fail', "Invalid JSON - %s" % ve, url)]
-
-        problems = []
+        if len(problems) > 0:
+            return problems 
 
 
         # We are allowing the single-object array
         # We are allowing the single-object array
         # e.g. [{'id':5, 'randomNumber':10}] for now,
         # e.g. [{'id':5, 'randomNumber':10}] for now,

+ 5 - 7
toolset/benchmark/test_types/fortune_type.py

@@ -1,6 +1,6 @@
 from benchmark.test_types.framework_test_type import FrameworkTestType
 from benchmark.test_types.framework_test_type import FrameworkTestType
 from benchmark.fortune_html_parser import FortuneHTMLParser
 from benchmark.fortune_html_parser import FortuneHTMLParser
-from benchmark.test_types.verifications import verify_headers
+from benchmark.test_types.verifications import basic_body_verification, verify_headers
 
 
 
 
 class FortuneTestType(FrameworkTestType):
 class FortuneTestType(FrameworkTestType):
@@ -26,18 +26,16 @@ class FortuneTestType(FrameworkTestType):
         url = base_url + self.fortune_url
         url = base_url + self.fortune_url
         headers, body = self.request_headers_and_body(url)
         headers, body = self.request_headers_and_body(url)
 
 
-        # Empty response
-        if body is None:
-            return [('fail', 'No response', url)]
-        elif len(body) == 0:
-            return [('fail', 'Empty Response', url)]
+        _, problems = basic_body_verification(body, is_json_check=False)
+
+        if len(problems) > 0:
+            return problems
 
 
         parser = FortuneHTMLParser()
         parser = FortuneHTMLParser()
         parser.feed(body)
         parser.feed(body)
         (valid, diff) = parser.isValidFortune(self.out)
         (valid, diff) = parser.isValidFortune(self.out)
 
 
         if valid:
         if valid:
-            problems = []
             problems += verify_headers(headers, url, should_be='html')
             problems += verify_headers(headers, url, should_be='html')
 
 
             if len(problems) == 0:
             if len(problems) == 0:

+ 4 - 12
toolset/benchmark/test_types/json_type.py

@@ -1,5 +1,5 @@
 from benchmark.test_types.framework_test_type import FrameworkTestType
 from benchmark.test_types.framework_test_type import FrameworkTestType
-from benchmark.test_types.verifications import verify_headers, verify_helloworld_object
+from benchmark.test_types.verifications import basic_body_verification, verify_headers, verify_helloworld_object
 
 
 import json
 import json
 
 
@@ -27,19 +27,11 @@ class JsonTestType(FrameworkTestType):
         url = base_url + self.json_url
         url = base_url + self.json_url
         headers, body = self.request_headers_and_body(url)
         headers, body = self.request_headers_and_body(url)
 
 
-        # Empty response
-        if body is None:
-            return [('fail', 'No response', url)]
-        elif len(body) == 0:
-            return [('fail', 'Empty Response', url)]
+        response, problems = basic_body_verification(body)
 
 
-        # Valid JSON?
-        try:
-            response = json.loads(body)
-        except ValueError as ve:
-            return [('fail', "Invalid JSON - %s" % ve, url)]
+        if len(problems) > 0:
+            return problems
 
 
-        problems = []
         problems += verify_helloworld_object(response, url)
         problems += verify_helloworld_object(response, url)
         problems += verify_headers(headers, url, should_be='json')
         problems += verify_headers(headers, url, should_be='json')
 
 

+ 5 - 8
toolset/benchmark/test_types/plaintext_type.py

@@ -1,5 +1,5 @@
 from benchmark.test_types.framework_test_type import FrameworkTestType
 from benchmark.test_types.framework_test_type import FrameworkTestType
-from benchmark.test_types.verifications import verify_headers
+from benchmark.test_types.verifications import basic_body_verification, verify_headers
 
 
 
 
 class PlaintextTestType(FrameworkTestType):
 class PlaintextTestType(FrameworkTestType):
@@ -17,11 +17,10 @@ class PlaintextTestType(FrameworkTestType):
         url = base_url + self.plaintext_url
         url = base_url + self.plaintext_url
         headers, body = self.request_headers_and_body(url)
         headers, body = self.request_headers_and_body(url)
 
 
-        # Empty response
-        if body is None:
-            return [('fail', 'No response', url)]
-        elif len(body) == 0:
-            return [('fail', 'Empty Response', url)]
+        _, problems = basic_body_verification(body, is_json_check=False)
+
+        if len(problems) > 0:
+            return problems
 
 
         # Case insensitive
         # Case insensitive
         orig = body
         orig = body
@@ -32,8 +31,6 @@ class PlaintextTestType(FrameworkTestType):
         if expected not in body:
         if expected not in body:
             return [('fail', "Could not find 'Hello, World!' in response.", url)]
             return [('fail', "Could not find 'Hello, World!' in response.", url)]
 
 
-        problems = []
-
         if extra_bytes > 0:
         if extra_bytes > 0:
             problems.append(
             problems.append(
                 ('warn',
                 ('warn',

+ 36 - 11
toolset/benchmark/test_types/verifications.py

@@ -1,6 +1,37 @@
 import json
 import json
 
 
 
 
+def basic_body_verification(body, is_json_check=True):
+    '''
+    Takes in a raw (stringy) response body, checks that it is non-empty,
+    and that it is valid JSON (i.e. can be deserialized into a dict/list of dicts)
+    Returns the deserialized body as a dict (or list of dicts), and also returns any
+    problems encountered, always as a list. If len(problems) > 0,
+    then the response body does not have to be examined further and the caller
+    should handle the failing problem(s).
+
+    Plaintext and Fortunes set `is_json_check` to False
+    '''
+
+    # Empty Response?
+    if body is None:
+        return None, [('fail', 'No response', url)]
+    elif len(body) == 0:
+        return None, [('fail', 'Empty response', url)]
+
+    # Valid JSON?
+    if is_json_check:
+        try:
+            response = json.loads(body)
+            return response, []
+        except ValueError as ve:
+            return None, [('fail', 'Invalid JSON: %s' % ve, url)]
+
+    # Fortunes and Plaintext only use this for the empty response tests
+    # they do not need or expect a dict back
+    return None, []
+
+
 def verify_headers(headers, url, should_be='json'):
 def verify_headers(headers, url, should_be='json'):
     '''
     '''
     Verifies the headers of a framework response
     Verifies the headers of a framework response
@@ -143,19 +174,13 @@ def verify_randomnumber_list(expected_len, headers, body, url, max_infraction='f
     '''
     '''
     Validates that the object is a list containing a number of 
     Validates that the object is a list containing a number of 
     randomnumber object. Should closely resemble:
     randomnumber object. Should closely resemble:
-    [{ "id": 2354, "randomNumber": 8952 }, { id: }]
+    [{ "id": 2354, "randomNumber": 8952 }, { "id": 4421, "randomNumber": 32 }, ... ]
     '''
     '''
-    if body is None:
-        return [(max_infraction, 'No response', url)]
-    elif len(body) == 0:
-        return [(max_infraction, 'Empty Response', url)]
-
-    try:
-        response = json.loads(body)
-    except ValueError as ve:
-        return [(max_infraction, "Invalid JSON - %s" % ve, url)]
+    
+    response, problems = basic_body_verification(body)
 
 
-    problems = []
+    if len(problems) > 0:
+        return problems
 
 
     # This path will be hit when the framework returns a single JSON object
     # This path will be hit when the framework returns a single JSON object
     # rather than a list containing one element. We allow this with a warn,
     # rather than a list containing one element. We allow this with a warn,