Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring downloader to separate module
- Loading branch information
Showing
4 changed files
with
66 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import urllib2 | ||
|
||
_USER_AGENT = ( | ||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' | ||
'Chrome/61.0.3163.100 Safari/537.36') | ||
|
||
|
||
class Error(Exception): | ||
pass | ||
|
||
|
||
class UnexpectedImageType(Error): | ||
pass | ||
|
||
|
||
def download_image_data(image_url): | ||
image_handle = urllib2.urlopen( | ||
urllib2.Request(image_url, headers={'User-Agent': _USER_AGENT})) | ||
if image_handle.info().type != 'image/jpeg': | ||
raise UnexpectedImageType('Expected image/jpeg, got ' + | ||
image_handle.info().type) | ||
return image_handle.read() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import unittest | ||
import urllib2 | ||
|
||
import mock | ||
|
||
from downloader import url | ||
|
||
|
||
class ImagesTest(unittest.TestCase): | ||
|
||
def setUp(self): | ||
mock_urlopen_patch = mock.patch( | ||
'downloader.url.urllib2.urlopen', autospec=True) | ||
self.addCleanup(mock_urlopen_patch.stop) | ||
self.mock_urlopen = mock_urlopen_patch.start() | ||
|
||
mock_request_patch = mock.patch( | ||
'downloader.url.urllib2.Request', autospec=True) | ||
self.addCleanup(mock_request_patch.stop) | ||
self.mock_request = mock_request_patch.start() | ||
|
||
def test_download_succeeds_when_server_is_ok(self): | ||
mock_handle = mock.Mock() | ||
mock_handle.info.return_value = mock.Mock(type='image/jpeg') | ||
mock_handle.read.return_value = 'dummy image data' | ||
self.mock_urlopen.return_value = mock_handle | ||
self.assertEqual( | ||
url.download_image_data('http://mock.com/image.jpg'), | ||
'dummy image data') | ||
|
||
def test_download_fails_when_server_returns_403(self): | ||
self.mock_urlopen.side_effect = urllib2.HTTPError( | ||
url='', code=404, msg='', hdrs=None, fp=None) | ||
with self.assertRaises(urllib2.HTTPError): | ||
url.download_image_data('http://mock.com/image.jpg') | ||
|
||
def test_download_fails_when_content_type_is_not_jpeg(self): | ||
mock_handle = mock.Mock() | ||
mock_handle.info.return_value = mock.Mock(type='image/png') | ||
mock_handle.read.return_value = 'dummy image data' | ||
self.mock_urlopen.return_value = mock_handle | ||
with self.assertRaises(url.UnexpectedImageType): | ||
url.download_image_data('http://mock.com/image.jpg') |