@mock.patch('google.auth.transport.requests.AuthorizedSession')
def test_download_to_file_with_failure(self, fake_session_factory):
    from google.cloud import exceptions
    blob_name = 'blob-name'
    transport = mock.Mock(spec=['request'])
    bad_response_headers = {'Content-Length': '9', 'Content-Type': 'text/html; charset=UTF-8'}
    transport.request.return_value = self._mock_requests_response(http_client.NOT_FOUND, bad_response_headers, content=b'Not found')
    fake_session_factory.return_value = transport
    client = mock.Mock(_credentials=_make_credentials(), spec=['_credentials'])
    bucket = _Bucket(client)
    blob = self._make_one(blob_name, bucket=bucket)
    blob._properties['mediaLink'] = 'http://test.invalid'
    file_obj = io.BytesIO()
    with self.assertRaises(exceptions.NotFound):
        blob.download_to_file(file_obj)
    self.assertEqual(file_obj.tell(), 0)
    fake_session_factory.assert_called_once_with(client._credentials)
    transport.request.assert_called_once_with('GET', blob.media_link, data=None, headers={})
@mock.patch('google.auth.transport.requests.AuthorizedSession')
def test_download_to_file_with_failure(self, fake_session_factory):
    from google.cloud import exceptions
    blob_name = 'blob-name'
    transport = mock.Mock(spec=['request'])
    bad_response_headers = {'Content-Length': '9', 'Content-Type': 'text/html; charset=UTF-8'}
    transport.request.return_value = self._mock_requests_response(http_client.NOT_FOUND, bad_response_headers, content=b'Not found')
    fake_session_factory.return_value = transport
    client = mock.Mock(_credentials=_make_credentials(), spec=['_credentials'])
    bucket = _Bucket(client)
    blob = self._make_one(blob_name, bucket=bucket)
    blob._properties['mediaLink'] = 'http://test.invalid'
    file_obj = io.BytesIO()
    with self.assertRaises(exceptions.NotFound):
        blob.download_to_file(file_obj)
    self.assertEqual(file_obj.tell(), 0)
    fake_session_factory.assert_called_once_with(client._credentials)
    transport.request.assert_called_once_with('GET', blob.media_link, data=None, headers={})