From fb3ac4291fbc7fe810d820d7269e382bb7e98d41 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 19 Nov 2024 09:40:59 +0100 Subject: [PATCH] code styling --- app/middleware.py | 5 +++-- test/main.py | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/middleware.py b/app/middleware.py index c96e520..7e3d20d 100644 --- a/app/middleware.py +++ b/app/middleware.py @@ -11,6 +11,8 @@ logger = logging.getLogger(__name__) class PatchMalformedJsonMiddleware(BaseHTTPMiddleware): # see oscar.krause/fastapi-dls#1 + REGEX = '(\"mac_address_list\"\:\s?\[)([\w\d])' + def __init__(self, app, enabled: bool): super().__init__(app) self.enabled = enabled @@ -28,8 +30,7 @@ class PatchMalformedJsonMiddleware(BaseHTTPMiddleware): body = body.replace('\t', '') body = body.replace('\n', '') - regex = '(\"mac_address_list\"\:\s?\[)([\w\d])' - s = re.sub(regex, r'\1"\2', body) + s = re.sub(PatchMalformedJsonMiddleware.REGEX, r'\1"\2', body) logger.debug(f'Fixed JSON: "{s}"') s = json.loads(s) # ensure json is now valid diff --git a/test/main.py b/test/main.py index 3aa229b..2e70558 100644 --- a/test/main.py +++ b/test/main.py @@ -111,11 +111,11 @@ def test_auth_v1_origin(): def test_auth_v1_origin_malformed_json(): # see oscar.krause/fastapi-dls#1 import re + from middleware import PatchMalformedJsonMiddleware # test regex (temporary, until this section is merged into main.py - json_test = '{"environment": {"fingerprint": {"mac_address_list": [ff:ff:ff:ff:ff:ff"]}}' - regex = '(\"mac_address_list\"\:\s?\[)([\w\d])' - replaced = re.sub(regex, r'\1"\2', json_test) + body = '{"environment": {"fingerprint": {"mac_address_list": [ff:ff:ff:ff:ff:ff"]}}' + replaced = re.sub(PatchMalformedJsonMiddleware.REGEX, r'\1"\2', body) assert replaced == '{"environment": {"fingerprint": {"mac_address_list": ["ff:ff:ff:ff:ff:ff"]}}'