This patch adds support for supplying custom request and response classes. As part of this work, a new "context" attribute was added to Request, and its type may be overridden by setting the "context_type" class attribute in a custom Request class. The alternative to using a class attribute would have been to pass the context type into the api, and then each time a class was instantiated, pass the type into it's initializer. After discussion on IRC, it was decided that using a class attribute would be more elegant. See commentary at: https://github.com/racker/falcon/pull/256 Closes #256 and #248
34 lines
959 B
Python
34 lines
959 B
Python
import falcon.testing as testing
|
|
|
|
from falcon.request import Request
|
|
|
|
|
|
class TestRequestContext(testing.TestBase):
|
|
|
|
def test_default_request_context(self):
|
|
env = testing.create_environ()
|
|
req = Request(env)
|
|
self.assertIsInstance(req.context, dict)
|
|
|
|
def test_custom_request_context(self):
|
|
|
|
# Define a Request-alike with a custom context type
|
|
class MyCustomContextType():
|
|
pass
|
|
|
|
class MyCustomRequest(Request):
|
|
context_type = MyCustomContextType
|
|
|
|
env = testing.create_environ()
|
|
req = MyCustomRequest(env)
|
|
self.assertIsInstance(req.context, MyCustomContextType)
|
|
|
|
def test_custom_request_context_failure(self):
|
|
|
|
# Define a Request-alike with a non-callable custom context type
|
|
class MyCustomRequest(Request):
|
|
context_type = False
|
|
|
|
env = testing.create_environ()
|
|
self.assertRaises(TypeError, MyCustomRequest, env)
|