From f30a2858ce35439c1d2bb36bf9d65fa417e8d568 Mon Sep 17 00:00:00 2001 From: Damien Churchill Date: Tue, 8 Mar 2011 14:43:09 +0000 Subject: [PATCH] convert the tests to use a local webserver instead of pinging deluge-torrent.org and damoxc.net --- deluge/tests/test_httpdownloader.py | 94 +++++++++++++++++++++++++---- 1 file changed, 81 insertions(+), 13 deletions(-) diff --git a/deluge/tests/test_httpdownloader.py b/deluge/tests/test_httpdownloader.py index 54b4c053d..edcbeccdb 100644 --- a/deluge/tests/test_httpdownloader.py +++ b/deluge/tests/test_httpdownloader.py @@ -1,17 +1,85 @@ +import os + from twisted.trial import unittest +from twisted.internet import reactor from twisted.python.failure import Failure +from twisted.web.http import FORBIDDEN, NOT_MODIFIED +from twisted.web.resource import Resource, ForbiddenResource +from twisted.web.server import Site from deluge.httpdownloader import download_file from deluge.log import setupLogger +from deluge.ui.web.common import compress from email.utils import formatdate +def rpath(*paths): + return os.path.join(os.path.dirname(__file__), *paths) + +class TestRedirectResource(Resource): + + def render(self, request): + request.redirect("http://localhost:51242/") + +class TestRenameResource(Resource): + + def render(self, request): + filename = request.args.get("filename", ["renamed_file"])[0] + request.setHeader("Content-Type", "text/plain") + request.setHeader("Content-Disposition", "attachment; filename=" + + filename) + return "This file should be called " + filename + +class TestCookieResource(Resource): + + def render(self, request): + request.setHeader("Content-Type", "text/plain") + if request.getCookie("password") is None: + return "Password cookie not set!" + + if request.getCookie("password") == "deluge": + return "COOKIE MONSTER!" + + return request.getCookie("password") + +class TestGzipResource(Resource): + + def render(self, request): + message = request.args.get("msg", ["EFFICIENCY!"])[0] + request.setHeader("Content-Type", "text/plain") + return compress(message, request) + +class TopLevelResource(Resource): + + addSlash = True + + def __init__(self): + Resource.__init__(self) + self.putChild("cookie", TestCookieResource()) + self.putChild("gzip", TestGzipResource()) + self.putChild("redirect", TestRedirectResource()) + self.putChild("rename", TestRenameResource()) + + def getChild(self, path, request): + if path == "": + return self + else: + return Resource.getChild(self, path, request) + + def render(self, request): + if request.getHeader("If-Modified-Since"): + request.setResponseCode(NOT_MODIFIED) + return "

Deluge HTTP Downloader tests webserver here

" + class DownloadFileTestCase(unittest.TestCase): + def setUp(self): setupLogger("warning", "log_file") + self.website = Site(TopLevelResource()) + self.webserver = reactor.listenTCP(51242, self.website) def tearDown(self): - pass + return self.webserver.stopListening() def assertContains(self, filename, contents): f = open(filename) @@ -34,19 +102,19 @@ class DownloadFileTestCase(unittest.TestCase): return filename def test_download(self): - d = download_file("http://deluge-torrent.org", "index.html") + d = download_file("http://localhost:51242/", "index.html") d.addCallback(self.assertEqual, "index.html") return d def test_download_without_required_cookies(self): - url = "http://damoxc.net/deluge/httpdownloader.php?test=cookie" + url = "http://localhost:51242/cookie" d = download_file(url, "none") d.addCallback(self.fail) d.addErrback(self.assertIsInstance, Failure) return d def test_download_with_required_cookies(self): - url = "http://damoxc.net/deluge/httpdownloader.php?test=cookie" + url = "http://localhost:51242/cookie" cookie = { "cookie" : "password=deluge" } d = download_file(url, "monster", headers=cookie) d.addCallback(self.assertEqual, "monster") @@ -54,61 +122,61 @@ class DownloadFileTestCase(unittest.TestCase): return d def test_download_with_rename(self): - url = "http://damoxc.net/deluge/httpdownloader.php?test=rename&filename=renamed" + url = "http://localhost:51242/rename?filename=renamed" d = download_file(url, "original") d.addCallback(self.assertEqual, "renamed") d.addCallback(self.assertContains, "This file should be called renamed") return d def test_download_with_rename_fail(self): - url = "http://damoxc.net/deluge/httpdownloader.php?test=rename&filename=renamed" + url = "http://localhost:51242/rename?filename=renamed" d = download_file(url, "original") d.addCallback(self.assertEqual, "original") d.addCallback(self.assertContains, "This file should be called renamed") return d def test_download_with_rename_sanitised(self): - url = "http://damoxc.net/deluge/httpdownloader.php?test=rename&filename=/etc/passwd" + url = "http://localhost:51242/rename?filename=/etc/passwd" d = download_file(url, "original") d.addCallback(self.assertEqual, "passwd") d.addCallback(self.assertContains, "This file should be called /etc/passwd") return d def test_download_with_rename_prevented(self): - url = "http://damoxc.net/deluge/httpdownloader.php?test=rename&filename=spam" + url = "http://localhost:51242/rename?filename=spam" d = download_file(url, "forced", force_filename=True) d.addCallback(self.assertEqual, "forced") d.addCallback(self.assertContains, "This file should be called spam") return d def test_download_with_gzip_encoding(self): - url = "http://damoxc.net/deluge/httpdownloader.php?test=gzip&msg=success" + url = "http://localhost:51242/gzip?msg=success" d = download_file(url, "gzip_encoded") d.addCallback(self.assertContains, "success") return d def test_download_with_gzip_encoding_disabled(self): - url = "http://damoxc.net/deluge/httpdownloader.php?test=gzip&msg=fail" + url = "http://localhost:51242/gzip?msg=fail" d = download_file(url, "gzip_encoded", allow_compression=False) d.addCallback(self.failIfContains, "fail") return d def test_page_redirect(self): - url = "http://damoxc.net/deluge/httpdownloader.php?test=redirect" + url = 'http://localhost:51242/redirect' d = download_file(url, "none") d.addCallback(self.fail) d.addErrback(self.assertIsInstance, Failure) return d def test_page_not_found(self): - d = download_file("http://does.not.exist", "none") + d = download_file("http://localhost:51242/page/not/found", "none") d.addCallback(self.fail) d.addErrback(self.assertIsInstance, Failure) return d def test_page_not_modified(self): headers = { 'If-Modified-Since' : formatdate(usegmt=True) } - d = download_file("http://deluge-torrent.org", "index.html", headers=headers) + d = download_file("http://localhost:51242/", "index.html", headers=headers) d.addCallback(self.fail) d.addErrback(self.assertIsInstance, Failure) return d