tests&small update

This commit is contained in:
Anton Zhyrney 2017-01-07 07:13:49 +02:00
parent 1317b1799c
commit 47a4f34cdf
3 changed files with 43 additions and 2 deletions

View File

@ -48,7 +48,7 @@ If you want to add decorator for class, you could set decorators variable
``` ```
class ViewWithDecorator(HTTPMethodView): class ViewWithDecorator(HTTPMethodView):
decorators = (some_decorator_here) decorators = [some_decorator_here]
def get(self, request, name): def get(self, request, name):
return text('Hello I have a decorator') return text('Hello I have a decorator')

View File

@ -32,7 +32,7 @@ class HTTPMethodView:
To add any decorator you could set it into decorators variable To add any decorator you could set it into decorators variable
""" """
decorators = () decorators = []
def dispatch_request(self, request, *args, **kwargs): def dispatch_request(self, request, *args, **kwargs):
handler = getattr(self, request.method.lower(), None) handler = getattr(self, request.method.lower(), None)

View File

@ -153,3 +153,44 @@ def test_with_middleware_response():
assert type(results[0]) is Request assert type(results[0]) is Request
assert type(results[1]) is Request assert type(results[1]) is Request
assert issubclass(type(results[2]), HTTPResponse) assert issubclass(type(results[2]), HTTPResponse)
def test_with_custom_class_methods():
app = Sanic('test_with_custom_class_methods')
class DummyView(HTTPMethodView):
global_var = 0
def _iternal_method(self):
self.global_var += 10
def get(self, request):
self._iternal_method()
return text('I am get method and global var is {}'.format(self.global_var))
app.add_route(DummyView.as_view(), '/')
request, response = sanic_endpoint_test(app, method="get")
assert response.text == 'I am get method and global var is 10'
def test_with_decorator():
app = Sanic('test_with_decorator')
results = []
def stupid_decorator(view):
def decorator(*args, **kwargs):
results.append(1)
return view(*args, **kwargs)
return decorator
class DummyView(HTTPMethodView):
decorators = [stupid_decorator]
def get(self, request):
return text('I am get method')
app.add_route(DummyView.as_view(), '/')
request, response = sanic_endpoint_test(app, method="get", debug=True)
assert response.text == 'I am get method'
assert results[0] == 1