Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/uwtools/tests/utils/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ def test_utils_tasks_filecopy__source_http(code, expected, src, tmp_path):
with patch.object(tasks, "requests") as requests:
response = requests.get()
response.status_code = code
response.content = b"data"
response.iter_content.return_value = iter([b"f", b"o", b"o"])
tasks.filecopy(src=src, dst=dst)
requests.get.assert_called_with(src, allow_redirects=True, timeout=3)
requests.get.assert_called_with(src, allow_redirects=True, stream=True, timeout=3)
assert dst.is_file() is expected


Expand Down Expand Up @@ -238,12 +238,13 @@ def test_utils_tasks_filecopy_http(ready_task, tmp_path):
patch.object(tasks.requests, "get") as get,
patch.object(tasks, "existing_http", wraps=ready_task) as existing_http,
):
response = Mock(status_code=200, content=b"data")
response = Mock(status_code=200)
response.iter_content.return_value = iter([b"f", b"o", b"o"])
get.return_value = response
tasks.filecopy_http(url=url, dst=dst)
existing_http.assert_called_once_with(url)
get.assert_called_once_with(url, allow_redirects=True, timeout=3)
assert dst.exists()
get.assert_called_once_with(url, allow_redirects=True, stream=True, timeout=3)
assert dst.read_bytes() == b"foo"


def test_utils_tasks_filecopy_local(tmp_path):
Expand Down
6 changes: 4 additions & 2 deletions src/uwtools/utils/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@ def filecopy_http(url: str, dst: Path, check: bool = True):
yield asset(dst, dst.is_file)
yield existing_http(url) if check else None
dst.parent.mkdir(parents=True, exist_ok=True)
response = requests.get(url, allow_redirects=True, timeout=3)
response = requests.get(url, allow_redirects=True, stream=True, timeout=3)
if (code := response.status_code) == HTTPStatus.OK:
dst.write_bytes(response.content)
with dst.open(mode="wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
else:
log.error("Could not get '%s', HTTP status was: %s", url, code)

Expand Down