From: Ben Darnell Date: Mon, 30 May 2011 01:12:16 +0000 (-0700) Subject: Check types of HTTPClient response objects and make them consistent X-Git-Tag: v2.0.0~46 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e5265194d62ffe0d0fa9b2d0630a50eefc3584ac;p=thirdparty%2Ftornado.git Check types of HTTPClient response objects and make them consistent --- diff --git a/tornado/httpclient.py b/tornado/httpclient.py index af77f777d..ac3e7f7ff 100644 --- a/tornado/httpclient.py +++ b/tornado/httpclient.py @@ -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 diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index 030d2640a..d5d2e8fdf 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -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 diff --git a/tornado/test/httpclient_test.py b/tornado/test/httpclient_test.py index 964ccd503..a3c499806 100644 --- a/tornado/test/httpclient_test.py +++ b/tornado/test/httpclient_test.py @@ -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)