Skip to content

Commit

Permalink
add exception when parsing cleint version
Browse files Browse the repository at this point in the history
  • Loading branch information
natthan-pigoux committed Jul 17, 2024
1 parent e7af916 commit 92fac94
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 7 additions & 4 deletions diracx-routers/src/diracx/routers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, Response
from fastapi.routing import APIRoute
from packaging.version import parse
from packaging.version import InvalidVersion, parse
from pydantic import TypeAdapter

# from starlette.types import ASGIApp
Expand Down Expand Up @@ -464,7 +464,6 @@ async def dispatch(self, request: Request, call_next) -> Response:
# status_code=HTTPStatus.BAD_REQUEST,
# detail="Client version header is missing.",
# )

elif self.is_version_too_old(client_version):
raise HTTPException(
status_code=HTTPStatus.UPGRADE_REQUIRED,
Expand All @@ -474,9 +473,13 @@ async def dispatch(self, request: Request, call_next) -> Response:
response = await call_next(request)
return response

def is_version_too_old(self, client_version: str) -> bool:
def is_version_too_old(self, client_version: str) -> bool | None:
"""Verify that client version is ge than min."""
return parse(client_version) < parse(self.min_client_version)
try:
return parse(client_version) < parse(self.min_client_version)
except InvalidVersion as iv_exc:
logger.info(iv_exc)
return None


# I'm not sure if this has to be define here:
Expand Down
8 changes: 8 additions & 0 deletions diracx-routers/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,11 @@ def test_min_client_version_lower_than_expected(test_client):
def test_client_version_not_in_header(test_client, caplog: pytest.LogCaptureFixture):
test_client.get("/", headers={})
assert "header is missing" in caplog.text

test_client.get("/", headers={"DiracX-Client-Version": ""})
assert "header is missing" in caplog.text


def test_wrong_client_version(test_client, caplog: pytest.LogCaptureFixture):
test_client.get("/", headers={"DiracX-Client-Version": "Unknown"})
assert "Invalid version: 'Unknown'" in caplog.text

0 comments on commit 92fac94

Please sign in to comment.