[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/tests/dochttp.py - add a -r option to drop the content for redirect responses

Fred L. Drake, Jr. fdrake at gmail.com
Mon Oct 4 12:09:13 EDT 2004


Log message for revision 27738:
  - add a -r option to drop the content for redirect responses
    (uses ellipses to match any content)
  - ignore EPIPE errors (other end of destination pipe was closed early;
    usually found when piped into a pager like "less")
  


Changed:
  U   Zope3/trunk/src/zope/app/tests/dochttp.py


-=-
Modified: Zope3/trunk/src/zope/app/tests/dochttp.py
===================================================================
--- Zope3/trunk/src/zope/app/tests/dochttp.py	2004-10-04 16:02:55 UTC (rev 27737)
+++ Zope3/trunk/src/zope/app/tests/dochttp.py	2004-10-04 16:09:13 UTC (rev 27738)
@@ -16,6 +16,7 @@
 $Id$
 """
 
+import errno
 import optparse
 import os
 import re
@@ -42,6 +43,9 @@
                   help="Request header to skip")
 parser.add_option("-O", "--skip-response-header", action="append",
                   help="Response header to skip")
+parser.add_option("-r", "--clean-redirects", action="store_true",
+                  help="Strip content from redirect responses",
+                  default=False)
 
 default_options = [
     '-e', 'html',
@@ -111,16 +115,32 @@
                 if skip_url.search(request.path):
                     break
             else:
-                output_test(request, response)
+                try:
+                    output_test(request, response, options.clean_redirects)
+                except IOError, e:
+                    if e.errno == errno.EPIPE:
+                        return
+                    raise
     
 
-def output_test(request, response):
+def output_test(request, response, clean_redirects=False):
     print
     print
     print '  >>> print http(r"""'
     print '  ...', '\n  ... '.join(request.lines())+'""")'
+    if response.code in (301, 302, 303) and clean_redirects:
+        if response.headers:
+            for i in range(len(response.headers)):
+                h, v = response.headers[i]
+                if h == "Content-Length":
+                    response.headers[i] = (h, "...")
+        lines = response.header_lines()
+        if lines:
+            lines.append("...")
+    else:
+        lines = response.lines()
     print ' ', '\n  '.join([line.rstrip() and line or '<BLANKLINE>'
-                             for line in response.lines()])
+                             for line in lines])
 
 class Message:
 
@@ -130,6 +150,9 @@
         start = file.readline().rstrip()
         if start:
             self.start = start
+            if start.startswith("HTTP/"):
+                # This is a response; extract the response code:
+                self.code = int(start.split()[1])
             headers = [split_header(header)
                        for header in rfc822.Message(file).headers
                        ]
@@ -151,16 +174,20 @@
         return bool(self.start)
 
     def lines(self):
+        output = self.header_lines()
+        if output:
+            output.extend(self.body)
+        return output
+
+    def header_lines(self):
         if self.start:
             output = [self.start]
             headers = ["%s: %s" % (name, v) for (name, v) in self.headers]
             headers.sort()
             output.extend(headers)
             output.append('')
-            output.extend(self.body)
         else:
             output = []
-
         return output
 
 headerre = re.compile('(\S+): (.+)$')



More information about the Zope3-Checkins mailing list