-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add wrap middleware and wrap permission (#141)
- Loading branch information
Showing
7 changed files
with
410 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any, cast | ||
|
||
from lilya.middleware.base import DefineMiddleware | ||
|
||
|
||
def wrap_middleware( | ||
middleware: DefineMiddleware | Any, | ||
) -> DefineMiddleware: | ||
""" | ||
Wraps the given middleware into a DefineMiddleware instance if it is not already one. | ||
Or else it will assume its a Lilya permission and wraps it. | ||
Args: | ||
permission (Union["BasePermission", Any]): The permission to be wrapped. | ||
Returns: | ||
BasePermission: The wrapped permission instance. | ||
""" | ||
if isinstance(middleware, DefineMiddleware): | ||
return middleware | ||
return DefineMiddleware(cast(Any, middleware)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any, cast | ||
|
||
from lilya.permissions.base import DefinePermission | ||
|
||
|
||
def wrap_permission( | ||
permission: DefinePermission | Any, | ||
) -> DefinePermission: | ||
""" | ||
Wraps the given permission into a BasePermission instance if it is not already one. | ||
Or else it will assume its a Lilya permission and wraps it. | ||
Args: | ||
permission (Union["BasePermission", Any]): The permission to be wrapped. | ||
Returns: | ||
BasePermission: The wrapped permission instance. | ||
""" | ||
if isinstance(permission, DefinePermission): | ||
return permission | ||
return DefinePermission(cast(Any, permission)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from lilya.apps import Lilya | ||
from lilya.middleware.compression import GZipMiddleware | ||
from lilya.responses import PlainText, StreamingResponse | ||
from lilya.routing import Path | ||
|
||
|
||
def test_gzip_responses(test_client_factory): | ||
def homepage(): | ||
return PlainText("x" * 4000, status_code=200) | ||
|
||
app = Lilya( | ||
routes=[Path("/", handler=homepage)], | ||
middleware=[GZipMiddleware], | ||
) | ||
|
||
client = test_client_factory(app) | ||
response = client.get("/", headers={"accept-encoding": "gzip"}) | ||
assert response.status_code == 200 | ||
assert response.text == "x" * 4000 | ||
assert response.headers["Content-Encoding"] == "gzip" | ||
assert int(response.headers["Content-Length"]) < 4000 | ||
|
||
|
||
def test_gzip_not_in_accept_encoding(test_client_factory): | ||
def homepage(request): | ||
return PlainText("x" * 4000, status_code=200) | ||
|
||
app = Lilya( | ||
routes=[Path("/", handler=homepage)], | ||
middleware=[GZipMiddleware], | ||
) | ||
|
||
client = test_client_factory(app) | ||
response = client.get("/", headers={"accept-encoding": "identity"}) | ||
assert response.status_code == 200 | ||
assert response.text == "x" * 4000 | ||
assert "Content-Encoding" not in response.headers | ||
assert int(response.headers["Content-Length"]) == 4000 | ||
|
||
|
||
def test_gzip_ignored_for_small_responses(test_client_factory): | ||
def homepage(request): | ||
return PlainText("OK", status_code=200) | ||
|
||
app = Lilya( | ||
routes=[Path("/", handler=homepage)], | ||
middleware=[GZipMiddleware], | ||
) | ||
|
||
client = test_client_factory(app) | ||
response = client.get("/", headers={"accept-encoding": "gzip"}) | ||
assert response.status_code == 200 | ||
assert response.text == "OK" | ||
assert "Content-Encoding" not in response.headers | ||
assert int(response.headers["Content-Length"]) == 2 | ||
|
||
|
||
def test_gzip_streaming_response(test_client_factory): | ||
def homepage(): | ||
async def generator(bytes, count): | ||
for _ in range(count): | ||
yield bytes | ||
|
||
streaming = generator(bytes=b"x" * 400, count=10) | ||
return StreamingResponse(streaming, status_code=200) | ||
|
||
app = Lilya( | ||
routes=[Path("/", handler=homepage)], | ||
middleware=[GZipMiddleware], | ||
) | ||
|
||
client = test_client_factory(app) | ||
response = client.get("/", headers={"accept-encoding": "gzip"}) | ||
assert response.status_code == 200 | ||
assert response.text == "x" * 4000 | ||
assert response.headers["Content-Encoding"] == "gzip" | ||
assert "Content-Length" not in response.headers | ||
|
||
|
||
def test_gzip_ignored_for_responses_with_encoding_set(test_client_factory): | ||
def homepage(): | ||
async def generator(bytes, count): | ||
for _ in range(count): | ||
yield bytes | ||
|
||
streaming = generator(bytes=b"x" * 400, count=10) | ||
return StreamingResponse(streaming, status_code=200, headers={"Content-Encoding": "text"}) | ||
|
||
app = Lilya( | ||
routes=[Path("/", handler=homepage)], | ||
middleware=[GZipMiddleware], | ||
) | ||
|
||
client = test_client_factory(app) | ||
response = client.get("/", headers={"accept-encoding": "gzip, text"}) | ||
assert response.status_code == 200 | ||
assert response.text == "x" * 4000 | ||
assert response.headers["Content-Encoding"] == "text" | ||
assert "Content-Length" not in response.headers |
Oops, something went wrong.