xref: /unit/test/test_static_variables.py (revision 1971:3410f9d2a662)
11965Szelenkov@nginx.comimport os
21965Szelenkov@nginx.comfrom pathlib import Path
31965Szelenkov@nginx.com
41965Szelenkov@nginx.comimport pytest
51965Szelenkov@nginx.comfrom unit.applications.proto import TestApplicationProto
61965Szelenkov@nginx.com
71965Szelenkov@nginx.com
81965Szelenkov@nginx.comclass TestStaticVariables(TestApplicationProto):
91965Szelenkov@nginx.com    prerequisites = {}
101965Szelenkov@nginx.com
111965Szelenkov@nginx.com    @pytest.fixture(autouse=True)
121965Szelenkov@nginx.com    def setup_method_fixture(self, temp_dir):
131965Szelenkov@nginx.com        os.makedirs(temp_dir + '/assets/dir')
141965Szelenkov@nginx.com        os.makedirs(temp_dir + '/assets/d$r')
151965Szelenkov@nginx.com        Path(temp_dir + '/assets/index.html').write_text('0123456789')
161965Szelenkov@nginx.com        Path(temp_dir + '/assets/dir/file').write_text('file')
171965Szelenkov@nginx.com        Path(temp_dir + '/assets/d$r/file').write_text('d$r')
181965Szelenkov@nginx.com
191965Szelenkov@nginx.com        self._load_conf(
201965Szelenkov@nginx.com            {
211965Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "routes"}},
221965Szelenkov@nginx.com                "routes": [{"action": {"share": temp_dir + "/assets$uri"}}],
231965Szelenkov@nginx.com            }
241965Szelenkov@nginx.com        )
251965Szelenkov@nginx.com
261965Szelenkov@nginx.com    def update_share(self, share):
271965Szelenkov@nginx.com        if isinstance(share, list):
281965Szelenkov@nginx.com            return self.conf(share, 'routes/0/action/share')
291965Szelenkov@nginx.com
301965Szelenkov@nginx.com        return self.conf('"' + share + '"', 'routes/0/action/share')
311965Szelenkov@nginx.com
32*1971Szelenkov@nginx.com    def test_static_variables(self, temp_dir):
331965Szelenkov@nginx.com        assert self.get(url='/index.html')['status'] == 200
341965Szelenkov@nginx.com        assert self.get(url='/d$r/file')['status'] == 200
351965Szelenkov@nginx.com
361965Szelenkov@nginx.com        assert 'success' in self.update_share('$uri')
371965Szelenkov@nginx.com        assert self.get(url=temp_dir + '/assets/index.html')['status'] == 200
381965Szelenkov@nginx.com
391965Szelenkov@nginx.com        assert 'success' in self.update_share(temp_dir + '/assets${uri}')
401965Szelenkov@nginx.com        assert self.get(url='/index.html')['status'] == 200
411965Szelenkov@nginx.com
42*1971Szelenkov@nginx.com    def test_static_variables_array(self, temp_dir):
431965Szelenkov@nginx.com        assert 'success' in self.update_share(
441965Szelenkov@nginx.com            [temp_dir + '/assets$uri', '$uri']
451965Szelenkov@nginx.com        )
461965Szelenkov@nginx.com
471965Szelenkov@nginx.com        assert self.get(url='/dir/file')['status'] == 200
481965Szelenkov@nginx.com        assert self.get(url=temp_dir + '/assets/index.html')['status'] == 200
491965Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 404
501965Szelenkov@nginx.com
511965Szelenkov@nginx.com        assert 'success' in self.conf(
521965Szelenkov@nginx.com            {
531965Szelenkov@nginx.com                "share": [temp_dir + '/assets$uri', '$uri'],
541965Szelenkov@nginx.com                "fallback": {"return": 201},
551965Szelenkov@nginx.com            },
561965Szelenkov@nginx.com            'routes/0/action',
571965Szelenkov@nginx.com        )
581965Szelenkov@nginx.com
591965Szelenkov@nginx.com        assert self.get(url='/dir/file')['status'] == 200
601965Szelenkov@nginx.com        assert self.get(url=temp_dir + '/assets/index.html')['status'] == 200
611965Szelenkov@nginx.com        assert self.get(url='/dir/blah')['status'] == 201
621965Szelenkov@nginx.com
63*1971Szelenkov@nginx.com    def test_static_variables_buildin_start(self, temp_dir):
641965Szelenkov@nginx.com        assert 'success' in self.update_share('$uri/assets/index.html')
651965Szelenkov@nginx.com        assert self.get(url=temp_dir)['status'] == 200
661965Szelenkov@nginx.com
67*1971Szelenkov@nginx.com    def test_static_variables_buildin_mid(self, temp_dir):
681965Szelenkov@nginx.com        assert 'success' in self.update_share(temp_dir + '$uri/index.html')
691965Szelenkov@nginx.com        assert self.get(url='/assets')['status'] == 200
701965Szelenkov@nginx.com
71*1971Szelenkov@nginx.com    def test_static_variables_buildin_end(self):
721965Szelenkov@nginx.com        assert self.get(url='/index.html')['status'] == 200
731965Szelenkov@nginx.com
74*1971Szelenkov@nginx.com    def test_static_variables_invalid(self, temp_dir):
751965Szelenkov@nginx.com        assert 'error' in self.update_share(temp_dir + '/assets/d$r$uri')
761965Szelenkov@nginx.com        assert 'error' in self.update_share(temp_dir + '/assets/$$uri')
771965Szelenkov@nginx.com        assert 'error' in self.update_share(
781965Szelenkov@nginx.com            [temp_dir + '/assets$uri', temp_dir + '/assets/dir', '$$uri']
791965Szelenkov@nginx.com        )
80