Skip to content
Open
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
4 changes: 2 additions & 2 deletions test/modules/filters/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ 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()


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))
45 changes: 45 additions & 0 deletions test/modules/filters/test_005_mod_ext_filter.py
Original file line number Diff line number Diff line change
@@ -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"
<Location "/extfiltertest/">
SetOutputFilter grepfilter
</Location>
""",
})
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}"
56 changes: 56 additions & 0 deletions test/modules/filters/test_006_mod_substitute.py
Original file line number Diff line number Diff line change
@@ -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}": """
<Location "/hello/">
AddOutputFilter SUBSTITUTE txt
Substitute "s/World/Folks/"
</Location>
""",
})
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}"
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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"
Expand All @@ -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:
Expand Down
43 changes: 43 additions & 0 deletions test/modules/filters/test_008_mod_include.py
Original file line number Diff line number Diff line change
@@ -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('<!--#echo var="DOCUMENT_NAME" --> 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}": """
<Location "/includestest/">
Options +Includes
AddOutputFilter INCLUDES .shtml
</Location>
""",
})
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 "<!--#" not in body, \
f"SSI directive was not processed: {body!r}"
assert "Hello from hello.shtml" in body, \
f"SSI output missing expected text: {body!r}"


74 changes: 74 additions & 0 deletions test/modules/filters/test_009_mod_deflate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import gzip
import os
import sys

import pytest

from pyhttpd.conf import HttpdConf

class TestModDeflateFilters:

@pytest.fixture(autouse=True, scope='class')
def _class_scope(self, env):
doc_dir = os.path.join(env.server_dir, "htdocs", "test1")

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!")

def test_filters_009_01(self, env):
"""Without Accept-Encoding: gzip the INFLATE filter decompresses
the .gz file so the client receives plain text."""
conf = HttpdConf(env, extras={
f"test1.{env.http_tld}": """
<Location "/filtertest/">
AddEncoding x-gzip .gz
FilterDeclare uncompress
FilterProvider uncompress INFLATE "%{req:Accept-Encoding} !~ /gzip/"
FilterChain uncompress
</Location>
""",
})
conf.add_vhost_test1()
conf.install()
assert env.apache_restart() == 0
url = env.mkurl("http", "test1", "/filtertest/hello.txt.gz")
r = env.curl_get(url)
assert r.response, "no response"
assert r.response["status"] == 200
# The filter should have decompressed the content.
ce = r.response["header"].get("content-encoding")
assert ce is None or "gzip" not in ce, \
f"expected no gzip Content-Encoding, got: {ce}"
body = r.response["body"].decode()
assert "Good morning, fellow!" in body, \
f"expected decompressed body, got: {body!r}"

def test_filters_009_02(self, env):
"""With Accept-Encoding: gzip the INFLATE filter is bypassed and the
file is served compressed; curl --compressed decompresses it."""
conf = HttpdConf(env, extras={
f"test1.{env.http_tld}": """
<Location "/filtertest/">
AddEncoding x-gzip .gz
FilterDeclare uncompress
FilterProvider uncompress INFLATE "%{req:Accept-Encoding} !~ /gzip/"
FilterChain uncompress
</Location>
""",
})
conf.add_vhost_test1()
conf.install()
assert env.apache_restart() == 0
url = env.mkurl("http", "test1", "/filtertest/hello.txt.gz")
r = env.curl_get(url, options=[
"-H", "Accept-Encoding: gzip",
"--compressed",
])
assert r.response, "no response"
assert r.response["status"] == 200
body = r.response["body"].decode()
assert "Good morning, fellow!" in body, \
f"expected original content (decompressed by curl), got: {body!r}"
Loading