summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/contrib/test_fixers.py35
-rw-r--r--tests/test_wsgi.py66
2 files changed, 75 insertions, 26 deletions
diff --git a/tests/contrib/test_fixers.py b/tests/contrib/test_fixers.py
index 0ff91a93..a6a29956 100644
--- a/tests/contrib/test_fixers.py
+++ b/tests/contrib/test_fixers.py
@@ -158,6 +158,41 @@ class TestServerFixer(object):
assert wsgi_headers['Location'] == '{}/foo/bar.hml'.format(
assumed_host)
+ @pytest.mark.parametrize(('environ', 'remote_addr', 'url_root'), (
+ pytest.param({
+ 'REMOTE_ADDR': '192.168.0.1',
+ 'HTTP_HOST': 'spam',
+ }, '192.168.0.1', 'http://spam/', id='basic'),
+ ))
+ def test_proxy_fix_new(self, environ, remote_addr, url_root):
+ map = Map([Rule('/parrot', endpoint='parrot')])
+
+ @Request.application
+ def app(request):
+ assert request.remote_addr == remote_addr
+ assert request.url_root == url_root
+
+ urls = map.bind_to_environ(request.environ)
+ assert urls.build('parrot') == '/'.join((
+ request.script_root, 'parrot'))
+ assert urls.match('/parrot')[0] == 'parrot'
+
+ return Response('success')
+
+ app = fixers.ProxyFix(app)
+
+ environ = create_environ(environ_overrides=environ)
+ if 'HTTP_HOST' not in environ:
+ del environ['HTTP_HOST']
+
+ response = Response.from_app(app, environ)
+ assert response.get_data() == b'success'
+
+ redirect_app = redirect('/parrot')
+ response = Response.from_app(redirect_app, environ)
+ location = response.get_wsgi_headers(environ)['Location']
+ assert location == '/'.join(url_root, )
+
def test_proxy_fix_forwarded_prefix(self):
@fixers.ProxyFix
@Request.application
diff --git a/tests/test_wsgi.py b/tests/test_wsgi.py
index 4fd99ed1..1e3c5ba8 100644
--- a/tests/test_wsgi.py
+++ b/tests/test_wsgi.py
@@ -98,32 +98,46 @@ def test_dispatchermiddleware():
assert b''.join(app_iter).strip() == b'NOT FOUND'
-def test_get_host_by_http_host():
- env = {'HTTP_HOST': 'example.org', 'wsgi.url_scheme': 'http'}
- assert wsgi.get_host(env) == 'example.org'
- env['HTTP_HOST'] = 'example.org:8080'
- assert wsgi.get_host(env) == 'example.org:8080'
- env['HOST_NAME'] = 'ignore me'
- assert wsgi.get_host(env) == 'example.org:8080'
-
-
-def test_get_host_by_server_name_and_port():
- env = {'SERVER_NAME': 'example.org', 'SERVER_PORT': '80',
- 'wsgi.url_scheme': 'http'}
- assert wsgi.get_host(env) == 'example.org'
- env['wsgi.url_scheme'] = 'https'
- assert wsgi.get_host(env) == 'example.org:80'
- env['SERVER_PORT'] = '8080'
- assert wsgi.get_host(env) == 'example.org:8080'
- env['SERVER_PORT'] = '443'
- assert wsgi.get_host(env) == 'example.org'
-
-
-def test_get_host_ignore_x_forwarded_for():
- env = {'HTTP_X_FORWARDED_HOST': 'forwarded',
- 'HTTP_HOST': 'example.org',
- 'wsgi.url_scheme': 'http'}
- assert wsgi.get_host(env) == 'example.org'
+@pytest.mark.parametrize(('environ', 'expect'), (
+ pytest.param({
+ 'HTTP_HOST': 'spam',
+ }, 'spam', id='host'),
+ pytest.param({
+ 'HTTP_HOST': 'spam:80',
+ }, 'spam', id='host, strip http port'),
+ pytest.param({
+ 'wsgi.url_scheme': 'https',
+ 'HTTP_HOST': 'spam:443',
+ }, 'spam', id='host, strip https port'),
+ pytest.param({
+ 'HTTP_HOST': 'spam:8080',
+ }, 'spam:8080', id='host, custom port'),
+ pytest.param({
+ 'HTTP_HOST': 'spam',
+ 'SERVER_NAME': 'eggs',
+ 'SERVER_PORT': '80',
+ }, 'spam', id='prefer host'),
+ pytest.param({
+ 'SERVER_NAME': 'eggs',
+ 'SERVER_PORT': '80'
+ }, 'eggs', id='name, ignore http port'),
+ pytest.param({
+ 'wsgi.url_scheme': 'https',
+ 'SERVER_NAME': 'eggs',
+ 'SERVER_PORT': '443'
+ }, 'eggs', id='name, ignore https port'),
+ pytest.param({
+ 'SERVER_NAME': 'eggs',
+ 'SERVER_PORT': '8080'
+ }, 'eggs:8080', id='name, custom port'),
+ pytest.param({
+ 'HTTP_HOST': 'ham',
+ 'HTTP_X_FORWARDED_HOST': 'eggs'
+ }, 'ham', id='ignore x-forwarded-host'),
+))
+def test_get_host(environ, expect):
+ environ.setdefault('wsgi.url_scheme', 'http')
+ assert wsgi.get_host(environ) == expect
def test_get_host_validate_trusted_hosts():