File: test_error_handlers.py

package info (click to toggle)
python-falcon 0.1.8-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 832 kB
  • ctags: 997
  • sloc: python: 4,006; makefile: 39; sh: 16
file content (104 lines) | stat: -rw-r--r-- 3,268 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
import json

import falcon
import falcon.testing as testing

import six

def capture_error(ex, req, resp, params):
    resp.status = falcon.HTTP_723
    resp.body = 'error: %s' % str(ex)


def handle_error_first(ex, req, resp, params):
    resp.status = falcon.HTTP_200
    resp.body = 'first error handler'


class CustomBaseException(Exception):
    pass


class CustomException(CustomBaseException):

    @staticmethod
    def handle(ex, req, resp, params):
        raise falcon.HTTPError(
            falcon.HTTP_792,
            six.u('Internet crashed!'),
            six.u('Catastrophic weather event'),
            href=six.u('http://example.com/api/inconvenient-truth'),
            href_text=six.u('Drill, baby drill!'))


class ErroredClassResource(object):
    def on_get(self, req, resp):
        raise Exception('Plain Exception')

    def on_head(self, req, resp):
        raise CustomBaseException('CustomBaseException')

    def on_delete(self, req, resp):
        raise CustomException('CustomException')


class TestErrorHandler(testing.TestBase):

    def test_caught_error(self):
        self.api.add_error_handler(Exception, capture_error)

        self.api.add_route(self.test_route, ErroredClassResource())

        body = self.simulate_request(self.test_route)
        self.assertEqual([b'error: Plain Exception'], body)

        body = self.simulate_request(self.test_route, method='HEAD')
        self.assertEqual(falcon.HTTP_723, self.srmock.status)
        self.assertEqual([], body)

    def test_uncaught_error(self):
        self.api.add_error_handler(CustomException, capture_error)

        self.api.add_route(self.test_route, ErroredClassResource())

        self.assertRaises(Exception,
                          self.simulate_request, self.test_route)

    def test_uncaught_error_else(self):
        self.api.add_route(self.test_route, ErroredClassResource())

        self.assertRaises(Exception,
                          self.simulate_request, self.test_route)

    def test_converted_error(self):
        self.api.add_error_handler(CustomException)

        self.api.add_route(self.test_route, ErroredClassResource())

        body = self.simulate_request(self.test_route, method='DELETE')
        self.assertEqual(falcon.HTTP_792, self.srmock.status)

        info = json.loads(body[0].decode())
        self.assertEqual('Internet crashed!', info['title'])

    def test_handle_not_defined(self):
        self.assertRaises(AttributeError,
                          self.api.add_error_handler, CustomBaseException)

    def test_subclass_error(self):
        self.api.add_error_handler(CustomBaseException, capture_error)

        self.api.add_route(self.test_route, ErroredClassResource())

        body = self.simulate_request(self.test_route, method='DELETE')
        self.assertEqual(falcon.HTTP_723, self.srmock.status)
        self.assertEqual([b'error: CustomException'], body)

    def test_error_order(self):
        self.api.add_error_handler(Exception, capture_error)
        self.api.add_error_handler(Exception, handle_error_first)

        self.api.add_route(self.test_route, ErroredClassResource())

        body = self.simulate_request(self.test_route)
        self.assertEqual([b'first error handler'], body)