Skip to content

Commit

Permalink
Provide User-Agent and Accept headers in URLFile requests (#2011)
Browse files Browse the repository at this point in the history
It turns out some providers require these headers to be present
otherwise they'll give an error response. This issue is not present
in the requests version of the codebase because the requests library
provides default headers on our behalf.
  • Loading branch information
aron authored Oct 21, 2024
1 parent 9cb5f35 commit 426b112
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
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

0 comments on commit 426b112

Please sign in to comment.