|
39 | 39 | URL_ALLOWED_NETLOCS = {"github.com", "readthedocs.com", "docs.python.org"} |
40 | 40 |
|
41 | 41 |
|
| 42 | +# Error messages for URL validation |
| 43 | +INVALID_SCHEME_ERROR = "Invalid URL scheme. Only 'https' is allowed." |
| 44 | +"""Scheme error message for URL validation. |
| 45 | +
|
| 46 | +Unit-Testing: |
| 47 | +
|
| 48 | + First set up test fixtures by importing utils. |
| 49 | +
|
| 50 | + >>> import docs.utils as _utils |
| 51 | + >>> |
| 52 | +
|
| 53 | + >>> _utils.INVALID_SCHEME_ERROR is not None |
| 54 | + True |
| 55 | + >>> type(_utils.INVALID_SCHEME_ERROR) is type(str()) |
| 56 | + True |
| 57 | + >>> len(_utils.INVALID_SCHEME_ERROR) > 0 |
| 58 | + True |
| 59 | + >>> |
| 60 | +
|
| 61 | +""" |
| 62 | + |
| 63 | + |
| 64 | +INVALID_DOMAIN_ERROR = f"Invalid or untrusted domain. Only {URL_ALLOWED_NETLOCS} are allowed." |
| 65 | +"""Domain error message for URL validation. |
| 66 | +
|
| 67 | +Unit-Testing: |
| 68 | +
|
| 69 | + First set up test fixtures by importing utils. |
| 70 | +
|
| 71 | + >>> import docs.utils as _utils |
| 72 | + >>> |
| 73 | +
|
| 74 | + >>> _utils.INVALID_DOMAIN_ERROR is not None |
| 75 | + True |
| 76 | + >>> type(_utils.INVALID_DOMAIN_ERROR) is type(str()) |
| 77 | + True |
| 78 | + >>> len(_utils.INVALID_DOMAIN_ERROR) > 0 |
| 79 | + True |
| 80 | + >>> |
| 81 | +
|
| 82 | +""" |
| 83 | + |
| 84 | + |
42 | 85 | def _validate_git_ref(ref: str) -> str: |
43 | 86 | """ |
44 | 87 | Validate if the provided string is a valid Git reference. |
@@ -171,10 +214,10 @@ def sanitize_url(url: str) -> str: |
171 | 214 | parsed_url = urlparse(url) |
172 | 215 | # Validate scheme |
173 | 216 | if parsed_url.scheme not in URL_ALLOWED_SCHEMES: |
174 | | - raise ValueError("Invalid URL scheme. Only 'https' is allowed.") |
| 217 | + raise ValueError(INVALID_SCHEME_ERROR) |
175 | 218 | # Validate netloc |
176 | 219 | if parsed_url.netloc not in URL_ALLOWED_NETLOCS: |
177 | | - raise ValueError(f"Invalid or untrusted domain. Only {URL_ALLOWED_NETLOCS} are allowed.") |
| 220 | + raise ValueError(INVALID_DOMAIN_ERROR) |
178 | 221 | # Sanitize path and query |
179 | 222 | sanitized_path = quote(parsed_url.path) |
180 | 223 | sanitized_query = quote(parsed_url.query) |
|
0 commit comments