@patch('pip.download.unpack_file')
def test_unpack_http_url_bad_downloaded_checksum(mock_unpack_file):
 base_url = 'http://www.example.com/somepackage.tgz'
 contents = 'downloaded'
 download_hash = hashlib.new('sha1', contents)
 link = Link(((base_url + '#sha1=') + download_hash.hexdigest()))
 session = Mock()
 session.get = Mock()
 response = session.get.return_value = MockResponse(contents)
 response.headers = {'content-type': 'application/x-tar'}
 response.url = base_url
 download_dir = mkdtemp()
 try:
  downloaded_file = os.path.join(download_dir, 'somepackage.tgz')
  _write_file(downloaded_file, 'some contents')
  unpack_http_url(link, 'location', download_dir=download_dir, session=session, hashes=Hashes({'sha1': [download_hash.hexdigest()]}))
  session.get.assert_called_once_with('http://www.example.com/somepackage.tgz', headers={'Accept-Encoding': 'identity'}, stream=True)
  with open(downloaded_file) as fh:
   assert (fh.read() == 'downloaded')
 finally:
  rmtree(download_dir)

@ patch ( 'pip.download.unpack_file' ) def test_unpack_http_url_bad_downloaded_checksum ( mock_unpack_file ) : base_url = 'http://www.example.com/somepackage.tgz' contents = 'downloaded' download_hash = hashlib . new ( 'sha1' , contents ) link = Link ( ( ( base_url + '#sha1=' ) + download_hash . hexdigest ( ) ) ) session = Mock ( ) session . get = Mock ( ) response = session . get . return_value = MockResponse ( contents ) response . headers = { 'content-type' : 'application/x-tar' } response . url = base_url download_dir = mkdtemp ( ) try : downloaded_file = os . path . join ( download_dir , 'somepackage.tgz' ) _write_file ( downloaded_file , 'some contents' ) unpack_http_url ( link , 'location' , download_dir = download_dir , session = session , hashes = Hashes ( { 'sha1' : [ download_hash . hexdigest ( ) ] } ) ) session . get . assert_called_once_with ( 'http://www.example.com/somepackage.tgz' , headers = { 'Accept-Encoding' : 'identity' } , stream = True ) with open ( downloaded_file ) as fh : assert ( fh . read ( ) == 'downloaded' ) finally : rmtree ( download_dir )
if already-downloaded file has bad checksum .