Coverage for api/utils/url.py: 75%

6 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 06:14 +0000

1from urllib.parse import urlparse 

2 

3 

4def add_protocol(url: str) -> str: 

5 """ 

6 Add protocol to URLs that lack them. 

7 

8 Some fields in the database contain incomplete URLs, leading to unexpected 

9 behavior in downstream consumers. This helper verifies that we always 

10 return fully formed URLs in such situations. 

11 

12 :param url: the URL to check and add scheme 

13 :return: the URL with the existing scheme, or ``https`` if one did not exist 

14 """ 

15 

16 parsed = urlparse(url) 

17 if parsed.scheme == "": 17 ↛ 18line 17 didn't jump to line 18 because the condition on line 17 was never true

18 return f"https://{url}" 

19 else: 

20 return url