diff options
author | Konstantin Pavlov <thresh@nginx.com> | 2023-05-10 10:29:16 -0700 |
---|---|---|
committer | Konstantin Pavlov <thresh@nginx.com> | 2023-05-10 10:29:16 -0700 |
commit | 69235c513277c64b513447d9b92c3c03d616f577 (patch) | |
tree | 0780c92ba28d92b547c85ea0bee5e3040e14dee2 /test/test_settings.py | |
parent | b9bc222021e77bbdfb12576b3e315b962cf6b399 (diff) | |
parent | faf97dc06058de1c929af33a68adb34d3932b374 (diff) | |
download | unit-1.30.0-1.tar.gz unit-1.30.0-1.tar.bz2 |
Merged with the default branch.1.30.0-1
Diffstat (limited to '')
-rw-r--r-- | test/test_settings.py | 167 |
1 files changed, 159 insertions, 8 deletions
diff --git a/test/test_settings.py b/test/test_settings.py index ad8929f8..21ab22d9 100644 --- a/test/test_settings.py +++ b/test/test_settings.py @@ -44,7 +44,7 @@ class TestSettings(TestApplicationPython): headers = {'Host': 'localhost', 'Connection': 'close'} for i in range(headers_num): - headers['Custom-header-' + str(i)] = 'a' * 8000 + headers[f'Custom-header-{i}'] = 'a' * 8000 assert self.get(headers=headers)['status'] == expect @@ -229,15 +229,12 @@ Connection: close sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(addr) - req = ( - """GET / HTTP/1.1 + req = f"""GET / HTTP/1.1 Host: localhost -X-Length: %d +X-Length: {data_len} Connection: close """ - % data_len - ) sock.sendall(req.encode()) @@ -259,10 +256,10 @@ Connection: close data_len = 1048576 if len(values) == 0 else 10 * max(values) - addr = temp_dir + '/sock' + addr = f'{temp_dir}/sock' assert 'success' in self.conf( - {"unix:" + addr: {'application': 'body_generate'}}, 'listeners' + {f'unix:{addr}': {'application': 'body_generate'}}, 'listeners' ) assert 'success' in self.conf({'http': {'send_timeout': 1}}, 'settings') @@ -396,3 +393,157 @@ Connection: close assert bool(resp), 'response from application 4' assert resp['status'] == 200, 'status 4' assert resp['body'] == body, 'body 4' + + def test_settings_log_route(self): + def count_fallbacks(): + return len(self.findall(r'"fallback" taken')) + + def check_record(template): + assert self.search_in_log(template) is not None + + def check_no_record(template): + assert self.search_in_log(template) is None + + def template_req_line(url): + return rf'\[notice\].*http request line "GET {url} HTTP/1\.1"' + + def template_selected(route): + return rf'\[notice\].*"{route}" selected' + + def template_discarded(route): + return rf'\[info\].*"{route}" discarded' + + def wait_for_request_log(status, uri, route): + assert self.get(url=uri)['status'] == status + assert self.wait_for_record(template_req_line(uri)) is not None + assert self.wait_for_record(template_selected(route)) is not None + + # routes array + + assert 'success' in self.conf( + { + "listeners": {"*:7080": {"pass": "routes"}}, + "routes": [ + { + "match": { + "uri": "/zero", + }, + "action": {"return": 200}, + }, + { + "action": {"return": 201}, + }, + ], + "applications": {}, + "settings": {"http": {"log_route": True}}, + } + ) + + wait_for_request_log(200, '/zero', 'routes/0') + check_no_record(r'discarded') + + wait_for_request_log(201, '/one', 'routes/1') + check_record(template_discarded('routes/0')) + + # routes object + + assert 'success' in self.conf( + { + "listeners": {"*:7080": {"pass": "routes/main"}}, + "routes": { + "main": [ + { + "match": { + "uri": "/named_route", + }, + "action": {"return": 200}, + }, + { + "action": {"return": 201}, + }, + ] + }, + "applications": {}, + "settings": {"http": {"log_route": True}}, + } + ) + + wait_for_request_log(200, '/named_route', 'routes/main/0') + check_no_record(template_discarded('routes/main')) + + wait_for_request_log(201, '/unnamed_route', 'routes/main/1') + check_record(template_discarded('routes/main/0')) + + # routes sequence + + assert 'success' in self.conf( + { + "listeners": {"*:7080": {"pass": "routes/first"}}, + "routes": { + "first": [ + { + "action": {"pass": "routes/second"}, + }, + ], + "second": [ + { + "action": {"return": 200}, + }, + ], + }, + "applications": {}, + "settings": {"http": {"log_route": True}}, + } + ) + + wait_for_request_log(200, '/sequence', 'routes/second/0') + check_record(template_selected('routes/first/0')) + + # fallback + + assert 'success' in self.conf( + { + "listeners": {"*:7080": {"pass": "routes/fall"}}, + "routes": { + "fall": [ + { + "action": { + "share": "/blah", + "fallback": {"pass": "routes/fall2"}, + }, + }, + ], + "fall2": [ + { + "action": {"return": 200}, + }, + ], + }, + "applications": {}, + "settings": {"http": {"log_route": True}}, + } + ) + + wait_for_request_log(200, '/', 'routes/fall2/0') + assert count_fallbacks() == 1 + check_record(template_selected('routes/fall/0')) + + assert self.head()['status'] == 200 + assert count_fallbacks() == 2 + + # disable log + + assert 'success' in self.conf({"log_route": False}, 'settings/http') + + url = '/disable_logging' + assert self.get(url=url)['status'] == 200 + + time.sleep(1) + + check_no_record(template_req_line(url)) + + # total + + assert len(self.findall(r'\[notice\].*http request line')) == 7 + assert len(self.findall(r'\[notice\].*selected')) == 10 + assert len(self.findall(r'\[info\].*discarded')) == 2 |