diff --git a/test/modules/filters/env.py b/test/modules/filters/env.py index 28496c30d99..149cdcdd4c7 100644 --- a/test/modules/filters/env.py +++ b/test/modules/filters/env.py @@ -12,7 +12,7 @@ class FiltersTestSetup(HttpdTestSetup): def __init__(self, env: 'HttpdTestEnv'): super().__init__(env=env) self.add_source_dir(os.path.dirname(inspect.getfile(FiltersTestSetup))) - self.add_modules(["substitute", "sed", "buffer", "brotli"]) + self.add_modules(["substitute", "sed", "buffer", "brotli", "ext_filter", "include", "deflate"]) self.add_cgi_module() @@ -20,7 +20,7 @@ class FiltersTestEnv(HttpdTestEnv): def __init__(self, pytestconfig=None): super().__init__(pytestconfig=pytestconfig) - self.add_httpd_log_modules(["substitute", "sed", "buffer", "brotli", "core"]) + self.add_httpd_log_modules(["substitute", "sed", "buffer", "brotli", "ext_filter", "core"]) def setup_httpd(self, setup: HttpdTestSetup = None): super().setup_httpd(setup=FiltersTestSetup(env=self)) diff --git a/test/modules/filters/test_005_mod_ext_filter.py b/test/modules/filters/test_005_mod_ext_filter.py new file mode 100644 index 00000000000..a5f4356aed6 --- /dev/null +++ b/test/modules/filters/test_005_mod_ext_filter.py @@ -0,0 +1,45 @@ +import gzip +import os +import sys + +import pytest + +from pyhttpd.conf import HttpdConf + +class TestModExtFilters: + + @pytest.fixture(autouse=True, scope='class') + def _class_scope(self, env): + doc_dir = os.path.join(env.server_dir, "htdocs", "test1") + + # --- ext_filter test data --- + ext_dir = os.path.join(doc_dir, "extfiltertest") + os.makedirs(ext_dir, exist_ok=True) + with open(os.path.join(ext_dir, "hello.txt.crap"), "w") as f: + f.write("crap not allowed during worktime\n" + "recycling can save the earth\n") + + @pytest.mark.skipif(sys.platform == "win32", + reason="/usr/bin/grep is not available on Windows") + def test_mod_ext_filter_005_01(self, env): + """ExtFilterDefine with grep keeps only matching lines.""" + conf = HttpdConf(env, extras={ + f"test1.{env.http_tld}": """ + ExtFilterDefine grepfilter cmd="/usr/bin/grep crap" + + SetOutputFilter grepfilter + + """, + }) + conf.add_vhost_test1() + conf.install() + assert env.apache_restart() == 0 + url = env.mkurl("http", "test1", "/extfiltertest/hello.txt.crap") + r = env.curl_get(url) + assert r.response, "no response" + assert r.response["status"] == 200 + body = r.response["body"].decode() + assert "crap not allowed during worktime" in body, \ + f"line containing 'crap' should survive the filter: {body!r}" + assert "recycling can save the earth" not in body, \ + f"line without 'crap' should be removed by grep: {body!r}" diff --git a/test/modules/filters/test_006_mod_substitute.py b/test/modules/filters/test_006_mod_substitute.py new file mode 100644 index 00000000000..f8479967404 --- /dev/null +++ b/test/modules/filters/test_006_mod_substitute.py @@ -0,0 +1,56 @@ +import os + +import pytest + +from pyhttpd.conf import HttpdConf + +# Basic mod_substitute body-replacement tests, ported from +# noe-tests ModSubstituteHttpdTest.groovy. + + +class TestModSubstitute: + + @pytest.fixture(autouse=True, scope='class') + def _class_scope(self, env): + doc_dir = os.path.join(env.server_dir, "htdocs", "test1") + hello_dir = os.path.join(doc_dir, "hello") + os.makedirs(hello_dir, exist_ok=True) + with open(os.path.join(hello_dir, "world.txt"), "w") as f: + f.write("Hello World!") + + # With a Substitute directive active, "World" is replaced by "Folks". + def test_mod_substitute_006_01_with_substitute_directive(self, env): + conf = HttpdConf(env, extras={ + f"test1.{env.http_tld}": """ + + AddOutputFilter SUBSTITUTE txt + Substitute "s/World/Folks/" + + """, + }) + conf.add_vhost_test1() + conf.install() + assert env.apache_restart() == 0 + url = env.mkurl("http", "test1", "/hello/world.txt") + r = env.curl_get(url) + assert r.response, "no response" + assert r.response["status"] == 200 + body = r.response["body"].decode() + assert "Hello Folks!" in body, f"expected substitution, got: {body!r}" + assert "Hello World!" not in body, \ + f"original text should not appear after substitution: {body!r}" + + # Without the Substitute directive the file is served unmodified. + def test_mod_substitute_006_02_no_substitute_directive(self, env): + conf = HttpdConf(env, extras={ + f"test1.{env.http_tld}": "", + }) + conf.add_vhost_test1() + conf.install() + assert env.apache_restart() == 0 + url = env.mkurl("http", "test1", "/hello/world.txt") + r = env.curl_get(url) + assert r.response, "no response" + assert r.response["status"] == 200 + body = r.response["body"].decode() + assert body == "Hello World!", f"expected original content, got: {body!r}" diff --git a/test/modules/filters/test_004_brotli_compression.py b/test/modules/filters/test_007_brotli_compression.py similarity index 93% rename from test/modules/filters/test_004_brotli_compression.py rename to test/modules/filters/test_007_brotli_compression.py index 022bda1d64c..ee3ea9951e7 100644 --- a/test/modules/filters/test_004_brotli_compression.py +++ b/test/modules/filters/test_007_brotli_compression.py @@ -23,7 +23,7 @@ def _class_scope(cls, env): # Request a small text file with Accept-Encoding: br. # Verify Content-Encoding: br header is present. - def test_brotli_004_01_br_header_present(self, env): + def test_brotli_007_01_br_header_present(self, env): url = env.mkurl("http", "test1", "/greeting.txt") r = env.curl_get(url, options=[ "-H", "Accept-Encoding: br", @@ -39,7 +39,7 @@ def test_brotli_004_01_br_header_present(self, env): "body should be compressed, not plain text" # Request a larger text file; verify brotli compression is applied. - def test_brotli_004_02_large_file(self, env): + def test_brotli_007_02_large_file(self, env): url = env.mkurl("http", "test1", "/lorem.txt") r = env.curl_get(url, options=[ "-H", "Accept-Encoding: br", @@ -56,7 +56,7 @@ def test_brotli_004_02_large_file(self, env): # When the client does not advertise Accept-Encoding: br the response # must be served uncompressed -- no Content-Encoding header. - def test_brotli_004_03_response_uncompressd(self, env): + def test_brotli_007_03_response_uncompressd(self, env): url = env.mkurl("http", "test1", "/greeting.txt") r = env.curl_get(url) assert r.response, "no response" @@ -68,7 +68,7 @@ def test_brotli_004_03_response_uncompressd(self, env): assert body.strip() == "Good morning, fellow!" # Binary / non-matching content types must not be compressed. - def test_brotli_004_04_binary_no_match_no_compression(self, env): + def test_brotli_007_04_binary_no_match_no_compression(self, env): bin_dir = os.path.join(env.server_dir, "htdocs", "test1") bin_path = os.path.join(bin_dir, "pixel.png") with open(bin_path, "wb") as f: diff --git a/test/modules/filters/test_008_mod_include.py b/test/modules/filters/test_008_mod_include.py new file mode 100644 index 00000000000..9c908e670e5 --- /dev/null +++ b/test/modules/filters/test_008_mod_include.py @@ -0,0 +1,43 @@ +import gzip +import os +import sys + +import pytest + +from pyhttpd.conf import HttpdConf + +class TestModIncludeFilters: + + @pytest.fixture(autouse=True, scope='class') + def _class_scope(self, env): + doc_dir = os.path.join(env.server_dir, "htdocs", "test1") + + ssi_dir = os.path.join(doc_dir, "includestest") + os.makedirs(ssi_dir, exist_ok=True) + with open(os.path.join(ssi_dir, "hello.shtml"), "w") as f: + f.write(' Hello from hello.shtml\n') + + def test_mod_include_008_01(self, env): + """Server-Side Includes expand DOCUMENT_NAME.""" + conf = HttpdConf(env, extras={ + f"test1.{env.http_tld}": """ + + Options +Includes + AddOutputFilter INCLUDES .shtml + + """, + }) + conf.add_vhost_test1() + conf.install() + assert env.apache_restart() == 0 + url = env.mkurl("http", "test1", "/includestest/hello.shtml") + r = env.curl_get(url) + assert r.response, "no response" + assert r.response["status"] == 200 + body = r.response["body"].decode() + assert "