File tree Expand file tree Collapse file tree 2 files changed +25
-13
lines changed Expand file tree Collapse file tree 2 files changed +25
-13
lines changed Original file line number Diff line number Diff line change
1
+ import logging
1
2
from pathlib import Path
2
3
3
4
import httpx
@@ -22,7 +23,7 @@ def test_configurable_timeout():
22
23
assert context .http_client .timeout .read == 17
23
24
24
25
25
- def test_client_version_check ():
26
+ def test_client_version_check (caplog ):
26
27
with Context .from_app (build_app (tree )) as context :
27
28
client = from_context (context )
28
29
@@ -31,11 +32,21 @@ def test_client_version_check():
31
32
with fail_with_status_code (HTTP_400_BAD_REQUEST ):
32
33
list (client )
33
34
34
- # Gibberish user agent should generate a 400 .
35
+ # Gibberish user agent should generate a warning and log entry .
35
36
context .http_client .headers ["user-agent" ] = "python-tiled/gibberish"
36
- with fail_with_status_code (HTTP_400_BAD_REQUEST ):
37
+ caplog .set_level (logging .WARNING )
38
+ with pytest .warns (UserWarning , match = r"gibberish" ):
37
39
list (client )
38
40
41
+ _ , LOG_LEVEL , LOG_MESSAGE = range (3 )
42
+ logged_warnings = tuple (
43
+ entry [LOG_MESSAGE ]
44
+ for entry in caplog .record_tuples
45
+ if entry [LOG_LEVEL ] == logging .WARNING
46
+ )
47
+ assert len (logged_warnings ) > 0
48
+ assert any ("gibberish" in message for message in logged_warnings )
49
+
39
50
40
51
def test_direct (tmpdir ):
41
52
profile_content = {
Original file line number Diff line number Diff line change 5
5
import secrets
6
6
import sys
7
7
import urllib .parse
8
+ import warnings
8
9
from contextlib import asynccontextmanager
9
10
from functools import lru_cache , partial
10
11
from pathlib import Path
@@ -715,18 +716,18 @@ async def client_compatibility_check(request: Request, call_next):
715
716
agent , _ , raw_version = user_agent .partition ("/" )
716
717
try :
717
718
parsed_version = packaging .version .parse (raw_version )
718
- except Exception :
719
- return JSONResponse (
720
- status_code = HTTP_400_BAD_REQUEST ,
721
- content = {
722
- "detail" : (
723
- f"Python Tiled client is version is reported as { raw_version } . "
724
- "This cannot be parsed as a valid version."
725
- ),
726
- },
719
+ except Exception as caught_exception :
720
+ invalid_version_message = (
721
+ f"Python Tiled client version is reported as { raw_version } . "
722
+ "This cannot be parsed as a valid version."
727
723
)
724
+ logger .warning (invalid_version_message )
725
+ if isinstance (caught_exception , packaging .version .InvalidVersion ):
726
+ warnings .warn (invalid_version_message )
728
727
else :
729
- if parsed_version < MINIMUM_SUPPORTED_PYTHON_CLIENT_VERSION :
728
+ if (not parsed_version .is_devrelease ) and (
729
+ parsed_version < MINIMUM_SUPPORTED_PYTHON_CLIENT_VERSION
730
+ ):
730
731
return JSONResponse (
731
732
status_code = HTTP_400_BAD_REQUEST ,
732
733
content = {
You can’t perform that action at this time.
0 commit comments