8000 add some tests for WSGIHTTPException · alex-python/pyramid@c49e9f4 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit c49e9f4

Browse files
committed
add some tests for WSGIHTTPException
1 parent 9b496b8 commit c49e9f4

File tree

2 files changed

+105
-11
lines changed

2 files changed

+105
-11
lines changed

pyramid/exceptions.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ def _default_app_iter(self):
222222
# (e.g. self.content_type or self.charset).
223223
html_comment = ''
224224
comment = self.comment or ''
225-
if 'html' in self.content_type or '':
225+
content_type = self.content_type or ''
226+
if 'html' in content_type:
226227
escape = _html_escape
227228
page_template = self.html_template_obj
228229
br = '<br/>'
@@ -231,7 +232,7 @@ def _default_app_iter(self):
231232
else:
232233
escape = _no_escape
233234
page_template = self.plain_template_obj
234-
br = '\r\n'
235+
br = '\n'
235236
if comment:
236237
html_comment = escape(comment)
237238
args = {
@@ -257,15 +258,11 @@ def _default_app_iter(self):
257258
yield page
258259
raise StopIteration
259260

260-
def wsgi_response(self):
261-
# bw compat only
262-
return self
263-
wsgi_response = property(wsgi_response)
264-
261+
@property
265262
def exception(self):
266263
# bw compat only
267264
return self
268-
exception = property(exception)
265+
wsgi_response = exception # bw compat only
269266

270267
class HTTPError(WSGIHTTPException):
271268
"""

pyramid/tests/test_exceptions.py

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ def test_call_with_nonexception(self):
9494
result = self._callFUT(None, request)
9595
self.assertEqual(result, context)
9696

97-
class Test_no_escape(unittest.TestCase):
97+
class Test__no_escape(unittest.TestCase):
9898
def _callFUT(self, val):
99-
from pyramid.exceptions import no_escape
100-
return no_escape(val)
99+
from pyramid.exceptions import _no_escape
100+
return _no_escape(val)
101101

102102
def test_null(self):
103103
self.assertEqual(self._callFUT(None), '')
@@ -112,6 +112,103 @@ def __unicode__(self):
112112
duo = DummyUnicodeObject()
113113
self.assertEqual(self._callFUT(duo), u'42')
114114

115+
class TestWSGIHTTPException(unittest.TestCase):
116+
def _getTargetClass(self):
117+
from pyramid.exceptions import WSGIHTTPException
118+
return WSGIHTTPException
119+
120+
def _makeOne(self, *arg, **kw):
121+
cls = self._getTargetClass()
122+
return cls(*arg, **kw)
123+
124+
def test_ctor_sets_detail(self):
125+
exc = self._makeOne('message')
126+
self.assertEqual(exc.detail, 'message')
127+
128+
def test_ctor_sets_comment(self):
129+
exc = self._makeOne(comment='comment')
130+
self.assertEqual(exc.comment, 'comment')
131+
132+
def test_ctor_calls_Exception_ctor(self):
133+
exc = self._makeOne('message')
134+
self.assertEqual(exc.message, 'message')
135+
136+
def test_ctor_calls_Response_ctor(self):
137+
exc = self._makeOne('message')
138+
self.assertEqual(exc.status, 'None None')
139+
140+
def test_ctor_extends_headers(self):
141+
exc = self._makeOne(headers=[('X-Foo', 'foo')])
142+
self.assertEqual(exc.headers.get('X-Foo'), 'foo')
143+
144+
def test_ctor_sets_body_template_obj(self):
145+
exc = self._makeOne(body_template='${foo}')
146+
self.assertEqual(
147+
exc.body_template_obj.substitute({'foo':'foo'}), 'foo')
148+
149+
def test_ctor_with_empty_body(self):
150+
cls = self._getTargetClass()
151+
class Subclass(cls):
152+
empty_body = True
153+
exc = Subclass()
154+
self.assertEqual(exc.content_type, None)
155+
self.assertEqual(exc.content_length, None)
156+
157+
def test_ctor_with_body_doesnt_set_default_app_iter(self):
158+
exc = self._makeOne(body='123')
159+
self.assertEqual(exc.app_iter, ['123'])
160+
161+
def test_ctor_with_unicode_body_doesnt_set_default_app_iter(self):
162+
exc = self._makeOne(unicode_body=u'123')
163+
self.assertEqual(exc.app_iter, ['123'])
164+
165+
def test_ctor_with_app_iter_doesnt_set_default_app_iter(self):
166+
exc = self._makeOne(app_iter=['123'])
167+
self.assertEqual(exc.app_iter, ['123'])
168+
169+
def test_ctor_with_body_sets_default_app_iter_html(self):
170+
cls = self._getTargetClass()
171+
class Subclass(cls):
172+
code = '200'
173+
title = 'OK'
174+
explanation = 'explanation'
175+
exc = Subclass('detail')
176+
body = list(exc.app_iter)[0]
177+
self.assertTrue(body.startswith('<html'))
178+
self.assertTrue('200 OK' in body)
179+
self.assertTrue('explanation' in body)
180+
self.assertTrue('detail' in body)
181+
182+
def test_ctor_with_body_sets_default_app_iter_text(self):
183+
cls = self._getTargetClass()
184+
class Subclass(cls):
185+
code = '200'
186+
title = 'OK'
187+
explanation = 'explanation'
188+
exc = Subclass('detail')
189+
exc.content_type = 'text/plain'
190+
body = list(exc.app_iter)[0]
191+
self.assertEqual(body, '200 OK\n\nexplanation\n\n\ndetail\n\n')
192+
193+
def test__str__detail(self):
194+
exc = self._makeOne()
195+
exc.detail = 'abc'
196+
self.assertEqual(str(exc), 'abc')
197+
198+
def test__str__explanation(self):
199+
exc = self._makeOne()
200+
exc.explanation = 'def'
201+
self.assertEqual(str(exc), 'def')
202+
203+
def test_wsgi_response(self):
204+
exc = self._makeOne()
205+
self.assertTrue(exc is exc.wsgi_response)
206+
207+
def test_exception(self):
208+
exc = self._makeOne()
209+
self.assertTrue(exc is exc.exception)
210+
211+
115212
class DummyRequest(object):
116213
exception = None
117214

0 commit comments

Comments
 (0)
0