From cd1b4927facbe9c3c6335fc51d931673e510ca0d Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Tue, 13 Sep 2016 16:18:37 +0100 Subject: [PATCH] Correctly handler a url argument to interceptor with no port Fixes #41 Without this the split fails when there is no : in netloc. --- test/test_interceptor.py | 13 +++++++++++++ wsgi_intercept/interceptor.py | 6 +++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/test/test_interceptor.py b/test/test_interceptor.py index 05c10e4..ba52de5 100644 --- a/test/test_interceptor.py +++ b/test/test_interceptor.py @@ -45,6 +45,19 @@ def test_interceptor_instance(): assert interceptor.url == 'http://%s:%s/foobar' % (hostname, port) +def test_intercept_by_url_no_port(): + # Test for https://github.com/cdent/wsgi-intercept/issues/41 + hostname = str(uuid4()) + url = 'http://%s/foobar' % hostname + interceptor = Httplib2Interceptor(app=app, url=url) + assert isinstance(interceptor, Interceptor) + assert interceptor.app == app + assert interceptor.host == hostname + assert interceptor.port == 80 + assert interceptor.script_name == '/foobar' + assert interceptor.url == url + + # http_lib def test_httpclient_interceptor_host(): diff --git a/wsgi_intercept/interceptor.py b/wsgi_intercept/interceptor.py index 3daea37..eac0d57 100644 --- a/wsgi_intercept/interceptor.py +++ b/wsgi_intercept/interceptor.py @@ -66,8 +66,12 @@ class Interceptor(object): None, None)) def _init_from_url(self, url): + port = None parsed_url = urlparse.urlsplit(url) - host, port = parsed_url.netloc.split(':') + if ':' in parsed_url.netloc: + host, port = parsed_url.netloc.split(':') + else: + host = parsed_url.netloc if not port: if parsed_url.scheme == 'https': port = 443