From 7934dcabbc3c2b585e8d3f8fcee7020ba26f1687 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Tue, 21 Feb 2023 17:21:29 +0000 Subject: Tests: switched to using f-strings. Previously, it was necessary to support older versions of Python for compatibility. F-strings were released in Python 3.6. Python 3.5 was marked as unsupported by the end of 2020, so now it's possible to start using f-strings safely for better readability and performance. --- test/test_settings.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'test/test_settings.py') diff --git a/test/test_settings.py b/test/test_settings.py index ad8929f8..3ddc608a 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') -- cgit From b24257c8f6348619595d4ad615a7edc7f565b592 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Tue, 21 Mar 2023 19:20:23 +0000 Subject: Tests: added tests for the "log_route" option. --- test/test_settings.py | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) (limited to 'test/test_settings.py') diff --git a/test/test_settings.py b/test/test_settings.py index 3ddc608a..21ab22d9 100644 --- a/test/test_settings.py +++ b/test/test_settings.py @@ -393,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 -- cgit