1717# Import the module under test
1818sys .path .insert (0 , '../internal_filesystem/lib' )
1919import mpos .net .download_manager as DownloadManager
20+ from mpos .testing .mocks import MockDownloadManager
2021
2122
2223class TestDownloadManager (unittest .TestCase ):
@@ -222,91 +223,74 @@ async def track_progress(percent):
222223 asyncio .run (run_test ())
223224
224225 def test_progress_with_explicit_total_size (self ):
225- """Test progress tracking with explicitly provided total_size."""
226+ """Test progress tracking with explicitly provided total_size using mock ."""
226227 import asyncio
227- import unittest
228228
229229 async def run_test ():
230+ # Use mock to avoid external service dependency
231+ mock_dm = MockDownloadManager ()
232+ mock_dm .set_download_data (b'x' * 3072 ) # 3KB of data
233+
230234 progress_calls = []
231235
232236 async def track_progress (percent ):
233237 progress_calls .append (percent )
234238
235- try :
236- data = await DownloadManager .download_url (
237- "https://httpbin.org/bytes/3072" , # 3KB
238- total_size = 3072 ,
239- progress_callback = track_progress
240- )
239+ data = await mock_dm .download_url (
240+ "https://example.com/bytes/3072" ,
241+ total_size = 3072 ,
242+ progress_callback = track_progress
243+ )
241244
242- self .assertIsNotNone (data )
243- self .assertTrue (len (progress_calls ) > 0 )
244- except RuntimeError as e :
245- # Skip test if httpbin.org is unavailable (HTTP 502, etc.)
246- if "HTTP" in str (e ):
247- self .skipTest (f"httpbin.org unavailable: { e } " )
248- raise
245+ self .assertIsNotNone (data )
246+ self .assertTrue (len (progress_calls ) > 0 )
247+ self .assertEqual (len (data ), 3072 )
249248
250249 asyncio .run (run_test ())
251250
252251 # ==================== Error Handling Tests ====================
253252
254253 def test_http_error_status (self ):
255- """Test handling of HTTP error status codes."""
254+ """Test handling of HTTP error status codes using mock ."""
256255 import asyncio
257256
258257 async def run_test ():
259- # Request 404 error from httpbin - should raise RuntimeError
260- try :
261- with self .assertRaises (RuntimeError ) as context :
262- data = await DownloadManager .download_url ("https://httpbin.org/status/404" )
263-
264- # Should raise RuntimeError with status code
265- # Accept either 404 or 502 (if httpbin is down)
266- error_msg = str (context .exception )
267- if "502" in error_msg :
268- self .skipTest (f"httpbin.org unavailable: { error_msg } " )
269- self .assertIn ("404" , error_msg )
270- except RuntimeError as e :
271- # If we get a 502 error, skip the test
272- if "502" in str (e ):
273- self .skipTest (f"httpbin.org unavailable: { e } " )
274- raise
258+ # Use mock to avoid external service dependency
259+ mock_dm = MockDownloadManager ()
260+ # Set fail_after_bytes to 0 to trigger immediate failure
261+ mock_dm .set_fail_after_bytes (0 )
262+
263+ # Should raise RuntimeError for HTTP error
264+ with self .assertRaises (OSError ):
265+ data = await mock_dm .download_url ("https://example.com/status/404" )
275266
276267 asyncio .run (run_test ())
277268
278269 def test_http_error_with_file_output (self ):
279- """Test that file download raises exception on HTTP error."""
270+ """Test that file download raises exception on HTTP error using mock ."""
280271 import asyncio
281272
282273 async def run_test ():
283274 outfile = f"{ self .temp_dir } /error_test.bin"
275+
276+ # Use mock to avoid external service dependency
277+ mock_dm = MockDownloadManager ()
278+ # Set fail_after_bytes to 0 to trigger immediate failure
279+ mock_dm .set_fail_after_bytes (0 )
280+
281+ # Should raise OSError for network error
282+ with self .assertRaises (OSError ):
283+ success = await mock_dm .download_url (
284+ "https://example.com/status/500" ,
285+ outfile = outfile
286+ )
284287
285- # Should raise RuntimeError for HTTP 500
288+ # File should not be created
286289 try :
287- with self .assertRaises (RuntimeError ) as context :
288- success = await DownloadManager .download_url (
289- "https://httpbin.org/status/500" ,
290- outfile = outfile
291- )
292-
293- # Should raise RuntimeError with status code
294- error_msg = str (context .exception )
295- if "502" in error_msg :
296- self .skipTest (f"httpbin.org unavailable: { error_msg } " )
297- self .assertIn ("500" , error_msg )
298-
299- # File should not be created
300- try :
301- os .stat (outfile )
302- self .fail ("File should not exist after failed download" )
303- except OSError :
304- pass # Expected - file doesn't exist
305- except RuntimeError as e :
306- # If we get a 502 error, skip the test
307- if "502" in str (e ):
308- self .skipTest (f"httpbin.org unavailable: { e } " )
309- raise
290+ os .stat (outfile )
291+ self .fail ("File should not exist after failed download" )
292+ except OSError :
293+ pass # Expected - file doesn't exist
310294
311295 asyncio .run (run_test ())
312296
@@ -345,12 +329,15 @@ async def run_test():
345329 # ==================== Edge Cases Tests ====================
346330
347331 def test_empty_response (self ):
348- """Test handling of empty (0-byte) downloads."""
332+ """Test handling of empty (0-byte) downloads using mock ."""
349333 import asyncio
350334
351335 async def run_test ():
352- # Download 0 bytes
353- data = await DownloadManager .download_url ("https://httpbin.org/bytes/0" )
336+ # Use mock to avoid external service dependency
337+ mock_dm = MockDownloadManager ()
338+ mock_dm .set_download_data (b'' ) # Empty data
339+
340+ data = await mock_dm .download_url ("https://example.com/bytes/0" )
354341
355342 self .assertIsNotNone (data )
356343 self .assertEqual (len (data ), 0 )
@@ -359,12 +346,15 @@ async def run_test():
359346 asyncio .run (run_test ())
360347
361348 def test_small_download (self ):
362- """Test downloading very small files (smaller than chunk size)."""
349+ """Test downloading very small files (smaller than chunk size) using mock ."""
363350 import asyncio
364351
365352 async def run_test ():
366- # Download 10 bytes (much smaller than 1KB chunk size)
367- data = await DownloadManager .download_url ("https://httpbin.org/bytes/10" )
353+ # Use mock to avoid external service dependency
354+ mock_dm = MockDownloadManager ()
355+ mock_dm .set_download_data (b'x' * 10 ) # 10 bytes
356+
357+ data = await mock_dm .download_url ("https://example.com/bytes/10" )
368358
369359 self .assertIsNotNone (data )
370360 self .assertEqual (len (data ), 10 )
0 commit comments