File: test_uri_templates.py

package info (click to toggle)
python-falcon 4.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,172 kB
  • sloc: python: 33,608; javascript: 92; sh: 50; makefile: 50
file content (604 lines) | stat: -rw-r--r-- 17,265 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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
"""Application tests for URI templates using simulate_get().

These tests differ from those in test_default_router in that they are
a collection of sanity-checks that exercise the full framework code
path via simulate_get(), vs. probing the router directly.
"""

from datetime import datetime
from datetime import timezone
import math
import uuid

import pytest

import falcon
from falcon import testing
from falcon.routing.util import SuffixedMethodNotFoundError

_TEST_UUID = uuid.uuid4()
_TEST_UUID_2 = uuid.uuid4()
_TEST_UUID_STR = str(_TEST_UUID)
_TEST_UUID_STR_2 = str(_TEST_UUID_2)
_TEST_UUID_STR_SANS_HYPHENS = _TEST_UUID_STR.replace('-', '')


def _as_params(*values, prefix=''):
    # NOTE(caselit): each value must be a tuple/list even when using one
    #   single argument
    return [
        pytest.param(*value, id=f'{prefix}_{i}' if prefix else f'{i}')
        for i, value in enumerate(values, 1)
    ]


class IDResource:
    def __init__(self):
        self.id = None
        self.name = None
        self.called = False

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


class NameResource:
    def __init__(self):
        self.id = None
        self.name = None
        self.called = False

    def on_get(self, req, resp, id, name):
        self.id = id
        self.name = name
        self.called = True


class NameAndDigitResource:
    def __init__(self):
        self.id = None
        self.name51 = None
        self.called = False

    def on_get(self, req, resp, id, name51):
        self.id = id
        self.name51 = name51
        self.called = True


class FileResource:
    def __init__(self):
        self.file_id = None
        self.called = False

    def on_get(self, req, resp, file_id):
        self.file_id = file_id
        self.called = True


class FileDetailsResource:
    def __init__(self):
        self.file_id = None
        self.ext = None
        self.called = False

    def on_get(self, req, resp, file_id, ext):
        self.file_id = file_id
        self.ext = ext
        self.called = True


class KitchenSinkResource:
    def __init__(self):
        self.call_count = 0
        self.kwargs = None
        self.uri_template = None

    def on_get(self, req, resp, **kwargs):
        self.call_count += 1
        self.kwargs = kwargs
        self.uri_template = req.uri_template


class ResourceWithSuffixRoutes:
    def __init__(self):
        self.get_called = False
        self.post_called = False
        self.put_called = False

    def on_get(self, req, resp, collection_id, item_id):
        self.collection_id = collection_id
        self.item_id = item_id
        self.get_called = True

    def on_post(self, req, resp, collection_id, item_id):
        self.collection_id = collection_id
        self.item_id = item_id
        self.post_called = True

    def on_put(self, req, resp, collection_id, item_id):
        self.collection_id = collection_id
        self.item_id = item_id
        self.put_called = True

    def on_get_collection(self, req, resp, collection_id):
        self.collection_id = collection_id
        self.get_called = True

    def on_post_collection(self, req, resp, collection_id):
        self.collection_id = collection_id
        self.post_called = True

    def on_put_collection(self, req, resp, collection_id):
        self.collection_id = collection_id
        self.put_called = True


@pytest.fixture
def resource():
    return testing.SimpleTestResource()


@pytest.fixture
def client(asgi, util):
    return testing.TestClient(util.create_app(asgi))


def test_root_path(client, resource):
    client.app.add_route('/', resource)
    client.simulate_get('/')
    assert resource.called


def test_no_vars(client, resource):
    client.app.add_route('/hello/world', resource)
    client.simulate_get('/hello/world')
    assert resource.called


def test_special_chars(client, resource):
    client.app.add_route('/hello/world.json', resource)
    client.app.add_route('/hello(world)', resource)

    client.simulate_get('/hello/world_json')
    assert not resource.called

    client.simulate_get('/helloworld')
    assert not resource.called

    client.simulate_get('/hello/world.json')
    assert resource.called

    client.simulate_get('/hello(world)')
    assert resource.called


@pytest.mark.parametrize(
    'field_name',
    [
        'id',
        'id123',
        'widget_id',
    ],
)
def test_single(client, resource, field_name):
    template = '/widgets/{{{}}}'.format(field_name)

    client.app.add_route(template, resource)

    client.simulate_get('/widgets/123')
    assert resource.called
    assert resource.captured_kwargs[field_name] == '123'


def test_single_path_segment(client):
    id_resource = IDResource()

    client.app.add_route('/thing-{id}', id_resource)
    client.simulate_get('/thing-foo')
    assert id_resource.id == 'foo'


@pytest.mark.parametrize(
    'uri_template,',
    [
        '/{id:int}',
        '/{id:int(3)}',
        '/{id:int(min=123)}',
        '/{id:int(min=123, max=123)}',
    ],
)
def test_int_converter(client, uri_template):
    resource1 = IDResource()
    client.app.add_route(uri_template, resource1)

    result = client.simulate_get('/123')

    assert result.status_code == 200
    assert resource1.called
    assert resource1.id == 123
    assert resource1.req.path == '/123'


@pytest.mark.parametrize('id_value', [2, 2.1, 1.9])
@pytest.mark.parametrize(
    'uri_template,',
    [
        '/{id:float}',
        '/{id:float(1)}',
        '/{id:float(min=1.9)}',
        '/{id:float(min=1.8, max=3)}',
    ],
)
def test_float_converter(client, uri_template, id_value):
    resource1 = IDResource()
    client.app.add_route(uri_template, resource1)

    result = client.simulate_get('/{0}'.format(id_value))

    assert result.status_code == 200
    assert resource1.called
    assert resource1.id == id_value
    assert resource1.req.path == '/{0}'.format(id_value)


@pytest.mark.parametrize('value', ['nan', '-inf', 'inf'])
def test_float_converter_non_finite_allowed(value, client):
    resource1 = IDResource()
    client.app.add_route('/{id:float(finite=False)}', resource1)

    result = client.simulate_get('/' + value)

    assert result.status_code == 200
    assert resource1.called
    assert not math.isfinite(resource1.id)


def test_float_converter_non_finite_disallowed(client):
    resource1 = IDResource()
    client.app.add_route('/{id:float}', resource1)

    result = client.simulate_get('/NaN')
    assert result.status_code == 404
    assert not resource1.called


@pytest.mark.parametrize(
    'uri_template,',
    [
        '/{id:int(2)}',
        '/{id:int(min=124)}',
        '/{id:int(num_digits=3, max=100)}',
    ],
)
def test_int_converter_rejections(client, uri_template):
    resource1 = IDResource()
    client.app.add_route(uri_template, resource1)

    result = client.simulate_get('/123')

    assert result.status_code == 404
    assert not resource1.called


@pytest.mark.parametrize(
    'uri_template, path, dt_expected',
    [
        (
            '/{start_year:int}-to-{timestamp:dt}',
            '/1961-to-1969-07-21T02:56:00Z',
            datetime(1969, 7, 21, 2, 56, 0, tzinfo=timezone.utc),
        ),
        (
            '/{start_year:int}-to-{timestamp:dt("%Y-%m-%d")}',
            '/1961-to-1969-07-21',
            datetime(1969, 7, 21),
        ),
        (
            '/{start_year:int}/{timestamp:dt("%Y-%m-%d %H:%M")}',
            '/1961/1969-07-21 14:30',
            datetime(1969, 7, 21, 14, 30),
        ),
        ('/{start_year:int}-to-{timestamp:dt("%Y-%m")}', '/1961-to-1969-07-21', None),
    ],
)
def test_datetime_converter(client, resource, uri_template, path, dt_expected):
    client.app.add_route(uri_template, resource)

    result = client.simulate_get(path)

    if dt_expected is None:
        assert result.status_code == 404
        assert not resource.called
    else:
        assert result.status_code == 200
        assert resource.called
        assert resource.captured_kwargs['start_year'] == 1961
        assert resource.captured_kwargs['timestamp'] == dt_expected


@pytest.mark.parametrize(
    'uri_template, path, expected',
    _as_params(
        (
            '/widgets/{widget_id:uuid}',
            '/widgets/' + _TEST_UUID_STR,
            {'widget_id': _TEST_UUID},
        ),
        (
            '/widgets/{widget_id:uuid}/orders',
            '/widgets/' + _TEST_UUID_STR_SANS_HYPHENS + '/orders',
            {'widget_id': _TEST_UUID},
        ),
        (
            '/versions/diff/{left:uuid()}...{right:uuid()}',
            '/versions/diff/{}...{}'.format(_TEST_UUID_STR, _TEST_UUID_STR_2),
            {
                'left': _TEST_UUID,
                'right': _TEST_UUID_2,
            },
        ),
        (
            '/versions/diff/{left:uuid}...{right:uuid()}',
            '/versions/diff/{}...{}'.format(_TEST_UUID_STR, _TEST_UUID_STR_2),
            {
                'left': _TEST_UUID,
                'right': _TEST_UUID_2,
            },
        ),
        (
            '/versions/diff/{left:uuid()}...{right:uuid}',
            '/versions/diff/{}...{}'.format(_TEST_UUID_STR, _TEST_UUID_STR_2),
            {
                'left': _TEST_UUID,
                'right': _TEST_UUID_2,
            },
        ),
        (
            '/widgets/{widget_id:uuid}/orders',
            '/widgets/' + _TEST_UUID_STR_SANS_HYPHENS[:-1] + '/orders',
            None,
        ),
        prefix='uuid_converter',
    ),
)
def test_uuid_converter(client, resource, uri_template, path, expected):
    client.app.add_route(uri_template, resource)

    result = client.simulate_get(path)

    if expected is None:
        assert result.status_code == 404
        assert not resource.called
    else:
        assert result.status_code == 200
        assert resource.called
        assert resource.captured_kwargs == expected


def test_uuid_converter_complex_segment(client, resource):
    client.app.add_route('/pages/{first:uuid}...{last:uuid}', resource)

    first_uuid = uuid.uuid4()
    last_uuid = uuid.uuid4()

    result = client.simulate_get('/pages/{}...{}'.format(first_uuid, last_uuid))

    assert result.status_code == 200
    assert resource.called
    assert resource.captured_kwargs['first'] == first_uuid
    assert resource.captured_kwargs['last'] == last_uuid


@pytest.mark.parametrize(
    'uri_template, path, expected',
    [
        ('/{food:spam}', '/something', {'food': 'spam!'}),
        (
            '/{food:spam(")")}:{food_too:spam("()")}',
            '/bacon:eggs',
            {'food': 'spam!', 'food_too': 'spam!'},
        ),
        (
            '/({food:spam()}){food_too:spam("()")}',
            '/(bacon)eggs',
            {'food': 'spam!', 'food_too': 'spam!'},
        ),
    ],
)
def test_converter_custom(client, resource, uri_template, path, expected):
    class SpamConverter:
        def __init__(self, useless_text=None):
            pass

        def convert(self, fragment):
            return 'spam!'

    client.app.router_options.converters['spam'] = SpamConverter
    client.app.add_route(uri_template, resource)

    result = client.simulate_get(path)

    assert result.status_code == 200
    assert resource.called
    assert resource.captured_kwargs == expected


def test_single_trailing_slash(client):
    resource1 = IDResource()
    client.app.add_route('/1/{id}/', resource1)
    assert client.simulate_get('/1/123').status_code == 404
    result = client.simulate_get('/1/123/')
    assert result.status == falcon.HTTP_200
    assert resource1.called
    assert resource1.id == '123'
    assert resource1.req.path == '/1/123/'

    resource2 = IDResource()
    client.app.add_route('/2/{id}/', resource2)
    result = client.simulate_get('/2/123')
    assert result.status == falcon.HTTP_404
    assert not resource2.called
    assert resource2.id is None

    resource3 = IDResource()
    client.app.add_route('/3/{id}', resource3)
    client.app.req_options.strip_url_path_trailing_slash = True
    result = client.simulate_get('/3/123/')
    assert result.status == falcon.HTTP_200
    assert resource3.called
    assert resource3.id == '123'
    assert resource3.req.path == '/3/123'

    resource4 = IDResource()
    client.app.add_route('/4/{id}', resource4)
    client.app.req_options.strip_url_path_trailing_slash = False
    result = client.simulate_get('/4/123/')
    assert result.status == falcon.HTTP_404
    assert not resource4.called
    assert resource4.id is None


def test_multiple(client):
    resource = NameResource()
    client.app.add_route('/messages/{id}/names/{name}', resource)

    test_id = 'bfb54d43-219b-4336-a623-6172f920592e'
    test_name = '758e3922-dd6d-4007-a589-50fba0789365'
    path = '/messages/' + test_id + '/names/' + test_name
    client.simulate_get(path)

    assert resource.called
    assert resource.id == test_id
    assert resource.name == test_name


@pytest.mark.parametrize(
    'uri_template',
    [
        '//',
        '//begin',
        '/end//',
        '/in//side',
    ],
)
def test_empty_path_component(client, resource, uri_template):
    with pytest.raises(ValueError):
        client.app.add_route(uri_template, resource)


@pytest.mark.parametrize(
    'uri_template',
    [
        '',
        'no',
        'no/leading_slash',
    ],
)
def test_relative_path(client, resource, uri_template):
    with pytest.raises(ValueError):
        client.app.add_route(uri_template, resource)


@pytest.mark.parametrize('reverse', [True, False])
def test_same_level_complex_var(client, reverse):
    file_resource = FileResource()
    details_resource = FileDetailsResource()

    routes = [
        ('/files/{file_id}', file_resource),
        ('/files/{file_id}.{ext}', details_resource),
    ]
    if reverse:
        routes.reverse()

    for uri_template, resource in routes:
        client.app.add_route(uri_template, resource)

    file_id_1 = 'bc6b201d-b449-4290-a061-8eeb9f7b1450'
    file_id_2 = '33b7f34c-6ee6-40e6-89a3-742a69b59de0'
    ext = 'a4581b95-bc36-4c08-a3c2-23ba266abdf2'
    path_1 = '/files/' + file_id_1
    path_2 = '/files/' + file_id_2 + '.' + ext

    client.simulate_get(path_1)
    assert file_resource.called
    assert file_resource.file_id == file_id_1

    client.simulate_get(path_2)
    assert details_resource.called
    assert details_resource.file_id == file_id_2
    assert details_resource.ext == ext


def test_adding_suffix_routes(client):
    resource_with_suffix_routes = ResourceWithSuffixRoutes()
    client.app.add_route(
        '/collections/{collection_id}/items/{item_id}', resource_with_suffix_routes
    )
    client.app.add_route(
        '/collections/{collection_id}/items',
        resource_with_suffix_routes,
        suffix='collection',
    )
    # GET
    client.simulate_get('/collections/123/items/456')
    assert resource_with_suffix_routes.collection_id == '123'
    assert resource_with_suffix_routes.item_id == '456'
    assert resource_with_suffix_routes.get_called
    client.simulate_get('/collections/foo/items')
    assert resource_with_suffix_routes.collection_id == 'foo'
    # POST
    client.simulate_post('/collections/foo234/items/foo456')
    assert resource_with_suffix_routes.collection_id == 'foo234'
    assert resource_with_suffix_routes.item_id == 'foo456'
    assert resource_with_suffix_routes.post_called
    client.simulate_post('/collections/foo123/items')
    assert resource_with_suffix_routes.collection_id == 'foo123'
    # PUT
    client.simulate_put('/collections/foo345/items/foo567')
    assert resource_with_suffix_routes.collection_id == 'foo345'
    assert resource_with_suffix_routes.item_id == 'foo567'
    assert resource_with_suffix_routes.put_called
    client.simulate_put('/collections/foo321/items')
    assert resource_with_suffix_routes.collection_id == 'foo321'


@pytest.mark.parametrize('reverse', [True, False])
def test_with_and_without_trailing_slash(client, reverse):
    routes = [
        ('/kitchen', KitchenSinkResource()),
        ('/kitchen/', KitchenSinkResource()),
        ('/kitchen/{item}', KitchenSinkResource()),
        ('/kitchen/{item}/', KitchenSinkResource()),
        ('/kitchen/sink', KitchenSinkResource()),
        ('/kitchen/sink/', KitchenSinkResource()),
    ]
    if reverse:
        routes.reverse()

    for route in routes:
        client.app.add_route(*route)

    for uri_template, resource in routes:
        item = None
        if '{item}' in uri_template:
            item = 'kettle' if uri_template.endswith('/') else 'teapot'
        resp = client.simulate_get(uri_template.replace('{item}', item or ''))

        assert resp.status_code == 200
        assert resource.call_count == 1
        assert resource.kwargs.get('item') == item
        assert resource.uri_template == uri_template


def test_custom_error_on_suffix_route_not_found(client):
    resource_with_suffix_routes = ResourceWithSuffixRoutes()
    with pytest.raises(SuffixedMethodNotFoundError):
        client.app.add_route(
            '/collections/{collection_id}/items',
            resource_with_suffix_routes,
            suffix='bad-alt',
        )