28
28
from google .auth .credentials import AnonymousCredentials
29
29
from google .oauth2 .service_account import Credentials
30
30
31
+ from google .cloud .storage import _helpers
31
32
from google .cloud .storage ._helpers import STORAGE_EMULATOR_ENV_VAR
32
33
from google .cloud .storage ._helpers import _get_default_headers
33
- from google .cloud .storage import _helpers
34
+ from google .cloud .storage . _http import Connection
34
35
from google .cloud .storage .retry import DEFAULT_RETRY
35
36
from google .cloud .storage .retry import DEFAULT_RETRY_IF_GENERATION_SPECIFIED
36
37
from tests .unit .test__helpers import GCCL_INVOCATION_TEST_CONST
@@ -119,7 +120,6 @@ def _make_one(self, *args, **kw):
119
120
120
121
def test_ctor_connection_type (self ):
121
122
from google .cloud ._http import ClientInfo
122
- from google .cloud .storage ._http import Connection
123
123
124
124
PROJECT = "PROJECT"
125
125
credentials = _make_credentials ()
@@ -179,8 +179,6 @@ def test_ctor_w_client_options_object(self):
179
179
)
180
180
181
181
def test_ctor_wo_project (self ):
182
- from google .cloud .storage ._http import Connection
183
-
184
182
PROJECT = "PROJECT"
185
183
credentials = _make_credentials (project = PROJECT )
186
184
@@ -193,8 +191,6 @@ def test_ctor_wo_project(self):
193
191
self .assertEqual (list (client ._batch_stack ), [])
194
192
195
193
def test_ctor_w_project_explicit_none (self ):
196
- from google .cloud .storage ._http import Connection
197
-
198
194
credentials = _make_credentials ()
199
195
200
196
client = self ._make_one (project = None , credentials = credentials )
@@ -207,7 +203,6 @@ def test_ctor_w_project_explicit_none(self):
207
203
208
204
def test_ctor_w_client_info (self ):
209
205
from google .cloud ._http import ClientInfo
210
- from google .cloud .storage ._http import Connection
211
206
212
207
credentials = _make_credentials ()
213
208
client_info = ClientInfo ()
@@ -239,8 +234,40 @@ def test_ctor_mtls(self):
239
234
self .assertEqual (client ._connection .ALLOW_AUTO_SWITCH_TO_MTLS_URL , False )
240
235
self .assertEqual (client ._connection .API_BASE_URL , "http://foo" )
241
236
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
+
242
269
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
244
271
host = "http://localhost:8080"
245
272
environ = {STORAGE_EMULATOR_ENV_VAR : host }
246
273
with mock .patch ("os.environ" , environ ):
@@ -250,16 +277,8 @@ def test_ctor_w_emulator_wo_project(self):
250
277
self .assertEqual (client ._connection .API_BASE_URL , host )
251
278
self .assertIsInstance (client ._connection .credentials , AnonymousCredentials )
252
279
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
-
261
280
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
263
282
host = "http://localhost:8080"
264
283
environ_project = "environ-project"
265
284
environ = {
@@ -289,9 +308,17 @@ def test_ctor_w_emulator_w_project_arg(self):
289
308
self .assertEqual (client ._connection .API_BASE_URL , host )
290
309
self .assertIsInstance (client ._connection .credentials , AnonymousCredentials )
291
310
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 )
294
317
318
+ self .assertEqual (client ._connection .API_BASE_URL , host )
319
+ self .assertIs (client ._connection .credentials , credentials )
320
+
321
+ def test_create_anonymous_client (self ):
295
322
klass = self ._get_target_class ()
296
323
client = klass .create_anonymous_client ()
297
324
0 commit comments