@@ -45,16 +45,26 @@ 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
+ def mock_response (self , url , content_type = 'image/jpeg' , file_type = None ):
57
+ IMAGEHEADER = {'image/jpeg' : b'\x00 ' * 6 + b'JFIF' ,
58
+ 'image/png' : b'\211 PNG\r \n \032 \n ' , }
59
+ if file_type is None :
60
+ file_type = content_type
61
+ responses .add (responses .GET , url ,
62
+ content_type = content_type ,
63
+ body = IMAGEHEADER .get (file_type , b'\x00 ' * 32 ))
54
64
55
- def mock_response ( self , content_type ):
56
- responses . add ( responses . GET , self . URL ,
57
- content_type = content_type )
65
+
66
+ class FetchImageTest ( FetchImageHelper , UseThePlugin ):
67
+ URL = 'http://example.com/test.jpg'
58
68
59
69
def setUp (self ):
60
70
super (FetchImageTest , self ).setUp ()
@@ -64,17 +74,23 @@ def setUp(self):
64
74
self .candidate = fetchart .Candidate (logger , url = self .URL )
65
75
66
76
def test_invalid_type_returns_none (self ):
67
- self .mock_response ('image/watercolour' )
77
+ self .mock_response (self . URL , 'image/watercolour' )
68
78
self .source .fetch_image (self .candidate , self .extra )
69
79
self .assertEqual (self .candidate .path , None )
70
80
71
81
def test_jpeg_type_returns_path (self ):
72
- self .mock_response ('image/jpeg' )
82
+ self .mock_response (self . URL , 'image/jpeg' )
73
83
self .source .fetch_image (self .candidate , self .extra )
74
84
self .assertNotEqual (self .candidate .path , None )
75
85
76
86
def test_extension_set_by_content_type (self ):
77
- self .mock_response ('image/png' )
87
+ self .mock_response (self .URL , 'image/png' )
88
+ self .source .fetch_image (self .candidate , self .extra )
89
+ self .assertEqual (os .path .splitext (self .candidate .path )[1 ], b'.png' )
90
+ self .assertExists (self .candidate .path )
91
+
92
+ def test_does_not_rely_on_server_content_type (self ):
93
+ self .mock_response (self .URL , 'image/jpeg' , 'image/png' )
78
94
self .source .fetch_image (self .candidate , self .extra )
79
95
self .assertEqual (os .path .splitext (self .candidate .path )[1 ], b'.png' )
80
96
self .assertExists (self .candidate .path )
@@ -128,7 +144,7 @@ def test_precedence_amongst_correct_files(self):
128
144
self .assertEqual (candidates , paths )
129
145
130
146
131
- class CombinedTest (UseThePlugin ):
147
+ class CombinedTest (FetchImageHelper , UseThePlugin ):
132
148
ASIN = 'xxxx'
133
149
MBID = 'releaseid'
134
150
AMAZON_URL = 'http://images.amazon.com/images/P/{0}.01.LZZZZZZZ.jpg' \
@@ -143,13 +159,6 @@ def setUp(self):
143
159
self .dpath = os .path .join (self .temp_dir , b'arttest' )
144
160
os .mkdir (self .dpath )
145
161
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
162
def test_main_interface_returns_amazon_art (self ):
154
163
self .mock_response (self .AMAZON_URL )
155
164
album = _common .Bag (asin = self .ASIN )
0 commit comments