2020-03-03 18:17:31 +00:00
|
|
|
import os
|
2020-05-15 03:20:56 +00:00
|
|
|
|
2020-09-16 20:31:15 +00:00
|
|
|
from conftest import skip_alert
|
2020-10-07 22:18:43 +00:00
|
|
|
from unit.applications.proto import TestApplicationProto
|
2020-03-03 18:17:31 +00:00
|
|
|
|
|
|
|
|
2020-03-27 15:50:09 +00:00
|
|
|
class TestStatic(TestApplicationProto):
|
|
|
|
prerequisites = {}
|
2020-03-03 18:17:31 +00:00
|
|
|
|
2020-09-16 20:31:15 +00:00
|
|
|
def setup_method(self):
|
|
|
|
super().setup_method()
|
2020-03-03 18:17:31 +00:00
|
|
|
|
2020-09-16 20:31:15 +00:00
|
|
|
os.makedirs(self.temp_dir + '/assets/dir')
|
|
|
|
with open(self.temp_dir + '/assets/index.html', 'w') as index:
|
2020-03-03 18:17:31 +00:00
|
|
|
index.write('0123456789')
|
|
|
|
|
2020-09-16 20:31:15 +00:00
|
|
|
os.makedirs(self.temp_dir + '/assets/403')
|
|
|
|
os.chmod(self.temp_dir + '/assets/403', 0o000)
|
2020-03-03 18:17:31 +00:00
|
|
|
|
|
|
|
self._load_conf(
|
|
|
|
{
|
|
|
|
"listeners": {
|
|
|
|
"*:7080": {"pass": "routes"},
|
2020-03-27 15:50:09 +00:00
|
|
|
"*:7081": {"pass": "routes"},
|
2020-03-03 18:17:31 +00:00
|
|
|
},
|
2020-09-16 20:31:15 +00:00
|
|
|
"routes": [{"action": {"share": self.temp_dir + "/assets"}}],
|
2020-03-27 15:50:09 +00:00
|
|
|
"applications": {},
|
2020-03-03 18:17:31 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-09-16 20:31:15 +00:00
|
|
|
def teardown_method(self):
|
|
|
|
os.chmod(self.temp_dir + '/assets/403', 0o777)
|
2020-03-03 18:17:31 +00:00
|
|
|
|
2020-09-16 20:31:15 +00:00
|
|
|
super().teardown_method()
|
2020-03-03 18:17:31 +00:00
|
|
|
|
2020-03-27 15:50:09 +00:00
|
|
|
def action_update(self, conf):
|
2020-09-16 20:31:15 +00:00
|
|
|
assert 'success' in self.conf(conf, 'routes/0/action')
|
2020-03-27 15:50:09 +00:00
|
|
|
|
2020-03-03 18:17:31 +00:00
|
|
|
def test_fallback(self):
|
2020-03-27 15:50:09 +00:00
|
|
|
self.action_update({"share": "/blah"})
|
2020-09-16 20:31:15 +00:00
|
|
|
assert self.get()['status'] == 404, 'bad path no fallback'
|
2020-03-03 18:17:31 +00:00
|
|
|
|
2020-03-27 15:50:09 +00:00
|
|
|
self.action_update({"share": "/blah", "fallback": {"return": 200}})
|
|
|
|
|
2020-03-03 18:17:31 +00:00
|
|
|
resp = self.get()
|
2020-09-16 20:31:15 +00:00
|
|
|
assert resp['status'] == 200, 'bad path fallback status'
|
|
|
|
assert resp['body'] == '', 'bad path fallback'
|
2020-03-03 18:17:31 +00:00
|
|
|
|
|
|
|
def test_fallback_valid_path(self):
|
2020-03-27 15:50:09 +00:00
|
|
|
self.action_update(
|
2020-09-16 20:31:15 +00:00
|
|
|
{"share": self.temp_dir + "/assets", "fallback": {"return": 200}}
|
2020-03-03 18:17:31 +00:00
|
|
|
)
|
|
|
|
resp = self.get()
|
2020-09-16 20:31:15 +00:00
|
|
|
assert resp['status'] == 200, 'fallback status'
|
|
|
|
assert resp['body'] == '0123456789', 'fallback'
|
2020-03-03 18:17:31 +00:00
|
|
|
|
|
|
|
resp = self.get(url='/403/')
|
2020-09-16 20:31:15 +00:00
|
|
|
assert resp['status'] == 200, 'fallback status 403'
|
|
|
|
assert resp['body'] == '', 'fallback 403'
|
2020-03-03 18:17:31 +00:00
|
|
|
|
|
|
|
resp = self.post()
|
2020-09-16 20:31:15 +00:00
|
|
|
assert resp['status'] == 200, 'fallback status 405'
|
|
|
|
assert resp['body'] == '', 'fallback 405'
|
2020-03-03 18:17:31 +00:00
|
|
|
|
2020-09-16 20:31:15 +00:00
|
|
|
assert self.get(url='/dir')['status'] == 301, 'fallback status 301'
|
2020-03-03 18:17:31 +00:00
|
|
|
|
|
|
|
def test_fallback_nested(self):
|
2020-03-27 15:50:09 +00:00
|
|
|
self.action_update(
|
|
|
|
{
|
|
|
|
"share": "/blah",
|
|
|
|
"fallback": {
|
|
|
|
"share": "/blah/blah",
|
|
|
|
"fallback": {"return": 200},
|
2020-03-03 18:17:31 +00:00
|
|
|
},
|
2020-03-27 15:50:09 +00:00
|
|
|
}
|
2020-03-03 18:17:31 +00:00
|
|
|
)
|
2020-03-27 15:50:09 +00:00
|
|
|
|
2020-03-03 18:17:31 +00:00
|
|
|
resp = self.get()
|
2020-09-16 20:31:15 +00:00
|
|
|
assert resp['status'] == 200, 'fallback nested status'
|
|
|
|
assert resp['body'] == '', 'fallback nested'
|
2020-03-03 18:17:31 +00:00
|
|
|
|
|
|
|
def test_fallback_share(self):
|
2020-03-27 15:50:09 +00:00
|
|
|
self.action_update(
|
|
|
|
{
|
|
|
|
"share": "/blah",
|
2020-09-16 20:31:15 +00:00
|
|
|
"fallback": {"share": self.temp_dir + "/assets"},
|
2020-03-27 15:50:09 +00:00
|
|
|
}
|
2020-03-03 18:17:31 +00:00
|
|
|
)
|
2020-03-27 15:50:09 +00:00
|
|
|
|
2020-03-03 18:17:31 +00:00
|
|
|
resp = self.get()
|
2020-09-16 20:31:15 +00:00
|
|
|
assert resp['status'] == 200, 'fallback share status'
|
|
|
|
assert resp['body'] == '0123456789', 'fallback share'
|
2020-03-03 18:17:31 +00:00
|
|
|
|
|
|
|
resp = self.head()
|
2020-09-16 20:31:15 +00:00
|
|
|
assert resp['status'] == 200, 'fallback share status HEAD'
|
|
|
|
assert resp['body'] == '', 'fallback share HEAD'
|
2020-03-03 18:17:31 +00:00
|
|
|
|
2020-09-16 20:31:15 +00:00
|
|
|
assert (
|
|
|
|
self.get(url='/dir')['status'] == 301
|
|
|
|
), 'fallback share status 301'
|
2020-03-03 18:17:31 +00:00
|
|
|
|
|
|
|
def test_fallback_proxy(self):
|
2020-09-16 20:31:15 +00:00
|
|
|
assert 'success' in self.conf(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"match": {"destination": "*:7081"},
|
|
|
|
"action": {"return": 200},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"action": {
|
|
|
|
"share": "/blah",
|
|
|
|
"fallback": {"proxy": "http://127.0.0.1:7081"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
],
|
|
|
|
'routes',
|
|
|
|
), 'configure fallback proxy route'
|
2020-03-27 15:50:09 +00:00
|
|
|
|
2020-03-03 18:17:31 +00:00
|
|
|
resp = self.get()
|
2020-09-16 20:31:15 +00:00
|
|
|
assert resp['status'] == 200, 'fallback proxy status'
|
|
|
|
assert resp['body'] == '', 'fallback proxy'
|
2020-03-03 18:17:31 +00:00
|
|
|
|
2020-04-20 13:04:55 +00:00
|
|
|
def test_fallback_proxy_loop(self):
|
2020-09-16 20:31:15 +00:00
|
|
|
skip_alert(
|
|
|
|
r'open.*/blah/index.html.*failed',
|
|
|
|
r'accept.*failed',
|
|
|
|
r'socket.*failed',
|
|
|
|
r'new connections are not accepted',
|
2020-04-20 13:04:55 +00:00
|
|
|
)
|
|
|
|
|
2020-03-27 15:50:09 +00:00
|
|
|
self.action_update(
|
2020-04-20 13:04:55 +00:00
|
|
|
{"share": "/blah", "fallback": {"proxy": "http://127.0.0.1:7080"}}
|
2020-03-03 18:17:31 +00:00
|
|
|
)
|
2020-04-20 13:04:55 +00:00
|
|
|
self.get(no_recv=True)
|
2020-03-03 18:17:31 +00:00
|
|
|
|
2020-09-16 20:31:15 +00:00
|
|
|
assert 'success' in self.conf_delete('listeners/*:7081')
|
2020-04-20 13:04:55 +00:00
|
|
|
self.get(read_timeout=1)
|
2020-03-03 18:17:31 +00:00
|
|
|
|
|
|
|
def test_fallback_invalid(self):
|
2020-03-27 15:50:09 +00:00
|
|
|
def check_error(conf):
|
2020-09-16 20:31:15 +00:00
|
|
|
assert 'error' in self.conf(conf, 'routes/0/action')
|
2020-03-27 15:50:09 +00:00
|
|
|
|
|
|
|
check_error({"share": "/blah", "fallback": {}})
|
|
|
|
check_error({"share": "/blah", "fallback": ""})
|
|
|
|
check_error({"return": 200, "fallback": {"share": "/blah"}})
|
|
|
|
check_error(
|
|
|
|
{"proxy": "http://127.0.0.1:7081", "fallback": {"share": "/blah"}}
|
2020-03-03 18:17:31 +00:00
|
|
|
)
|
2020-03-27 15:50:09 +00:00
|
|
|
check_error({"fallback": {"share": "/blah"}})
|