File: test_redirects.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 (105 lines) | stat: -rw-r--r-- 3,324 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
import pytest

import falcon
import falcon.testing as testing


@pytest.fixture
def client(asgi, util):
    app = util.create_app(asgi)

    resource = RedirectingResource()
    app.add_route('/', resource)

    return testing.TestClient(app)


@pytest.fixture
def client_exercising_headers(asgi, util):
    app = util.create_app(asgi)

    resource = RedirectingResourceWithHeaders()
    app.add_route('/', resource)

    return testing.TestClient(app)


class RedirectingResource:
    # NOTE(kgriffs): You wouldn't necessarily use these types of
    # http methods with these types of redirects; this is only
    # done to simplify testing.

    def on_get(self, req, resp):
        raise falcon.HTTPMovedPermanently('/moved/perm')

    def on_post(self, req, resp):
        raise falcon.HTTPFound('/found')

    def on_put(self, req, resp):
        raise falcon.HTTPSeeOther('/see/other')

    def on_delete(self, req, resp):
        raise falcon.HTTPTemporaryRedirect('/tmp/redirect')

    def on_head(self, req, resp):
        raise falcon.HTTPPermanentRedirect('/perm/redirect')


class RedirectingResourceWithHeaders:
    # NOTE(kgriffs): You wouldn't necessarily use these types of
    # http methods with these types of redirects; this is only
    # done to simplify testing.

    def on_get(self, req, resp):
        raise falcon.HTTPMovedPermanently('/moved/perm', headers={'foo': 'bar'})

    def on_post(self, req, resp):
        raise falcon.HTTPFound('/found', headers={'foo': 'bar'})

    def on_put(self, req, resp):
        raise falcon.HTTPSeeOther('/see/other', headers={'foo': 'bar'})

    def on_delete(self, req, resp):
        raise falcon.HTTPTemporaryRedirect('/tmp/redirect', headers={'foo': 'bar'})

    def on_head(self, req, resp):
        raise falcon.HTTPPermanentRedirect('/perm/redirect', headers={'foo': 'bar'})


class TestRedirects:
    @pytest.mark.parametrize(
        'method,expected_status,expected_location',
        [
            ('GET', falcon.HTTP_301, '/moved/perm'),
            ('POST', falcon.HTTP_302, '/found'),
            ('PUT', falcon.HTTP_303, '/see/other'),
            ('DELETE', falcon.HTTP_307, '/tmp/redirect'),
            ('HEAD', falcon.HTTP_308, '/perm/redirect'),
        ],
    )
    def test_redirect(self, client, method, expected_status, expected_location):
        result = client.simulate_request(path='/', method=method)

        assert not result.content
        assert result.status == expected_status
        assert result.headers['location'] == expected_location

    @pytest.mark.parametrize(
        'method,expected_status,expected_location',
        [
            ('GET', falcon.HTTP_301, '/moved/perm'),
            ('POST', falcon.HTTP_302, '/found'),
            ('PUT', falcon.HTTP_303, '/see/other'),
            ('DELETE', falcon.HTTP_307, '/tmp/redirect'),
            ('HEAD', falcon.HTTP_308, '/perm/redirect'),
        ],
    )
    def test_redirect_with_headers(
        self, client_exercising_headers, method, expected_status, expected_location
    ):
        result = client_exercising_headers.simulate_request(path='/', method=method)

        assert not result.content
        assert result.status == expected_status
        assert result.headers['location'] == expected_location
        assert result.headers['foo'] == 'bar'