From ece8236e48546e81c2468bc6c56d4d3278787b7f Mon Sep 17 00:00:00 2001 From: Cameron Dale Date: Mon, 10 Dec 2007 23:27:18 -0800 Subject: [PATCH] Add a unittest to the HTTPDownloader. --- HTTPDownloader.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/HTTPDownloader.py b/HTTPDownloader.py index 7d96444..e894c8d 100644 --- a/HTTPDownloader.py +++ b/HTTPDownloader.py @@ -1,8 +1,13 @@ +import unittest + from twisted.web2.client import http from twisted.internet.defer import Deferred +from twisted.internet.protocol import ClientFactory +from twisted.web2.client.interfaces import IHTTPClientManager +from zope.interface import implements -class HTTPClientManager(object): +class HTTPClientManager(ClientFactory): """A manager for all HTTP requests to a site. @@ -10,6 +15,8 @@ class HTTPClientManager(object): implements(IHTTPClientManager) + protocol = HTTPClientProtocol + def __init__(self, host, port): self.host = host self.port = port @@ -20,7 +27,7 @@ class HTTPClientManager(object): self.pending_requests = [] def get(self, path): - uri = 'http://' + self.host + ':' + self.port + path + uri = 'http://' + self.host + ':' + str(self.port) + path request = http.ClientRequest('GET', uri, {}, None) request.responseDefer = Deferred() self.pending_requests.append(request) @@ -57,3 +64,17 @@ class HTTPClientManager(object): del self.client if self.pending_requests: self._submitRequest() + +class TestDownloader(unittest.TestCase): + + def test_download(self): + h = HTTPClientManager('www.google.ca', 80) + def print_resutls(result): + print result + + d = h.get('/index.html') + d.addCallback(print_results) + reactor.run() + +if __name__ == '__main__': + unittest.main() -- 2.39.5