summaryrefslogtreecommitdiffhomepage
path: root/test/test_variables.py
blob: bbb8f76920b5acca483d26a1dcdac506b3ff144b (plain) (blame)
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
from unit.applications.proto import TestApplicationProto


class TestVariables(TestApplicationProto):
    prerequisites = {}

    def setup_method(self):
        assert 'success' in self.conf(
            {
                "listeners": {"*:7080": {"pass": "routes/$method"}},
                "routes": {
                    "GET": [{"action": {"return": 201}}],
                    "POST": [{"action": {"return": 202}}],
                    "3": [{"action": {"return": 203}}],
                    "4*": [{"action": {"return": 204}}],
                    "blahGET}": [{"action": {"return": 205}}],
                    "5GET": [{"action": {"return": 206}}],
                    "GETGET": [{"action": {"return": 207}}],
                    "localhost": [{"action": {"return": 208}}],
                },
            },
        ), 'configure routes'

    def conf_routes(self, routes):
        assert 'success' in self.conf(routes, 'listeners/*:7080/pass')

    def test_variables_method(self):
        assert self.get()['status'] == 201, 'method GET'
        assert self.post()['status'] == 202, 'method POST'

    def test_variables_uri(self):
        self.conf_routes("\"routes$uri\"")

        assert self.get(url='/3')['status'] == 203, 'uri'
        assert self.get(url='/4*')['status'] == 204, 'uri 2'
        assert self.get(url='/4%2A')['status'] == 204, 'uri 3'

    def test_variables_host(self):
        self.conf_routes("\"routes/$host\"")

        def check_host(host, status=208):
            assert (
                self.get(headers={'Host': host, 'Connection': 'close'})[
                    'status'
                ]
                == status
            )

        check_host('localhost')
        check_host('localhost.')
        check_host('localhost:7080')
        check_host('.localhost', 404)
        check_host('www.localhost', 404)
        check_host('localhost1', 404)

    def test_variables_many(self):
        self.conf_routes("\"routes$uri$method\"")
        assert self.get(url='/5')['status'] == 206, 'many'

        self.conf_routes("\"routes${uri}${method}\"")
        assert self.get(url='/5')['status'] == 206, 'many 2'

        self.conf_routes("\"routes${uri}$method\"")
        assert self.get(url='/5')['status'] == 206, 'many 3'

        self.conf_routes("\"routes/$method$method\"")
        assert self.get()['status'] == 207, 'many 4'

        self.conf_routes("\"routes/$method$uri\"")
        assert self.get()['status'] == 404, 'no route'
        assert self.get(url='/blah')['status'] == 404, 'no route 2'

    def test_variables_replace(self):
        assert self.get()['status'] == 201

        self.conf_routes("\"routes$uri\"")
        assert self.get(url='/3')['status'] == 203

        self.conf_routes("\"routes/${method}\"")
        assert self.post()['status'] == 202

        self.conf_routes("\"routes${uri}\"")
        assert self.get(url='/4*')['status'] == 204

        self.conf_routes("\"routes/blah$method}\"")
        assert self.get()['status'] == 205

    def test_variables_upstream(self):
        assert 'success' in self.conf(
            {
                "listeners": {
                    "*:7080": {"pass": "upstreams$uri"},
                    "*:7081": {"pass": "routes/one"},
                },
                "upstreams": {
                    "1": {
                        "servers": {
                            "127.0.0.1:7081": {},
                        },
                    },
                },
                "routes": {
                    "one": [{"action": {"return": 200}}],
                },
            },
        ), 'upstreams initial configuration'

        assert self.get(url='/1')['status'] == 200
        assert self.get(url='/2')['status'] == 404

    def test_variables_invalid(self):
        def check_variables(routes):
            assert 'error' in self.conf(
                routes, 'listeners/*:7080/pass'
            ), 'invalid variables'

        check_variables("\"routes$\"")
        check_variables("\"routes${\"")
        check_variables("\"routes${}\"")
        check_variables("\"routes$ur\"")
        check_variables("\"routes$uriblah\"")
        check_variables("\"routes${uri\"")
        check_variables("\"routes${{uri}\"")