@@ -45,16 +45,29 @@ def setUp(self):
45
45
self .plugin = fetchart .FetchArtPlugin ()
46
46
47
47
48
- class FetchImageTest (UseThePlugin ):
49
- URL = 'http://example.com/test.jpg'
50
-
48
+ class FetchImageHelper (_common .TestCase ):
49
+ """Helper mixin for mocking requests when fetching images
50
+ with remote art sources.
51
+ """
51
52
@responses .activate
52
53
def run (self , * args , ** kwargs ):
53
- super (FetchImageTest , self ).run (* args , ** kwargs )
54
+ super (FetchImageHelper , self ).run (* args , ** kwargs )
55
+
56
+ IMAGEHEADER = {'image/jpeg' : b'\x00 ' * 6 + b'JFIF' ,
57
+ 'image/png' : b'\211 PNG\r \n \032 \n ' , }
54
58
55
- def mock_response (self , content_type ):
56
- responses .add (responses .GET , self .URL ,
57
- content_type = content_type )
59
+ def mock_response (self , url , content_type = 'image/jpeg' , file_type = None ):
60
+ if file_type is None :
61
+ file_type = content_type
62
+ responses .add (responses .GET , url ,
63
+ content_type = content_type ,
64
+ # imghdr reads 32 bytes
65
+ body = self .IMAGEHEADER .get (
66
+ file_type , b'' ).ljust (32 , b'\x00 ' ))
67
+
68
+
69
+ class FetchImageTest (FetchImageHelper , UseThePlugin ):
70
+ URL = 'http://example.com/test.jpg'
58
71
59
72
def setUp (self ):
60
73
super (FetchImageTest , self ).setUp ()
@@ -64,17 +77,23 @@ def setUp(self):
64
77
self .candidate = fetchart .Candidate (logger , url = self .URL )
65
78
66
79
def test_invalid_type_returns_none (self ):
67
- self .mock_response ('image/watercolour' )
80
+ self .mock_response (self . URL , 'image/watercolour' )
68
81
self .source .fetch_image (self .candidate , self .extra )
69
82
self .assertEqual (self .candidate .path , None )
70
83
71
84
def test_jpeg_type_returns_path (self ):
72
- self .mock_response ('image/jpeg' )
85
+ self .mock_response (self . URL , 'image/jpeg' )
73
86
self .source .fetch_image (self .candidate , self .extra )
74
87
self .assertNotEqual (self .candidate .path , None )
75
88
76
89
def test_extension_set_by_content_type (self ):
77
- self .mock_response ('image/png' )
90
+ self .mock_response (self .URL , 'image/png' )
91
+ self .source .fetch_image (self .candidate , self .extra )
92
+ self .assertEqual (os .path .splitext (self .candidate .path )[1 ], b'.png' )
93
+ self .assertExists (self .candidate .path )
94
+
95
+ def test_does_not_rely_on_server_content_type (self ):
96
+ self .mock_response (self .URL , 'image/jpeg' , 'image/png' )
78
97
self .source .fetch_image (self .candidate , self .extra )
79
98
self .assertEqual (os .path .splitext (self .candidate .path )[1 ], b'.png' )
80
99
self .assertExists (self .candidate .path )
@@ -128,7 +147,7 @@ def test_precedence_amongst_correct_files(self):
128
147
self .assertEqual (candidates , paths )
129
148
130
149
131
- class CombinedTest (UseThePlugin ):
150
+ class CombinedTest (FetchImageHelper , UseThePlugin ):
132
151
ASIN = 'xxxx'
133
152
MBID = 'releaseid'
134
153
AMAZON_URL = 'http://images.amazon.com/images/P/{0}.01.LZZZZZZZ.jpg' \
@@ -143,13 +162,6 @@ def setUp(self):
143
162
self .dpath = os .path .join (self .temp_dir , b'arttest' )
144
163
os .mkdir (self .dpath )
145
164
146
- @responses .activate
147
- def run (self , * args , ** kwargs ):
148
- super (CombinedTest , self ).run (* args , ** kwargs )
149
-
150
- def mock_response (self , url , content_type = 'image/jpeg' ):
151
- responses .add (responses .GET , url , content_type = content_type )
152
-
153
165
def test_main_interface_returns_amazon_art (self ):
154
166
self .mock_response (self .AMAZON_URL )
155
167
album = _common .Bag (asin = self .ASIN )
0 commit comments