From 9bbe837258deb9fe36024d7e3cf7b3197b40ce79 Mon Sep 17 00:00:00 2001 From: Paul Lodge Date: Wed, 7 Oct 2026 12:43:47 +0200 Subject: [PATCH 1/3] Add mod_substitute, ext_filter, SSI, and deflate tests Fix filters test: remove FilesMatch inside Location (invalid nesting) Co-Authored-By: Claude Opus 4.6 --- test/modules/filters/env.py | 4 +- test/modules/filters/test_004_substitute.py | 56 +++++++ test/modules/filters/test_005_misc_filters.py | 146 ++++++++++++++++++ 3 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 test/modules/filters/test_004_substitute.py create mode 100644 test/modules/filters/test_005_misc_filters.py 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_004_substitute.py b/test/modules/filters/test_004_substitute.py new file mode 100644 index 00000000000..edbea4ce592 --- /dev/null +++ b/test/modules/filters/test_004_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 TestSubstitute: + + @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_filters_004_01(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_filters_004_02(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_005_misc_filters.py b/test/modules/filters/test_005_misc_filters.py new file mode 100644 index 00000000000..b816840dd61 --- /dev/null +++ b/test/modules/filters/test_005_misc_filters.py @@ -0,0 +1,146 @@ +import gzip +import os +import sys + +import pytest + +from pyhttpd.conf import HttpdConf + +# Miscellaneous filter tests ported from noe-tests FiltersHttpdTest.groovy: +# mod_ext_filter, mod_include (SSI), and mod_deflate with FilterChain. + + +class TestMiscFilters: + + @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") + + # --- SSI test data --- + 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') + + # --- deflate / FilterChain test data --- + gz_dir = os.path.join(doc_dir, "filtertest") + os.makedirs(gz_dir, exist_ok=True) + gz_path = os.path.join(gz_dir, "hello.txt.gz") + with gzip.open(gz_path, "wb") as f: + f.write(b"Good morning, fellow!") + + # --- mod_ext_filter --------------------------------------------------- + + @pytest.mark.skipif(sys.platform == "win32", + reason="/usr/bin/grep is not available on Windows") + def test_filters_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}" + + # --- mod_include (SSI) ------------------------------------------------ + + def test_filters_005_02(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 " Hello from hello.shtml\n') - - # --- deflate / FilterChain test data --- - gz_dir = os.path.join(doc_dir, "filtertest") - os.makedirs(gz_dir, exist_ok=True) - gz_path = os.path.join(gz_dir, "hello.txt.gz") - with gzip.open(gz_path, "wb") as f: - f.write(b"Good morning, fellow!") - - # --- mod_ext_filter --------------------------------------------------- - - @pytest.mark.skipif(sys.platform == "win32", - reason="/usr/bin/grep is not available on Windows") - def test_filters_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}" - - # --- mod_include (SSI) ------------------------------------------------ - - def test_filters_005_02(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 " 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 "