Reject a URL fixture with an unexpected scheme
A fixture URL can be syntactically parseable while still being outside the kind of resource a test expects. This example parses an ftp fixture with urllib.parse.urlsplit, then compares the resulting scheme with an explicit allowlist containing only https. The assertion first records that parsing produced ftp; require_https_fixture then raises ValueError with a deterministic message. The try block demonstrates the rejected outcome without allowing the exception to stop the example.
urlsplit separates a URL into scheme, network location, path, query, and fragment components. It is useful here because the code needs only the scheme, not a network request. The fixture is an in-memory string and the program does not open it. This API is available in Python 3.2 and later.
The check is deliberately narrow: it rejects schemes other than https, but it does not establish that a hostname is trustworthy, that a path exists, or that the URL is safe to fetch. Python's URL parsing documentation also cautions that its parsing functions do not validate every input security property. Add separate host, path, and credential rules when those are requirements.
See the official urllib.parse documentation. AI assistance disclosure: this article was drafted with AI assistance and should be adapted to the surrounding test policy.
from urllib.parse import urlsplit
def require_https_fixture(url):
parts = urlsplit(url)
if parts.scheme != "https":
raise ValueError(f"unexpected fixture scheme: {parts.scheme!r}")
return parts
fixture = "ftp://fixtures.example.test/archive.json"
parts = urlsplit(fixture)
assert parts.scheme == "ftp"
try:
require_https_fixture(fixture)
except ValueError as error:
print(error)
unexpected fixture scheme: 'ftp'