summaryrefslogtreecommitdiffhomepage
path: root/test/test_njs.py
blob: 86a8a4ba44922bbd6b283f6ec4891669ce6d40a8 (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
import os

import pytest
from unit.applications.proto import TestApplicationProto
from unit.option import option
from unit.utils import waitforfiles


class TestNJS(TestApplicationProto):
    prerequisites = {'modules': {'njs': 'any'}}

    @pytest.fixture(autouse=True)
    def setup_method_fixture(self, temp_dir):
        assert 'success' in self.conf(
            {
                "listeners": {"*:7080": {"pass": "routes"}},
                "routes": [
                    {"action": {"share": f"{temp_dir}/assets$uri"}}
                ],
            }
        )

    def create_files(self, *files):
        assets_dir = f'{option.temp_dir}/assets/'
        os.makedirs(assets_dir)

        [open(assets_dir + f, 'a') for f in files]
        waitforfiles(*[assets_dir + f for f in files])

    def set_share(self, share):
        assert 'success' in self.conf(share, 'routes/0/action/share')

    def check_expression(self, expression, url='/'):
        self.set_share(f'"`{option.temp_dir}/assets{expression}`"')
        assert self.get(url=url)['status'] == 200

    def test_njs_template_string(self, temp_dir):
        self.create_files('str', '`string`', '`backtick', 'l1\nl2')

        self.check_expression('/str')
        self.check_expression('/\\\\`backtick')
        self.check_expression('/l1\\nl2')

        self.set_share(f'"{temp_dir}/assets/`string`"')
        assert self.get()['status'] == 200

    def test_njs_template_expression(self):
        self.create_files('str', 'localhost')

        self.check_expression('${uri}', '/str')
        self.check_expression('${uri}${host}')
        self.check_expression('${uri + host}')
        self.check_expression('${uri + `${host}`}')

    def test_njs_iteration(self):
        self.create_files('Connection,Host', 'close,localhost')

        self.check_expression('/${Object.keys(headers).sort().join()}')
        self.check_expression('/${Object.values(headers).sort().join()}')

    def test_njs_variables(self, temp_dir):
        self.create_files('str', 'localhost', '127.0.0.1')

        self.check_expression('/${host}')
        self.check_expression('/${remoteAddr}')
        self.check_expression('/${headers.Host}')

        self.set_share(f'"`{temp_dir}/assets/${{cookies.foo}}`"')
        assert (
            self.get(headers={'Cookie': 'foo=str', 'Connection': 'close'})[
                'status'
            ]
            == 200
        ), 'cookies'

        self.set_share(f'"`{temp_dir}/assets/${{args.foo}}`"')
        assert self.get(url='/?foo=str')['status'] == 200, 'args'

    def test_njs_invalid(self, skip_alert):
        skip_alert(r'js exception:')

        def check_invalid(template):
            assert 'error' in self.conf(template, 'routes/0/action/share')

        check_invalid('"`a"')
        check_invalid('"`a``"')
        check_invalid('"`a`/"')

        def check_invalid_resolve(template):
            assert 'success' in self.conf(template, 'routes/0/action/share')
            assert self.get()['status'] == 500

        check_invalid_resolve('"`${a}`"')
        check_invalid_resolve('"`${uri.a.a}`"')