|
| 1 | +import io |
| 2 | +from unittest import mock |
| 3 | +from unittest.mock import AsyncMock, Mock |
| 4 | + |
| 5 | +import httpx |
| 6 | +import pytest |
| 7 | +from cog.server.clients import ClientManager |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.asyncio |
| 11 | +async def test_upload_file(): |
| 12 | + mock_fh = io.BytesIO() |
| 13 | + mock_client = AsyncMock(spec=httpx.AsyncClient) |
| 14 | + |
| 15 | + mock_response = Mock(spec=httpx.Response) |
| 16 | + mock_response.status_code = 201 |
| 17 | + mock_response.text = "" |
| 18 | + mock_response.headers = {} |
| 19 | + mock_response.url = "http://example.com/upload/file?some-gubbins" |
| 20 | + |
| 21 | + mock_client.put.return_value = mock_response |
| 22 | + |
| 23 | + client_manager = ClientManager() |
| 24 | + client_manager.file_client = mock_client |
| 25 | + |
| 26 | + final_url = await client_manager.upload_file( |
| 27 | + mock_fh, url="http://example.com/upload", prediction_id=None |
| 28 | + ) |
| 29 | + |
| 30 | + assert final_url == "http://example.com/upload/file" |
| 31 | + mock_client.put.assert_called_with( |
| 32 | + "http://example.com/upload/file", |
| 33 | + content=mock.ANY, |
| 34 | + headers={ |
| 35 | + "Content-Type": "application/octet-stream", |
| 36 | + }, |
| 37 | + timeout=mock.ANY, |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.asyncio |
| 42 | +async def test_upload_file_with_prediction_id(): |
| 43 | + mock_fh = io.BytesIO() |
| 44 | + mock_client = AsyncMock(spec=httpx.AsyncClient) |
| 45 | + |
| 46 | + mock_response = Mock(spec=httpx.Response) |
| 47 | + mock_response.status_code = 201 |
| 48 | + mock_response.text = "" |
| 49 | + mock_response.headers = {} |
| 50 | + mock_response.url = "http://example.com/upload/file?some-gubbins" |
| 51 | + |
| 52 | + mock_client.put.return_value = mock_response |
| 53 | + |
| 54 | + client_manager = ClientManager() |
| 55 | + client_manager.file_client = mock_client |
| 56 | + |
| 57 | + final_url = await client_manager.upload_file( |
| 58 | + mock_fh, url="http://example.com/upload", prediction_id="abc123" |
| 59 | + ) |
| 60 | + |
| 61 | + assert final_url == "http://example.com/upload/file" |
| 62 | + mock_client.put.assert_called_with( |
| 63 | + "http://example.com/upload/file", |
| 64 | + content=mock.ANY, |
| 65 | + headers={ |
| 66 | + "Content-Type": "application/octet-stream", |
| 67 | + "X-Prediction-ID": "abc123", |
| 68 | + }, |
| 69 | + timeout=mock.ANY, |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.asyncio |
| 74 | +async def test_upload_file_with_location(): |
| 75 | + mock_fh = io.BytesIO() |
| 76 | + mock_client = AsyncMock(spec=httpx.AsyncClient) |
| 77 | + |
| 78 | + mock_response = Mock(spec=httpx.Response) |
| 79 | + mock_response.status_code = 201 |
| 80 | + mock_response.text = "" |
| 81 | + mock_response.headers = { |
| 82 | + "location": "http://cdn.example.com/bucket/file?some-gubbins" |
| 83 | + } |
| 84 | + mock_response.url = "http://example.com/upload/file?some-gubbins" |
| 85 | + |
| 86 | + mock_client.put.return_value = mock_response |
| 87 | + |
| 88 | + client_manager = ClientManager() |
| 89 | + client_manager.file_client = mock_client |
| 90 | + |
| 91 | + final_url = await client_manager.upload_file( |
| 92 | + mock_fh, url="http://example.com/upload", prediction_id="abc123" |
| 93 | + ) |
| 94 | + |
| 95 | + assert final_url == "http://cdn.example.com/bucket/file" |
| 96 | + mock_client.put.assert_called_with( |
| 97 | + "http://example.com/upload/file", |
| 98 | + content=mock.ANY, |
| 99 | + headers={ |
| 100 | + "Content-Type": "application/octet-stream", |
| 101 | + "X-Prediction-ID": "abc123", |
| 102 | + }, |
| 103 | + timeout=mock.ANY, |
| 104 | + ) |
0 commit comments