Add a context property to the Response class, to mirror the context property that is already available on the Request class.
40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
from falcon import Response
|
|
import falcon.testing as testing
|
|
|
|
|
|
class TestRequestContext(testing.TestBase):
|
|
|
|
def test_default_response_context(self):
|
|
resp = Response()
|
|
self.assertIsInstance(resp.context, dict)
|
|
|
|
def test_custom_response_context(self):
|
|
|
|
class MyCustomContextType(object):
|
|
pass
|
|
|
|
class MyCustomResponse(Response):
|
|
context_type = MyCustomContextType
|
|
|
|
resp = MyCustomResponse()
|
|
self.assertIsInstance(resp.context, MyCustomContextType)
|
|
|
|
def test_custom_response_context_failure(self):
|
|
|
|
class MyCustomResponse(Response):
|
|
context_type = False
|
|
|
|
self.assertRaises(TypeError, MyCustomResponse)
|
|
|
|
def test_custom_response_context_factory(self):
|
|
|
|
def create_context(resp):
|
|
return {'resp': resp}
|
|
|
|
class MyCustomResponse(Response):
|
|
context_type = create_context
|
|
|
|
resp = MyCustomResponse()
|
|
self.assertIsInstance(resp.context, dict)
|
|
self.assertIs(resp.context['resp'], resp)
|