1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
from unittest import mock
import pytest
import requests
import responses
from mopidy.internal import http
TIMEOUT = 1000
URI = "http://example.com/foo.txt"
BODY = "This is the contents of foo.txt."
@pytest.fixture
def session():
return requests.Session()
@pytest.fixture
def session_mock():
return mock.Mock(spec=requests.Session)
@responses.activate
def test_download_on_server_side_error(session, caplog):
responses.add(responses.GET, URI, body=BODY, status=500)
result = http.download(session, URI)
assert result is None
assert "Problem downloading" in caplog.text
def test_download_times_out_if_connection_times_out(session_mock, caplog):
session_mock.get.side_effect = requests.exceptions.Timeout
result = http.download(session_mock, URI, timeout=1.0)
session_mock.get.assert_called_once_with(URI, timeout=1.0, stream=True)
assert result is None
assert (
f"Download of {URI!r} failed due to connection timeout after 1.000s"
in caplog.text
)
@responses.activate
def test_download_times_out_if_download_is_slow(session, caplog):
responses.add(responses.GET, URI, body=BODY, content_type="text/plain")
with mock.patch.object(http, "time") as time_mock:
time_mock.time.side_effect = [0, TIMEOUT + 1]
result = http.download(session, URI)
assert result is None
assert (
f"Download of {URI!r} failed due to download taking more than 1.000s"
in caplog.text
)
|