瀏覽代碼

Update plaintext_type

Zane Kansil 10 年之前
父節點
當前提交
30e95ca455
共有 1 個文件被更改,包括 62 次插入14 次删除
  1. 62 14
      toolset/benchmark/test_types/plaintext_type.py

+ 62 - 14
toolset/benchmark/test_types/plaintext_type.py

@@ -1,14 +1,19 @@
 from benchmark.test_types.framework_test_type import FrameworkTestType
 
 class PlaintextTestType(FrameworkTestType):
+
   def __init__(self):
-    args = ['plaintext_url']
-    FrameworkTestType.__init__(self, name='plaintext', requires_db=False, accept_header=self.accept_plaintext, args=args)
+    kwargs = {
+      'name': 'plaintext',
+      'requires_db': False,
+      'accept_header': self.accept_plaintext,
+      'args': ['plaintext_url']
+    }
+    FrameworkTestType.__init__(self, **kwargs)
 
   def verify(self, base_url):
     url = base_url + self.plaintext_url
-    full_response = self._curl(url)
-    body = self._curl_body(url)
+    headers, body = self.request_headers_and_body(url)
 
     # Empty response
     if body is None:
@@ -19,20 +24,63 @@ class PlaintextTestType(FrameworkTestType):
     # Case insensitive
     orig = body
     body = body.lower()
+    expected = "hello, world!"
+    extra_bytes = len(body) - len(expected)
+
+    if expected not in body:
+      return [('fail', "Could not find 'Hello, World!' in response.", url)]
 
-    if "hello, world!" not in body:
-      return [('fail', """Could not find 'Hello, World!' in response.""", url)]
+    problems = []
 
-    if len("hello, world!") < len(body):
-      return [('warn', """Server is returning %s more bytes than are required.
-This may negatively affect benchmark performance.""" % (len(body) - len("hello, world!")), url)]
+    if extra_bytes > 0:
+      problems.append(
+        ('warn',
+         ("Server is returning %s more bytes than are required. "
+          "This may negatively affect benchmark performance."
+          % (extra_bytes)),
+         url))
 
-    # Ensure required response headers are present
-    if any(v.lower() not in full_response.lower() for v in ('Server','Date','Content-Type: text/plain')) \
-       or all(v.lower() not in full_response.lower() for v in ('Content-Length','Transfer-Encoding')):
-      return [('warn','Required response header missing.',url)]
+    problems += self._verifyHeaders(headers, url)
 
-    return [('pass', '', url)]
+    if len(problems) == 0:
+      return [('pass', '', url)]
+    else:
+      return problems
 
   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
+