Skip to content

Commit

Permalink
Checks HTTP response status code, fixes #6
Browse files Browse the repository at this point in the history
On error in the HTTP status code, the callback function will no
longer be called.
  • Loading branch information
AndreMiras committed Apr 19, 2020
1 parent 937e5a9 commit 8aa5e57
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions kivy_garden/mapview/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def _download_url(self, url, callback, kwargs):
if DEBUG:
print("Downloader: download(url) {}".format(url))
response = requests.get(url, **kwargs)
response.raise_for_status()
return callback, (url, response)

def _load_tile(self, tile):
Expand Down
11 changes: 9 additions & 2 deletions tests/test_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,21 @@ def test_download(self):
assert len(downloader._futures) == 0

def test_download_status_error(self):
"""Error status code should be checked, but is currently not."""
"""
Error status code should be checked.
Callback function will not be invoked on error.
"""
callback = mock.Mock()
url = "https://httpstat.us/404"
status_code = 404
downloader = Downloader.instance()
assert len(downloader._futures) == 0
downloader.download(url, callback)
with patch_requests_get(status_code=status_code) as m_get:
downloader.download(url, callback)
assert m_get.call_args_list == [mock.call(url)]
assert len(downloader._futures) == 1
assert callback.call_args_list == []
while len(downloader._futures) > 0:
Clock.tick()
assert callback.call_args_list == []
assert len(downloader._futures) == 0
12 changes: 8 additions & 4 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from unittest import mock

from requests.models import Response

def patch_requests_get(response_json=None):
response = mock.Mock()
response.return_value.json.return_value = response_json
return mock.patch("requests.get", response)

def patch_requests_get(response_json=None, status_code=200):
response = Response()
response.json = lambda: response_json
response.status_code = status_code
m_get = mock.Mock(return_value=response)
return mock.patch("requests.get", m_get)

0 comments on commit 8aa5e57

Please sign in to comment.