1616<Purpose>
1717 Unit test for RequestsFetcher.
1818"""
19+ # Help with Python 2 compatibility, where the '/' operator performs
20+ # integer division.
21+ from __future__ import division
1922
2023import logging
2124import os
2225import io
2326import sys
2427import unittest
2528import tempfile
29+ import math
2630
2731import tuf
2832import tuf .exceptions
3135
3236from tests import utils
3337
34-
3538logger = logging .getLogger (__name__ )
3639
3740
@@ -77,15 +80,12 @@ def tearDown(self):
7780
7881 # Test: Normal case.
7982 def test_fetch (self ):
80-
8183 for chunk in self .fetcher .fetch (self .url , self .file_length ):
8284 self .temp_file .write (chunk )
8385
8486 self .temp_file .seek (0 )
8587 temp_file_data = self .temp_file .read ().decode ('utf-8' )
8688 self .assertEqual (self .file_contents , temp_file_data )
87- self .assertEqual (self .file_length , len (temp_file_data ))
88-
8989
9090 # Test if fetcher downloads file up to a required length
9191 def test_fetch_restricted_length (self ):
@@ -96,7 +96,7 @@ def test_fetch_restricted_length(self):
9696 self .assertEqual (self .temp_file .tell (), self .file_length - 4 )
9797
9898
99- # Test if fetcher does not downlad more than actual file length
99+ # Test that fetcher does not download more than actual file length
100100 def test_fetch_upper_length (self ):
101101 for chunk in self .fetcher .fetch (self .url , self .file_length + 4 ):
102102 self .temp_file .write (chunk )
@@ -107,9 +107,34 @@ def test_fetch_upper_length(self):
107107
108108 # Test incorrect URL parsing
109109 def test_url_parsing (self ):
110- with self .assertRaises (tuf .exceptions .URLParsingError ) as cm :
111- for chunk in self .fetcher .fetch (self .random_string (), self .file_length ):
112- self .temp_file .write (chunk )
110+ with self .assertRaises (tuf .exceptions .URLParsingError ):
111+ self .fetcher .fetch (self .random_string (), self .file_length )
112+
113+
114+ # Test: Normal case with url data downloaded in more than one chunk
115+ def test_fetch_in_chunks (self ):
116+ # Set smaller chunk size to ensure that the file will be downloaded
117+ # in more than one chunk
118+ default_chunk_size = tuf .settings .CHUNK_SIZE
119+ tuf .settings .CHUNK_SIZE = 4
120+
121+ # expected_chunks_count: 3
122+ expected_chunks_count = math .ceil (self .file_length / tuf .settings .CHUNK_SIZE )
123+ self .assertEqual (expected_chunks_count , 3 )
124+
125+ chunks_count = 0
126+ for chunk in self .fetcher .fetch (self .url , self .file_length ):
127+ self .temp_file .write (chunk )
128+ chunks_count += 1
129+
130+ self .temp_file .seek (0 )
131+ temp_file_data = self .temp_file .read ().decode ('utf-8' )
132+ self .assertEqual (self .file_contents , temp_file_data )
133+ # Check that we calculate chunks as expected
134+ self .assertEqual (chunks_count , expected_chunks_count )
135+
136+ # Restore default settings
137+ tuf .settings .CHUNK_SIZE = default_chunk_size
113138
114139
115140
0 commit comments