Add basic auth support to SimpleAsyncHTTPClient

This commit is contained in:
Ben Darnell 2010-10-21 14:58:30 -07:00
parent 83d623a1c5
commit 3d19e10355
2 changed files with 15 additions and 0 deletions

View File

@ -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

View File

@ -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==")