-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
5 changed files
with
92 additions
and
1 deletion.
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,46 @@ | ||
from itertools import count | ||
|
||
from sanic import Request, Sanic | ||
from sanic.compat import Header | ||
from sanic.models.protocol_types import TransportProtocol | ||
|
||
|
||
class CountedRequest(Request): | ||
__slots__ = () | ||
|
||
_counter = count() | ||
count = next(_counter) | ||
|
||
def __init__( | ||
self, | ||
url_bytes: bytes, | ||
headers: Header, | ||
version: str, | ||
method: str, | ||
transport: TransportProtocol, | ||
app: Sanic, | ||
head: bytes = b"", | ||
stream_id: int = 0, | ||
): | ||
super().__init__( | ||
url_bytes, | ||
headers, | ||
version, | ||
method, | ||
transport, | ||
app, | ||
head, | ||
stream_id, | ||
) | ||
self.__class__._increment() | ||
if hasattr(self.app, "multiplexer"): | ||
self.app.multiplexer.state["request_count"] = self.__class__.count | ||
|
||
@classmethod | ||
def _increment(cls): | ||
cls.count = next(cls._counter) | ||
|
||
@classmethod | ||
def reset_count(cls): | ||
cls._counter = count() | ||
cls.count = next(cls._counter) |
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,42 @@ | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from sanic import Sanic | ||
from sanic.compat import Header | ||
from sanic.response import json | ||
from sanic_testing.reusable import ReusableClient | ||
|
||
from sanic_ext import CountedRequest | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def reset_counter(): | ||
yield | ||
CountedRequest.reset_count() | ||
|
||
|
||
def test_counter_increments(app: Sanic): | ||
app.request_class = CountedRequest | ||
|
||
@app.get("/") | ||
async def handler(request: CountedRequest): | ||
return json({"count": request.count}) | ||
|
||
@app.get("/info") | ||
async def info(request: CountedRequest): | ||
return json({"state": request.app.m.state}) | ||
|
||
with ReusableClient(app) as client: | ||
for i in range(1, 10): | ||
_, response = client.get("/") | ||
assert response.json["count"] == i | ||
|
||
|
||
def test_counter_increment_on_state(app: Sanic): | ||
mock = Mock() | ||
mock.state = {} | ||
app.multiplexer = mock | ||
|
||
for i in range(1, 10): | ||
CountedRequest(b"/", Header({}), "", "", Mock(), app) | ||
assert CountedRequest.count == i |