From: Brian Warner Date: Wed, 25 Feb 2009 01:33:00 +0000 (-0700) Subject: tests/no_network: move GET into the GridTestMixin class X-Git-Url: https://git.rkrishnan.org/listings/using.html?a=commitdiff_plain;h=2e7f64d39288e6b32cb6b1a648161d60f4df4476;p=tahoe-lafs%2Ftahoe-lafs.git tests/no_network: move GET into the GridTestMixin class --- diff --git a/src/allmydata/test/common_web.py b/src/allmydata/test/common_web.py index 770e3462..477daa00 100644 --- a/src/allmydata/test/common_web.py +++ b/src/allmydata/test/common_web.py @@ -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 diff --git a/src/allmydata/test/no_network.py b/src/allmydata/test/no_network.py index 79233d4f..421bd2a0 100644 --- a/src/allmydata/test/no_network.py +++ b/src/allmydata/test/no_network.py @@ -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 diff --git a/src/allmydata/test/test_web.py b/src/allmydata/test/test_web.py index 99705d71..a5b28bd4 100644 --- a/src/allmydata/test/test_web.py +++ b/src/allmydata/test/test_web.py @@ -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