util:wsgi: WSGITestCase should not subclass WSGICaller.
authorW. Trevor King <wking@tremily.us>
Mon, 3 Sep 2012 17:54:07 +0000 (13:54 -0400)
committerW. Trevor King <wking@tremily.us>
Mon, 3 Sep 2012 17:54:07 +0000 (13:54 -0400)
The old implementation caused a number of unittest errors.

This commit adds a WSGICaller instance as WSGITestCase.caller, and
provides a wrapper .getURL method which copies the
status/headers/etc. from the caller back into the WSGITestCase.

libbe/util/wsgi.py

index 37d0e94fafca7174a71b06ffa85f83cac33d6898..136acc11755710e13c5e86d39a109595d64fb7cf 100644 (file)
@@ -725,16 +725,16 @@ class WSGICaller (object):
                 env['QUERY_STRING'] = data
         for key,value in environ.items():
             env[key] = value
-        return ''.join(app(env, self._start_response))
+        return ''.join(app(env, self.start_response))
 
-    def _start_response(self, status, response_headers, exc_info=None):
+    def start_response(self, status, response_headers, exc_info=None):
         self.status = status
         self.response_headers = response_headers
         self.exc_info = exc_info
 
 
 if libbe.TESTING:
-    class WSGITestCase (unittest.TestCase, WSGICaller):
+    class WSGITestCase (unittest.TestCase):
         def setUp(self):
             self.logstream = StringIO.StringIO()
             self.logger = logging.getLogger('be-wsgi-test')
@@ -744,7 +744,14 @@ if libbe.TESTING:
             self.logger.propagate = False
             console.setLevel(logging.INFO)
             self.logger.setLevel(logging.INFO)
+            self.caller = WSGICaller()
 
+        def getURL(self, *args, **kwargs):
+            content = self.caller.getURL(*args, **kwargs)
+            self.status = self.caller.status
+            self.response_headers = self.caller.response_headers
+            self.exc_info = self.caller.exc_info
+            return content
 
     class WSGI_ObjectTestCase (WSGITestCase):
         def setUp(self):
@@ -753,22 +760,23 @@ if libbe.TESTING:
 
         def test_error(self):
             contents = self.app.error(
-                environ=self.default_environ,
-                start_response=self.start_response,
+                environ=self.caller.default_environ,
+                start_response=self.caller.start_response,
                 error=123,
                 message='Dummy Error',
                 headers=[('X-Dummy-Header','Dummy Value')])
             self.failUnless(contents == ['Dummy Error'], contents)
-            self.failUnless(self.status == '123 Dummy Error', self.status)
-            self.failUnless(self.response_headers == [
+            self.failUnless(
+                self.caller.status == '123 Dummy Error', self.caller.status)
+            self.failUnless(self.caller.response_headers == [
                     ('Content-Type','text/plain'),
                     ('X-Dummy-Header','Dummy Value')],
-                            self.response_headers)
-            self.failUnless(self.exc_info == None, self.exc_info)
+                            self.caller.response_headers)
+            self.failUnless(self.caller.exc_info == None, self.caller.exc_info)
 
         def test_log_request(self):
             self.app.log_request(
-                environ=self.default_environ, status='-1 OK', bytes=123)
+                environ=self.caller.default_environ, status='-1 OK', bytes=123)
             log = self.logstream.getvalue()
             self.failUnless(log.startswith('192.168.0.123 -'), log)