improve the core tests to use a built in webserver

This commit is contained in:
Damien Churchill 2011-04-20 18:32:55 +01:00
commit bcb636dda4
4 changed files with 63 additions and 9 deletions

View file

@ -1,3 +1,5 @@
import os
import tempfile import tempfile
import deluge.configmanager import deluge.configmanager
@ -10,6 +12,9 @@ def set_tmp_config_dir():
deluge.configmanager.set_config_dir(config_directory) deluge.configmanager.set_config_dir(config_directory)
return config_directory return config_directory
def rpath(*args):
return os.path.join(os.path.dirname(__file__), *args)
import gettext import gettext
import locale import locale
import pkg_resources import pkg_resources

View file

@ -1,5 +1,10 @@
from twisted.trial import unittest from twisted.trial import unittest
from twisted.internet import reactor
from twisted.python.failure import Failure 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: try:
from hashlib import sha1 as sha from hashlib import sha1 as sha
@ -8,19 +13,62 @@ except ImportError:
import os import os
import common import common
rpath = common.rpath
from deluge.core.rpcserver import RPCServer from deluge.core.rpcserver import RPCServer
from deluge.core.core import Core from deluge.core.core import Core
from deluge.ui.web.common import compress
import deluge.component as component import deluge.component as component
import deluge.error 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): class CoreTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
common.set_tmp_config_dir() common.set_tmp_config_dir()
self.rpcserver = RPCServer(listen=False) self.rpcserver = RPCServer(listen=False)
self.core = Core() self.core = Core()
d = component.start() return component.start().addCallback(self.startWebserver)
return d
def startWebserver(self, result):
self.website = Site(TopLevelResource())
self.webserver = reactor.listenTCP(51242, self.website)
return result
def tearDown(self): def tearDown(self):
@ -28,6 +76,7 @@ class CoreTestCase(unittest.TestCase):
component._ComponentRegistry.components = {} component._ComponentRegistry.components = {}
del self.rpcserver del self.rpcserver
del self.core del self.core
return self.webserver.stopListening()
return component.shutdown().addCallback(on_shutdown) return component.shutdown().addCallback(on_shutdown)
@ -44,7 +93,7 @@ class CoreTestCase(unittest.TestCase):
self.assertEquals(torrent_id, info_hash) self.assertEquals(torrent_id, info_hash)
def test_add_torrent_url(self): 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 = {} options = {}
info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00" info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00"
@ -53,7 +102,7 @@ class CoreTestCase(unittest.TestCase):
return d return d
def test_add_torrent_url_with_cookie(self): def test_add_torrent_url_with_cookie(self):
url = "http://deluge-torrent.org/test_torrent.php?test=cookie" url = "http://localhost:51242/cookie"
options = {} options = {}
headers = { "Cookie" : "password=deluge" } headers = { "Cookie" : "password=deluge" }
info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00" info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00"
@ -67,7 +116,7 @@ class CoreTestCase(unittest.TestCase):
return d return d
def test_add_torrent_url_with_redirect(self): def test_add_torrent_url_with_redirect(self):
url = "http://deluge-torrent.org/test_torrent.php?test=redirect" url = "http://localhost:51242/redirect"
options = {} options = {}
info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00" info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00"
@ -77,7 +126,7 @@ class CoreTestCase(unittest.TestCase):
return d return d
def test_add_torrent_url_with_partial_download(self): 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 = {} options = {}
info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00" info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00"

View file

@ -13,8 +13,8 @@ from deluge.ui.web.common import compress
from email.utils import formatdate from email.utils import formatdate
def rpath(*paths): import common
return os.path.join(os.path.dirname(__file__), *paths) rpath = common.rpath
class TestRedirectResource(Resource): class TestRedirectResource(Resource):
@ -26,7 +26,7 @@ class TestRenameResource(Resource):
def render(self, request): def render(self, request):
filename = request.args.get("filename", ["renamed_file"])[0] filename = request.args.get("filename", ["renamed_file"])[0]
request.setHeader("Content-Type", "text/plain") request.setHeader("Content-Type", "text/plain")
request.setHeader("Content-Disposition", "attachment; filename=" + request.setHeader("Content-Disposition", "attachment; filename="
filename) filename)
return "This file should be called " + filename return "This file should be called " + filename

Binary file not shown.