-
-
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.
- Loading branch information
Showing
4 changed files
with
270 additions
and
0 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,82 @@ | ||
from structlog import get_logger | ||
|
||
from lilya.responses import PlainText | ||
from lilya.routing import Include, Path | ||
from lilya.testclient import create_client | ||
|
||
logger = get_logger() | ||
|
||
|
||
class BeforePathRequest: | ||
async def __call__(self, scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
logger.info(f"Before path request: {app.state.app_request}") | ||
|
||
|
||
class AfterPathRequest: | ||
async def __call__(self, scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After path request: {app.state.app_request}") | ||
|
||
|
||
class BeforeIncludeRequest: | ||
async def __call__(self, scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
logger.info(f"Before include request: {app.state.app_request}") | ||
|
||
|
||
class AfterIncludeRequest: | ||
async def __call__(self, scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After include request: {app.state.app_request}") | ||
|
||
|
||
class BeforeAppRequest: | ||
async def __call__(self, scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request = 1 | ||
logger.info(f"Before app request: {app.state.app_request}") | ||
|
||
|
||
class AfterAppRequest: | ||
async def __call__(self, scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After app request: {app.state.app_request}") | ||
|
||
|
||
def test_all_layers_request(): | ||
async def index(request): | ||
state = request.app.state | ||
return PlainText(f"State: {state.app_request}") | ||
|
||
with create_client( | ||
routes=[ | ||
Include( | ||
"/", | ||
routes=[ | ||
Path( | ||
"/", | ||
index, | ||
before_request=[BeforePathRequest], | ||
after_request=[AfterPathRequest], | ||
) | ||
], | ||
before_request=[BeforeIncludeRequest], | ||
after_request=[AfterIncludeRequest], | ||
), | ||
], | ||
before_request=[BeforeAppRequest], | ||
after_request=[AfterAppRequest], | ||
) as client: | ||
response = client.get("/") | ||
|
||
assert response.status_code == 200 | ||
assert response.text == "State: 3" |
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,82 @@ | ||
from structlog import get_logger | ||
|
||
from lilya.responses import PlainText | ||
from lilya.routing import Include, Path | ||
from lilya.testclient import create_client | ||
|
||
logger = get_logger() | ||
|
||
|
||
class BeforePathRequest: | ||
def __call__(self, scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
logger.info(f"Before path request: {app.state.app_request}") | ||
|
||
|
||
class AfterPathRequest: | ||
def __call__(self, scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After path request: {app.state.app_request}") | ||
|
||
|
||
class BeforeIncludeRequest: | ||
def __call__(self, scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
logger.info(f"Before include request: {app.state.app_request}") | ||
|
||
|
||
class AfterIncludeRequest: | ||
def __call__(self, scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After include request: {app.state.app_request}") | ||
|
||
|
||
class BeforeAppRequest: | ||
def __call__(self, scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request = 1 | ||
logger.info(f"Before app request: {app.state.app_request}") | ||
|
||
|
||
class AfterAppRequest: | ||
def __call__(self, scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After app request: {app.state.app_request}") | ||
|
||
|
||
def test_all_layers_request(): | ||
async def index(request): | ||
state = request.app.state | ||
return PlainText(f"State: {state.app_request}") | ||
|
||
with create_client( | ||
routes=[ | ||
Include( | ||
"/", | ||
routes=[ | ||
Path( | ||
"/", | ||
index, | ||
before_request=[BeforePathRequest], | ||
after_request=[AfterPathRequest], | ||
) | ||
], | ||
before_request=[BeforeIncludeRequest], | ||
after_request=[AfterIncludeRequest], | ||
), | ||
], | ||
before_request=[BeforeAppRequest], | ||
after_request=[AfterAppRequest], | ||
) as client: | ||
response = client.get("/") | ||
|
||
assert response.status_code == 200 | ||
assert response.text == "State: 3" |
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,76 @@ | ||
from structlog import get_logger | ||
|
||
from lilya.responses import PlainText | ||
from lilya.routing import Include, Path | ||
from lilya.testclient import create_client | ||
|
||
logger = get_logger() | ||
|
||
|
||
def before_path_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
logger.info(f"Before path request: {app.state.app_request}") | ||
|
||
|
||
def after_path_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After path request: {app.state.app_request}") | ||
|
||
|
||
def before_include_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
logger.info(f"Before include request: {app.state.app_request}") | ||
|
||
|
||
def after_include_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After include request: {app.state.app_request}") | ||
|
||
|
||
def before_app_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request = 1 | ||
logger.info(f"Before app request: {app.state.app_request}") | ||
|
||
|
||
def after_app_request(scope, receive, send): | ||
app = scope["app"] | ||
app.state.app_request += 1 | ||
|
||
logger.info(f"After app request: {app.state.app_request}") | ||
|
||
|
||
def test_all_layers_request(): | ||
async def index(request): | ||
state = request.app.state | ||
return PlainText(f"State: {state.app_request}") | ||
|
||
with create_client( | ||
routes=[ | ||
Include( | ||
"/", | ||
routes=[ | ||
Path( | ||
"/", | ||
index, | ||
before_request=[before_path_request], | ||
after_request=[after_path_request], | ||
) | ||
], | ||
before_request=[before_include_request], | ||
after_request=[after_include_request], | ||
), | ||
], | ||
before_request=[before_app_request], | ||
after_request=[after_app_request], | ||
) as client: | ||
response = client.get("/") | ||
|
||
assert response.status_code == 200 | ||
assert response.text == "State: 3" |