summaryrefslogtreecommitdiffhomepage
path: root/test/test_njs_modules.py
blob: 10ea03a7332363e1deeadb4ea5df50fbab62e49c (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
95
96
97
98
99
from unit.applications.proto import TestApplicationProto
from unit.option import option

prerequisites = {'modules': {'njs': 'any'}}


class TestNJSModules(TestApplicationProto):
    def njs_script_load(self, module, name=None, expect='success'):
        if name is None:
            name = module

        with open(f'{option.test_dir}/njs/{module}/script.js', 'rb') as s:
            assert expect in self.conf(s.read(), f'/js_modules/{name}')

    def test_njs_modules(self):
        self.njs_script_load('next')

        assert 'export' in self.conf_get('/js_modules/next')
        assert 'error' in self.conf_post('"blah"', '/js_modules/next')

        assert 'success' in self.conf(
            {
                "settings": {"js_module": "next"},
                "listeners": {"*:7080": {"pass": "routes/first"}},
                "routes": {
                    "first": [{"action": {"pass": "`routes/${next.route()}`"}}],
                    "next": [{"action": {"return": 200}}],
                },
            }
        )
        assert self.get()['status'] == 200, 'string'

        assert 'success' in self.conf({"js_module": ["next"]}, 'settings')
        assert self.get()['status'] == 200, 'array'

        # add one more value to array

        assert len(self.conf_get('/js_modules').keys()) == 1

        self.njs_script_load('next', 'next_2')

        assert len(self.conf_get('/js_modules').keys()) == 2

        assert 'success' in self.conf_post('"next_2"', 'settings/js_module')
        assert self.get()['status'] == 200, 'array len 2'

        assert 'success' in self.conf(
            '"`routes/${next_2.route()}`"', 'routes/first/0/action/pass'
        )
        assert self.get()['status'] == 200, 'array new'

        # can't update exsisting script

        self.njs_script_load('global_this', 'next', expect='error')

        # delete modules

        assert 'error' in self.conf_delete('/js_modules/next_2')
        assert 'success' in self.conf_delete('settings/js_module')
        assert 'success' in self.conf_delete('/js_modules/next_2')

    def test_njs_modules_import(self):
        self.njs_script_load('import_from')

        assert 'success' in self.conf(
            {
                "settings": {"js_module": "import_from"},
                "listeners": {"*:7080": {"pass": "routes/first"}},
                "routes": {
                    "first": [
                        {"action": {"pass": "`routes/${import_from.num()}`"}}
                    ],
                    "number": [{"action": {"return": 200}}],
                },
            }
        )
        assert self.get()['status'] == 200

    def test_njs_modules_this(self):
        self.njs_script_load('global_this')

        assert 'success' in self.conf(
            {
                "settings": {"js_module": "global_this"},
                "listeners": {"*:7080": {"pass": "routes/first"}},
                "routes": {
                    "first": [
                        {"action": {"pass": "`routes/${global_this.str()}`"}}
                    ],
                    "string": [{"action": {"return": 200}}],
                },
            }
        )
        assert self.get()['status'] == 200

    def test_njs_modules_invalid(self, skip_alert):
        skip_alert(r'.*JS compile module.*failed.*')

        self.njs_script_load('invalid', expect='error')