diff --git a/test/modules/rewrite/__init__.py b/test/modules/mappers/__init__.py similarity index 100% rename from test/modules/rewrite/__init__.py rename to test/modules/mappers/__init__.py diff --git a/test/modules/rewrite/conftest.py b/test/modules/mappers/conftest.py similarity index 79% rename from test/modules/rewrite/conftest.py rename to test/modules/mappers/conftest.py index 93e8cba9422..a3e5cf0f236 100644 --- a/test/modules/rewrite/conftest.py +++ b/test/modules/mappers/conftest.py @@ -4,20 +4,20 @@ import pytest -from .env import RewriteTestEnv +from .env import MappersTestEnv sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) def pytest_report_header(config, start_path): - env = RewriteTestEnv() + env = MappersTestEnv() return f"rewrite [apache: {env.get_httpd_version()}, mpm: {env.mpm_module}, {env.prefix}]" @pytest.fixture(scope="package") -def env(pytestconfig) -> RewriteTestEnv: +def env(pytestconfig) -> MappersTestEnv: logging.getLogger('').setLevel(level=logging.INFO) - env = RewriteTestEnv(pytestconfig=pytestconfig) + env = MappersTestEnv(pytestconfig=pytestconfig) env.setup_httpd() env.apache_access_log_clear() env.httpd_error_log.clear_log() diff --git a/test/modules/rewrite/env.py b/test/modules/mappers/env.py similarity index 68% rename from test/modules/rewrite/env.py rename to test/modules/mappers/env.py index cacd1556836..f76d3bcfa8f 100644 --- a/test/modules/rewrite/env.py +++ b/test/modules/mappers/env.py @@ -7,19 +7,19 @@ log = logging.getLogger(__name__) -class RewriteTestSetup(HttpdTestSetup): +class MappersTestSetup(HttpdTestSetup): def __init__(self, env: 'HttpdTestEnv'): super().__init__(env=env) - self.add_source_dir(os.path.dirname(inspect.getfile(RewriteTestSetup))) + self.add_source_dir(os.path.dirname(inspect.getfile(MappersTestSetup))) self.add_modules(["rewrite", "proxy", "proxy_http"]) -class RewriteTestEnv(HttpdTestEnv): +class MappersTestEnv(HttpdTestEnv): def __init__(self, pytestconfig=None): super().__init__(pytestconfig=pytestconfig) self.add_httpd_log_modules(["rewrite", "core"]) def setup_httpd(self, setup: HttpdTestSetup = None): - super().setup_httpd(setup=RewriteTestSetup(env=self)) + super().setup_httpd(setup=MappersTestSetup(env=self)) diff --git a/test/modules/rewrite/test_002_open_redirect.py b/test/modules/mappers/test_001_rewrite.py similarity index 57% rename from test/modules/rewrite/test_002_open_redirect.py rename to test/modules/mappers/test_001_rewrite.py index e59fc8fbc15..c9f0cffbcdd 100644 --- a/test/modules/rewrite/test_002_open_redirect.py +++ b/test/modules/mappers/test_001_rewrite.py @@ -6,7 +6,68 @@ from pyhttpd.conf import HttpdConf +class TestRewriteRedirect: + + @pytest.fixture(autouse=True, scope='class') + def _class_scope(self, env): + conf = HttpdConf(env) + # VirtualHost that strips "www." prefix and redirects + conf.start_vhost( + domains=[f"www.rewrite.{env.http_tld}"], + port=env.http_port, + doc_root="htdocs", + with_ssl=False, + ) + conf.add([ + "RewriteEngine On", + r"RewriteCond %{HTTP_HOST} ^www\.(.+)", + r"RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]", + ]) + conf.end_vhost() + # VirtualHost that prepends "www." and redirects + conf.start_vhost( + domains=[f"rewrite.{env.http_tld}"], + port=env.http_port, + doc_root="htdocs", + with_ssl=False, + ) + conf.add([ + "RewriteEngine On", + r"RewriteCond %{HTTP_HOST} !^www\.", + r"RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]", + ]) + conf.end_vhost() + conf.install() + assert env.apache_restart() == 0 + yield + + # Redirect www.domain -> non-www domain + def test_rewrite_001_01_domain_to_non_domain(self, env): + url = f"http://www.rewrite.{env.http_tld}:{env.http_port}/" + r = env.curl_raw(url, options=[]) + assert r.exit_code == 0, f"curl failed: {r}" + assert r.response["status"] == 301 + location = r.response["header"]["location"] + expected = f"http://rewrite.{env.http_tld}:{env.http_port}/" + assert location == expected, ( + f"expected redirect to '{expected}', got '{location}'" + ) + + # Redirect non-www domain -> www.domain + def test_rewrite_001_02_non_domain_to_domain(self, env): + url = f"http://rewrite.{env.http_tld}:{env.http_port}/" + r = env.curl_raw(url, options=[]) + assert r.exit_code == 0, f"curl failed: {r}" + assert r.response["status"] == 301 + location = r.response["header"]["location"] + expected = f"http://www.rewrite.{env.http_tld}:{env.http_port}/" + assert location == expected, ( + f"expected redirect to '{expected}', " + f"got '{location}'" + ) + class TestRewriteOpenRedirect: + @pytest.fixture(autouse=True, scope='class') def _class_scope(self, env): doc_dir = os.path.join(env.server_dir, "htdocs", "test1") diff --git a/test/modules/rewrite/test_001_redirect.py b/test/modules/rewrite/test_001_redirect.py deleted file mode 100644 index 2a9192bd71e..00000000000 --- a/test/modules/rewrite/test_001_redirect.py +++ /dev/null @@ -1,64 +0,0 @@ -import pytest - -from pyhttpd.conf import HttpdConf - - -class TestRewriteRedirect: - - @pytest.fixture(autouse=True, scope='class') - def _class_scope(self, env): - conf = HttpdConf(env) - # VirtualHost that strips "www." prefix and redirects - conf.start_vhost( - domains=[f"www.rewrite.{env.http_tld}"], - port=env.http_port, - doc_root="htdocs", - with_ssl=False, - ) - conf.add([ - "RewriteEngine On", - r"RewriteCond %{HTTP_HOST} ^www\.(.+)", - r"RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]", - ]) - conf.end_vhost() - # VirtualHost that prepends "www." and redirects - conf.start_vhost( - domains=[f"rewrite.{env.http_tld}"], - port=env.http_port, - doc_root="htdocs", - with_ssl=False, - ) - conf.add([ - "RewriteEngine On", - r"RewriteCond %{HTTP_HOST} !^www\.", - r"RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]", - ]) - conf.end_vhost() - conf.install() - assert env.apache_restart() == 0 - yield - - # Redirect www.domain -> non-www domain - def test_rewrite_001_01_domain_to_non_domain(self, env): - url = f"http://www.rewrite.{env.http_tld}:{env.http_port}/" - r = env.curl_raw(url, options=[]) - assert r.exit_code == 0, f"curl failed: {r}" - assert r.response["status"] == 301 - location = r.response["header"]["location"] - expected = f"http://rewrite.{env.http_tld}:{env.http_port}/" - assert location == expected, ( - f"expected redirect to '{expected}', got '{location}'" - ) - - # Redirect non-www domain -> www.domain - def test_rewrite_001_02_non_domain_to_domain(self, env): - url = f"http://rewrite.{env.http_tld}:{env.http_port}/" - r = env.curl_raw(url, options=[]) - assert r.exit_code == 0, f"curl failed: {r}" - assert r.response["status"] == 301 - location = r.response["header"]["location"] - expected = f"http://www.rewrite.{env.http_tld}:{env.http_port}/" - assert location == expected, ( - f"expected redirect to '{expected}', " - f"got '{location}'" - )