Correctly set wsgi.url_scheme when using https

Fixes #26. Thanks to @Vultaire for the patch.

When a HTTPSConnection is used, make sure the faked socket is aware so
the environ can be properly set.
This commit is contained in:
Chris Dent
2014-11-09 18:06:32 +00:00
parent 5da3198cc5
commit 4afa5f3f50
5 changed files with 29 additions and 2 deletions

View File

@@ -36,6 +36,9 @@ def test_http_other_port():
assert content == b'WSGI intercept successful!\n'
assert app.success()
environ = app.get_internals()
assert environ['wsgi.url_scheme'] == 'http'
def test_bogus_domain():
with InstalledApp(wsgi_app.simple_app, host=HOST, port=80):
@@ -57,6 +60,9 @@ def test_https_default_port():
resp, content = http.request('https://some_hopefully_nonexistant_domain/')
assert app.success()
environ = app.get_internals()
assert environ['wsgi.url_scheme'] == 'https'
def test_app_error():
with InstalledApp(wsgi_app.raises_app, host=HOST, port=80):

View File

@@ -29,6 +29,8 @@ def test_http_other_port():
resp = requests.get('http://some_hopefully_nonexistant_domain:8080/')
assert resp.content == b'WSGI intercept successful!\n'
assert app.success()
environ = app.get_internals()
assert environ['wsgi.url_scheme'] == 'http'
def test_bogus_domain():
@@ -50,6 +52,8 @@ def test_https_default_port():
resp = requests.get('https://some_hopefully_nonexistant_domain/')
assert resp.content == b'WSGI intercept successful!\n'
assert app.success()
environ = app.get_internals()
assert environ['wsgi.url_scheme'] == 'https'
def test_app_error():

View File

@@ -28,6 +28,8 @@ def test_http_other_port():
with InstalledApp(wsgi_app.simple_app, host=HOST, port=8080) as app:
url_lib.urlopen('http://some_hopefully_nonexistant_domain:8080/')
assert app.success()
environ = app.get_internals()
assert environ['wsgi.url_scheme'] == 'http'
def test_https():
@@ -40,6 +42,8 @@ def test_https_default_port():
with InstalledApp(wsgi_app.simple_app, host=HOST, port=443) as app:
url_lib.urlopen('https://some_hopefully_nonexistant_domain/')
assert app.success()
environ = app.get_internals()
assert environ['wsgi.url_scheme'] == 'https'
def test_app_error():

View File

@@ -26,6 +26,16 @@ def test_simple_override_default_port():
assert app.success()
def test_https_in_environ():
with InstalledApp(wsgi_app.simple_app, host=HOST, port=443) as app:
http = httplib2.Http()
resp, content = http.request(
'https://some_hopefully_nonexistant_domain/', 'GET')
assert app.success()
internal_env = app.get_internals()
assert internal_env['wsgi.url_scheme'] == 'https'
def test_more_interesting():
expected_uri = '/%E4%B8%96%E4%B8%8A%E5%8E%9F%E4%BE%86%E9%82%84%E6%9C%89%E3%80%8C%E7%BE%9A%E7%89%9B%E3%80%8D%E9%80%99%E7%A8%AE%E5%8B%95%E7%89%A9%EF%BC%81%2Fbarney?bar=baz%20zoom'
with InstalledApp(wsgi_app.more_interesting_app, host=HOST) as app:

View File

@@ -336,7 +336,7 @@ class wsgi_fake_socket:
data has been sent to the socket by the request class;
2. non-persistent (i.e. non-HTTP/1.1) connections.
"""
def __init__(self, app, host, port, script_name):
def __init__(self, app, host, port, script_name, https=False):
self.app = app # WSGI app object
self.host = host
self.port = port
@@ -346,6 +346,7 @@ class wsgi_fake_socket:
self.write_results = [] # results from the 'write_fn'
self.results = None # results from running the app
self.output = BytesIO() # all output from the app, incl headers
self.https = https
def makefile(self, *args, **kwargs):
"""
@@ -392,6 +393,8 @@ class wsgi_fake_socket:
# build the environ dictionary.
environ = make_environ(inp, self.host, self.port, self.script_name)
if self.https:
environ['wsgi.url_scheme'] = 'https'
# run the application.
try:
@@ -549,7 +552,7 @@ class WSGI_HTTPSConnection(HTTPSConnection, WSGI_HTTPConnection):
sys.stderr.write('INTERCEPTING call to %s:%s\n' %
(self.host, self.port,))
self.sock = wsgi_fake_socket(app, self.host, self.port,
script_name)
script_name, https=True)
else:
HTTPSConnection.connect(self)