42
42
DATA_DIR = os .path .join (os .path .dirname (__file__ ), 'data' )
43
43
44
44
45
- # TODO(craigcitro): This is duplicated from
46
- # googleapiclient.test_discovery; consolidate these definitions.
47
45
def assertUrisEqual (testcase , expected , actual ):
48
46
"""Test that URIs are the same, up to reordering of query parameters."""
49
47
expected = urllib .parse .urlparse (expected )
@@ -357,67 +355,41 @@ def test_environment_caching(self):
357
355
# is cached.
358
356
self .assertTrue (client ._in_gae_environment ())
359
357
360
- def _environment_check_gce_helper (self , status_ok = True , socket_error = False ,
358
+ def _environment_check_gce_helper (self , status_ok = True ,
361
359
server_software = '' ):
362
- response = mock .Mock ()
363
360
if status_ok :
364
- response .status = http_client .OK
365
- response .getheader = mock .Mock (
366
- name = 'getheader' ,
367
- return_value = client ._DESIRED_METADATA_FLAVOR )
361
+ headers = {'status' : http_client .OK }
362
+ headers .update (client ._GCE_HEADERS )
368
363
else :
369
- response .status = http_client .NOT_FOUND
370
-
371
- connection = mock .Mock ()
372
- connection .getresponse = mock .Mock (name = 'getresponse' ,
373
- return_value = response )
374
- if socket_error :
375
- connection .getresponse .side_effect = socket .error ()
364
+ headers = {'status' : http_client .NOT_FOUND }
376
365
366
+ http = http_mock .HttpMock (headers = headers )
377
367
with mock .patch ('oauth2client.client.os' ) as os_module :
378
368
os_module .environ = {client ._SERVER_SOFTWARE : server_software }
379
- with mock .patch ('oauth2client.client.six' ) as six_module :
380
- http_client_module = six_module .moves .http_client
381
- http_client_module .HTTPConnection = mock .Mock (
382
- name = 'HTTPConnection' , return_value = connection )
383
-
369
+ with mock .patch ('oauth2client.transport.get_http_object' ,
370
+ return_value = http ) as new_http :
384
371
if server_software == '' :
385
372
self .assertFalse (client ._in_gae_environment ())
386
373
else :
387
374
self .assertTrue (client ._in_gae_environment ())
388
375
389
- if status_ok and not socket_error and server_software == '' :
376
+ if status_ok and server_software == '' :
390
377
self .assertTrue (client ._in_gce_environment ())
391
378
else :
392
379
self .assertFalse (client ._in_gce_environment ())
393
380
381
+ # Verify mocks.
394
382
if server_software == '' :
395
- http_client_module .HTTPConnection .assert_called_once_with (
396
- client ._GCE_METADATA_HOST ,
383
+ new_http .assert_called_once_with (
397
384
timeout = client .GCE_METADATA_TIMEOUT )
398
- connection .getresponse .assert_called_once_with ()
399
- # Remaining calls are not "getresponse"
400
- headers = {
401
- client ._METADATA_FLAVOR_HEADER : (
402
- client ._DESIRED_METADATA_FLAVOR ),
403
- }
404
- self .assertEqual (connection .method_calls , [
405
- mock .call .request ('GET' , '/' ,
406
- headers = headers ),
407
- mock .call .close (),
408
- ])
409
- self .assertEqual (response .method_calls , [])
410
- if status_ok and not socket_error :
411
- response .getheader .assert_called_once_with (
412
- client ._METADATA_FLAVOR_HEADER )
385
+ self .assertEqual (http .requests , 1 )
386
+ self .assertEqual (http .uri , client ._GCE_METADATA_URI )
387
+ self .assertEqual (http .method , 'GET' )
388
+ self .assertIsNone (http .body )
389
+ self .assertEqual (http .headers , client ._GCE_HEADERS )
413
390
else :
414
- self .assertEqual (
415
- http_client_module .HTTPConnection .mock_calls , [])
416
- self .assertEqual (connection .getresponse .mock_calls , [])
417
- # Remaining calls are not "getresponse"
418
- self .assertEqual (connection .method_calls , [])
419
- self .assertEqual (response .method_calls , [])
420
- self .assertEqual (response .getheader .mock_calls , [])
391
+ new_http .assert_not_called ()
392
+ self .assertEqual (http .requests , 0 )
421
393
422
394
def test_environment_check_gce_production (self ):
423
395
self ._environment_check_gce_helper (status_ok = True )
@@ -426,8 +398,21 @@ def test_environment_check_gce_prod_with_working_gae_imports(self):
426
398
with mock_module_import ('google.appengine' ):
427
399
self ._environment_check_gce_helper (status_ok = True )
428
400
429
- def test_environment_check_gce_timeout (self ):
430
- self ._environment_check_gce_helper (socket_error = True )
401
+ @mock .patch ('oauth2client.client.os.environ' ,
402
+ new = {client ._SERVER_SOFTWARE : '' })
403
+ @mock .patch ('oauth2client.transport.get_http_object' ,
404
+ return_value = object ())
405
+ @mock .patch ('oauth2client.transport.request' ,
406
+ side_effect = socket .timeout ())
407
+ def test_environment_check_gce_timeout (self , mock_request , new_http ):
408
+ self .assertFalse (client ._in_gae_environment ())
409
+ self .assertFalse (client ._in_gce_environment ())
410
+
411
+ # Verify mocks.
412
+ new_http .assert_called_once_with (timeout = client .GCE_METADATA_TIMEOUT )
413
+ mock_request .assert_called_once_with (
414
+ new_http .return_value , client ._GCE_METADATA_URI ,
415
+ headers = client ._GCE_HEADERS )
431
416
432
417
def test_environ_check_gae_module_unknown (self ):
433
418
with mock_module_import ('google.appengine' ):
0 commit comments