]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
tests/no_network: move GET into the GridTestMixin class
authorBrian Warner <warner@allmydata.com>
Wed, 25 Feb 2009 01:33:00 +0000 (18:33 -0700)
committerBrian Warner <warner@allmydata.com>
Wed, 25 Feb 2009 01:33:00 +0000 (18:33 -0700)
src/allmydata/test/common_web.py
src/allmydata/test/no_network.py
src/allmydata/test/test_web.py

index 770e346294e03a0f63b2826e9929cecc6a1cbc31..477daa00c0b2bf5a07972effdb7ae6bb5d3fafbb 100644 (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
index 79233d4f1dc4f967a3cb11658bfca86d7ca6b37d..421bd2a02c5bc795a32406b22c328820f04747f5 100644 (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
index 99705d7192e1a0b0aecd94530c47fe4919f0c90b..a5b28bd4538768a87f733d4ce890dd541fba78ce 100644 (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