webish: improve test coverage of WebDownloadTarget

This commit is contained in:
Brian Warner 2007-07-17 12:16:45 -07:00
parent 2aff1ccd44
commit 3197e1d39b
2 changed files with 30 additions and 4 deletions

View File

@ -4,7 +4,7 @@ from zope.interface import implements
from twisted.application import service
from twisted.trial import unittest
from twisted.internet import defer
from twisted.web import client, error
from twisted.web import client, error, http
from twisted.python import failure, log
from allmydata import webish, interfaces, dirnode, uri
from allmydata.encode import NotEnoughPeersError
@ -186,6 +186,9 @@ class Web(unittest.TestCase):
baz_file = self.makefile(2)
sub.children["baz.txt"] = baz_file
self._bad_file_uri = self.makefile(3)
del self.files[self._bad_file_uri]
rodir = self.makedir()
rodir._mutable = False
v.public_root.children["readonly"] = rodir.get_uri()
@ -334,6 +337,24 @@ class Web(unittest.TestCase):
self.fail("%s was supposed to Error(404), not get '%s'" %
(which, res))
def shouldHTTPError(self, res, which, code=None, substring=None,
response_substring=None):
if isinstance(res, failure.Failure):
res.trap(error.Error)
if code is not None:
self.failUnlessEqual(res.value.status, str(code))
if substring:
self.failUnless(substring in str(res),
"substring '%s' not in '%s'"
% (substring, str(res)))
if response_substring:
self.failUnless(response_substring in res.value.response,
"respose substring '%s' not in '%s'"
% (response_substring, res.value.response))
else:
self.fail("%s was supposed to Error(%s), not get '%s'" %
(which, code, res))
def test_create(self): # YES
pass
@ -956,9 +977,14 @@ class Web(unittest.TestCase):
return d
def test_GET_URI_URL_missing(self): # YES
base = "/uri/missing?t=json"
base = "/uri/%s" % self._bad_file_uri.replace("/","!")
d = self.GET(base)
d.addBoth(self.should404, "test_GET_URI_URL_missing")
d.addBoth(self.shouldHTTPError, "test_GET_URI_URL_missing",
http.GONE, response_substring="NotEnoughPeersError")
# TODO: how can we exercise both sides of WebDownloadTarget.fail
# here? we must arrange for a download to fail after target.open()
# has been called, and then inspect the response to see that it is
# shorter than we expected.
return d
def test_PUT_NEWFILEURL_uri(self): # YES

View File

@ -237,7 +237,7 @@ class WebDownloadTarget:
# error message.
msg = str(why.type)
msg.replace("\n", "|")
self._req.setResponseCode(http.INTERNAL_SERVER_ERROR, msg)
self._req.setResponseCode(http.GONE, msg)
self._req.setHeader("content-type", "text/plain")
# TODO: HTML-formatted exception?
self._req.write(str(why))