From 698680d8942feed023cdcfe85e9ca5b67261de2e Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Thu, 28 Jul 2022 14:13:03 +0100 Subject: [PATCH] Tests: added tests for the log format. Also added tests for the following variables: $request_line, $time_local, $bytes_sent, and $status. --- test/test_access_log.py | 80 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/test/test_access_log.py b/test/test_access_log.py index 5d242a1a..f7343d21 100644 --- a/test/test_access_log.py +++ b/test/test_access_log.py @@ -15,6 +15,15 @@ class TestAccessLog(TestApplicationPython): '"' + option.temp_dir + '/access.log"', 'access_log' ), 'access_log configure' + def set_format(self, format): + assert 'success' in self.conf( + { + 'path': option.temp_dir + '/access.log', + 'format': format, + }, + 'access_log', + ), 'access_log format' + def wait_for_record(self, pattern, name='access.log'): return super().wait_for_record(pattern, name) @@ -263,3 +272,74 @@ Connection: close self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', 'new.log') is not None ), 'change' + + def test_access_log_format(self): + self.load('empty') + + def check_format(format, expect, url='/'): + self.set_format(format) + + assert self.get(url=url)['status'] == 200 + assert self.wait_for_record(expect) is not None, 'found' + + format = 'BLAH\t0123456789' + check_format(format, format) + check_format('$uri $status $uri $status', '/ 200 / 200') + + def test_access_log_variables(self): + self.load('mirror') + + # $time_local + + self.set_format('$uri $time_local $uri') + assert self.get(url='/time_local')['status'] == 200 + assert self.wait_for_record('/time_local') is not None, 'time log' + date = self.search_in_log( + r'^\/time_local (.*) \/time_local$', 'access.log' + )[1] + assert ( + abs( + self.date_to_sec_epoch(date, '%d/%b/%Y:%X %z') + - time.mktime(time.localtime()) + ) + < 5 + ), '$time_local' + + # $request_line + + self.set_format('$request_line') + assert self.get(url='/r_line')['status'] == 200 + assert self.wait_for_record(r'^GET \/r_line HTTP\/1\.1$') is not None + + # $body_bytes_sent + + self.set_format('$uri $body_bytes_sent') + body = '0123456789' * 50 + self.post( + url='/bbs', + headers={ + 'Host': 'localhost', + 'Connection': 'close', + 'Content-Type': 'text/html', + }, + body=body, + read_timeout=1, + ) + assert ( + self.wait_for_record(r'^\/bbs ' + str(len(body)) + r'$') is not None + ), '$body_bytes_sent' + + def test_access_log_incorrect(self, skip_alert): + skip_alert(r'failed to apply new conf') + + assert 'error' in self.conf( + option.temp_dir + '/blah/access.log' 'access_log/path', + ), 'access_log path incorrect' + + assert 'error' in self.conf( + { + 'path': option.temp_dir + '/access.log', + 'format': '$remote_add', + }, + 'access_log', + ), 'access_log format incorrect'