xref: /unit/test/test_njs.py (revision 2330:4b1f175f9c88)
1import os
2
3from unit.applications.proto import TestApplicationProto
4from unit.option import option
5from unit.utils import waitforfiles
6
7
8class TestNJS(TestApplicationProto):
9    prerequisites = {'modules': {'njs': 'any'}}
10
11    def setup_method(self):
12        assert 'success' in self.conf(
13            {
14                "listeners": {"*:7080": {"pass": "routes"}},
15                "routes": [
16                    {"action": {"share": f"{option.temp_dir}/assets$uri"}}
17                ],
18            }
19        )
20
21    def create_files(self, *files):
22        assets_dir = f'{option.temp_dir}/assets/'
23        os.makedirs(assets_dir)
24
25        [open(assets_dir + f, 'a') for f in files]
26        waitforfiles(*[assets_dir + f for f in files])
27
28    def set_share(self, share):
29        assert 'success' in self.conf(share, 'routes/0/action/share')
30
31    def check_expression(self, expression, url='/'):
32        self.set_share(f'"`{option.temp_dir}/assets{expression}`"')
33        assert self.get(url=url)['status'] == 200
34
35    def test_njs_template_string(self, temp_dir):
36        self.create_files('str', '`string`', '`backtick', 'l1\nl2')
37
38        self.check_expression('/str')
39        self.check_expression('/\\\\`backtick')
40        self.check_expression('/l1\\nl2')
41
42        self.set_share(f'"{temp_dir}/assets/`string`"')
43        assert self.get()['status'] == 200
44
45    def test_njs_template_expression(self, temp_dir):
46        self.create_files('str', 'localhost')
47
48        self.check_expression('${uri}', '/str')
49        self.check_expression('${uri}${host}')
50        self.check_expression('${uri + host}')
51        self.check_expression('${uri + `${host}`}')
52
53    def test_njs_iteration(self, temp_dir):
54        self.create_files('Connection,Host', 'close,localhost')
55
56        self.check_expression('/${Object.keys(headers).sort().join()}')
57        self.check_expression('/${Object.values(headers).sort().join()}')
58
59    def test_njs_variables(self, temp_dir):
60        self.create_files('str', 'localhost', '127.0.0.1')
61
62        self.check_expression('/${host}')
63        self.check_expression('/${remoteAddr}')
64        self.check_expression('/${headers.Host}')
65
66        self.set_share(f'"`{temp_dir}/assets/${{cookies.foo}}`"')
67        assert (
68            self.get(headers={'Cookie': 'foo=str', 'Connection': 'close'})[
69                'status'
70            ]
71            == 200
72        ), 'cookies'
73
74        self.set_share(f'"`{temp_dir}/assets/${{args.foo}}`"')
75        assert self.get(url='/?foo=str')['status'] == 200, 'args'
76
77    def test_njs_invalid(self, temp_dir, skip_alert):
78        skip_alert(r'js exception:')
79
80        def check_invalid(template):
81            assert 'error' in self.conf(template, 'routes/0/action/share')
82
83        check_invalid('"`a"')
84        check_invalid('"`a``"')
85        check_invalid('"`a`/"')
86
87        def check_invalid_resolve(template):
88            assert 'success' in self.conf(template, 'routes/0/action/share')
89            assert self.get()['status'] == 500
90
91        check_invalid_resolve('"`${a}`"')
92        check_invalid_resolve('"`${uri.a.a}`"')
93