Skip to content

Commit

Permalink
New Root URL: Disallow the use of real network requests to probe URLs…
Browse files Browse the repository at this point in the history
… while running tests
  • Loading branch information
davidfstr committed Jan 13, 2024
1 parent 105ff07 commit 51a4033
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/crystal/url_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,6 @@ def _candidate_urls_from_user_input(url_input: str) -> List[str]:
* If additionally no www. domain prefix is given,
try first without the prefix then with the prefix.
"""
# While running tests, unless CRYSTAL_URLOPEN_MOCKED=True,
# assume any URL input is already a valid URL without
# performing any (possibly real) network requests
if os.environ.get('CRYSTAL_RUNNING_TESTS', 'False') == 'True':
if os.environ.get('CRYSTAL_URLOPEN_MOCKED', 'False') == 'True':
# OK
pass
else:
# Assume valid, without performing any network requests
return [url_input]

if url_input.strip() == '':
return [url_input]

Expand Down Expand Up @@ -172,6 +161,23 @@ def _resolve_url_from_candidates(
if len(url_candidates) == 1:
return url_candidates[0]

# Disallow network requests while running tests,
# unless CRYSTAL_URLOPEN_MOCKED=True
if os.environ.get('CRYSTAL_RUNNING_TESTS', 'False') == 'True':
if os.environ.get('CRYSTAL_URLOPEN_MOCKED', 'False') == 'True':
# OK
pass
else:
raise AssertionError(
'Attempting to resolve URL candidates with real '
'network requests while automated tests are running. '

'Please either (1) enter URLs with http:// or https:// '
'schema that does not need to be resolved, or (2) '
'mock the urlopen() function with something like '
'_urlopen_responding_with().'
)

# Look for URL candidate which can be fetched successfully
for url_candidate in url_candidates:
if did_cancel_func():
Expand Down

0 comments on commit 51a4033

Please sign in to comment.