From 8a42d5fb41cbca27f64ced4b8f087fb1001fac23 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Fri, 18 Oct 2013 18:34:29 +0100 Subject: [PATCH 1/3] Use builtin_str for all auto-set header values. --- requests/models.py | 6 +++--- test_requests.py | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/requests/models.py b/requests/models.py index ee2ca9e2..c3c9a10c 100644 --- a/requests/models.py +++ b/requests/models.py @@ -407,7 +407,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): raise NotImplementedError('Streamed bodies and files are mutually exclusive.') if length is not None: - self.headers['Content-Length'] = str(length) + self.headers['Content-Length'] = builtin_str(length) else: self.headers['Transfer-Encoding'] = 'chunked' else: @@ -433,12 +433,12 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): def prepare_content_length(self, body): if hasattr(body, 'seek') and hasattr(body, 'tell'): body.seek(0, 2) - self.headers['Content-Length'] = str(body.tell()) + self.headers['Content-Length'] = builtin_str(body.tell()) body.seek(0, 0) elif body is not None: l = super_len(body) if l: - self.headers['Content-Length'] = str(l) + self.headers['Content-Length'] = builtin_str(l) elif self.method not in ('GET', 'HEAD'): self.headers['Content-Length'] = '0' diff --git a/test_requests.py b/test_requests.py index 18fb10ed..57260275 100755 --- a/test_requests.py +++ b/test_requests.py @@ -684,6 +684,14 @@ class RequestsTestCase(unittest.TestCase): self.assertTrue('multipart/form-data' in p.headers['Content-Type']) + def test_autoset_header_values_are_native(self): + data = 'this is a string' + length = '16' + req = requests.Request('POST', httpbin('post'), data=data) + p = req.prepare() + + self.assertEqual(p.headers['Content-Length'], length) + class TestContentEncodingDetection(unittest.TestCase): From 333ac101baffd01c6eca1c4c61b1d620e4d8ca70 Mon Sep 17 00:00:00 2001 From: Kevin Burke Date: Sun, 20 Oct 2013 00:46:41 -0700 Subject: [PATCH 2/3] Add clarification to MissingSchema error --- requests/models.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requests/models.py b/requests/models.py index ee2ca9e2..c836c609 100644 --- a/requests/models.py +++ b/requests/models.py @@ -324,7 +324,8 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): scheme, auth, host, port, path, query, fragment = parse_url(url) if not scheme: - raise MissingSchema("Invalid URL %r: No schema supplied" % url) + raise MissingSchema("Invalid URL %r: No schema supplied. " + "Perhaps you meant http://%s?" % (url, url)) if not host: raise InvalidURL("Invalid URL %r: No host supplied" % url) From b5d2160ef8e5eb0b163a69e33f5deb31ecfabd32 Mon Sep 17 00:00:00 2001 From: Kevin Burke Date: Sun, 20 Oct 2013 10:17:54 -0700 Subject: [PATCH 3/3] use .format() --- requests/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/requests/models.py b/requests/models.py index c836c609..8a06430e 100644 --- a/requests/models.py +++ b/requests/models.py @@ -324,8 +324,8 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): scheme, auth, host, port, path, query, fragment = parse_url(url) if not scheme: - raise MissingSchema("Invalid URL %r: No schema supplied. " - "Perhaps you meant http://%s?" % (url, url)) + raise MissingSchema("Invalid URL {0!r}: No schema supplied. " + "Perhaps you meant http://{0}?".format(url)) if not host: raise InvalidURL("Invalid URL %r: No host supplied" % url)