summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorAndrei Zeliankou <zelenkov@nginx.com>2024-01-24 16:09:41 +0000
committerandrey-zelenkov <xim.andrew@gmail.com>2024-01-29 17:54:26 +0000
commitad3645074e368e7277fa2c25d8f87ebd1f522e87 (patch)
treedf0b20065dd4483d7027e8dbad1622a219cf0a08 /test
parentdcbff27d9bc88c9e2049c52a441fa67d36ca7efc (diff)
downloadunit-ad3645074e368e7277fa2c25d8f87ebd1f522e87.tar.gz
unit-ad3645074e368e7277fa2c25d8f87ebd1f522e87.tar.bz2
Tests: "if" option in access logging.
Conditional access logging was introduced here: https://github.com/nginx/unit/commit/4c91bebb50d06b28e369d68b23022caa072cf62d
Diffstat (limited to 'test')
-rw-r--r--test/test_access_log.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/test/test_access_log.py b/test/test_access_log.py
index 873c941a..1b0ec8ad 100644
--- a/test/test_access_log.py
+++ b/test/test_access_log.py
@@ -28,6 +28,10 @@ def set_format(log_format):
), 'access_log format'
+def set_if(condition):
+ assert 'success' in client.conf(f'"{condition}"', 'access_log/if')
+
+
def test_access_log_keepalive(wait_for_record):
load('mirror')
@@ -308,6 +312,62 @@ def test_access_log_variables(wait_for_record):
), '$body_bytes_sent'
+def test_access_log_if(search_in_file, wait_for_record):
+ load('empty')
+ set_format('$uri')
+
+ def try_if(condition):
+ set_if(condition)
+ assert client.get(url=f'/{condition}')['status'] == 200
+
+ # const
+
+ try_if('')
+ try_if('0')
+ try_if('false')
+ try_if('undefined')
+ try_if('!')
+ try_if('!null')
+ try_if('1')
+
+ # variable
+
+ set_if('$arg_foo')
+ assert client.get(url='/bar?bar')['status'] == 200
+ assert client.get(url='/foo_empty?foo')['status'] == 200
+ assert client.get(url='/foo?foo=1')['status'] == 200
+
+ # check results
+
+ assert wait_for_record(r'^/foo$', 'access.log') is not None
+
+ assert search_in_file(r'^/$', 'access.log') is None
+ assert search_in_file(r'^/0$', 'access.log') is None
+ assert search_in_file(r'^/false$', 'access.log') is None
+ assert search_in_file(r'^/undefined$', 'access.log') is None
+ assert search_in_file(r'^/!$', 'access.log') is not None
+ assert search_in_file(r'^/!null$', 'access.log') is not None
+ assert search_in_file(r'^/1$', 'access.log') is not None
+
+ assert search_in_file(r'^/bar$', 'access.log') is None
+ assert search_in_file(r'^/foo_empty$', 'access.log') is None
+
+
+def test_access_log_if_njs(require, search_in_file, wait_for_record):
+ require({'modules': {'njs': 'any'}})
+
+ load('empty')
+ set_format('$uri')
+
+ set_if('`${args.foo == \'1\'}`')
+
+ assert client.get(url='/foo_2?foo=2')['status'] == 200
+ assert client.get(url='/foo_1?foo=1')['status'] == 200
+
+ assert wait_for_record(r'^/foo_1$', 'access.log') is not None
+ assert search_in_file(r'^/foo_2$', 'access.log') is None
+
+
def test_access_log_incorrect(temp_dir, skip_alert):
skip_alert(r'failed to apply new conf')
@@ -323,3 +383,5 @@ def test_access_log_incorrect(temp_dir, skip_alert):
},
'access_log',
), 'access_log format incorrect'
+
+ assert 'error' in client.conf('$arg_', 'access_log/if')