1from pathlib import Path 2 3import pytest 4 5from unit.applications.proto import ApplicationProto 6 7client = ApplicationProto() 8 9 10@pytest.fixture(autouse=True) 11def setup_method_fixture(temp_dir): 12 Path(f'{temp_dir}/assets').mkdir() 13 for ext in ['.xml', '.mp4', '.php', '', '.txt', '.html', '.png']: 14 Path(f'{temp_dir}/assets/file{ext}').write_text(ext, encoding='utf-8') 15 16 Path(f'{temp_dir}/assets/index.html').write_text('index', encoding='utf-8') 17 18 assert 'success' in client.conf( 19 { 20 "listeners": { 21 "*:8080": {"pass": "routes"}, 22 "*:8081": {"pass": "routes"}, 23 }, 24 "routes": [{"action": {"share": f'{temp_dir}/assets$uri'}}], 25 "applications": {}, 26 } 27 ) 28 29 30def action_update(conf): 31 assert 'success' in client.conf(conf, 'routes/0/action') 32 33 34def check_body(http_url, body): 35 resp = client.get(url=http_url) 36 assert resp['status'] == 200, 'status' 37 assert resp['body'] == body, 'body' 38 39 40def test_static_types_basic(temp_dir): 41 action_update({"share": f'{temp_dir}/assets$uri'}) 42 check_body('/index.html', 'index') 43 check_body('/file.xml', '.xml') 44 45 action_update( 46 {"share": f'{temp_dir}/assets$uri', "types": "application/xml"} 47 ) 48 check_body('/file.xml', '.xml') 49 50 action_update( 51 {"share": f'{temp_dir}/assets$uri', "types": ["application/xml"]} 52 ) 53 check_body('/file.xml', '.xml') 54 55 action_update({"share": f'{temp_dir}/assets$uri', "types": [""]}) 56 assert client.get(url='/file.xml')['status'] == 403, 'no mtype' 57 58 59def test_static_types_wildcard(temp_dir): 60 action_update( 61 {"share": f'{temp_dir}/assets$uri', "types": ["application/*"]} 62 ) 63 check_body('/file.xml', '.xml') 64 assert client.get(url='/file.mp4')['status'] == 403, 'app * mtype mp4' 65 66 action_update({"share": f'{temp_dir}/assets$uri', "types": ["video/*"]}) 67 assert client.get(url='/file.xml')['status'] == 403, 'video * mtype xml' 68 check_body('/file.mp4', '.mp4') 69 70 71def test_static_types_negation(temp_dir): 72 action_update( 73 {"share": f'{temp_dir}/assets$uri', "types": ["!application/xml"]} 74 ) 75 assert client.get(url='/file.xml')['status'] == 403, 'forbidden negation' 76 check_body('/file.mp4', '.mp4') 77 78 # sorting negation 79 action_update( 80 { 81 "share": f'{temp_dir}/assets$uri', 82 "types": ["!video/*", "image/png", "!image/jpg"], 83 } 84 ) 85 assert client.get(url='/file.mp4')['status'] == 403, 'negation sort mp4' 86 check_body('/file.png', '.png') 87 assert client.get(url='/file.jpg')['status'] == 403, 'negation sort jpg' 88 89 90def test_static_types_regex(temp_dir): 91 action_update( 92 { 93 "share": f'{temp_dir}/assets$uri', 94 "types": ["~text/(html|plain)"], 95 } 96 ) 97 assert client.get(url='/file.php')['status'] == 403, 'regex fail' 98 check_body('/file.html', '.html') 99 check_body('/file.txt', '.txt') 100 101 102def test_static_types_case(temp_dir): 103 action_update( 104 {"share": f'{temp_dir}/assets$uri', "types": ["!APpliCaTiOn/xMl"]} 105 ) 106 check_body('/file.mp4', '.mp4') 107 assert ( 108 client.get(url='/file.xml')['status'] == 403 109 ), 'mixed case xml negation' 110 111 action_update({"share": f'{temp_dir}/assets$uri', "types": ["vIdEo/mp4"]}) 112 assert client.get(url='/file.mp4')['status'] == 200, 'mixed case' 113 assert ( 114 client.get(url='/file.xml')['status'] == 403 115 ), 'mixed case video negation' 116 117 action_update({"share": f'{temp_dir}/assets$uri', "types": ["vIdEo/*"]}) 118 check_body('/file.mp4', '.mp4') 119 assert ( 120 client.get(url='/file.xml')['status'] == 403 121 ), 'mixed case video * negation' 122 123 124def test_static_types_fallback(temp_dir): 125 assert 'success' in client.conf( 126 [ 127 { 128 "match": {"destination": "*:8081"}, 129 "action": {"return": 200}, 130 }, 131 { 132 "action": { 133 "share": f'{temp_dir}/assets$uri', 134 "types": ["!application/x-httpd-php"], 135 "fallback": {"proxy": "http://127.0.0.1:8081"}, 136 } 137 }, 138 ], 139 'routes', 140 ), 'configure fallback proxy route' 141 142 check_body('/file.php', '') 143 check_body('/file.mp4', '.mp4') 144 145 146def test_static_types_index(temp_dir): 147 action_update( 148 {"share": f'{temp_dir}/assets$uri', "types": "application/xml"} 149 ) 150 check_body('/', 'index') 151 check_body('/file.xml', '.xml') 152 assert client.get(url='/index.html')['status'] == 403, 'forbidden mtype' 153 assert client.get(url='/file.mp4')['status'] == 403, 'forbidden mtype' 154 155 156def test_static_types_custom_mime(temp_dir): 157 assert 'success' in client.conf( 158 { 159 "listeners": {"*:8080": {"pass": "routes"}}, 160 "routes": [{"action": {"share": f'{temp_dir}/assets$uri'}}], 161 "applications": {}, 162 "settings": { 163 "http": {"static": {"mime_types": {"test/mime-type": ["file"]}}} 164 }, 165 } 166 ) 167 168 action_update({"share": f'{temp_dir}/assets$uri', "types": [""]}) 169 assert client.get(url='/file')['status'] == 403, 'forbidden custom mime' 170 171 action_update( 172 {"share": f'{temp_dir}/assets$uri', "types": ["test/mime-type"]} 173 ) 174 check_body('/file', '') 175