Python sanic.views.HTTPMethodView() Examples
The following are 19
code examples of sanic.views.HTTPMethodView().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sanic.views
, or try the search function
.
Example #1
Source File: test_views.py From sanic with MIT License | 6 votes |
def test_with_middleware_response(app): results = [] @app.middleware("request") async def process_request(request): results.append(request) @app.middleware("response") async def process_response(request, response): results.append(request) results.append(response) class DummyView(HTTPMethodView): def get(self, request): return text("I am get method") app.add_route(DummyView.as_view(), "/") request, response = app.test_client.get("/") assert response.text == "I am get method" assert type(results[0]) is Request assert type(results[1]) is Request assert isinstance(results[2], HTTPResponse)
Example #2
Source File: test_views.py From sanic with MIT License | 6 votes |
def test_with_custom_class_methods(app): class DummyView(HTTPMethodView): global_var = 0 def _iternal_method(self): self.global_var += 10 def get(self, request): self._iternal_method() return text( f"I am get method and global var " f"is {self.global_var}" ) app.add_route(DummyView.as_view(), "/") request, response = app.test_client.get("/") assert response.text == "I am get method and global var is 10"
Example #3
Source File: test_views.py From sanic with MIT License | 6 votes |
def test_with_decorator(app): 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 = app.test_client.get("/") assert response.text == "I am get method" assert results[0] == 1
Example #4
Source File: decorators.py From sanic-jwt with MIT License | 6 votes |
def protected(initialized_on=None, **kw): def decorator(f): @wraps(f) async def decorated_function(request, *args, **kwargs): if issubclass(request.__class__, HTTPMethodView): request = args[0] kwargs.update( { "initialized_on": initialized_on, "kw": kw, "request": request, "f": f, } ) return await _do_protection(*args, **kwargs) return decorated_function return decorator
Example #5
Source File: test_request_stream.py From sanic with MIT License | 5 votes |
def test_request_stream_100_continue(app, headers, expect_raise_exception): class SimpleView(HTTPMethodView): @stream_decorator async def post(self, request): assert isinstance(request.stream, StreamBuffer) result = "" while True: body = await request.stream.read() if body is None: break result += body.decode("utf-8") return text(result) app.add_route(SimpleView.as_view(), "/method_view") assert app.is_request_stream is True if not expect_raise_exception: request, response = app.test_client.post( "/method_view", data=data, headers={"EXPECT": "100-continue"} ) assert response.status == 200 assert response.text == data else: with pytest.raises(ValueError) as e: app.test_client.post( "/method_view", data=data, headers={"EXPECT": "100-continue-extra"}, ) assert "Unknown Expect: 100-continue-extra" in str(e)
Example #6
Source File: test_views.py From annotated-py-projects with MIT License | 5 votes |
def test_with_middleware_response(): app = Sanic('test_with_middleware_response') results = [] @app.middleware('request') async def process_response(request): results.append(request) @app.middleware('response') async def process_response(request, response): results.append(request) results.append(response) class DummyView(HTTPMethodView): def get(self, request): return text('I am get method') app.add_route(DummyView(), '/') request, response = sanic_endpoint_test(app) assert response.text == 'I am get method' assert type(results[0]) is Request assert type(results[1]) is Request assert issubclass(type(results[2]), HTTPResponse)
Example #7
Source File: test_views.py From annotated-py-projects with MIT License | 5 votes |
def test_with_bp_with_url_prefix(): app = Sanic('test_with_bp_with_url_prefix') bp = Blueprint('test_text', url_prefix='/test1') class DummyView(HTTPMethodView): def get(self, request): return text('I am get method') bp.add_route(DummyView(), '/') app.blueprint(bp) request, response = sanic_endpoint_test(app, uri='/test1/') assert response.text == 'I am get method'
Example #8
Source File: test_views.py From annotated-py-projects with MIT License | 5 votes |
def test_with_bp(): app = Sanic('test_with_bp') bp = Blueprint('test_text') class DummyView(HTTPMethodView): def get(self, request): return text('I am get method') bp.add_route(DummyView(), '/') app.blueprint(bp) request, response = sanic_endpoint_test(app) assert response.text == 'I am get method'
Example #9
Source File: test_views.py From annotated-py-projects with MIT License | 5 votes |
def test_argument_methods(): app = Sanic('test_argument_methods') class DummyView(HTTPMethodView): def get(self, request, my_param_here): return text('I am get method with %s' % my_param_here) app.add_route(DummyView(), '/<my_param_here>') request, response = sanic_endpoint_test(app, uri='/test123') assert response.text == 'I am get method with test123'
Example #10
Source File: test_views.py From annotated-py-projects with MIT License | 5 votes |
def test_unexisting_methods(): app = Sanic('test_unexisting_methods') class DummyView(HTTPMethodView): def get(self, request): return text('I am get method') app.add_route(DummyView(), '/') request, response = sanic_endpoint_test(app, method="get") assert response.text == 'I am get method' request, response = sanic_endpoint_test(app, method="post") assert response.text == 'Error: Method POST not allowed for URL /'
Example #11
Source File: test_views.py From annotated-py-projects with MIT License | 5 votes |
def test_methods(): app = Sanic('test_methods') class DummyView(HTTPMethodView): def get(self, request): return text('I am get method') def post(self, request): return text('I am post method') def put(self, request): return text('I am put method') def patch(self, request): return text('I am patch method') def delete(self, request): return text('I am delete method') app.add_route(DummyView(), '/') request, response = sanic_endpoint_test(app, method="get") assert response.text == 'I am get method' request, response = sanic_endpoint_test(app, method="post") assert response.text == 'I am post method' request, response = sanic_endpoint_test(app, method="put") assert response.text == 'I am put method' request, response = sanic_endpoint_test(app, method="patch") assert response.text == 'I am patch method' request, response = sanic_endpoint_test(app, method="delete") assert response.text == 'I am delete method'
Example #12
Source File: decorators.py From sanic-jwt with MIT License | 5 votes |
def inject_user(initialized_on=None, **kw): def decorator(f): @wraps(f) async def decorated_function(request, *args, **kwargs): if issubclass(request.__class__, HTTPMethodView): request = args[0] if initialized_on and isinstance( initialized_on, Blueprint ): # noqa instance = initialized_on else: instance = request.app with instant_config(instance, request=request, **kw): if request.method == "OPTIONS": return await utils.call( f, request, *args, **kwargs ) # noqa payload = await instance.auth.extract_payload(request, verify=False) user = await utils.call( instance.auth.retrieve_user, request, payload ) response = f(request, user=user, *args, **kwargs) return await response return decorated_function return decorator
Example #13
Source File: test_initialize.py From sanic-jwt with MIT License | 5 votes |
def test_initialize_with_custom_endpoint_not_subclassed(): class SubclassHTTPMethodView(HTTPMethodView): async def options(self, request): return text("", status=204) async def get(self, request): return text("ok") app = Sanic("sanic-jwt-test") with pytest.raises(exceptions.InvalidClassViewsFormat): Initialize( app, authenticate=lambda: True, class_views=[("/subclass", SubclassHTTPMethodView)], )
Example #14
Source File: test_views.py From sanic with MIT License | 5 votes |
def test_methods(app, method): class DummyView(HTTPMethodView): async def get(self, request): assert request.stream is None return text("", headers={"method": "GET"}) def post(self, request): return text("", headers={"method": "POST"}) async def put(self, request): return text("", headers={"method": "PUT"}) def head(self, request): return text("", headers={"method": "HEAD"}) def options(self, request): return text("", headers={"method": "OPTIONS"}) async def patch(self, request): return text("", headers={"method": "PATCH"}) def delete(self, request): return text("", headers={"method": "DELETE"}) app.add_route(DummyView.as_view(), "/") assert app.is_request_stream is False request, response = getattr(app.test_client, method.lower())("/") assert response.headers["method"] == method
Example #15
Source File: test_request_stream.py From sanic with MIT License | 5 votes |
def test_request_stream_method_view(app): """for self.is_request_stream = True""" class SimpleView(HTTPMethodView): def get(self, request): assert request.stream is None return text("OK") @stream_decorator async def post(self, request): assert isinstance(request.stream, StreamBuffer) result = "" while True: body = await request.stream.read() if body is None: break result += body.decode("utf-8") return text(result) app.add_route(SimpleView.as_view(), "/method_view") assert app.is_request_stream is True request, response = app.test_client.get("/method_view") assert response.status == 200 assert response.text == "OK" request, response = app.test_client.post("/method_view", data=data) assert response.status == 200 assert response.text == data
Example #16
Source File: test_views.py From sanic with MIT License | 5 votes |
def test_with_bp_with_url_prefix(app): bp = Blueprint("test_text", url_prefix="/test1") class DummyView(HTTPMethodView): def get(self, request): return text("I am get method") bp.add_route(DummyView.as_view(), "/") app.blueprint(bp) request, response = app.test_client.get("/test1/") assert response.text == "I am get method"
Example #17
Source File: test_views.py From sanic with MIT License | 5 votes |
def test_with_bp(app): bp = Blueprint("test_text") class DummyView(HTTPMethodView): def get(self, request): assert request.stream is None return text("I am get method") bp.add_route(DummyView.as_view(), "/") app.blueprint(bp) request, response = app.test_client.get("/") assert app.is_request_stream is False assert response.text == "I am get method"
Example #18
Source File: test_views.py From sanic with MIT License | 5 votes |
def test_argument_methods(app): class DummyView(HTTPMethodView): def get(self, request, my_param_here): return text("I am get method with %s" % my_param_here) app.add_route(DummyView.as_view(), "/<my_param_here>") request, response = app.test_client.get("/test123") assert response.text == "I am get method with test123"
Example #19
Source File: test_views.py From sanic with MIT License | 5 votes |
def test_unexisting_methods(app): class DummyView(HTTPMethodView): def get(self, request): return text("I am get method") app.add_route(DummyView.as_view(), "/") request, response = app.test_client.get("/") assert response.text == "I am get method" request, response = app.test_client.post("/") assert "Method POST not allowed for URL /" in response.text