Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.

Commit 43c4bfd

Browse files
committed
tests: "if" option in http route match
1 parent debd61c commit 43c4bfd

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

test/test_routing.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,3 +2009,60 @@ def test_routes_match_destination_proxy():
20092009
), 'proxy configure'
20102010

20112011
assert client.get()['status'] == 200, 'proxy'
2012+
2013+
2014+
def test_routes_match_if():
2015+
2016+
def set_if(condition):
2017+
assert 'success' in client.conf(f'"{condition}"', 'routes/0/match/if')
2018+
2019+
def try_if(condition, status):
2020+
set_if(condition)
2021+
assert client.get(url=f'/{condition}')['status'] == status
2022+
2023+
assert 'success' in client.conf(
2024+
{
2025+
"listeners": {"*:8080": {"pass": "routes"}},
2026+
"routes": [
2027+
{
2028+
"match": {"method": "GET"},
2029+
"action": {"return": 200},
2030+
}
2031+
],
2032+
"applications": {},
2033+
}
2034+
), 'routing configure'
2035+
2036+
# const
2037+
2038+
try_if('', 404)
2039+
try_if('0', 404)
2040+
try_if('false', 404)
2041+
try_if('undefined', 404)
2042+
try_if('!', 200)
2043+
try_if('!null', 200)
2044+
try_if('1', 200)
2045+
2046+
# variable
2047+
2048+
set_if('$arg_foo')
2049+
assert client.get(url='/bar?bar')['status'] == 404
2050+
assert client.get(url='/foo_empty?foo')['status'] == 404
2051+
assert client.get(url='/foo?foo=1')['status'] == 200
2052+
2053+
set_if('!$arg_foo')
2054+
assert client.get(url='/bar?bar')['status'] == 200
2055+
assert client.get(url='/foo_empty?foo')['status'] == 200
2056+
assert client.get(url='/foo?foo=1')['status'] == 404
2057+
2058+
# njs
2059+
2060+
set_if('`${args.foo == \'1\'}`')
2061+
assert client.get(url='/foo_1?foo=1')['status'] == 200
2062+
assert client.get(url='/foo_2?foo=2')['status'] == 404
2063+
2064+
set_if('!`${args.foo == \'1\'}`')
2065+
assert client.get(url='/foo_1?foo=1')['status'] == 404
2066+
assert client.get(url='/foo_2?foo=2')['status'] == 200
2067+
2068+
assert 'error' in client.conf('$arg_', 'routes/0/match/if')

0 commit comments

Comments
 (0)