diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index 34a06e5d..7f3c98a9 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -88,6 +88,11 @@ class _HTTPConnection(object): def _on_connect(self, parsed): if "Host" not in self.request.headers: self.request.headers["Host"] = parsed.netloc + if self.request.auth_username: + auth = "%s:%s" % (self.request.auth_username, + self.request.auth_password) + self.request.headers["Authorization"] = ("Basic %s" % + auth.encode("base64")) has_body = self.request.method in ("POST", "PUT") if has_body: assert self.request.body is not None diff --git a/tornado/test/simple_httpclient_test.py b/tornado/test/simple_httpclient_test.py index a30ea8fa..1bece2a3 100644 --- a/tornado/test/simple_httpclient_test.py +++ b/tornado/test/simple_httpclient_test.py @@ -21,12 +21,17 @@ class ChunkHandler(RequestHandler): self.flush() self.write("qwer") +class AuthHandler(RequestHandler): + def get(self): + self.finish(self.request.headers["Authorization"]) + class SimpleHTTPClientTestCase(AsyncHTTPTestCase, LogTrapTestCase): def get_app(self): return Application([ ("/hello", HelloWorldHandler), ("/post", PostHandler), ("/chunk", ChunkHandler), + ("/auth", AuthHandler), ]) def setUp(self): @@ -67,3 +72,8 @@ class SimpleHTTPClientTestCase(AsyncHTTPTestCase, LogTrapTestCase): streaming_callback=chunks.append) self.assertEqual(chunks, ["asdf", "qwer"]) self.assertFalse(response.body) + + def test_basic_auth(self): + self.assertEqual(self.fetch("/auth", auth_username="Aladdin", + auth_password="open sesame").body, + "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==")