From: Cameron Dale Date: Tue, 11 Dec 2007 07:27:18 +0000 (-0800) Subject: Add a unittest to the HTTPDownloader. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=ece8236e48546e81c2468bc6c56d4d3278787b7f;p=quix0rs-apt-p2p.git Add a unittest to the HTTPDownloader. --- 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()