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