Skip to content

Provide User-Agent and Accept headers in URLFile requests #2011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2024
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
8 changes: 7 additions & 1 deletion python/cog/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,13 @@ def __wrapped__(self) -> Any:
# is that the book keeping for closing the response needs to be
# handled elsewhere. There's probably a better design for this
# in the long term.
res = urllib.request.urlopen(url) # noqa: S310
from . import __version__ as cog_version

req = urllib.request.Request( # noqa: S310
url,
headers={"User-agent": f"cog/{cog_version}", "Accept": "*/*"},
)
res = urllib.request.urlopen(req) # noqa: S310
object.__setattr__(self, "__target__", res)

return res
Expand Down
8 changes: 6 additions & 2 deletions python/tests/server/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ async def test_upload_files_with_url_file(urlopen_mock, respx_mock):

assert uploader.call_count == 1
assert urlopen_mock.call_count == 1
assert urlopen_mock.call_args[0][0] == "https://example.com/cdn/my_file.txt"
assert (
urlopen_mock.call_args[0][0].full_url == "https://example.com/cdn/my_file.txt"
)


@pytest.mark.asyncio
Expand All @@ -164,4 +166,6 @@ async def test_upload_files_with_url_file_with_retry(urlopen_mock, respx_mock):

assert uploader.call_count == 3
assert urlopen_mock.call_count == 1
assert urlopen_mock.call_args[0][0] == "https://example.com/cdn/my_file.txt"
assert (
urlopen_mock.call_args[0][0].full_url == "https://example.com/cdn/my_file.txt"
)
16 changes: 15 additions & 1 deletion python/tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import io
import pickle
import urllib.request
from urllib.response import addinfourl
from email.message import Message
from unittest import mock
from urllib.response import addinfourl

import pytest
from cog import __version__
from cog.types import Secret, URLFile, get_filename_from_url, get_filename_from_urlopen


Expand All @@ -26,6 +27,19 @@ def test_urlfile_protocol_validation():
URLFile("data:text/plain,hello")


@mock.patch("urllib.request.urlopen", return_value=file_fixture("hello world"))
def test_urlfile_headers(mock_urlopen: mock.Mock):
u = URLFile("https://example.com/some-path", filename="my_file.txt")
u.read()

assert mock_urlopen.call_count == 1

req: urllib.request.Request = mock_urlopen.call_args[0][0]
assert req.full_url == "https://example.com/some-path"
assert req.headers.get("User-agent") == f"cog/{__version__}"
assert req.headers.get("Accept") == "*/*"


@mock.patch("urllib.request.urlopen", return_value=file_fixture("hello world"))
def test_urlfile_custom_filename(mock_urlopen):
u = URLFile("https://example.com/some-path", filename="my_file.txt")
Expand Down
Loading