xref: /unit/test/test_php_targets.py (revision 2055:84cb1ec94bae)
1from unit.applications.lang.php import TestApplicationPHP
2from unit.option import option
3
4
5class TestPHPTargets(TestApplicationPHP):
6    prerequisites = {'modules': {'php': 'any'}}
7
8    def test_php_application_targets(self):
9        assert 'success' in self.conf(
10            {
11                "listeners": {"*:7080": {"pass": "routes"}},
12                "routes": [
13                    {
14                        "match": {"uri": "/1"},
15                        "action": {"pass": "applications/targets/1"},
16                    },
17                    {
18                        "match": {"uri": "/2"},
19                        "action": {"pass": "applications/targets/2"},
20                    },
21                    {"action": {"pass": "applications/targets/default"}},
22                ],
23                "applications": {
24                    "targets": {
25                        "type": self.get_application_type(),
26                        "processes": {"spare": 0},
27                        "targets": {
28                            "1": {
29                                "script": "1.php",
30                                "root": option.test_dir + "/php/targets",
31                            },
32                            "2": {
33                                "script": "2.php",
34                                "root": option.test_dir + "/php/targets/2",
35                            },
36                            "default": {
37                                "index": "index.php",
38                                "root": option.test_dir + "/php/targets",
39                            },
40                        },
41                    }
42                },
43            }
44        )
45
46        assert self.get(url='/1')['body'] == '1'
47        assert self.get(url='/2')['body'] == '2'
48        assert self.get(url='/blah')['status'] == 503  # TODO 404
49        assert self.get(url='/')['body'] == 'index'
50
51        assert 'success' in self.conf(
52            "\"1.php\"", 'applications/targets/targets/default/index'
53        ), 'change targets index'
54        assert self.get(url='/')['body'] == '1'
55
56        assert 'success' in self.conf_delete(
57            'applications/targets/targets/default/index'
58        ), 'remove targets index'
59        assert self.get(url='/')['body'] == 'index'
60
61    def test_php_application_targets_error(self):
62        assert 'success' in self.conf(
63            {
64                "listeners": {
65                    "*:7080": {"pass": "applications/targets/default"}
66                },
67                "applications": {
68                    "targets": {
69                        "type": self.get_application_type(),
70                        "processes": {"spare": 0},
71                        "targets": {
72                            "default": {
73                                "index": "index.php",
74                                "root": option.test_dir + "/php/targets",
75                            },
76                        },
77                    }
78                },
79            }
80        ), 'initial configuration'
81        assert self.get()['status'] == 200
82
83        assert 'error' in self.conf(
84            {"pass": "applications/targets/blah"}, 'listeners/*:7080'
85        ), 'invalid targets pass'
86        assert 'error' in self.conf(
87            '"' + option.test_dir + '/php/targets\"',
88            'applications/targets/root',
89        ), 'invalid root'
90        assert 'error' in self.conf(
91            '"index.php"', 'applications/targets/index'
92        ), 'invalid index'
93        assert 'error' in self.conf(
94            '"index.php"', 'applications/targets/script'
95        ), 'invalid script'
96        assert 'error' in self.conf_delete(
97            'applications/targets/default/root'
98        ), 'root remove'
99