xref: /unit/test/test_static_types.py (revision 2066:242192963d93)
1from pathlib import Path
2
3import pytest
4from unit.applications.proto import TestApplicationProto
5
6
7class TestStaticTypes(TestApplicationProto):
8    prerequisites = {}
9
10    @pytest.fixture(autouse=True)
11    def setup_method_fixture(self, temp_dir):
12        Path(temp_dir + '/assets').mkdir()
13        for ext in ['.xml', '.mp4', '.php', '', '.txt', '.html', '.png']:
14            Path(temp_dir + '/assets/file' + ext).write_text(ext)
15
16        Path(temp_dir + '/assets/index.html').write_text('index')
17
18        self._load_conf(
19            {
20                "listeners": {
21                    "*:7080": {"pass": "routes"},
22                    "*:7081": {"pass": "routes"},
23                },
24                "routes": [{"action": {"share": temp_dir + "/assets$uri"}}],
25                "applications": {},
26            }
27        )
28
29    def action_update(self, conf):
30        assert 'success' in self.conf(conf, 'routes/0/action')
31
32    def check_body(self, http_url, body):
33        resp = self.get(url=http_url)
34        assert resp['status'] == 200, 'status'
35        assert resp['body'] == body, 'body'
36
37    def test_static_types_basic(self, temp_dir):
38        self.action_update({"share": temp_dir + "/assets$uri"})
39        self.check_body('/index.html', 'index')
40        self.check_body('/file.xml', '.xml')
41
42        self.action_update(
43            {"share": temp_dir + "/assets$uri", "types": "application/xml"}
44        )
45        self.check_body('/file.xml', '.xml')
46
47        self.action_update(
48            {"share": temp_dir + "/assets$uri", "types": ["application/xml"]}
49        )
50        self.check_body('/file.xml', '.xml')
51
52        self.action_update({"share": temp_dir + "/assets$uri", "types": [""]})
53        assert self.get(url='/file.xml')['status'] == 403, 'no mtype'
54
55    def test_static_types_wildcard(self, temp_dir):
56        self.action_update(
57            {"share": temp_dir + "/assets$uri", "types": ["application/*"]}
58        )
59        self.check_body('/file.xml', '.xml')
60        assert self.get(url='/file.mp4')['status'] == 403, 'app * mtype mp4'
61
62        self.action_update(
63            {"share": temp_dir + "/assets$uri", "types": ["video/*"]}
64        )
65        assert self.get(url='/file.xml')['status'] == 403, 'video * mtype xml'
66        self.check_body('/file.mp4', '.mp4')
67
68    def test_static_types_negation(self, temp_dir):
69        self.action_update(
70            {"share": temp_dir + "/assets$uri", "types": ["!application/xml"]}
71        )
72        assert self.get(url='/file.xml')['status'] == 403, 'forbidden negation'
73        self.check_body('/file.mp4', '.mp4')
74
75        # sorting negation
76        self.action_update(
77            {
78                "share": temp_dir + "/assets$uri",
79                "types": ["!video/*", "image/png", "!image/jpg"],
80            }
81        )
82        assert self.get(url='/file.mp4')['status'] == 403, 'negation sort mp4'
83        self.check_body('/file.png', '.png')
84        assert self.get(url='/file.jpg')['status'] == 403, 'negation sort jpg'
85
86    def test_static_types_regex(self, temp_dir):
87        self.action_update(
88            {
89                "share": temp_dir + "/assets$uri",
90                "types": ["~text/(html|plain)"],
91            }
92        )
93        assert self.get(url='/file.php')['status'] == 403, 'regex fail'
94        self.check_body('/file.html', '.html')
95        self.check_body('/file.txt', '.txt')
96
97    def test_static_types_case(self, temp_dir):
98        self.action_update(
99            {"share": temp_dir + "/assets$uri", "types": ["!APpliCaTiOn/xMl"]}
100        )
101        self.check_body('/file.mp4', '.mp4')
102        assert (
103            self.get(url='/file.xml')['status'] == 403
104        ), 'mixed case xml negation'
105
106        self.action_update(
107            {"share": temp_dir + "/assets$uri", "types": ["vIdEo/mp4"]}
108        )
109        assert self.get(url='/file.mp4')['status'] == 200, 'mixed case'
110        assert (
111            self.get(url='/file.xml')['status'] == 403
112        ), 'mixed case video negation'
113
114        self.action_update(
115            {"share": temp_dir + "/assets$uri", "types": ["vIdEo/*"]}
116        )
117        self.check_body('/file.mp4', '.mp4')
118        assert (
119            self.get(url='/file.xml')['status'] == 403
120        ), 'mixed case video * negation'
121
122    def test_static_types_fallback(self, temp_dir):
123        assert 'success' in self.conf(
124            [
125                {
126                    "match": {"destination": "*:7081"},
127                    "action": {"return": 200},
128                },
129                {
130                    "action": {
131                        "share": temp_dir + "/assets$uri",
132                        "types": ["!application/x-httpd-php"],
133                        "fallback": {"proxy": "http://127.0.0.1:7081"},
134                    }
135                },
136            ],
137            'routes',
138        ), 'configure fallback proxy route'
139
140        self.check_body('/file.php', '')
141        self.check_body('/file.mp4', '.mp4')
142
143    def test_static_types_index(self, temp_dir):
144        self.action_update(
145            {"share": temp_dir + "/assets$uri", "types": "application/xml"}
146        )
147        self.check_body('/', 'index')
148        self.check_body('/file.xml', '.xml')
149        assert self.get(url='/index.html')['status'] == 403, 'forbidden mtype'
150        assert self.get(url='/file.mp4')['status'] == 403, 'forbidden mtype'
151
152    def test_static_types_custom_mime(self, temp_dir):
153        self._load_conf(
154            {
155                "listeners": {"*:7080": {"pass": "routes"}},
156                "routes": [{"action": {"share": temp_dir + "/assets$uri"}}],
157                "applications": {},
158                "settings": {
159                    "http": {
160                        "static": {"mime_types": {"test/mime-type": ["file"]}}
161                    }
162                },
163            }
164        )
165
166        self.action_update({"share": temp_dir + "/assets$uri", "types": [""]})
167        assert self.get(url='/file')['status'] == 403, 'forbidden custom mime'
168
169        self.action_update(
170            {"share": temp_dir + "/assets$uri", "types": ["test/mime-type"]}
171        )
172        self.check_body('/file', '')
173