Log message for revision 106833:
  Coverage for __init__, retry.

Changed:
  U   Zope/branches/tseaver-fix_wsgi/src/ZPublisher/HTTPResponse.py
  U   Zope/branches/tseaver-fix_wsgi/src/ZPublisher/tests/testHTTPResponse.py

-=-
Modified: Zope/branches/tseaver-fix_wsgi/src/ZPublisher/HTTPResponse.py
===================================================================
--- Zope/branches/tseaver-fix_wsgi/src/ZPublisher/HTTPResponse.py       
2009-12-21 20:37:38 UTC (rev 106832)
+++ Zope/branches/tseaver-fix_wsgi/src/ZPublisher/HTTPResponse.py       
2009-12-21 20:38:20 UTC (rev 106833)
@@ -152,6 +152,7 @@
     """ #'
 
     body = ''
+    base = ''
     realm = 'Zope'
     _error_format = 'text/html'
     _locked_status = 0
@@ -163,9 +164,14 @@
     # 2 - ignore accept-encoding (i.e. force)
     use_HTTP_content_compression = 0
 
-    def __init__(self, body='', status=200, headers=None,
-                 stdout=sys.stdout, stderr=sys.stderr,):
-        """ Creates a new response using the given values.
+    def __init__(self,
+                 body='',
+                 status=200,
+                 headers=None,
+                 stdout=sys.stdout,
+                 stderr=sys.stderr,
+                ):
+        """ Create a new response using the given values.
         """
         if headers is None:
             headers = {}
@@ -178,9 +184,10 @@
             headers['status'] = "200 OK"
         else:
             self.setStatus(status)
-        self.base = ''
+
         if body:
             self.setBody(body)
+
         self.cookies = {}
         self.stdout = stdout
         self.stderr = stderr
@@ -481,20 +488,23 @@
                 return body
             else:
                 if ct.startswith('text/') or ct.startswith('application/'):
-                    self.headers['content-type'] = '%s; charset=%s' % (ct, 
default_encoding)
+                    self.headers['content-type'] = '%s; charset=%s' % (ct,
+                                                            default_encoding)
 
         # Use the default character encoding
         body = body.encode(default_encoding, 'replace')
         body = fix_xml_preamble(body, default_encoding)
         return body
 
-    def setBase(self,base):
+    def setBase(self, base):
         """Set the base URL for the returned document.
-        If base is None, or the document already has a base, do nothing."""
+
+        If base is None, or the document already has a base, do nothing.
+        """
         if base is None:
             base = ''
         elif not base.endswith('/'):
-            base = base+'/'
+            base = base + '/'
         self.base = str(base)
 
     def insertBase(self,

Modified: 
Zope/branches/tseaver-fix_wsgi/src/ZPublisher/tests/testHTTPResponse.py
===================================================================
--- Zope/branches/tseaver-fix_wsgi/src/ZPublisher/tests/testHTTPResponse.py     
2009-12-21 20:37:38 UTC (rev 106832)
+++ Zope/branches/tseaver-fix_wsgi/src/ZPublisher/tests/testHTTPResponse.py     
2009-12-21 20:38:20 UTC (rev 106833)
@@ -13,23 +13,151 @@
 
         return self._getTargetClass()(*args, **kw)
 
-    def test_setStatus_with_exceptions(self):
+    def test_ctor_defaults(self):
+        import sys
+        response = self._makeOne()
+        self.assertEqual(response.headers, {'status': '200 OK'}) # XXX WTF?
+        self.assertEqual(response.accumulated_headers, [])
+        self.assertEqual(response.status, 200)
+        self.assertEqual(response.errmsg, 'OK')
+        self.assertEqual(response.base, '')
+        self.assertEqual(response.body, '')
+        self.assertEqual(response.cookies, {})
+        self.failUnless(response.stdout is sys.stdout)
+        self.failUnless(response.stderr is sys.stderr)
 
+    def test_ctor_w_body(self):
+        response = self._makeOne(body='ABC')
+        self.assertEqual(response.body, 'ABC')
+
+    def test_ctor_w_headers(self):
+        response = self._makeOne(headers={'foo': 'bar'})
+        self.assertEqual(response.headers, {'foo': 'bar',
+                                            'status': '200 OK', # XXX WTF
+                                           })
+
+    def test_ctor_w_status_code(self):
+        response = self._makeOne(status=401)
+        self.assertEqual(response.status, 401)
+        self.assertEqual(response.errmsg, 'Unauthorized')
+        self.assertEqual(response.headers,
+                         {'status': '401 Unauthorized'}) # XXX WTF?
+
+    def test_ctor_w_status_errmsg(self):
+        response = self._makeOne(status='Unauthorized')
+        self.assertEqual(response.status, 401)
+        self.assertEqual(response.errmsg, 'Unauthorized')
+        self.assertEqual(response.headers,
+                         {'status': '401 Unauthorized'}) # XXX WTF?
+
+    def test_ctor_w_status_exception(self):
         from zExceptions import Unauthorized
+        response = self._makeOne(status=Unauthorized)
+        self.assertEqual(response.status, 401)
+        self.assertEqual(response.errmsg, 'Unauthorized')
+        self.assertEqual(response.headers,
+                         {'status': '401 Unauthorized'}) # XXX WTF?
+
+    def test_retry(self):
+        STDOUT, STDERR = object(), object()
+        response = self._makeOne(stdout=STDOUT, stderr=STDERR)
+        cloned = response.retry()
+        self.failUnless(isinstance(cloned, self._getTargetClass()))
+        self.failUnless(cloned.stdout is STDOUT)
+        self.failUnless(cloned.stderr is STDERR)
+
+    def test_ctor_charset_no_content_type_header(self):
+        response = self._makeOne(body='foo')
+        self.assertEqual(response.headers.get('content-type'),
+                         'text/plain; charset=iso-8859-15')
+
+    def test_ctor_charset_text_header_no_charset_defaults_latin1(self):
+        response = self._makeOne(body='foo',
+                                 headers={'content-type': 'text/plain'})
+        self.assertEqual(response.headers.get('content-type'),
+                         'text/plain; charset=iso-8859-15')
+
+    def test_ctor_charset_application_header_no_header(self):
+        response = self._makeOne(body='foo',
+                                 headers={'content-type': 'application/foo'})
+        self.assertEqual(response.headers.get('content-type'),
+                         'application/foo')
+
+    def test_ctor_charset_application_header_with_header(self):
+        response = self._makeOne(body='foo',
+                                 headers={'content-type':
+                                        'application/foo; charset: something'})
+        self.assertEqual(response.headers.get('content-type'),
+                         'application/foo; charset: something')
+    
+    def test_ctor_charset_unicode_body_application_header(self):
+        BODY = unicode('ärger', 'iso-8859-15')
+        response = self._makeOne(body=BODY,
+                                 headers={'content-type': 'application/foo'})
+        self.assertEqual(response.headers.get('content-type'),
+                         'application/foo; charset=iso-8859-15')
+        self.assertEqual(response.body, 'ärger')
+
+    def test_ctor_charset_unicode_body_application_header_diff_encoding(self):
+        BODY = unicode('ärger', 'iso-8859-15')
+        response = self._makeOne(body=BODY,
+                                 headers={'content-type':
+                                            'application/foo; charset=utf-8'})
+        self.assertEqual(response.headers.get('content-type'),
+                         'application/foo; charset=utf-8')
+        # Body is re-encoded to match the header
+        self.assertEqual(response.body, BODY.encode('utf-8'))
+
+    def test_ctor_body_recodes_to_match_content_type_charset(self):
+        xml = (u'<?xml version="1.0" encoding="iso-8859-15" ?>\n'
+                '<foo><bar/></foo>')
+        response = self._makeOne(body=xml, headers={'content-type':
+                                            'text/xml; charset=utf-8'})
+        self.assertEqual(response.body, xml.replace('iso-8859-15', 'utf-8'))
+
+    def test_ctor_body_already_matches_charset_unchanged(self):
+        xml = (u'<?xml version="1.0" encoding="iso-8859-15" ?>\n'
+                '<foo><bar/></foo>')
+        response = self._makeOne(body=xml, headers={'content-type':
+                                            'text/xml; charset=iso-8859-15'})
+        self.assertEqual(response.body, xml)
+
+    def test_setStatus_BadRequest(self):
+        from zExceptions import BadRequest
+        response = self._makeOne()
+        response.setStatus(BadRequest)
+        self.assertEqual(response.status, 400)
+
+    def test_setStatus_Unauthorized(self):
+        from zExceptions import Unauthorized
+        response = self._makeOne()
+        response.setStatus(Unauthorized)
+        self.assertEqual(response.status, 401)
+
+    def test_setStatus_Forbidden(self):
         from zExceptions import Forbidden
+        response = self._makeOne()
+        response.setStatus(Forbidden)
+        self.assertEqual(response.status, 403)
+
+    def test_setStatus_NotFound(self):
         from zExceptions import NotFound
-        from zExceptions import BadRequest
+        response = self._makeOne()
+        response.setStatus(NotFound)
+        self.assertEqual(response.status, 404)
+
+    def test_setStatus_ResourceLockedError(self):
+        response = self._makeOne()
+        from webdav.Lockable import ResourceLockedError
+        response.setStatus(ResourceLockedError)
+        self.assertEqual(response.status, 423)
+
+    def test_setStatus_InternalError(self):
         from zExceptions import InternalError
+        response = self._makeOne()
+        response.setStatus(InternalError)
+        self.assertEqual(response.status, 500)
 
-        for exc_type, code in ((Unauthorized, 401),
-                               (Forbidden, 403),
-                               (NotFound, 404),
-                               (BadRequest, 400),
-                               (InternalError, 500)):
-            response = self._makeOne()
-            response.setStatus(exc_type)
-            self.assertEqual(response.status, code)
-
     def test_setCookie_no_attrs(self):
         response = self._makeOne()
         response.setCookie('foo', 'bar')
@@ -116,15 +244,6 @@
         self.assertEqual(len(cookies), 1)
         self.assertEqual(cookies[0], 'Set-Cookie: foo="bar"')
 
-    def test_expireCookie(self):
-        response = self._makeOne()
-        response.expireCookie('foo', path='/')
-        cookie = response.cookies.get('foo', None)
-        self.failUnless(cookie)
-        self.assertEqual(cookie.get('expires'), 'Wed, 31-Dec-97 23:59:59 GMT')
-        self.assertEqual(cookie.get('max_age'), 0)
-        self.assertEqual(cookie.get('path'), '/')
-
     def test_setCookie_w_httponly_true_value(self):
         response = self._makeOne()
         response.setCookie('foo', 'bar', http_only=True)
@@ -149,6 +268,15 @@
         self.assertEqual(len(cookie_list), 1)
         self.assertEqual(cookie_list[0], 'Set-Cookie: foo="bar"')
 
+    def test_expireCookie(self):
+        response = self._makeOne()
+        response.expireCookie('foo', path='/')
+        cookie = response.cookies.get('foo', None)
+        self.failUnless(cookie)
+        self.assertEqual(cookie.get('expires'), 'Wed, 31-Dec-97 23:59:59 GMT')
+        self.assertEqual(cookie.get('max_age'), 0)
+        self.assertEqual(cookie.get('path'), '/')
+
     def test_expireCookie1160(self):
         # Verify that the cookie is expired even if an expires kw arg is passed
         # http://zope.org/Collectors/Zope/1160
@@ -196,64 +324,6 @@
         self.assertEqual(response.getHeader('SPAM', literal=True), 'eggs')
         self.assertEqual(response.getHeader('spam'), None)
 
-    def test_setStatus_ResourceLockedError(self):
-        response = self._makeOne()
-        from webdav.Lockable import ResourceLockedError
-        response.setStatus(ResourceLockedError)
-        self.assertEqual(response.status, 423)
-
-    def test_ctor_charset_no_header(self):
-        response = self._makeOne(body='foo')
-        self.assertEqual(response.headers.get('content-type'),
-                         'text/plain; charset=iso-8859-15')
-
-    def test_ctor_charset_text_header(self):
-        response = self._makeOne(body='foo',
-                    headers={'content-type': 'text/plain'})
-        self.assertEqual(response.headers.get('content-type'),
-                         'text/plain; charset=iso-8859-15')
-
-    def test_ctor_charset_application_header_no_header(self):
-        response = self._makeOne(body='foo',
-                    headers={'content-type': 'application/foo'})
-        self.assertEqual(response.headers.get('content-type'),
-                         'application/foo')
-
-    def test_ctor_charset_application_header_with_header(self):
-        response = self._makeOne(body='foo', headers={'content-type':
-                                        'application/foo; charset: something'})
-        self.assertEqual(response.headers.get('content-type'),
-                         'application/foo; charset: something')
-    
-    def test_ctor_charset_application_header_unicode(self):
-        response = self._makeOne(body=unicode('ärger', 'iso-8859-15'),
-                    headers={'content-type': 'application/foo'})
-        self.assertEqual(response.headers.get('content-type'),
-                         'application/foo; charset=iso-8859-15')
-        self.assertEqual(response.body, 'ärger')
-
-    def test_ctor_charset_application_header_unicode_1(self):
-        response = self._makeOne(body=unicode('ärger', 'iso-8859-15'),
-                    headers={'content-type': 'application/foo; charset=utf-8'})
-        self.assertEqual(response.headers.get('content-type'),
-                         'application/foo; charset=utf-8')
-        self.assertEqual(response.body, unicode('ärger',
-                         'iso-8859-15').encode('utf-8'))
-
-    def test_ctor_body_recodes_to_match_content_type_charset(self):
-        xml = (u'<?xml version="1.0" encoding="iso-8859-15" ?>\n'
-                '<foo><bar/></foo>')
-        response = self._makeOne(body=xml, headers={'content-type':
-                                            'text/xml; charset=utf-8'})
-        self.assertEqual(response.body, xml.replace('iso-8859-15', 'utf-8'))
-
-    def test_ctor_body_already_matches_charset_unchanged(self):
-        xml = (u'<?xml version="1.0" encoding="iso-8859-15" ?>\n'
-                '<foo><bar/></foo>')
-        response = self._makeOne(body=xml, headers={'content-type':
-                                            'text/xml; charset=iso-8859-15'})
-        self.assertEqual(response.body, xml)
-
     def test_addHeader_drops_CRLF(self):
         # RFC2616 disallows CRLF in a header value.
         response = self._makeOne()

_______________________________________________
Zope-Checkins maillist  -  Zope-Checkins@zope.org
https://mail.zope.org/mailman/listinfo/zope-checkins

Reply via email to