]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Check types of HTTPClient response objects and make them consistent
authorBen Darnell <ben@bendarnell.com>
Mon, 30 May 2011 01:12:16 +0000 (18:12 -0700)
committerBen Darnell <ben@bendarnell.com>
Mon, 30 May 2011 01:12:16 +0000 (18:12 -0700)
tornado/httpclient.py
tornado/simple_httpclient.py
tornado/test/httpclient_test.py

index af77f777dd3d7a442d428dfa03b161033ae93c19..ac3e7f7ff6806919aed23da65fc495e56d994fb4 100644 (file)
@@ -168,12 +168,12 @@ class HTTPRequest(object):
         self.proxy_port = proxy_port
         self.proxy_username = proxy_username
         self.proxy_password = proxy_password
-        self.url = utf8(url)
+        self.url = url
         self.method = method
         self.headers = headers
         self.body = utf8(body)
-        self.auth_username = utf8(auth_username)
-        self.auth_password = utf8(auth_password)
+        self.auth_username = auth_username
+        self.auth_password = auth_password
         self.connect_timeout = connect_timeout
         self.request_timeout = request_timeout
         self.follow_redirects = follow_redirects
index 030d2640ade0d5881217fc2db665f9537ca53372..d5d2e8fdfe6e5c7b99953292aa1f8652e8ccb2dd 100644 (file)
@@ -1,7 +1,7 @@
 #!/usr/bin/env python
 from __future__ import with_statement
 
-from tornado.escape import utf8, _unicode
+from tornado.escape import utf8, _unicode, native_str
 from tornado.httpclient import HTTPRequest, HTTPResponse, HTTPError, AsyncHTTPClient
 from tornado.httputil import HTTPHeaders
 from tornado.ioloop import IOLoop
@@ -273,7 +273,7 @@ class _HTTPConnection(object):
                                   error=HTTPError(599, "Connection closed")))
 
     def _on_headers(self, data):
-        data = data.decode("latin1")
+        data = native_str(data.decode("latin1"))
         first_line, _, header_data = data.partition("\r\n")
         match = re.match("HTTP/1.[01] ([0-9]+)", first_line)
         assert match
@@ -318,7 +318,7 @@ class _HTTPConnection(object):
             self.code in (301, 302)):
             new_request = copy.copy(self.request)
             new_request.url = urlparse.urljoin(self.request.url,
-                                               utf8(self.headers["Location"]))
+                                               self.headers["Location"])
             new_request.max_redirects -= 1
             del new_request.headers["Host"]
             new_request.original_request = original_request
index 964ccd50370fce7d7fc4791e2b9861f95e0057d8..a3c49980620042ac34bdbfa180efd918c45f8731 100644 (file)
@@ -12,7 +12,7 @@ from contextlib import closing
 from tornado.escape import utf8
 from tornado.httpclient import AsyncHTTPClient
 from tornado.testing import AsyncHTTPTestCase, LogTrapTestCase, get_unused_port
-from tornado.util import b
+from tornado.util import b, bytes_type
 from tornado.web import Application, RequestHandler, asynchronous, url
 
 class HelloWorldHandler(RequestHandler):
@@ -159,7 +159,7 @@ class HTTPClientCommonTestCase(AsyncHTTPTestCase, LogTrapTestCase):
 
         response = self.fetch("/countdown/2")
         self.assertEqual(200, response.code)
-        self.assertTrue(response.effective_url.endswith(b("/countdown/0")))
+        self.assertTrue(response.effective_url.endswith("/countdown/0"))
         self.assertEqual(b("Zero"), response.body)
 
     def test_max_redirects(self):
@@ -167,8 +167,8 @@ class HTTPClientCommonTestCase(AsyncHTTPTestCase, LogTrapTestCase):
         self.assertEqual(302, response.code)
         # We requested 5, followed three redirects for 4, 3, 2, then the last
         # unfollowed redirect is to 1.
-        self.assertTrue(response.request.url.endswith(b("/countdown/5")))
-        self.assertTrue(response.effective_url.endswith(b("/countdown/2")))
+        self.assertTrue(response.request.url.endswith("/countdown/5"))
+        self.assertTrue(response.effective_url.endswith("/countdown/2"))
         self.assertTrue(response.headers["Location"].endswith("/countdown/1"))
 
     def test_credentials_in_url(self):
@@ -214,3 +214,10 @@ class HTTPClientCommonTestCase(AsyncHTTPTestCase, LogTrapTestCase):
         self.http_client.fetch(url, self.stop, allow_ipv6=True)
         response = self.wait()
         self.assertEqual(response.body, b("Hello world!"))
+
+    def test_types(self):
+        response = self.fetch("/hello")
+        self.assertEqual(type(response.body), bytes_type)
+        self.assertEqual(type(response.headers["Content-Type"]), str)
+        self.assertEqual(type(response.code), int)
+        self.assertEqual(type(response.effective_url), str)