summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorZhidao HONG <z.hong@f5.com>2024-08-18 22:49:59 +0800
committerZhidao HONG <z.hong@f5.com>2024-08-20 09:17:24 +0800
commit43c4bfdcd1e925e5ec06e59b86a884279947671d (patch)
treee6ade68802731c939f513c134788677b588705f8 /test
parentdebd61c3a4f7e5817bf842c2166217929ef80c88 (diff)
downloadunit-43c4bfdcd1e925e5ec06e59b86a884279947671d.tar.gz
unit-43c4bfdcd1e925e5ec06e59b86a884279947671d.tar.bz2
tests: "if" option in http route match
Diffstat (limited to 'test')
-rw-r--r--test/test_routing.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/test/test_routing.py b/test/test_routing.py
index 0b6eced2..c419779a 100644
--- a/test/test_routing.py
+++ b/test/test_routing.py
@@ -2009,3 +2009,60 @@ def test_routes_match_destination_proxy():
), 'proxy configure'
assert client.get()['status'] == 200, 'proxy'
+
+
+def test_routes_match_if():
+
+ def set_if(condition):
+ assert 'success' in client.conf(f'"{condition}"', 'routes/0/match/if')
+
+ def try_if(condition, status):
+ set_if(condition)
+ assert client.get(url=f'/{condition}')['status'] == status
+
+ assert 'success' in client.conf(
+ {
+ "listeners": {"*:8080": {"pass": "routes"}},
+ "routes": [
+ {
+ "match": {"method": "GET"},
+ "action": {"return": 200},
+ }
+ ],
+ "applications": {},
+ }
+ ), 'routing configure'
+
+ # const
+
+ try_if('', 404)
+ try_if('0', 404)
+ try_if('false', 404)
+ try_if('undefined', 404)
+ try_if('!', 200)
+ try_if('!null', 200)
+ try_if('1', 200)
+
+ # variable
+
+ set_if('$arg_foo')
+ assert client.get(url='/bar?bar')['status'] == 404
+ assert client.get(url='/foo_empty?foo')['status'] == 404
+ assert client.get(url='/foo?foo=1')['status'] == 200
+
+ 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'] == 404
+
+ # njs
+
+ set_if('`${args.foo == \'1\'}`')
+ assert client.get(url='/foo_1?foo=1')['status'] == 200
+ assert client.get(url='/foo_2?foo=2')['status'] == 404
+
+ set_if('!`${args.foo == \'1\'}`')
+ assert client.get(url='/foo_1?foo=1')['status'] == 404
+ assert client.get(url='/foo_2?foo=2')['status'] == 200
+
+ assert 'error' in client.conf('$arg_', 'routes/0/match/if')