File: test_httpexceptions.py

package info (click to toggle)
python-pyramid 1.10.2+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,052 kB
  • sloc: python: 42,982; makefile: 41
file content (529 lines) | stat: -rw-r--r-- 18,182 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import unittest

from pyramid.compat import bytes_, string_types, text_


class Test_exception_response(unittest.TestCase):
    def _callFUT(self, *arg, **kw):
        from pyramid.httpexceptions import exception_response

        return exception_response(*arg, **kw)

    def test_status_400(self):
        from pyramid.httpexceptions import HTTPBadRequest

        self.assertTrue(isinstance(self._callFUT(400), HTTPBadRequest))

    def test_status_404(self):
        from pyramid.httpexceptions import HTTPNotFound

        self.assertTrue(isinstance(self._callFUT(404), HTTPNotFound))

    def test_status_500(self):
        from pyramid.httpexceptions import HTTPInternalServerError

        self.assertTrue(
            isinstance(self._callFUT(500), HTTPInternalServerError)
        )

    def test_status_201(self):
        from pyramid.httpexceptions import HTTPCreated

        self.assertTrue(isinstance(self._callFUT(201), HTTPCreated))

    def test_extra_kw(self):
        resp = self._callFUT(404, headers=[('abc', 'def')])
        self.assertEqual(resp.headers['abc'], 'def')


class Test_default_exceptionresponse_view(unittest.TestCase):
    def _callFUT(self, context, request):
        from pyramid.httpexceptions import default_exceptionresponse_view

        return default_exceptionresponse_view(context, request)

    def test_call_with_exception(self):
        context = Exception()
        result = self._callFUT(context, None)
        self.assertEqual(result, context)

    def test_call_with_nonexception(self):
        request = DummyRequest()
        context = Exception()
        request.exception = context
        result = self._callFUT(None, request)
        self.assertEqual(result, context)


class Test__no_escape(unittest.TestCase):
    def _callFUT(self, val):
        from pyramid.httpexceptions import _no_escape

        return _no_escape(val)

    def test_null(self):
        self.assertEqual(self._callFUT(None), '')

    def test_not_basestring(self):
        self.assertEqual(self._callFUT(42), '42')

    def test_unicode(self):
        class DummyUnicodeObject(object):
            def __unicode__(self):
                return text_('42')

        duo = DummyUnicodeObject()
        self.assertEqual(self._callFUT(duo), text_('42'))


class TestHTTPException(unittest.TestCase):
    def _getTargetClass(self):
        from pyramid.httpexceptions import HTTPException

        return HTTPException

    def _getTargetSubclass(
        self,
        code='200',
        title='OK',
        explanation='explanation',
        empty_body=False,
    ):
        cls = self._getTargetClass()

        class Subclass(cls):
            pass

        Subclass.empty_body = empty_body
        Subclass.code = code
        Subclass.title = title
        Subclass.explanation = explanation
        return Subclass

    def _makeOne(self, *arg, **kw):
        cls = self._getTargetClass()
        return cls(*arg, **kw)

    def test_implements_IResponse(self):
        from pyramid.interfaces import IResponse

        cls = self._getTargetClass()
        self.assertTrue(IResponse.implementedBy(cls))

    def test_provides_IResponse(self):
        from pyramid.interfaces import IResponse

        inst = self._getTargetClass()()
        self.assertTrue(IResponse.providedBy(inst))

    def test_implements_IExceptionResponse(self):
        from pyramid.interfaces import IExceptionResponse

        cls = self._getTargetClass()
        self.assertTrue(IExceptionResponse.implementedBy(cls))

    def test_provides_IExceptionResponse(self):
        from pyramid.interfaces import IExceptionResponse

        inst = self._getTargetClass()()
        self.assertTrue(IExceptionResponse.providedBy(inst))

    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, '520 Unknown Error')

    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._getTargetSubclass(empty_body=True)
        exc = cls()
        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=b'123')
        self.assertEqual(exc.app_iter, [b'123'])

    def test_ctor_with_unicode_body_doesnt_set_default_app_iter(self):
        exc = self._makeOne(unicode_body=text_('123'))
        self.assertEqual(exc.app_iter, [b'123'])

    def test_ctor_with_app_iter_doesnt_set_default_app_iter(self):
        exc = self._makeOne(app_iter=[b'123'])
        self.assertEqual(exc.app_iter, [b'123'])

    def test_ctor_with_body_sets_default_app_iter_html(self):
        cls = self._getTargetSubclass()
        exc = cls('detail')
        environ = _makeEnviron()
        environ['HTTP_ACCEPT'] = 'text/html'
        start_response = DummyStartResponse()
        body = list(exc(environ, start_response))[0]
        self.assertTrue(body.startswith(b'<html'))
        self.assertTrue(b'200 OK' in body)
        self.assertTrue(b'explanation' in body)
        self.assertTrue(b'detail' in body)

    def test_ctor_with_body_sets_default_app_iter_text(self):
        cls = self._getTargetSubclass()
        exc = cls('detail')
        environ = _makeEnviron()
        start_response = DummyStartResponse()
        body = list(exc(environ, start_response))[0]
        self.assertEqual(body, b'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)

    def test__calls_start_response(self):
        cls = self._getTargetSubclass()
        exc = cls()
        environ = _makeEnviron()
        start_response = DummyStartResponse()
        exc(environ, start_response)
        self.assertTrue(start_response.headerlist)
        self.assertEqual(start_response.status, '200 OK')

    def test_call_returns_same_body_called_twice(self):
        # optimization
        cls = self._getTargetSubclass()
        exc = cls()
        environ = _makeEnviron()
        environ['HTTP_ACCEPT'] = '*/*'
        start_response = DummyStartResponse()
        app_iter = exc(environ, start_response)
        self.assertEqual(app_iter[0], exc.body)

    def test__default_app_iter_no_comment_plain(self):
        cls = self._getTargetSubclass()
        exc = cls()
        environ = _makeEnviron()
        start_response = DummyStartResponse()
        body = list(exc(environ, start_response))[0]
        for header in start_response.headerlist:
            if header[0] == 'Content-Type':
                self.assertEqual(header[1], 'text/plain; charset=UTF-8')
        self.assertEqual(body, b'200 OK\n\nexplanation\n\n\n\n\n')

    def test__default_app_iter_with_comment_plain(self):
        cls = self._getTargetSubclass()
        exc = cls(comment='comment')
        environ = _makeEnviron()
        start_response = DummyStartResponse()
        body = list(exc(environ, start_response))[0]
        for header in start_response.headerlist:
            if header[0] == 'Content-Type':
                self.assertEqual(header[1], 'text/plain; charset=UTF-8')
        self.assertEqual(body, b'200 OK\n\nexplanation\n\n\n\ncomment\n')

    def test__default_app_iter_no_comment_html(self):
        cls = self._getTargetSubclass()
        exc = cls()
        environ = _makeEnviron()
        start_response = DummyStartResponse()
        body = list(exc(environ, start_response))[0]
        for header in start_response.headerlist:
            if header[0] == 'Content-Type':
                self.assertEqual(header[1], 'text/plain; charset=UTF-8')
        self.assertFalse(b'<!-- ' in body)

    def test__content_type(self):
        cls = self._getTargetSubclass()
        exc = cls()
        environ = _makeEnviron()
        start_response = DummyStartResponse()
        exc(environ, start_response)
        for header in start_response.headerlist:
            if header[0] == 'Content-Type':
                self.assertEqual(header[1], 'text/plain; charset=UTF-8')

    def test__content_type_default_is_html(self):
        cls = self._getTargetSubclass()
        exc = cls()
        environ = _makeEnviron()
        environ['HTTP_ACCEPT'] = '*/*'
        start_response = DummyStartResponse()
        exc(environ, start_response)
        for header in start_response.headerlist:
            if header[0] == 'Content-Type':
                self.assertEqual(header[1], 'text/html; charset=UTF-8')

    def test__content_type_text_html(self):
        cls = self._getTargetSubclass()
        exc = cls()
        environ = _makeEnviron()
        environ['HTTP_ACCEPT'] = 'text/html'
        start_response = DummyStartResponse()
        exc(environ, start_response)
        for header in start_response.headerlist:
            if header[0] == 'Content-Type':
                self.assertEqual(header[1], 'text/html; charset=UTF-8')

    def test__content_type_application_json(self):
        cls = self._getTargetSubclass()
        exc = cls()
        environ = _makeEnviron()
        environ['HTTP_ACCEPT'] = 'application/json'
        start_response = DummyStartResponse()
        exc(environ, start_response)
        for header in start_response.headerlist:
            if header[0] == 'Content-Type':
                self.assertEqual(header[1], 'application/json')

    def test__content_type_invalid(self):
        cls = self._getTargetSubclass()
        exc = cls()
        environ = _makeEnviron()
        environ['HTTP_ACCEPT'] = 'invalid'
        start_response = DummyStartResponse()
        exc(environ, start_response)
        for header in start_response.headerlist:
            if header[0] == 'Content-Type':
                self.assertEqual(header[1], 'text/html; charset=UTF-8')

    def test__default_app_iter_with_comment_ampersand(self):
        cls = self._getTargetSubclass()
        exc = cls(comment='comment & comment')
        environ = _makeEnviron()
        environ['HTTP_ACCEPT'] = 'text/html'
        start_response = DummyStartResponse()
        body = list(exc(environ, start_response))[0]
        for header in start_response.headerlist:
            if header[0] == 'Content-Type':
                self.assertEqual(header[1], 'text/html; charset=UTF-8')
        self.assertTrue(b'<!-- comment &amp; comment -->' in body)

    def test__default_app_iter_with_comment_html(self):
        cls = self._getTargetSubclass()
        exc = cls(comment='comment & comment')
        environ = _makeEnviron()
        environ['HTTP_ACCEPT'] = 'text/html'
        start_response = DummyStartResponse()
        body = list(exc(environ, start_response))[0]
        self.assertTrue(b'<!-- comment &amp; comment -->' in body)

    def test__default_app_iter_with_comment_json(self):
        cls = self._getTargetSubclass()
        exc = cls(comment='comment & comment')
        environ = _makeEnviron()
        environ['HTTP_ACCEPT'] = 'application/json'
        start_response = DummyStartResponse()
        body = list(exc(environ, start_response))[0]
        import json

        retval = json.loads(body.decode('UTF-8'))
        self.assertEqual(retval['code'], '200 OK')
        self.assertEqual(retval['title'], 'OK')

    def test__default_app_iter_with_custom_json(self):
        def json_formatter(status, body, title, environ):
            return {
                'message': body,
                'code': status,
                'title': title,
                'custom': environ['CUSTOM_VARIABLE'],
            }

        cls = self._getTargetSubclass()
        exc = cls(comment='comment', json_formatter=json_formatter)
        environ = _makeEnviron()
        environ['HTTP_ACCEPT'] = 'application/json'
        environ['CUSTOM_VARIABLE'] = 'custom!'
        start_response = DummyStartResponse()
        body = list(exc(environ, start_response))[0]
        import json

        retval = json.loads(body.decode('UTF-8'))
        self.assertEqual(retval['code'], '200 OK')
        self.assertEqual(retval['title'], 'OK')
        self.assertEqual(retval['custom'], 'custom!')

    def test_custom_body_template(self):
        cls = self._getTargetSubclass()
        exc = cls(body_template='${REQUEST_METHOD}')
        environ = _makeEnviron()
        start_response = DummyStartResponse()
        body = list(exc(environ, start_response))[0]
        self.assertEqual(body, b'200 OK\n\nGET')

    def test_custom_body_template_with_custom_variable_doesnt_choke(self):
        cls = self._getTargetSubclass()
        exc = cls(body_template='${REQUEST_METHOD}')
        environ = _makeEnviron()

        class Choke(object):
            def __str__(self):  # pragma no cover
                raise ValueError

        environ['gardentheory.user'] = Choke()
        start_response = DummyStartResponse()
        body = list(exc(environ, start_response))[0]
        self.assertEqual(body, b'200 OK\n\nGET')

    def test_body_template_unicode(self):
        cls = self._getTargetSubclass()
        la = text_(b'/La Pe\xc3\xb1a', 'utf-8')
        environ = _makeEnviron(unicodeval=la)
        exc = cls(body_template='${unicodeval}')
        start_response = DummyStartResponse()
        body = list(exc(environ, start_response))[0]
        self.assertEqual(body, b'200 OK\n\n/La Pe\xc3\xb1a')

    def test_allow_detail_non_str(self):
        exc = self._makeOne(detail={'error': 'This is a test'})
        self.assertIsInstance(exc.__str__(), string_types)


class TestRenderAllExceptionsWithoutArguments(unittest.TestCase):
    def _doit(self, content_type):
        from pyramid.httpexceptions import status_map

        L = []
        self.assertTrue(status_map)
        for v in status_map.values():
            environ = _makeEnviron()
            start_response = DummyStartResponse()
            exc = v()
            exc.content_type = content_type
            result = list(exc(environ, start_response))[0]
            if exc.empty_body:
                self.assertEqual(result, b'')
            else:
                self.assertTrue(bytes_(exc.status) in result)
            L.append(result)
        self.assertEqual(len(L), len(status_map))

    def test_it_plain(self):
        self._doit('text/plain')

    def test_it_html(self):
        self._doit('text/html')


class Test_HTTPMove(unittest.TestCase):
    def _makeOne(self, *arg, **kw):
        from pyramid.httpexceptions import _HTTPMove

        return _HTTPMove(*arg, **kw)

    def test_it_location_none_valueerrors(self):
        # Constructing a HTTPMove instance with location=None should
        # throw a ValueError from __init__ so that a more-confusing
        # exception won't be thrown later from .prepare(environ)
        self.assertRaises(ValueError, self._makeOne, location=None)

    def test_it_location_not_passed(self):
        exc = self._makeOne()
        self.assertEqual(exc.location, '')

    def test_it_location_passed(self):
        exc = self._makeOne(location='foo')
        self.assertEqual(exc.location, 'foo')

    def test_it_location_firstarg(self):
        exc = self._makeOne('foo')
        self.assertEqual(exc.location, 'foo')

    def test_it_call_with_default_body_tmpl(self):
        exc = self._makeOne(location='foo')
        environ = _makeEnviron()
        start_response = DummyStartResponse()
        app_iter = exc(environ, start_response)
        self.assertEqual(
            app_iter[0],
            (
                b'520 Unknown Error\n\nThe resource has been moved to foo; '
                b'you should be redirected automatically.\n\n'
            ),
        )


class TestHTTPForbidden(unittest.TestCase):
    def _makeOne(self, *arg, **kw):
        from pyramid.httpexceptions import HTTPForbidden

        return HTTPForbidden(*arg, **kw)

    def test_it_result_not_passed(self):
        exc = self._makeOne()
        self.assertEqual(exc.result, None)

    def test_it_result_passed(self):
        exc = self._makeOne(result='foo')
        self.assertEqual(exc.result, 'foo')


class TestHTTPMethodNotAllowed(unittest.TestCase):
    def _makeOne(self, *arg, **kw):
        from pyramid.httpexceptions import HTTPMethodNotAllowed

        return HTTPMethodNotAllowed(*arg, **kw)

    def test_it_with_default_body_tmpl(self):
        exc = self._makeOne()
        environ = _makeEnviron()
        start_response = DummyStartResponse()
        app_iter = exc(environ, start_response)
        self.assertEqual(
            app_iter[0],
            (
                b'405 Method Not Allowed\n\nThe method GET is not '
                b'allowed for this resource. \n\n\n'
            ),
        )


class DummyRequest(object):
    exception = None


class DummyStartResponse(object):
    def __call__(self, status, headerlist):
        self.status = status
        self.headerlist = headerlist


def _makeEnviron(**kw):
    environ = {
        'REQUEST_METHOD': 'GET',
        'wsgi.url_scheme': 'http',
        'SERVER_NAME': 'localhost',
        'SERVER_PORT': '80',
    }
    environ.update(kw)
    return environ