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
121 changes: 92 additions & 29 deletions test/modules/proxy/test_03_uwsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,80 @@

from pyhttpd.conf import HttpdConf
from .uwsgi_faker import UwsgiFaker
from .env import TCPFaker


UWSGI_PORT = 5200

class _UWSGIFaker(TCPFaker):

@staticmethod
def hello(data):
body = b"Hello"
return (
b"HTTP/1.1 200 OK\r\n"
b"Content-Type: text/plain\r\n"
b"Content-Length: 5\r\n"
b"\r\n"
+ body
)

@pytest.fixture(autouse=True, scope="class")
def _proxy_uwsgi_setup(env):
faker = UwsgiFaker(port=UWSGI_PORT)
faker.start()

conf = HttpdConf(env)
conf.start_vhost(
domains=[env.d_reverse],
port=env.http_port,
with_ssl=False,
)
conf.add([
"LogLevel proxy_uwsgi:debug",
f"ProxyPass / uwsgi://127.0.0.1:{UWSGI_PORT}/",
"LogLevel trace4",
])
conf.end_vhost()
conf.install()

assert env.apache_restart() == 0

yield

faker.stop()

@staticmethod
def empty_header(data):
body = b"Hello"
return (
b"HTTP/1.1 200 OK\r\n"
b"X-Empty:\r\n"
b"Content-Length: 5\r\n"
b"\r\n"
+ body
)

class TestProxyUwsgi:

def test_proxy_03_001(self, env):
@pytest.fixture(scope="function")
def _proxy_uwsgi_setup(self, env):
faker = UwsgiFaker(port=UWSGI_PORT)
faker.start()

conf = HttpdConf(env)
conf.start_vhost(
domains=[env.d_reverse],
port=env.http_port,
with_ssl=False,
)
conf.add([
"LogLevel proxy_uwsgi:debug",
f"ProxyPass / uwsgi://127.0.0.1:{UWSGI_PORT}/",
"LogLevel trace4",
])
conf.end_vhost()
conf.install()

assert env.apache_restart() == 0

yield

faker.stop()

@pytest.fixture(scope='function')
def _class_scope(self, env):
if not env.has_shared_module("proxy_uwsgi"):
pytest.skip("mod_proxy_uwsgi not available")
faker = _UWSGIFaker("127.0.0.1", env.http_port2)
faker.start()
conf = HttpdConf(env)
conf.start_vhost(domains=[f"test1.{env.http_tld}"], port=env.http_port)
conf.add([
f"ProxyPass / uwsgi://127.0.0.1:{env.http_port2}/",
])
conf.end_vhost()
conf.install()
assert env.apache_restart() == 0
yield faker
faker.stop()

def test_proxy_03_001(self, env, _proxy_uwsgi_setup):
"""uWSGI backend response is forwarded correctly."""
r = env.curl_get(
f"http://{env.d_reverse}:{env.http_port}/",
Expand All @@ -53,7 +93,7 @@ def test_proxy_03_001(self, env):
assert r.response["status"] == 200
assert r.json["host"] == "uwsgi-faker"

def test_proxy_03_002(self, env):
def test_proxy_03_002(self, env, _proxy_uwsgi_setup):
"""No Transfer-Encoding header reaches the client."""
r = env.curl_get(
f"http://{env.d_reverse}:{env.http_port}/",
Expand All @@ -62,7 +102,7 @@ def test_proxy_03_002(self, env):
assert r.response["status"] == 200
assert "transfer-encoding" not in r.response["header"]

def test_proxy_03_003(self, env):
def test_proxy_03_003(self, env, _proxy_uwsgi_setup):
"""Transfer-Encoding is stripped by mod_proxy_uwsgi."""
r = env.curl_get(
f"http://{env.d_reverse}:{env.http_port}/?te=1",
Expand All @@ -85,4 +125,27 @@ def test_proxy_03_003(self, env):
re.compile(
r".*uwsgi: removing hop-by-hop header 'Transfer-Encoding'"
)
)
)

# verify uwsgi request header
def test_proxy_003_04(self, env, _class_scope):
_class_scope._make_response = _UWSGIFaker.hello
r = env.curl_get(env.mkurl("http", "test1", "/"))
assert r.response["status"] == 200
assert r.response["body"] == b"Hello"

data = _class_scope._request

assert data[0] == 0x00 # standard WSGI request
datasize = data[1] + (data[2] * 256) # read from 16bit little-endian
assert data[3] == 0x00 # standard WSGI request
assert len(data) == 4 + datasize

# empty backend response header values are valid
def test_proxy_003_05(self, env, _class_scope):
_class_scope._make_response = _UWSGIFaker.empty_header
r = env.curl_get(env.mkurl("http", "test1", "/"))
assert r.response["status"] == 200
assert "x-empty" in r.response["header"]
assert r.response["body"] == b"Hello"

72 changes: 0 additions & 72 deletions test/modules/proxy/test_05_uwsgi.py

This file was deleted.