From e08b853a0c001bda8215819c9cbcb7381e8c2c24 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Fri, 10 Aug 2012 17:24:00 +0100 Subject: [PATCH 1/2] Test for throwing useful exception on bad label. --- tests/test_requests.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_requests.py b/tests/test_requests.py index f43ccac8..3bbcfdf4 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -19,6 +19,7 @@ from requests.compat import str, StringIO from requests import HTTPError from requests import get, post, head, put from requests.auth import HTTPBasicAuth, HTTPDigestAuth +from requests.exceptions import InvalidURL if 'HTTPBIN_URL' not in os.environ: os.environ['HTTPBIN_URL'] = 'http://httpbin.org/' @@ -1062,6 +1063,10 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): """Test that `bytes` can be used as the values of `files`.""" post(httpbin('post'), files={'test': b'test'}) + def test_invalid_urls_throw_requests_exception(self): + """Test that URLs with invalid labels throw + Requests.exceptions.InvalidURL instead of UnicodeError.""" + self.assertRaises(InvalidURL, get, 'http://.google.com/') if __name__ == '__main__': unittest.main() From 79d53d3b8acf021203853e12e8fe0eeb0b742b51 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Fri, 10 Aug 2012 17:29:12 +0100 Subject: [PATCH 2/2] Throw InvalidURL not UnicodeError on bad label. --- requests/models.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/requests/models.py b/requests/models.py index 136427fe..ae3c1be1 100644 --- a/requests/models.py +++ b/requests/models.py @@ -413,7 +413,10 @@ class Request(object): if not scheme in SCHEMAS: raise InvalidSchema("Invalid scheme %r" % scheme) - netloc = netloc.encode('idna').decode('utf-8') + try: + netloc = netloc.encode('idna').decode('utf-8') + except UnicodeError: + raise InvalidURL('URL has an invalid label.') if not path: path = '/'