[Zope-Checkins] SVN: Zope/branches/tseaver-fix_wsgi/src/ZPublisher/ Coverage for redirect, _encode_unicode, and quoteHtml.
Tres Seaver
tseaver at palladion.com
Mon Dec 21 22:34:39 EST 2009
Log message for revision 106843:
Coverage for redirect, _encode_unicode, and quoteHtml.
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-22 03:03:48 UTC (rev 106842)
+++ Zope/branches/tseaver-fix_wsgi/src/ZPublisher/HTTPResponse.py 2009-12-22 03:34:38 UTC (rev 106843)
@@ -602,6 +602,13 @@
return self.use_HTTP_content_compression
+ def redirect(self, location, status=302, lock=0):
+ """Cause a redirection without raising an error"""
+ self.setStatus(status, lock=lock)
+ self.setHeader('Location', location)
+
+ return str(location)
+
# The following two methods are part of a private protocol with the
# publisher for handling fatal import errors and TTW shutdown requests.
_shutdown_flag = None
@@ -664,21 +671,7 @@
tb = format_exception(t, v, tb, as_html=as_html)
return '\n'.join(tb)
- def redirect(self, location, status=302, lock=0):
- """Cause a redirection without raising an error"""
- self.setStatus(status)
- self.setHeader('Location', location)
- location = str(location)
-
- if lock:
- # Don't let anything change the status code.
- # The "lock" argument needs to be set when redirecting
- # from a standard_error_message page.
- self._locked_status = 1
- return location
-
-
def _html(self,title,body):
return ("<html>\n"
"<head>\n<title>%s</title>\n</head>\n"
Modified: Zope/branches/tseaver-fix_wsgi/src/ZPublisher/tests/testHTTPResponse.py
===================================================================
--- Zope/branches/tseaver-fix_wsgi/src/ZPublisher/tests/testHTTPResponse.py 2009-12-22 03:03:48 UTC (rev 106842)
+++ Zope/branches/tseaver-fix_wsgi/src/ZPublisher/tests/testHTTPResponse.py 2009-12-22 03:34:38 UTC (rev 106843)
@@ -4,6 +4,17 @@
class HTTPResponseTests(unittest.TestCase):
+ _old_default_encoding = None
+
+ def tearDown(self):
+ if self._old_default_encoding is not None:
+ self._setDefaultEncoding(self._old_default_encoding)
+
+ def _setDefaultEncoding(self, value):
+ from ZPublisher import HTTPResponse as module
+ (module.default_encoding,
+ self._old_default_encoding) = (value, module.default_encoding)
+
def _getTargetClass(self):
from ZPublisher.HTTPResponse import HTTPResponse
@@ -81,7 +92,7 @@
'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,
@@ -691,7 +702,88 @@
response.setBody('foo' * 100) # body must get smaller on compression
self.assertEqual(response.getHeader('Vary'), None)
+ def test_redirect_defaults(self):
+ URL = 'http://example.com'
+ response = self._makeOne()
+ result = response.redirect(URL)
+ self.assertEqual(result, URL)
+ self.assertEqual(response.status, 302)
+ self.assertEqual(response.getHeader('Location'), URL)
+ self.failIf(response._locked_status)
+ def test_redirect_explicit_status(self):
+ URL = 'http://example.com'
+ response = self._makeOne()
+ result = response.redirect(URL, status=307)
+ self.assertEqual(response.status, 307)
+ self.assertEqual(response.getHeader('Location'), URL)
+ self.failIf(response._locked_status)
+
+ def test_redirect_w_lock(self):
+ URL = 'http://example.com'
+ response = self._makeOne()
+ result = response.redirect(URL, lock=True)
+ self.assertEqual(response.status, 302)
+ self.assertEqual(response.getHeader('Location'), URL)
+ self.failUnless(response._locked_status)
+
+ def test__encode_unicode_no_content_type_uses_default_encoding(self):
+ self._setDefaultEncoding('UTF8')
+ UNICODE = u'<h1>Tr\u0039s Bien</h1>'
+ response = self._makeOne()
+ self.assertEqual(response._encode_unicode(UNICODE),
+ UNICODE.encode('UTF8'))
+
+ def test__encode_unicode_w_content_type_no_charset_updates_charset(self):
+ self._setDefaultEncoding('UTF8')
+ UNICODE = u'<h1>Tr\u0039s Bien</h1>'
+ response = self._makeOne()
+ response.setHeader('Content-Type', 'text/html')
+ self.assertEqual(response._encode_unicode(UNICODE),
+ UNICODE.encode('UTF8'))
+ response.getHeader('Content-Type', 'text/html; charset=UTF8')
+
+ def test__encode_unicode_w_content_type_w_charset(self):
+ self._setDefaultEncoding('UTF8')
+ UNICODE = u'<h1>Tr\u0039s Bien</h1>'
+ response = self._makeOne()
+ response.setHeader('Content-Type', 'text/html; charset=latin1')
+ self.assertEqual(response._encode_unicode(UNICODE),
+ UNICODE.encode('latin1'))
+ response.getHeader('Content-Type', 'text/html; charset=latin1')
+
+ def test__encode_unicode_w_content_type_w_charset_xml_preamble(self):
+ self._setDefaultEncoding('UTF8')
+ PREAMBLE = u'<?xml version="1.0" ?>'
+ ELEMENT = u'<element>Tr\u0039s Bien</element>'
+ UNICODE = u'\n'.join([PREAMBLE, ELEMENT])
+ response = self._makeOne()
+ response.setHeader('Content-Type', 'text/html; charset=latin1')
+ self.assertEqual(response._encode_unicode(UNICODE),
+ '<?xml version="1.0" encoding="latin1" ?>\n'
+ + ELEMENT.encode('latin1'))
+ response.getHeader('Content-Type', 'text/html; charset=latin1')
+
+ def test_quoteHTML(self):
+ BEFORE = '<p>This is a story about a boy named "Sue"</p>'
+ AFTER = ('<p>This is a story about a boy named '
+ '"Sue"</p>')
+ response = self._makeOne()
+ self.assertEqual(response.quoteHTML(BEFORE), AFTER)
+
+ #TODO
+ # def test_notFoundError
+ # def test_forbiddenError
+ # def test_debugError
+ # def test_badRequestError
+ # def test__unauthorized
+ # def test_unauthorized
+ # def test_exception*
+ # def test__cookie_list ?
+ # def test___str__*
+ # def test_write_already_wrote
+ # def test_write_not_already_wrote
+
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(HTTPResponseTests, 'test'))
More information about the Zope-Checkins
mailing list