xref: /unit/test/test_static_types.py (revision 2066:242192963d93)
11902Szelenkov@nginx.comfrom pathlib import Path
21902Szelenkov@nginx.com
31902Szelenkov@nginx.comimport pytest
41902Szelenkov@nginx.comfrom unit.applications.proto import TestApplicationProto
51902Szelenkov@nginx.com
61902Szelenkov@nginx.com
71902Szelenkov@nginx.comclass TestStaticTypes(TestApplicationProto):
81902Szelenkov@nginx.com    prerequisites = {}
91902Szelenkov@nginx.com
101902Szelenkov@nginx.com    @pytest.fixture(autouse=True)
111902Szelenkov@nginx.com    def setup_method_fixture(self, temp_dir):
121902Szelenkov@nginx.com        Path(temp_dir + '/assets').mkdir()
131902Szelenkov@nginx.com        for ext in ['.xml', '.mp4', '.php', '', '.txt', '.html', '.png']:
141902Szelenkov@nginx.com            Path(temp_dir + '/assets/file' + ext).write_text(ext)
151902Szelenkov@nginx.com
161902Szelenkov@nginx.com        Path(temp_dir + '/assets/index.html').write_text('index')
171902Szelenkov@nginx.com
181902Szelenkov@nginx.com        self._load_conf(
191902Szelenkov@nginx.com            {
201902Szelenkov@nginx.com                "listeners": {
211902Szelenkov@nginx.com                    "*:7080": {"pass": "routes"},
221902Szelenkov@nginx.com                    "*:7081": {"pass": "routes"},
231902Szelenkov@nginx.com                },
241960Sz.hong@f5.com                "routes": [{"action": {"share": temp_dir + "/assets$uri"}}],
251902Szelenkov@nginx.com                "applications": {},
261902Szelenkov@nginx.com            }
271902Szelenkov@nginx.com        )
281902Szelenkov@nginx.com
291902Szelenkov@nginx.com    def action_update(self, conf):
301902Szelenkov@nginx.com        assert 'success' in self.conf(conf, 'routes/0/action')
311902Szelenkov@nginx.com
321902Szelenkov@nginx.com    def check_body(self, http_url, body):
331902Szelenkov@nginx.com        resp = self.get(url=http_url)
341902Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
351902Szelenkov@nginx.com        assert resp['body'] == body, 'body'
361902Szelenkov@nginx.com
371902Szelenkov@nginx.com    def test_static_types_basic(self, temp_dir):
381960Sz.hong@f5.com        self.action_update({"share": temp_dir + "/assets$uri"})
391902Szelenkov@nginx.com        self.check_body('/index.html', 'index')
401902Szelenkov@nginx.com        self.check_body('/file.xml', '.xml')
411902Szelenkov@nginx.com
421902Szelenkov@nginx.com        self.action_update(
431960Sz.hong@f5.com            {"share": temp_dir + "/assets$uri", "types": "application/xml"}
441902Szelenkov@nginx.com        )
451902Szelenkov@nginx.com        self.check_body('/file.xml', '.xml')
461902Szelenkov@nginx.com
471902Szelenkov@nginx.com        self.action_update(
481960Sz.hong@f5.com            {"share": temp_dir + "/assets$uri", "types": ["application/xml"]}
491902Szelenkov@nginx.com        )
501902Szelenkov@nginx.com        self.check_body('/file.xml', '.xml')
511902Szelenkov@nginx.com
521960Sz.hong@f5.com        self.action_update({"share": temp_dir + "/assets$uri", "types": [""]})
531902Szelenkov@nginx.com        assert self.get(url='/file.xml')['status'] == 403, 'no mtype'
541902Szelenkov@nginx.com
551902Szelenkov@nginx.com    def test_static_types_wildcard(self, temp_dir):
561902Szelenkov@nginx.com        self.action_update(
571960Sz.hong@f5.com            {"share": temp_dir + "/assets$uri", "types": ["application/*"]}
581902Szelenkov@nginx.com        )
591902Szelenkov@nginx.com        self.check_body('/file.xml', '.xml')
601902Szelenkov@nginx.com        assert self.get(url='/file.mp4')['status'] == 403, 'app * mtype mp4'
611902Szelenkov@nginx.com
621902Szelenkov@nginx.com        self.action_update(
631960Sz.hong@f5.com            {"share": temp_dir + "/assets$uri", "types": ["video/*"]}
641902Szelenkov@nginx.com        )
651902Szelenkov@nginx.com        assert self.get(url='/file.xml')['status'] == 403, 'video * mtype xml'
661902Szelenkov@nginx.com        self.check_body('/file.mp4', '.mp4')
671902Szelenkov@nginx.com
681902Szelenkov@nginx.com    def test_static_types_negation(self, temp_dir):
691902Szelenkov@nginx.com        self.action_update(
701960Sz.hong@f5.com            {"share": temp_dir + "/assets$uri", "types": ["!application/xml"]}
711902Szelenkov@nginx.com        )
721902Szelenkov@nginx.com        assert self.get(url='/file.xml')['status'] == 403, 'forbidden negation'
731902Szelenkov@nginx.com        self.check_body('/file.mp4', '.mp4')
741902Szelenkov@nginx.com
751902Szelenkov@nginx.com        # sorting negation
761902Szelenkov@nginx.com        self.action_update(
771902Szelenkov@nginx.com            {
781960Sz.hong@f5.com                "share": temp_dir + "/assets$uri",
791902Szelenkov@nginx.com                "types": ["!video/*", "image/png", "!image/jpg"],
801902Szelenkov@nginx.com            }
811902Szelenkov@nginx.com        )
821902Szelenkov@nginx.com        assert self.get(url='/file.mp4')['status'] == 403, 'negation sort mp4'
831902Szelenkov@nginx.com        self.check_body('/file.png', '.png')
841902Szelenkov@nginx.com        assert self.get(url='/file.jpg')['status'] == 403, 'negation sort jpg'
851902Szelenkov@nginx.com
861902Szelenkov@nginx.com    def test_static_types_regex(self, temp_dir):
871902Szelenkov@nginx.com        self.action_update(
88*2066Szelenkov@nginx.com            {
89*2066Szelenkov@nginx.com                "share": temp_dir + "/assets$uri",
90*2066Szelenkov@nginx.com                "types": ["~text/(html|plain)"],
91*2066Szelenkov@nginx.com            }
921902Szelenkov@nginx.com        )
931902Szelenkov@nginx.com        assert self.get(url='/file.php')['status'] == 403, 'regex fail'
941902Szelenkov@nginx.com        self.check_body('/file.html', '.html')
951902Szelenkov@nginx.com        self.check_body('/file.txt', '.txt')
961902Szelenkov@nginx.com
971902Szelenkov@nginx.com    def test_static_types_case(self, temp_dir):
981902Szelenkov@nginx.com        self.action_update(
991960Sz.hong@f5.com            {"share": temp_dir + "/assets$uri", "types": ["!APpliCaTiOn/xMl"]}
1001902Szelenkov@nginx.com        )
1011902Szelenkov@nginx.com        self.check_body('/file.mp4', '.mp4')
1021902Szelenkov@nginx.com        assert (
1031902Szelenkov@nginx.com            self.get(url='/file.xml')['status'] == 403
1041902Szelenkov@nginx.com        ), 'mixed case xml negation'
1051902Szelenkov@nginx.com
1061902Szelenkov@nginx.com        self.action_update(
1071960Sz.hong@f5.com            {"share": temp_dir + "/assets$uri", "types": ["vIdEo/mp4"]}
1081902Szelenkov@nginx.com        )
1091902Szelenkov@nginx.com        assert self.get(url='/file.mp4')['status'] == 200, 'mixed case'
1101902Szelenkov@nginx.com        assert (
1111902Szelenkov@nginx.com            self.get(url='/file.xml')['status'] == 403
1121902Szelenkov@nginx.com        ), 'mixed case video negation'
1131902Szelenkov@nginx.com
1141902Szelenkov@nginx.com        self.action_update(
1151960Sz.hong@f5.com            {"share": temp_dir + "/assets$uri", "types": ["vIdEo/*"]}
1161902Szelenkov@nginx.com        )
1171902Szelenkov@nginx.com        self.check_body('/file.mp4', '.mp4')
1181902Szelenkov@nginx.com        assert (
1191902Szelenkov@nginx.com            self.get(url='/file.xml')['status'] == 403
1201902Szelenkov@nginx.com        ), 'mixed case video * negation'
1211902Szelenkov@nginx.com
1221902Szelenkov@nginx.com    def test_static_types_fallback(self, temp_dir):
1231902Szelenkov@nginx.com        assert 'success' in self.conf(
1241902Szelenkov@nginx.com            [
1251902Szelenkov@nginx.com                {
1261902Szelenkov@nginx.com                    "match": {"destination": "*:7081"},
1271902Szelenkov@nginx.com                    "action": {"return": 200},
1281902Szelenkov@nginx.com                },
1291902Szelenkov@nginx.com                {
1301902Szelenkov@nginx.com                    "action": {
1311960Sz.hong@f5.com                        "share": temp_dir + "/assets$uri",
1321902Szelenkov@nginx.com                        "types": ["!application/x-httpd-php"],
1331902Szelenkov@nginx.com                        "fallback": {"proxy": "http://127.0.0.1:7081"},
1341902Szelenkov@nginx.com                    }
1351902Szelenkov@nginx.com                },
1361902Szelenkov@nginx.com            ],
1371902Szelenkov@nginx.com            'routes',
1381902Szelenkov@nginx.com        ), 'configure fallback proxy route'
1391902Szelenkov@nginx.com
1401902Szelenkov@nginx.com        self.check_body('/file.php', '')
1411902Szelenkov@nginx.com        self.check_body('/file.mp4', '.mp4')
1421902Szelenkov@nginx.com
1431902Szelenkov@nginx.com    def test_static_types_index(self, temp_dir):
1441902Szelenkov@nginx.com        self.action_update(
1451960Sz.hong@f5.com            {"share": temp_dir + "/assets$uri", "types": "application/xml"}
1461902Szelenkov@nginx.com        )
1471902Szelenkov@nginx.com        self.check_body('/', 'index')
1481902Szelenkov@nginx.com        self.check_body('/file.xml', '.xml')
1491960Sz.hong@f5.com        assert self.get(url='/index.html')['status'] == 403, 'forbidden mtype'
1501902Szelenkov@nginx.com        assert self.get(url='/file.mp4')['status'] == 403, 'forbidden mtype'
1511902Szelenkov@nginx.com
1521902Szelenkov@nginx.com    def test_static_types_custom_mime(self, temp_dir):
1531902Szelenkov@nginx.com        self._load_conf(
1541902Szelenkov@nginx.com            {
1551902Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "routes"}},
1561960Sz.hong@f5.com                "routes": [{"action": {"share": temp_dir + "/assets$uri"}}],
1571902Szelenkov@nginx.com                "applications": {},
1581902Szelenkov@nginx.com                "settings": {
1591902Szelenkov@nginx.com                    "http": {
1601902Szelenkov@nginx.com                        "static": {"mime_types": {"test/mime-type": ["file"]}}
1611902Szelenkov@nginx.com                    }
1621902Szelenkov@nginx.com                },
1631902Szelenkov@nginx.com            }
1641902Szelenkov@nginx.com        )
1651902Szelenkov@nginx.com
1661960Sz.hong@f5.com        self.action_update({"share": temp_dir + "/assets$uri", "types": [""]})
1671902Szelenkov@nginx.com        assert self.get(url='/file')['status'] == 403, 'forbidden custom mime'
1681902Szelenkov@nginx.com
1691902Szelenkov@nginx.com        self.action_update(
1701960Sz.hong@f5.com            {"share": temp_dir + "/assets$uri", "types": ["test/mime-type"]}
1711902Szelenkov@nginx.com        )
1721902Szelenkov@nginx.com        self.check_body('/file', '')
173