Commits

jason kirtland committed 6f8e7d5

- Reverted back to initializing CapturedResponse with environ
- Simplified contrib.response construction

  • Participants
  • Parent commits e3f892e

Comments (0)

Files changed (4)

File pegboard/base.py

 
     """
 
-    def __init__(self, peg):
+    def __init__(self, environ):
+        """Construct a CapturedResponse.
+
+        :param environ: the active environ for the owning Peg.
+
+        """
         self.status = None
         self.headers = None
         self.captured = False
         self.app = app
         self.environ = self.environ_copier(environ)
         self.environ['pegboard.peg'] = self
-        self.response = self.response_factory(self)
+        self.response = self.response_factory(self.environ)
         self.board = board
         self.parent = parent
         self.children = []

File pegboard/contrib/response.py

 
     Configuration and use example::
 
+      from pegboard.contrib.response import CapturedWerkzeugResponse
+
       class WerkPeg(pegboard.base.Peg):
-          response_factory = pegboard.contrib.response.CapturedWerkzeugResponse
+          response_factory = CapturedWerkzeugResponse.for_environ
 
       class MyApplication(pegboard.base.PegboardApplication):
          peg_factory = WerkPeg
 
     """
 
-    def __init__(self, peg, response=None, **kw):
+    def __init__(self, response=None, **kw):
         self.iterable = None
-        wrappers.BaseResponse.__init__(self, response, **kw)
-        if response is None:
-            # Werkzeug will convert None to an empty container via .response =
-            self.iterable = None
-            self.captured = False
-        else:
-            self.captured = True
+        wrappers.BaseResponse.__init__(self, response=response, **kw)
+        # base will blindly set .response to [] if response == None
+        self.captured = response is not None
+
+    @classmethod
+    def for_environ(cls, environ):
+        return cls()
 
     def set_response(self, value):
+        self.captured = value is not None
         if value is None:
-            self.iterable = None
+            self.iterable = []
         elif isinstance(value, basestring):
             self.iterable = [value]
         elif callable(value):
             self.iterable = value
+        elif isinstance(value, list) and value == []:
+            self.iterable = value
         else:
             self.iterable = iter(value)
 

File tests/contrib/test_response.py

     def setup(self):
         class WerkPeg(pegboard.base.Peg):
             response_factory = \
-              pegboard.contrib.response.CapturedWerkzeugResponse
+              pegboard.contrib.response.CapturedWerkzeugResponse.for_environ
 
         class App(pegboard.base.PegboardApplication):
             peg_factory = WerkPeg
         status, headers, response = call_as_wsgi(runnable)
 
         assert status == '200 OK'
-        factory = self.App.peg_factory.response_factory
+        specimen = self.App.peg_factory.response_factory({})
         mime_type = "%s; charset=%s" % (
-            factory.default_mimetype, factory.charset)
+            specimen.default_mimetype, specimen.charset)
         have_header_(headers, 'Content-Type', mime_type)
         have_header_(headers, 'Set-Cookie')
         assert response == 'Hello world!'

File tests/contrib/test_response_ext.py

     finally:
         del caller
 
-class BaseResponse(CapturedWerkzeugResponse):
-    """Bridges the pegboard implementation with Werkzeug's constructor."""
-    def __init__(self, response=None, **kw):
-        CapturedWerkzeugResponse.__init__(self, {}, **kw)
-        if response is not None:
-            self.response = response
-
+BaseResponse = CapturedWerkzeugResponse
 
 class Response(BaseResponse,
                wrappers.ETagResponseMixin,