Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions test/modules/rewrite/env.py → test/modules/mappers/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
64 changes: 0 additions & 64 deletions test/modules/rewrite/test_001_redirect.py

This file was deleted.

Loading