Skip to content

Commit

Permalink
Functional tests for cookie http_chain rules (#206)
Browse files Browse the repository at this point in the history
Fixes tempesta-tech/tempesta#1544

Signed-off-by: Aleksey Mikhaylov <[email protected]>
  • Loading branch information
ttaym authored Feb 21, 2022
1 parent f56c9c9 commit 1f27886
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 7 deletions.
78 changes: 73 additions & 5 deletions cache/test_cache_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ class TestCacheControl(tester.TempestaTest):
tempesta_template = {
'config' :
"""
server ${general_ip}:8000;
cache 2;
srv_group default {
server ${general_ip}:8000;
}
vhost vh1 {
proxy_pass default;
}
%(tempesta_config)s
"""
}
Expand All @@ -41,7 +46,7 @@ class TestCacheControl(tester.TempestaTest):
'%(response_headers)s\r\n'
},
]

tempesta_config = '''
cache_fulfill * *;
'''
Expand Down Expand Up @@ -155,7 +160,7 @@ def _test(self):
else:
self.assertEqual(2, len(srv.requests),
"response is cached while it should not be")

if self.should_be_cached:
self.check_cached_response_headers(cached_response)
else:
Expand All @@ -175,7 +180,7 @@ def test(self):
# - "NotCached" - response is normally not cached (forwarded upstream);
# - "Ignore" - the directive is disabled by cache_control_ignore, default
# behaviour ensues.
# - empty value for default behaviour,
# - empty value for default behaviour,
# For example, ResponseMustRevalidateIgnore - testing "must-revalidate"
# in the request, which should be ignored due to cache_control_ignore.

Expand Down Expand Up @@ -689,7 +694,7 @@ class CCArgNoCacheCached2(TestCacheControl, SingleTest):
cached_headers = {'Remove-me': None, 'Remove-me-2': '"arg"',
'Cache-control': 'no-cache="remove-me"'}
should_be_cached = True

class CCArgNoCacheCached3(TestCacheControl, SingleTest):
tempesta_config = '''
cache_fulfill * *;
Expand Down Expand Up @@ -744,3 +749,66 @@ class CCArgBothNoCacheCached(TestCacheControl, SingleTest):
cached_headers = {'Set-cookie': None, 'remove-me-2': None,
'Cache-control': 'no-cache="set-cookie, Remove-me-2"'}
should_be_cached = True

#########################################################
# Http chain cache action test
#########################################################
# bypass cache
class HttpChainCacheActionBypass(TestCacheControl, SingleTest):
tempesta_config = '''
cache_fulfill * *;
http_chain {
cookie "foo_items_in_cart" == "*" -> $$cache = 0;
cookie "comment_author_*" == "*" -> $$cache = 0;
cookie "wordpress_logged_in*" == "*" -> $$cache = 0;
-> vh1;
}
'''
request_headers = {'Cookie': 'foo_items_in_cart='}
response_headers = {}
should_be_cached = False

# bypass cache due to override later
class HttpChainCacheActionOverrideBypass(TestCacheControl, SingleTest):
tempesta_config = '''
cache_fulfill * *;
http_chain {
cookie "foo_items_in_cart" == "*" -> $$cache = 0;
cookie "comment_author_*" == "*" -> $$cache = 1;
cookie "wordpress_logged_in*" == "*" -> $$cache = 0;
-> vh1;
}
'''
request_headers = {'Cookie': 'comment_author_name=john; wordpress_logged_in=true'}
response_headers = {}
should_be_cached = False

# honour cache due to override later
class HttpChainCacheActionOverrideCached(TestCacheControl, SingleTest):
tempesta_config = '''
cache_fulfill * *;
http_chain {
cookie "foo_items_in_cart" == "*" -> $$cache = 0;
cookie "comment_author_*" == "*" -> $$cache = 1;
cookie "wordpress_logged_in*" == "*" -> $$cache = 0;
-> vh1;
}
'''
request_headers = {'Cookie': 'foo_items_in_cart=; comment_author_name=john'}
response_headers = {}
should_be_cached = True

# honour cache
class HttpChainCacheActionCached(TestCacheControl, SingleTest):
tempesta_config = '''
cache_fulfill * *;
http_chain {
cookie "foo_items_in_cart" == "*" -> $$cache = 0;
cookie "comment_author_*" == "*" -> $$cache = 1;
cookie "wordpress_logged_in*" == "*" -> $$cache = 0;
-> vh1;
}
'''
request_headers = {'Cookie': 'comment_author_name=john'}
response_headers = {}
should_be_cached = True
32 changes: 30 additions & 2 deletions http_rules/test_http_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from framework import tester

__author__ = 'Tempesta Technologies, Inc.'
__copyright__ = 'Copyright (C) 2018 Tempesta Technologies, Inc.'
__copyright__ = 'Copyright (C) 2022 Tempesta Technologies, Inc.'
__license__ = 'GPL2'

class HttpTablesTest(tester.TempestaTest):
Expand Down Expand Up @@ -75,6 +75,15 @@ class HttpTablesTest(tester.TempestaTest):
'response_content' :
'HTTP/1.1 200 OK\r\n'
'Content-Length: 0\r\n\r\n'
},
{
'id' : 7,
'type' : 'deproxy',
'port' : '8007',
'response' : 'static',
'response_content' :
'HTTP/1.1 200 OK\r\n'
'Content-Length: 0\r\n\r\n'
}
]

Expand Down Expand Up @@ -103,6 +112,9 @@ class HttpTablesTest(tester.TempestaTest):
srv_group grp7 {
server ${server_ip}:8006;
}
srv_group grp8 {
server ${server_ip}:8007;
}
vhost vh1 {
proxy_pass grp1;
}
Expand All @@ -124,6 +136,9 @@ class HttpTablesTest(tester.TempestaTest):
vhost vh7 {
proxy_pass grp7;
}
vhost vh8 {
proxy_pass grp8;
}
http_chain chain1 {
uri == "/static*" -> vh1;
uri == "*.php" -> vh2;
Expand All @@ -143,6 +158,7 @@ class HttpTablesTest(tester.TempestaTest):
hdr referer == "http://example.*" -> chain3;
hdr host == "bad.host.com" -> block;
hdr host == "bar*" -> vh5;
cookie "tempesta" == "*" -> vh8;
mark == 1 -> vh7;
mark == 2 -> vh6;
mark == 3 -> vh5;
Expand Down Expand Up @@ -196,6 +212,12 @@ class HttpTablesTest(tester.TempestaTest):
'type' : 'deproxy',
'addr' : "${tempesta_ip}",
'port' : '80'
},
{
'id' : 7,
'type' : 'deproxy',
'addr' : "${tempesta_ip}",
'port' : '80'
}
]

Expand Down Expand Up @@ -241,7 +263,13 @@ class HttpTablesTest(tester.TempestaTest):
('host'),
('bad.host.com'),
True
)
),
(
('/baz/index.html'),
('cookie'),
('tempesta=test'),
False
),
]

chains = []
Expand Down

0 comments on commit 1f27886

Please sign in to comment.