2016-12-04 01:50:32 +00:00
|
|
|
from sanic.exceptions import PayloadTooLarge
|
2017-03-28 10:50:09 +01:00
|
|
|
from sanic.response import text
|
2016-12-04 01:50:32 +00:00
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_payload_too_large_from_error_handler(app):
|
|
|
|
app.config.REQUEST_MAX_SIZE = 1
|
2016-12-04 01:50:32 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/1")
|
2017-03-29 04:38:45 +01:00
|
|
|
async def handler1(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2016-12-04 01:50:32 +00:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
@app.exception(PayloadTooLarge)
|
2017-03-29 04:38:45 +01:00
|
|
|
def handler_exception(request, exception):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("Payload Too Large from error_handler.", 413)
|
2016-12-04 01:50:32 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
response = app.test_client.get("/1", gather_request=False)
|
2016-12-04 01:50:32 +00:00
|
|
|
assert response.status == 413
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "Payload Too Large from error_handler."
|
2016-12-04 01:50:32 +00:00
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_payload_too_large_at_data_received_default(app):
|
|
|
|
app.config.REQUEST_MAX_SIZE = 1
|
2016-12-04 01:50:32 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/1")
|
2017-03-29 04:38:45 +01:00
|
|
|
async def handler2(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2016-12-04 01:50:32 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
response = app.test_client.get("/1", gather_request=False)
|
2016-12-04 01:50:32 +00:00
|
|
|
assert response.status == 413
|
2020-03-02 13:32:12 +00:00
|
|
|
assert "Request header" in response.text
|
2016-12-04 01:50:32 +00:00
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_payload_too_large_at_on_header_default(app):
|
|
|
|
app.config.REQUEST_MAX_SIZE = 500
|
2016-12-04 01:50:32 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.post("/1")
|
2017-03-29 04:38:45 +01:00
|
|
|
async def handler3(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2016-12-04 01:50:32 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
data = "a" * 1000
|
|
|
|
response = app.test_client.post("/1", gather_request=False, data=data)
|
2016-12-04 01:50:32 +00:00
|
|
|
assert response.status == 413
|
2020-03-02 13:32:12 +00:00
|
|
|
assert "Request body" in response.text
|