Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_bp_combined_limit(self):
app, limiter = self.build_app(config={}, global_limits=['1/day'])
bp = Blueprint('/bp')
limiter.limit('1 per hour')(bp)
@bp.route("/bp1")
@limiter.limit('2 per hour')
async def bp_t1(request):
return text("bp_t1")
app.blueprint(bp)
cli = app.test_client
self.assertEqual(200, cli.get("/bp1")[1].status)
self.assertEqual(200, cli.get("/bp1")[1].status)
self.assertEqual(429, cli.get("/bp1")[1].status)
# demo start
from sanic import Sanic, Blueprint
from sanic.response import text
from sanic_limiter import Limiter, get_remote_address
app = Sanic(__name__)
limiter = Limiter(app, global_limits=['1 per hour', '10 per day'], key_func=get_remote_address)
bp = Blueprint('some_bp')
limiter.limit("2 per hour")(bp)
@bp.route("/bp1")
async def bp_t1(request):
return text("bp_t1")
@app.route("/t1")
@limiter.limit("100 per hour;10/minute")
async def t1(request):
return text("t1")
@app.route("/t2")
async def t2(request):
async def test(request, commit):
global RUNNING
if RUNNING:
raise InvalidUsage('Please wait for the current test to finish')
try:
RUNNING = True
run_data = await git.test_commit(commit)
finally:
RUNNING = False
return response.json(run_data)
async def handler(request):
return text("OK")
async def post(request, id):
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)
async def handler(request):
return text("OK")
async def foo_root_handler(request):
return text("OK") # noqa
def handler(request):
response = text("pass")
response.cookies["test"] = "pass"
response.cookies["test"]["expires"] = expires
return response
def get_resource_hander(request, resource_id):
resource = {"resource_id": resource_id}
return json(resource)
def setUp(self):
self.app = Sanic(__name__)
@self.app.route('/defaults', methods=['GET', 'POST', 'HEAD', 'OPTIONS'])
@cross_origin(self.app)
def defaults(request):
return text('Should only return headers on pre-flight OPTIONS request')
@self.app.route('/test_methods_defined', methods=['POST', 'OPTIONS'])
@cross_origin(self.app, methods=['POST'])
def test_get(request):
return text('Only allow POST')