tests/no_network: move GET into the GridTestMixin class

This commit is contained in:
Brian Warner 2009-02-24 18:33:00 -07:00
parent 6f981e4eab
commit 2e7f64d392
3 changed files with 43 additions and 37 deletions

View File

@ -1,6 +1,7 @@
import re
from twisted.internet import defer
from twisted.web import client
from nevow.testutil import FakeRequest
from nevow import inevow, context
@ -58,3 +59,24 @@ class WebRenderingMixin:
s = re.sub(r'\s+', ' ', s)
return s
class MyGetter(client.HTTPPageGetter):
handleStatus_206 = lambda self: self.handleStatus_200()
class HTTPClientHEADFactory(client.HTTPClientFactory):
protocol = MyGetter
def noPage(self, reason):
# Twisted-2.5.0 and earlier had a bug, in which they would raise an
# exception when the response to a HEAD request had no body (when in
# fact they are defined to never have a body). This was fixed in
# Twisted-8.0 . To work around this, we catch the
# PartialDownloadError and make it disappear.
if (reason.check(client.PartialDownloadError)
and self.method.upper() == "HEAD"):
self.page("")
return
return client.HTTPClientFactory.noPage(self, reason)
class HTTPClientGETFactory(client.HTTPClientFactory):
protocol = MyGetter

View File

@ -16,6 +16,7 @@
import os.path
import sha
from twisted.application import service
from twisted.internet import reactor
from foolscap import Referenceable
from foolscap.eventual import fireEventually
from base64 import b32encode
@ -24,6 +25,7 @@ from allmydata.client import Client
from allmydata.storage.server import StorageServer, storage_index_to_dir
from allmydata.util import fileutil, idlib, hashutil, rrefutil
from allmydata.introducer.client import RemoteServiceConnector
from allmydata.test.common_web import HTTPClientGETFactory
class IntentionalError(Exception):
pass
@ -283,3 +285,19 @@ class GridTestMixin:
sharedata = open(i_sharefile, "rb").read()
corruptdata = corruptor(sharedata)
open(i_sharefile, "wb").write(corruptdata)
def GET(self, urlpath, followRedirect=False, return_response=False,
method="GET", clientnum=0, **kwargs):
# if return_response=True, this fires with (data, statuscode,
# respheaders) instead of just data.
assert not isinstance(urlpath, unicode)
url = self.client_baseurls[clientnum] + urlpath
factory = HTTPClientGETFactory(url, method=method,
followRedirect=followRedirect, **kwargs)
reactor.connectTCP("localhost", self.client_webports[clientnum],factory)
d = factory.deferred
def _got_data(data):
return (data, factory.status, factory.response_headers)
if return_response:
d.addCallback(_got_data)
return factory.deferred

View File

@ -23,6 +23,9 @@ from allmydata.mutable.common import UnrecoverableFileError
import common_util as testutil
from allmydata.test.no_network import GridTestMixin
from allmydata.test.common_web import HTTPClientGETFactory, \
HTTPClientHEADFactory
# create a fake uploader/downloader, and a couple of fake dirnodes, then
# create a webserver that works against them
@ -115,27 +118,6 @@ class FakeClient(service.MultiService):
def list_all_helper_statuses(self):
return []
class MyGetter(client.HTTPPageGetter):
handleStatus_206 = lambda self: self.handleStatus_200()
class HTTPClientHEADFactory(client.HTTPClientFactory):
protocol = MyGetter
def noPage(self, reason):
# Twisted-2.5.0 and earlier had a bug, in which they would raise an
# exception when the response to a HEAD request had no body (when in
# fact they are defined to never have a body). This was fixed in
# Twisted-8.0 . To work around this, we catch the
# PartialDownloadError and make it disappear.
if (reason.check(client.PartialDownloadError)
and self.method.upper() == "HEAD"):
self.page("")
return
return client.HTTPClientFactory.noPage(self, reason)
class HTTPClientGETFactory(client.HTTPClientFactory):
protocol = MyGetter
class WebMixin(object):
def setUp(self):
self.s = FakeClient()
@ -2546,22 +2528,6 @@ class Util(unittest.TestCase):
class Grid(GridTestMixin, WebErrorMixin, unittest.TestCase, ShouldFailMixin):
def GET(self, urlpath, followRedirect=False, return_response=False,
method="GET", clientnum=0, **kwargs):
# if return_response=True, this fires with (data, statuscode,
# respheaders) instead of just data.
assert not isinstance(urlpath, unicode)
url = self.client_baseurls[clientnum] + urlpath
factory = HTTPClientGETFactory(url, method=method,
followRedirect=followRedirect, **kwargs)
reactor.connectTCP("localhost", self.client_webports[clientnum],factory)
d = factory.deferred
def _got_data(data):
return (data, factory.status, factory.response_headers)
if return_response:
d.addCallback(_got_data)
return factory.deferred
def CHECK(self, ign, which, args, clientnum=0):
fileurl = self.fileurls[which]
url = fileurl + "?" + args