Skip to content

Commit

Permalink
tests: fixed dynamic Werkzeug version using in User-Agent header
Browse files Browse the repository at this point in the history
  • Loading branch information
alexted committed Oct 27, 2024
1 parent 6018bde commit 75d26e0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/minimal-async/src/minimal_async/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,4 @@ async def one_decorator() -> str:
@jsonrpc_headers
async def multi_decorators() -> t.Dict[str, t.Any]:
await asyncio.sleep(0)
return {'terminal_id': request.get_json(silent=True).get('terminal_id', 0), 'headers': str(request.headers)}
return {'terminal_id': request.get_json(silent=True).get('terminal_id', 0), 'headers': request.headers}
13 changes: 8 additions & 5 deletions examples/minimal-async/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

from werkzeug.datastructures import Headers

from tests.utils import EqMock

if t.TYPE_CHECKING:
from flask.testing import FlaskClient

Expand Down Expand Up @@ -166,12 +168,13 @@ def test_multi_decorators(client: 'FlaskClient') -> None:
'id': 1,
'jsonrpc': '2.0',
'result': {
'headers': 'User-Agent: Werkzeug/3.0.4\r\n'
'Host: localhost\r\n'
'Content-Type: application/json\r\n'
'Content-Length: 78\r\n'
'\r\n',
'terminal_id': 1,
'headers': {
'User-Agent': EqMock(),
'Host': 'localhost',
'Content-Type': 'application/json',
'Content-Length': '78',
}
},
}
assert rv.headers == Headers(
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/src/minimal/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@ def one_decorator() -> str:
@check_terminal_id
@jsonrpc_headers
def multi_decorators() -> t.Dict[str, t.Any]:
return {'terminal_id': request.get_json(silent=True).get('terminal_id', 0), 'headers': str(request.headers)}
return {'terminal_id': request.get_json(silent=True).get('terminal_id', 0), 'headers': request.headers}
16 changes: 9 additions & 7 deletions examples/minimal/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import typing as t

from werkzeug.datastructures import Headers

from tests.utils import EqMock

if t.TYPE_CHECKING:
from flask.testing import FlaskClient

Expand Down Expand Up @@ -87,7 +88,7 @@ def test_not_notify(client: 'FlaskClient') -> None:
'code': -32600,
'data': {
'message': "The method 'App.notNotify' doesn't allow Notification Request "
"object (without an 'id' member)"
"object (without an 'id' member)"
},
'message': 'Invalid Request',
'name': 'InvalidRequestError',
Expand Down Expand Up @@ -166,12 +167,13 @@ def test_multi_decorators(client: 'FlaskClient') -> None:
'id': 1,
'jsonrpc': '2.0',
'result': {
'headers': 'User-Agent: Werkzeug/3.0.4\r\n'
'Host: localhost\r\n'
'Content-Type: application/json\r\n'
'Content-Length: 78\r\n'
'\r\n',
'terminal_id': 1,
'headers': {
'User-Agent': EqMock(),
'Host': 'localhost',
'Content-Type': 'application/json',
'Content-Length': '78',
}
},
}
assert rv.headers == Headers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'headers': {
'content-type': 'application/json',
'content-length': '113',
'server': 'Werkzeug/3.0.4 Python/3.13.0',
'server': 'Werkzeug/3.0.6 Python/3.13.0',
'date': 'Fri, 15 Nov 2013 20:15:18 GMT',
'data': {
'jsonrpc': '2.0',
Expand Down
25 changes: 25 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import Any


class EqMock:
value: Any = None

def __init__(self, remember: bool = False) -> None:
self.remember: bool = remember

def __eq__(self, other: Any) -> bool: # noqa: ANN401
if self.remember and self.value is not None:
return self.value == other
else:
assert other, other

if self.remember:
self.value = other

return True

def __repr__(self) -> str:
return repr(self.value) if self.remember else super().__repr__()

def __str__(self) -> str:
return str(self.value) if self.remember else super().__str__()

0 comments on commit 75d26e0

Please sign in to comment.