Browse Source

Separate out header verification

Zane Kansil 10 years ago
parent
commit
83808da039

+ 2 - 33
toolset/benchmark/test_types/db_type.py

@@ -1,4 +1,5 @@
 from benchmark.test_types.framework_test_type import FrameworkTestType
+from benchmark.test_types.verifications import verify_headers
 
 import json
 
@@ -55,45 +56,13 @@ class DBTestType(FrameworkTestType):
 
         # Verify response content
         problems += self._verifyObject(response, url)
-        problems += self._verifyHeaders(headers, url)
+        problems += verify_headers(headers, url, should_be='json')
 
         if len(problems) == 0:
             return [('pass', '', url)]
         else:
             return problems
 
-    def _verifyHeaders(self, headers, url):
-        '''Verifies the response headers'''
-
-        problems = []
-
-        if any(v.lower() not in headers for v in ('Server', 'Date', 'Content-Type')):
-            problems.append(
-                ('warn', 'Required response header missing: %s' % v, url))
-        elif all(v.lower() not in headers for v in ('Content-Length', 'Transfer-Encoding')):
-            problems.append(
-                ('warn',
-                 'Required response size header missing, please include either "Content-Length" or "Transfer-Encoding"',
-                 url))
-        else:
-            content_type = headers.get('Content-Type', None)
-            expected_type = 'application/json'
-            includes_charset = 'application/json; charset=utf-8'
-            if content_type == includes_charset:
-                problems.append(
-                    ('warn',
-                     ("Content encoding \"%s\" found where \"%s\" is acceptable.\n"
-                      "Additional response bytes may negatively affect benchmark performance."
-                      % (includes_charset, expected_type)),
-                     url))
-            elif content_type != expected_type:
-                problems.append(
-                    ('warn',
-                     'Unexpected content encoding, found %s, expected %s' % (
-                         content_type, expected_type),
-                     url))
-        return problems
-
     # Prime refactor target. This method is also utilized by the multiple queries
     # and updates tests, yet is not separated out nicely.
     def _verifyObject(self, db_object, url, max_infraction='fail'):

+ 2 - 35
toolset/benchmark/test_types/fortune_type.py

@@ -1,5 +1,6 @@
 from benchmark.test_types.framework_test_type import FrameworkTestType
 from benchmark.fortune_html_parser import FortuneHTMLParser
+from benchmark.test_types.verifications import verify_headers
 
 
 class FortuneTestType(FrameworkTestType):
@@ -37,7 +38,7 @@ class FortuneTestType(FrameworkTestType):
 
         if valid:
             problems = []
-            problems += self._verifyHeaders(headers, url)
+            problems += verify_headers(headers, url, should_be='html')
 
             if len(problems) == 0:
                 return [('pass', '', url)]
@@ -88,37 +89,3 @@ class FortuneTestType(FrameworkTestType):
             # If there were errors reading the diff, then no diff information
             pass
         return problems
-
-    def _verifyHeaders(self, headers, url):
-        '''Verifies the response headers for the Fortunes test'''
-
-        problems = []
-
-        if any(v.lower() not in headers for v in ('Server', 'Date', 'Content-Type')):
-            problems.append(
-                ('warn', 'Required response header missing: %s' % v, url))
-        elif all(v.lower() not in headers for v in ('Content-Length', 'Transfer-Encoding')):
-            problems.append(
-                ('warn',
-                 ('Required response size header missing, '
-                  'please include either "Content-Length" or "Transfer-Encoding"'),
-                    url))
-        else:
-            content_type = headers.get('Content-Type', '')
-            expected_type = 'text/html'
-            includes_charset = expected_type + '; charset=utf-8'
-
-            if content_type.lower() == includes_charset:
-                problems.append(
-                    ('warn',
-                     ("Content encoding \"%s\" found where \"%s\" is acceptable.\n"
-                      "Additional response bytes may negatively affect benchmark performance."
-                      % (includes_charset, expected_type)),
-                     url))
-            elif content_type != expected_type:
-                problems.append(
-                    ('warn',
-                     'Unexpected content encoding, found %s, expected %s' % (
-                         content_type, expected_type),
-                     url))
-        return problems

+ 2 - 33
toolset/benchmark/test_types/json_type.py

@@ -1,4 +1,5 @@
 from benchmark.test_types.framework_test_type import FrameworkTestType
+from benchmark.test_types.verifications import verify_headers
 
 import json
 
@@ -40,7 +41,7 @@ class JsonTestType(FrameworkTestType):
 
         problems = []
         problems += self._verifyObject(response, url)
-        problems += self._verifyHeaders(headers, url)
+        problems += verify_headers(headers, url, should_be='json')
 
         if len(problems) > 0:
             return problems
@@ -73,35 +74,3 @@ class JsonTestType(FrameworkTestType):
                 return [('fail', "Expected message of 'hello, world!', got '%s'" % message)]
 
         return problems
-
-    def _verifyHeaders(self, headers, url):
-        '''Verifies the response headers'''
-
-        problems = []
-
-        if any(v.lower() not in headers for v in ('Server', 'Date', 'Content-Type')):
-            problems.append(
-                ('warn', 'Required response header missing: %s' % v, url))
-        elif all(v.lower() not in headers for v in ('Content-Length', 'Transfer-Encoding')):
-            problems.append(
-                ('warn',
-                 'Required response size header missing, please include either "Content-Length" or "Transfer-Encoding"',
-                 url))
-        else:
-            content_type = headers.get('Content-Type', None)
-            expected_type = 'application/json'
-            includes_charset = 'application/json; charset=utf-8'
-            if content_type == includes_charset:
-                problems.append(
-                    ('warn',
-                     ("Content encoding \"%s\" found where \"%s\" is acceptable.\n"
-                      "Additional response bytes may negatively affect benchmark performance."
-                      % (includes_charset, expected_type)),
-                     url))
-            elif content_type != expected_type:
-                problems.append(
-                    ('warn',
-                     'Unexpected content encoding, found %s, expected %s' % (
-                         content_type, expected_type),
-                     url))
-        return problems

+ 2 - 35
toolset/benchmark/test_types/plaintext_type.py

@@ -1,4 +1,5 @@
 from benchmark.test_types.framework_test_type import FrameworkTestType
+from benchmark.test_types.verifications import verify_headers
 
 
 class PlaintextTestType(FrameworkTestType):
@@ -41,7 +42,7 @@ class PlaintextTestType(FrameworkTestType):
                   % (extra_bytes)),
                  url))
 
-        problems += self._verifyHeaders(headers, url)
+        problems += verify_headers(headers, url, should_be='plaintext')
 
         if len(problems) == 0:
             return [('pass', '', url)]
@@ -50,37 +51,3 @@ class PlaintextTestType(FrameworkTestType):
 
     def get_url(self):
         return self.plaintext_url
-
-    def _verifyHeaders(self, headers, url):
-        '''Verifies the response headers for the Plaintext test'''
-
-        problems = []
-
-        if any(v.lower() not in headers for v in ('Server', 'Date', 'Content-Type')):
-            problems.append(
-                ('warn', 'Required response header missing: %s' % v, url))
-        elif all(v.lower() not in headers for v in ('Content-Length', 'Transfer-Encoding')):
-            problems.append(
-                ('warn',
-                 ('Required response size header missing, '
-                  'please include either "Content-Length" or "Transfer-Encoding"'),
-                 url))
-        else:
-            content_type = headers.get('Content-Type', '')
-            expected_type = 'text/plain'
-            includes_charset = expected_type + '; charset=utf-8'
-
-            if content_type.lower() == includes_charset:
-                problems.append(
-                    ('warn',
-                     ("Content encoding \"%s\" found where \"%s\" is acceptable.\n"
-                      "Additional response bytes may negatively affect benchmark performance."
-                      % (includes_charset, expected_type)),
-                     url))
-            elif content_type != expected_type:
-                problems.append(
-                    ('warn',
-                     'Unexpected content encoding, found %s, expected %s' % (
-                         content_type, expected_type),
-                     url))
-        return problems

+ 3 - 2
toolset/benchmark/test_types/query_type.py

@@ -1,5 +1,6 @@
 from benchmark.test_types.framework_test_type import FrameworkTestType
 from benchmark.test_types.db_type import DBTestType
+from benchmark.test_types.verifications import verify_headers
 
 import json
 
@@ -74,7 +75,7 @@ class QueryTestType(DBTestType):
 
                 problems += self._verifyBodyList(
                     expected_len, headers, body, case_url, max_infraction)
-                problems += self._verifyHeaders(headers, case_url)
+                problems += verify_headers(headers, case_url)
 
             except ValueError:
                 warning = (
@@ -99,7 +100,7 @@ class QueryTestType(DBTestType):
                     # parameter input
                     problems += self._verifyBodyList(
                         expected_len, headers, body, case_url, max_infraction)
-                    problems += self._verifyHeaders(headers, case_url)
+                    problems += verify_headers(headers, case_url)
 
         return problems
 

+ 41 - 0
toolset/benchmark/test_types/verifications.py

@@ -0,0 +1,41 @@
+def verify_headers(headers, url, should_be='json'):
+    '''
+    Verifies the headers of a framework response
+    param `should_be` is a switch for the three acceptable content types
+    '''
+
+    types = {
+        'json': 'application/json',
+        'html': 'text/html',
+        'plaintext': 'text/plain'
+    }
+    expected_type = types[should_be]
+    includes_charset = expected_type + '; charset=utf-8'
+
+    problems = []
+
+    if any(v.lower() not in headers for v in ('Server', 'Date', 'Content-Type')):
+        problems.append(
+            ('warn', 'Required response header missing: %s' % v, url))
+    elif all(v.lower() not in headers for v in ('Content-Length', 'Transfer-Encoding')):
+        problems.append(
+            ('warn',
+             'Required response size header missing, please include either "Content-Length" or "Transfer-Encoding"',
+             url))
+    else:
+        content_type = headers.get('Content-Type', None)
+
+        if content_type.lower() == includes_charset:
+            problems.append(
+                ('warn',
+                 ("Content encoding found \"%s\" where \"%s\" is acceptable.\n"
+                  "Additional response bytes may negatively affect benchmark performance."
+                  % (includes_charset, expected_type)),
+                 url))
+        elif content_type != expected_type:
+            problems.append(
+                ('warn',
+                 'Unexpected content encoding, found \"%s\", expected \"%s\"' % (
+                     content_type, expected_type),
+                 url))
+    return problems