summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2011-05-29 03:33:16 -0400
committerChris McDonough <chrism@plope.com>2011-05-29 03:33:16 -0400
commitc49e9f42e7932599a5a4864c9a17b494592c98c3 (patch)
tree669be53d9fdf2879e630a8a495c3f7239abebea3
parent9b496b8b248911ca8bd6c2bce08b73ee894809c4 (diff)
downloadpyramid-c49e9f42e7932599a5a4864c9a17b494592c98c3.tar.gz
pyramid-c49e9f42e7932599a5a4864c9a17b494592c98c3.tar.bz2
pyramid-c49e9f42e7932599a5a4864c9a17b494592c98c3.zip
add some tests for WSGIHTTPException
-rw-r--r--pyramid/exceptions.py13
-rw-r--r--pyramid/tests/test_exceptions.py103
2 files changed, 105 insertions, 11 deletions
diff --git a/pyramid/exceptions.py b/pyramid/exceptions.py
index c2db99627..8b3c75549 100644
--- a/pyramid/exceptions.py
+++ b/pyramid/exceptions.py
@@ -222,7 +222,8 @@ ${body}''')
# (e.g. self.content_type or self.charset).
html_comment = ''
comment = self.comment or ''
- if 'html' in self.content_type or '':
+ content_type = self.content_type or ''
+ if 'html' in content_type:
escape = _html_escape
page_template = self.html_template_obj
br = '<br/>'
@@ -231,7 +232,7 @@ ${body}''')
else:
escape = _no_escape
page_template = self.plain_template_obj
- br = '\r\n'
+ br = '\n'
if comment:
html_comment = escape(comment)
args = {
@@ -257,15 +258,11 @@ ${body}''')
yield page
raise StopIteration
- def wsgi_response(self):
- # bw compat only
- return self
- wsgi_response = property(wsgi_response)
-
+ @property
def exception(self):
# bw compat only
return self
- exception = property(exception)
+ wsgi_response = exception # bw compat only
class HTTPError(WSGIHTTPException):
"""
diff --git a/pyramid/tests/test_exceptions.py b/pyramid/tests/test_exceptions.py
index e3e44c9f2..bbeb280f4 100644
--- a/pyramid/tests/test_exceptions.py
+++ b/pyramid/tests/test_exceptions.py
@@ -94,10 +94,10 @@ class Test_default_exceptionresponse_view(unittest.TestCase):
result = self._callFUT(None, request)
self.assertEqual(result, context)
-class Test_no_escape(unittest.TestCase):
+class Test__no_escape(unittest.TestCase):
def _callFUT(self, val):
- from pyramid.exceptions import no_escape
- return no_escape(val)
+ from pyramid.exceptions import _no_escape
+ return _no_escape(val)
def test_null(self):
self.assertEqual(self._callFUT(None), '')
@@ -112,6 +112,103 @@ class Test_no_escape(unittest.TestCase):
duo = DummyUnicodeObject()
self.assertEqual(self._callFUT(duo), u'42')
+class TestWSGIHTTPException(unittest.TestCase):
+ def _getTargetClass(self):
+ from pyramid.exceptions import WSGIHTTPException
+ return WSGIHTTPException
+
+ def _makeOne(self, *arg, **kw):
+ cls = self._getTargetClass()
+ return cls(*arg, **kw)
+
+ def test_ctor_sets_detail(self):
+ exc = self._makeOne('message')
+ self.assertEqual(exc.detail, 'message')
+
+ def test_ctor_sets_comment(self):
+ exc = self._makeOne(comment='comment')
+ self.assertEqual(exc.comment, 'comment')
+
+ def test_ctor_calls_Exception_ctor(self):
+ exc = self._makeOne('message')
+ self.assertEqual(exc.message, 'message')
+
+ def test_ctor_calls_Response_ctor(self):
+ exc = self._makeOne('message')
+ self.assertEqual(exc.status, 'None None')
+
+ def test_ctor_extends_headers(self):
+ exc = self._makeOne(headers=[('X-Foo', 'foo')])
+ self.assertEqual(exc.headers.get('X-Foo'), 'foo')
+
+ def test_ctor_sets_body_template_obj(self):
+ exc = self._makeOne(body_template='${foo}')
+ self.assertEqual(
+ exc.body_template_obj.substitute({'foo':'foo'}), 'foo')
+
+ def test_ctor_with_empty_body(self):
+ cls = self._getTargetClass()
+ class Subclass(cls):
+ empty_body = True
+ exc = Subclass()
+ self.assertEqual(exc.content_type, None)
+ self.assertEqual(exc.content_length, None)
+
+ def test_ctor_with_body_doesnt_set_default_app_iter(self):
+ exc = self._makeOne(body='123')
+ self.assertEqual(exc.app_iter, ['123'])
+
+ def test_ctor_with_unicode_body_doesnt_set_default_app_iter(self):
+ exc = self._makeOne(unicode_body=u'123')
+ self.assertEqual(exc.app_iter, ['123'])
+
+ def test_ctor_with_app_iter_doesnt_set_default_app_iter(self):
+ exc = self._makeOne(app_iter=['123'])
+ self.assertEqual(exc.app_iter, ['123'])
+
+ def test_ctor_with_body_sets_default_app_iter_html(self):
+ cls = self._getTargetClass()
+ class Subclass(cls):
+ code = '200'
+ title = 'OK'
+ explanation = 'explanation'
+ exc = Subclass('detail')
+ body = list(exc.app_iter)[0]
+ self.assertTrue(body.startswith('<html'))
+ self.assertTrue('200 OK' in body)
+ self.assertTrue('explanation' in body)
+ self.assertTrue('detail' in body)
+
+ def test_ctor_with_body_sets_default_app_iter_text(self):
+ cls = self._getTargetClass()
+ class Subclass(cls):
+ code = '200'
+ title = 'OK'
+ explanation = 'explanation'
+ exc = Subclass('detail')
+ exc.content_type = 'text/plain'
+ body = list(exc.app_iter)[0]
+ self.assertEqual(body, '200 OK\n\nexplanation\n\n\ndetail\n\n')
+
+ def test__str__detail(self):
+ exc = self._makeOne()
+ exc.detail = 'abc'
+ self.assertEqual(str(exc), 'abc')
+
+ def test__str__explanation(self):
+ exc = self._makeOne()
+ exc.explanation = 'def'
+ self.assertEqual(str(exc), 'def')
+
+ def test_wsgi_response(self):
+ exc = self._makeOne()
+ self.assertTrue(exc is exc.wsgi_response)
+
+ def test_exception(self):
+ exc = self._makeOne()
+ self.assertTrue(exc is exc.exception)
+
+
class DummyRequest(object):
exception = None