GIT-1591 Strict Slashes behavior fix (#1594)
* fix: GIT-1591: fix strict_slashes option inheriting behavior Signed-off-by: Harsha Narayana <harsha2k4@gmail.com> * doc: GIT-1591: add documentation exlaining the strict_slashes behavior Signed-off-by: Harsha Narayana <harsha2k4@gmail.com> * fix: GIT-1591: fix deprecated for test_client Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
This commit is contained in:
committed by
Stephen Sadowski
parent
c15158224b
commit
13079c6e30
@@ -687,3 +687,49 @@ def test_register_blueprint(app, debug):
|
||||
"version 1.0. Please use the blueprint method"
|
||||
" instead"
|
||||
)
|
||||
|
||||
|
||||
def test_strict_slashes_behavior_adoption(app):
|
||||
app.strict_slashes = True
|
||||
|
||||
@app.get("/test")
|
||||
def handler_test(request):
|
||||
return text("Test")
|
||||
|
||||
assert app.test_client.get("/test")[1].status == 200
|
||||
assert app.test_client.get("/test/")[1].status == 404
|
||||
|
||||
bp = Blueprint("bp")
|
||||
|
||||
@bp.get("/one", strict_slashes=False)
|
||||
def one(request):
|
||||
return text("one")
|
||||
|
||||
@bp.get("/second")
|
||||
def second(request):
|
||||
return text("second")
|
||||
|
||||
app.blueprint(bp)
|
||||
|
||||
assert app.test_client.get("/one")[1].status == 200
|
||||
assert app.test_client.get("/one/")[1].status == 200
|
||||
|
||||
assert app.test_client.get("/second")[1].status == 200
|
||||
assert app.test_client.get("/second/")[1].status == 404
|
||||
|
||||
bp2 = Blueprint("bp2", strict_slashes=False)
|
||||
|
||||
@bp2.get("/third")
|
||||
def third(request):
|
||||
return text("third")
|
||||
|
||||
app.blueprint(bp2)
|
||||
assert app.test_client.get("/third")[1].status == 200
|
||||
assert app.test_client.get("/third/")[1].status == 200
|
||||
|
||||
@app.get("/f1", strict_slashes=False)
|
||||
def f1(request):
|
||||
return text("f1")
|
||||
|
||||
assert app.test_client.get("/f1")[1].status == 200
|
||||
assert app.test_client.get("/f1/")[1].status == 200
|
||||
|
||||
@@ -24,7 +24,9 @@ old_conn = None
|
||||
class ReusableSanicConnectionPool(httpcore.ConnectionPool):
|
||||
async def acquire_connection(self, origin):
|
||||
global old_conn
|
||||
connection = self.active_connections.pop_by_origin(origin, http2_only=True)
|
||||
connection = self.active_connections.pop_by_origin(
|
||||
origin, http2_only=True
|
||||
)
|
||||
if connection is None:
|
||||
connection = self.keepalive_connections.pop_by_origin(origin)
|
||||
|
||||
@@ -187,11 +189,7 @@ class ReuseableSanicTestClient(SanicTestClient):
|
||||
self._session = ResusableSanicSession()
|
||||
try:
|
||||
response = await getattr(self._session, method.lower())(
|
||||
url,
|
||||
verify=False,
|
||||
timeout=request_keepalive,
|
||||
*args,
|
||||
**kwargs,
|
||||
url, verify=False, timeout=request_keepalive, *args, **kwargs
|
||||
)
|
||||
except NameError:
|
||||
raise Exception(response.status_code)
|
||||
|
||||
@@ -110,7 +110,7 @@ def test_redirect_with_header_injection(redirect_app):
|
||||
|
||||
|
||||
@pytest.mark.parametrize("test_str", ["sanic-test", "sanictest", "sanic test"])
|
||||
async def test_redirect_with_params(app, test_client, test_str):
|
||||
async def test_redirect_with_params(app, sanic_client, test_str):
|
||||
@app.route("/api/v1/test/<test>/")
|
||||
async def init_handler(request, test):
|
||||
assert test == test_str
|
||||
@@ -121,7 +121,7 @@ async def test_redirect_with_params(app, test_client, test_str):
|
||||
assert test == test_str
|
||||
return text("OK")
|
||||
|
||||
test_cli = await test_client(app)
|
||||
test_cli = await sanic_client(app)
|
||||
|
||||
response = await test_cli.get("/api/v1/test/{}/".format(quote(test_str)))
|
||||
assert response.status == 200
|
||||
|
||||
@@ -4,7 +4,7 @@ import contextlib
|
||||
from sanic.response import stream, text
|
||||
|
||||
|
||||
async def test_request_cancel_when_connection_lost(loop, app, test_client):
|
||||
async def test_request_cancel_when_connection_lost(loop, app, sanic_client):
|
||||
app.still_serving_cancelled_request = False
|
||||
|
||||
@app.get("/")
|
||||
@@ -14,7 +14,7 @@ async def test_request_cancel_when_connection_lost(loop, app, test_client):
|
||||
app.still_serving_cancelled_request = True
|
||||
return text("OK")
|
||||
|
||||
test_cli = await test_client(app)
|
||||
test_cli = await sanic_client(app)
|
||||
|
||||
# schedule client call
|
||||
task = loop.create_task(test_cli.get("/"))
|
||||
@@ -33,7 +33,7 @@ async def test_request_cancel_when_connection_lost(loop, app, test_client):
|
||||
assert app.still_serving_cancelled_request is False
|
||||
|
||||
|
||||
async def test_stream_request_cancel_when_conn_lost(loop, app, test_client):
|
||||
async def test_stream_request_cancel_when_conn_lost(loop, app, sanic_client):
|
||||
app.still_serving_cancelled_request = False
|
||||
|
||||
@app.post("/post/<id>", stream=True)
|
||||
@@ -53,7 +53,7 @@ async def test_stream_request_cancel_when_conn_lost(loop, app, test_client):
|
||||
|
||||
return stream(streaming)
|
||||
|
||||
test_cli = await test_client(app)
|
||||
test_cli = await sanic_client(app)
|
||||
|
||||
# schedule client call
|
||||
task = loop.create_task(test_cli.post("/post/1"))
|
||||
|
||||
@@ -111,7 +111,6 @@ def test_request_stream_app(app):
|
||||
result += body.decode("utf-8")
|
||||
return text(result)
|
||||
|
||||
|
||||
assert app.is_request_stream is True
|
||||
|
||||
request, response = app.test_client.get("/get")
|
||||
|
||||
@@ -13,15 +13,12 @@ class DelayableSanicConnectionPool(httpcore.ConnectionPool):
|
||||
self._request_delay = request_delay
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
async def send(
|
||||
self,
|
||||
request,
|
||||
stream=False,
|
||||
ssl=None,
|
||||
timeout=None,
|
||||
):
|
||||
async def send(self, request, stream=False, ssl=None, timeout=None):
|
||||
connection = await self.acquire_connection(request.url.origin)
|
||||
if connection.h11_connection is None and connection.h2_connection is None:
|
||||
if (
|
||||
connection.h11_connection is None
|
||||
and connection.h2_connection is None
|
||||
):
|
||||
await connection.connect(ssl=ssl, timeout=timeout)
|
||||
if self._request_delay:
|
||||
await asyncio.sleep(self._request_delay)
|
||||
|
||||
@@ -231,9 +231,7 @@ def test_chunked_streaming_returns_correct_content(streaming_app):
|
||||
assert response.text == "foo,bar"
|
||||
|
||||
|
||||
def test_non_chunked_streaming_adds_correct_headers(
|
||||
non_chunked_streaming_app
|
||||
):
|
||||
def test_non_chunked_streaming_adds_correct_headers(non_chunked_streaming_app):
|
||||
request, response = non_chunked_streaming_app.test_client.get("/")
|
||||
assert "Transfer-Encoding" not in response.headers
|
||||
assert response.headers["Content-Type"] == "text/csv"
|
||||
|
||||
Reference in New Issue
Block a user