File: test_http_method_routing.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 (199 lines) | stat: -rw-r--r-- 5,598 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
from functools import wraps

from testtools.matchers import Contains

import falcon
import falcon.testing as testing

HTTP_METHODS = (
    'CONNECT',
    'DELETE',
    'GET',
    'HEAD',
    'OPTIONS',
    'POST',
    'PUT',
    'TRACE',
    'PATCH'
)


class ThingsResource(object):
    def __init__(self):
        self.called = False

        # Test non-callable attribute
        self.on_patch = {}

    # Field names ordered differently than in uri template
    def on_get(self, req, resp, sid, id):
        self.called = True

        self.req, self.resp = req, resp
        resp.status = falcon.HTTP_204

    # Field names ordered the same as in uri template
    def on_head(self, req, resp, id, sid):
        self.called = True

        self.req, self.resp = req, resp
        resp.status = falcon.HTTP_204

    def on_put(self, req, resp, id, sid):
        self.called = True

        self.req, self.resp = req, resp
        resp.status = falcon.HTTP_201


class Stonewall(object):
    pass


def capture(func):
    @wraps(func)
    def with_capture(*args, **kwargs):
        self = args[0]
        self.called = True
        self.req, self.resp = args[1:]
        func(*args, **kwargs)

    return with_capture


def selfless_decorator(func):
    def faulty(req, resp, foo, bar):
        pass

    return faulty


class MiscResource(object):
    def __init__(self):
        self.called = False

    @capture
    def on_get(self, req, resp):
        resp.status = falcon.HTTP_204

    @capture
    def on_head(self, req, resp):
        resp.status = falcon.HTTP_204

    @capture
    def on_put(self, req, resp):
        resp.status = falcon.HTTP_400

    @capture
    def on_patch(self, req, resp):
        pass


class GetWithFaultyPutResource(object):
    def __init__(self):
        self.called = False

    @capture
    def on_get(self, req, resp):
        resp.status = falcon.HTTP_204

    def on_put(self, req, resp, param):
        raise TypeError()


class FaultyDecoratedResource(object):

    @selfless_decorator
    def on_get(self, req, resp):
        pass


class TestHttpMethodRouting(testing.TestBase):

    def before(self):
        self.api.add_route('/stonewall', Stonewall())

        self.resource_things = ThingsResource()
        self.api.add_route('/things', self.resource_things)
        self.api.add_route('/things/{id}/stuff/{sid}', self.resource_things)

        self.resource_misc = MiscResource()
        self.api.add_route('/misc', self.resource_misc)

        self.resource_get_with_faulty_put = GetWithFaultyPutResource()
        self.api.add_route('/get_with_param/{param}',
                           self.resource_get_with_faulty_put)

    def test_get(self):
        self.simulate_request('/things/42/stuff/57')
        self.assertEqual(self.srmock.status, falcon.HTTP_204)
        self.assertTrue(self.resource_things.called)

    def test_put(self):
        self.simulate_request('/things/42/stuff/1337', method='PUT')
        self.assertEqual(self.srmock.status, falcon.HTTP_201)
        self.assertTrue(self.resource_things.called)

    def test_post_not_allowed(self):
        self.simulate_request('/things/42/stuff/1337', method='POST')
        self.assertEqual(self.srmock.status, falcon.HTTP_405)
        self.assertFalse(self.resource_things.called)

    def test_misc(self):
        for method in ['GET', 'HEAD', 'PUT', 'PATCH']:
            self.resource_misc.called = False
            self.simulate_request('/misc', method=method)
            self.assertTrue(self.resource_misc.called)
            self.assertEqual(self.resource_misc.req.method, method)

    def test_methods_not_allowed_simple(self):
        for method in ['GET', 'HEAD', 'PUT', 'PATCH']:
            self.simulate_request('/stonewall', method=method)
            self.assertEqual(self.srmock.status, falcon.HTTP_405)

    def test_methods_not_allowed_complex(self):
        for method in HTTP_METHODS:
            if method in ('GET', 'PUT', 'HEAD', 'OPTIONS'):
                continue

            self.resource_things.called = False
            self.simulate_request('/things/84/stuff/65', method=method)

            self.assertFalse(self.resource_things.called)
            self.assertEqual(self.srmock.status, falcon.HTTP_405)

            headers = self.srmock.headers
            allow_header = ('allow', 'GET, HEAD, PUT, OPTIONS')

            self.assertThat(headers, Contains(allow_header))

    def test_method_not_allowed_with_param(self):
        for method in HTTP_METHODS:
            if method in ('GET', 'PUT', 'OPTIONS'):
                continue

            self.resource_get_with_faulty_put.called = False
            self.simulate_request(
                '/get_with_param/bogus_param', method=method)

            self.assertFalse(self.resource_get_with_faulty_put.called)
            self.assertEqual(self.srmock.status, falcon.HTTP_405)

            headers = self.srmock.headers
            allow_header = ('allow', 'GET, PUT, OPTIONS')

            self.assertThat(headers, Contains(allow_header))

    def test_default_on_options(self):
        self.simulate_request('/things/84/stuff/65', method='OPTIONS')
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        headers = self.srmock.headers
        allow_header = ('allow', 'GET, HEAD, PUT')

        self.assertThat(headers, Contains(allow_header))

    def test_bogus_method(self):
        self.simulate_request('/things', method=self.getUniqueString())
        self.assertFalse(self.resource_things.called)
        self.assertEqual(self.srmock.status, falcon.HTTP_400)