2828from google .auth .credentials import AnonymousCredentials
2929from google .oauth2 .service_account import Credentials
3030
31+ from google .cloud .storage import _helpers
3132from google .cloud .storage ._helpers import STORAGE_EMULATOR_ENV_VAR
3233from google .cloud .storage ._helpers import _get_default_headers
33- from google .cloud .storage import _helpers
34+ from google .cloud .storage . _http import Connection
3435from google .cloud .storage .retry import DEFAULT_RETRY
3536from google .cloud .storage .retry import DEFAULT_RETRY_IF_GENERATION_SPECIFIED
3637from tests .unit .test__helpers import GCCL_INVOCATION_TEST_CONST
@@ -119,7 +120,6 @@ def _make_one(self, *args, **kw):
119120
120121 def test_ctor_connection_type (self ):
121122 from google .cloud ._http import ClientInfo
122- from google .cloud .storage ._http import Connection
123123
124124 PROJECT = "PROJECT"
125125 credentials = _make_credentials ()
@@ -179,8 +179,6 @@ def test_ctor_w_client_options_object(self):
179179 )
180180
181181 def test_ctor_wo_project (self ):
182- from google .cloud .storage ._http import Connection
183-
184182 PROJECT = "PROJECT"
185183 credentials = _make_credentials (project = PROJECT )
186184
@@ -193,8 +191,6 @@ def test_ctor_wo_project(self):
193191 self .assertEqual (list (client ._batch_stack ), [])
194192
195193 def test_ctor_w_project_explicit_none (self ):
196- from google .cloud .storage ._http import Connection
197-
198194 credentials = _make_credentials ()
199195
200196 client = self ._make_one (project = None , credentials = credentials )
@@ -207,7 +203,6 @@ def test_ctor_w_project_explicit_none(self):
207203
208204 def test_ctor_w_client_info (self ):
209205 from google .cloud ._http import ClientInfo
210- from google .cloud .storage ._http import Connection
211206
212207 credentials = _make_credentials ()
213208 client_info = ClientInfo ()
@@ -239,8 +234,40 @@ def test_ctor_mtls(self):
239234 self .assertEqual (client ._connection .ALLOW_AUTO_SWITCH_TO_MTLS_URL , False )
240235 self .assertEqual (client ._connection .API_BASE_URL , "http://foo" )
241236
237+ def test_ctor_w_custom_endpoint_use_auth (self ):
238+ custom_endpoint = "storage-example.p.googleapis.com"
239+ client = self ._make_one (client_options = {"api_endpoint" : custom_endpoint })
240+ self .assertEqual (client ._connection .API_BASE_URL , custom_endpoint )
241+ self .assertIsNotNone (client .project )
242+ self .assertIsInstance (client ._connection , Connection )
243+ self .assertIsNotNone (client ._connection .credentials )
244+ self .assertNotIsInstance (client ._connection .credentials , AnonymousCredentials )
245+
246+ def test_ctor_w_custom_endpoint_bypass_auth (self ):
247+ custom_endpoint = "storage-example.p.googleapis.com"
248+ client = self ._make_one (
249+ client_options = {"api_endpoint" : custom_endpoint },
250+ use_auth_w_custom_endpoint = False ,
251+ )
252+ self .assertEqual (client ._connection .API_BASE_URL , custom_endpoint )
253+ self .assertEqual (client .project , None )
254+ self .assertIsInstance (client ._connection , Connection )
255+ self .assertIsInstance (client ._connection .credentials , AnonymousCredentials )
256+
257+ def test_ctor_w_custom_endpoint_w_credentials (self ):
258+ PROJECT = "PROJECT"
259+ custom_endpoint = "storage-example.p.googleapis.com"
260+ credentials = _make_credentials (project = PROJECT )
261+ client = self ._make_one (
262+ credentials = credentials , client_options = {"api_endpoint" : custom_endpoint }
263+ )
264+ self .assertEqual (client ._connection .API_BASE_URL , custom_endpoint )
265+ self .assertEqual (client .project , PROJECT )
266+ self .assertIsInstance (client ._connection , Connection )
267+ self .assertIs (client ._connection .credentials , credentials )
268+
242269 def test_ctor_w_emulator_wo_project (self ):
243- # avoids authentication if STORAGE_EMULATOR_ENV_VAR is set
270+ # bypasses authentication if STORAGE_EMULATOR_ENV_VAR is set
244271 host = "http://localhost:8080"
245272 environ = {STORAGE_EMULATOR_ENV_VAR : host }
246273 with mock .patch ("os.environ" , environ ):
@@ -250,16 +277,8 @@ def test_ctor_w_emulator_wo_project(self):
250277 self .assertEqual (client ._connection .API_BASE_URL , host )
251278 self .assertIsInstance (client ._connection .credentials , AnonymousCredentials )
252279
253- # avoids authentication if storage emulator is set through api_endpoint
254- client = self ._make_one (
255- client_options = {"api_endpoint" : "http://localhost:8080" }
256- )
257- self .assertIsNone (client .project )
258- self .assertEqual (client ._connection .API_BASE_URL , host )
259- self .assertIsInstance (client ._connection .credentials , AnonymousCredentials )
260-
261280 def test_ctor_w_emulator_w_environ_project (self ):
262- # avoids authentication and infers the project from the environment
281+ # bypasses authentication and infers the project from the environment
263282 host = "http://localhost:8080"
264283 environ_project = "environ-project"
265284 environ = {
@@ -289,9 +308,17 @@ def test_ctor_w_emulator_w_project_arg(self):
289308 self .assertEqual (client ._connection .API_BASE_URL , host )
290309 self .assertIsInstance (client ._connection .credentials , AnonymousCredentials )
291310
292- def test_create_anonymous_client (self ):
293- from google .cloud .storage ._http import Connection
311+ def test_ctor_w_emulator_w_credentials (self ):
312+ host = "http://localhost:8080"
313+ environ = {STORAGE_EMULATOR_ENV_VAR : host }
314+ credentials = _make_credentials ()
315+ with mock .patch ("os.environ" , environ ):
316+ client = self ._make_one (credentials = credentials )
294317
318+ self .assertEqual (client ._connection .API_BASE_URL , host )
319+ self .assertIs (client ._connection .credentials , credentials )
320+
321+ def test_create_anonymous_client (self ):
295322 klass = self ._get_target_class ()
296323 client = klass .create_anonymous_client ()
297324
@@ -1269,6 +1296,28 @@ def test_create_bucket_w_environ_project_w_emulator(self):
12691296 _target_object = bucket ,
12701297 )
12711298
1299+ def test_create_bucket_w_custom_endpoint (self ):
1300+ custom_endpoint = "storage-example.p.googleapis.com"
1301+ client = self ._make_one (client_options = {"api_endpoint" : custom_endpoint })
1302+ bucket_name = "bucket-name"
1303+ api_response = {"name" : bucket_name }
1304+ client ._post_resource = mock .Mock ()
1305+ client ._post_resource .return_value = api_response
1306+
1307+ bucket = client .create_bucket (bucket_name )
1308+
1309+ expected_path = "/b"
1310+ expected_data = api_response
1311+ expected_query_params = {"project" : client .project }
1312+ client ._post_resource .assert_called_once_with (
1313+ expected_path ,
1314+ expected_data ,
1315+ query_params = expected_query_params ,
1316+ timeout = self ._get_default_timeout (),
1317+ retry = DEFAULT_RETRY ,
1318+ _target_object = bucket ,
1319+ )
1320+
12721321 def test_create_bucket_w_conflict_w_user_project (self ):
12731322 from google .cloud .exceptions import Conflict
12741323
@@ -2055,6 +2104,37 @@ def test_list_buckets_w_environ_project_w_emulator(self):
20552104 retry = DEFAULT_RETRY ,
20562105 )
20572106
2107+ def test_list_buckets_w_custom_endpoint (self ):
2108+ from google .cloud .storage .client import _item_to_bucket
2109+
2110+ custom_endpoint = "storage-example.p.googleapis.com"
2111+ client = self ._make_one (client_options = {"api_endpoint" : custom_endpoint })
2112+ client ._list_resource = mock .Mock (spec = [])
2113+
2114+ iterator = client .list_buckets ()
2115+
2116+ self .assertIs (iterator , client ._list_resource .return_value )
2117+
2118+ expected_path = "/b"
2119+ expected_item_to_value = _item_to_bucket
2120+ expected_page_token = None
2121+ expected_max_results = None
2122+ expected_page_size = None
2123+ expected_extra_params = {
2124+ "project" : client .project ,
2125+ "projection" : "noAcl" ,
2126+ }
2127+ client ._list_resource .assert_called_once_with (
2128+ expected_path ,
2129+ expected_item_to_value ,
2130+ page_token = expected_page_token ,
2131+ max_results = expected_max_results ,
2132+ extra_params = expected_extra_params ,
2133+ page_size = expected_page_size ,
2134+ timeout = self ._get_default_timeout (),
2135+ retry = DEFAULT_RETRY ,
2136+ )
2137+
20582138 def test_list_buckets_w_defaults (self ):
20592139 from google .cloud .storage .client import _item_to_bucket
20602140
0 commit comments