diff --git a/deluge/tests/common.py b/deluge/tests/common.py index 4c9f1769e..84722c560 100644 --- a/deluge/tests/common.py +++ b/deluge/tests/common.py @@ -1,3 +1,5 @@ +import os + import tempfile import deluge.configmanager @@ -10,6 +12,9 @@ def set_tmp_config_dir(): deluge.configmanager.set_config_dir(config_directory) return config_directory +def rpath(*args): + return os.path.join(os.path.dirname(__file__), *args) + import gettext import locale import pkg_resources diff --git a/deluge/tests/test_core.py b/deluge/tests/test_core.py index f56a95772..57991681c 100644 --- a/deluge/tests/test_core.py +++ b/deluge/tests/test_core.py @@ -1,5 +1,10 @@ from twisted.trial import unittest +from twisted.internet import reactor from twisted.python.failure import Failure +from twisted.web.http import FORBIDDEN +from twisted.web.resource import Resource +from twisted.web.server import Site +from twisted.web.static import File try: from hashlib import sha1 as sha @@ -8,19 +13,62 @@ except ImportError: import os import common +rpath = common.rpath from deluge.core.rpcserver import RPCServer from deluge.core.core import Core +from deluge.ui.web.common import compress import deluge.component as component import deluge.error +class TestCookieResource(Resource): + + def render(self, request): + if request.getCookie("password") != "deluge": + request.setResponseCode(FORBIDDEN) + return + + request.setHeader("Content-Type", "application/x-bittorrent") + return open(rpath("ubuntu-9.04-desktop-i386.iso.torrent")).read() + +class TestPartialDownload(Resource): + + def render(self, request): + data = open(rpath("ubuntu-9.04-desktop-i386.iso.torrent")).read() + request.setHeader("Content-Type", len(data)) + request.setHeader("Content-Type", "application/x-bittorrent") + if request.requestHeaders.hasHeader("accept-encoding"): + return compress(data, request) + return data + +class TestRedirectResource(Resource): + + def render(self, request): + request.redirect("/ubuntu-9.04-desktop-i386.iso.torrent") + return "" + +class TopLevelResource(Resource): + + addSlash = True + + def __init__(self): + Resource.__init__(self) + self.putChild("cookie", TestCookieResource()) + self.putChild("partial", TestPartialDownload()) + self.putChild("redirect", TestRedirectResource()) + self.putChild("ubuntu-9.04-desktop-i386.iso.torrent", File(common.rpath("ubuntu-9.04-desktop-i386.iso.torrent"))) + class CoreTestCase(unittest.TestCase): def setUp(self): common.set_tmp_config_dir() self.rpcserver = RPCServer(listen=False) self.core = Core() - d = component.start() - return d + return component.start().addCallback(self.startWebserver) + + def startWebserver(self, result): + self.website = Site(TopLevelResource()) + self.webserver = reactor.listenTCP(51242, self.website) + return result def tearDown(self): @@ -28,6 +76,7 @@ class CoreTestCase(unittest.TestCase): component._ComponentRegistry.components = {} del self.rpcserver del self.core + return self.webserver.stopListening() return component.shutdown().addCallback(on_shutdown) @@ -44,7 +93,7 @@ class CoreTestCase(unittest.TestCase): self.assertEquals(torrent_id, info_hash) def test_add_torrent_url(self): - url = "http://deluge-torrent.org/ubuntu-9.04-desktop-i386.iso.torrent" + url = "http://localhost:51242/ubuntu-9.04-desktop-i386.iso.torrent" options = {} info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00" @@ -53,7 +102,7 @@ class CoreTestCase(unittest.TestCase): return d def test_add_torrent_url_with_cookie(self): - url = "http://deluge-torrent.org/test_torrent.php?test=cookie" + url = "http://localhost:51242/cookie" options = {} headers = { "Cookie" : "password=deluge" } info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00" @@ -67,7 +116,7 @@ class CoreTestCase(unittest.TestCase): return d def test_add_torrent_url_with_redirect(self): - url = "http://deluge-torrent.org/test_torrent.php?test=redirect" + url = "http://localhost:51242/redirect" options = {} info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00" @@ -77,7 +126,7 @@ class CoreTestCase(unittest.TestCase): return d def test_add_torrent_url_with_partial_download(self): - url = "http://deluge-torrent.org/test_torrent.php?test=partial" + url = "http://localhost:51242/partial" options = {} info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00" diff --git a/deluge/tests/test_httpdownloader.py b/deluge/tests/test_httpdownloader.py index edcbeccdb..7715cc6bf 100644 --- a/deluge/tests/test_httpdownloader.py +++ b/deluge/tests/test_httpdownloader.py @@ -13,8 +13,8 @@ from deluge.ui.web.common import compress from email.utils import formatdate -def rpath(*paths): - return os.path.join(os.path.dirname(__file__), *paths) +import common +rpath = common.rpath class TestRedirectResource(Resource): @@ -26,7 +26,7 @@ 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=" + + request.setHeader("Content-Disposition", "attachment; filename=" filename) return "This file should be called " + filename diff --git a/deluge/tests/ubuntu-9.04-desktop-i386.iso.torrent b/deluge/tests/ubuntu-9.04-desktop-i386.iso.torrent new file mode 100644 index 000000000..b55c9aec1 Binary files /dev/null and b/deluge/tests/ubuntu-9.04-desktop-i386.iso.torrent differ