ApiClientTest

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.

Defined (1)

The class is defined in the following location(s).

/lib/vendor/google/apiclient/tests/general/ApiClientTest.php  
  1. class ApiClientTest extends BaseTest { 
  2. public function testClient() { 
  3. $client = new Google_Client(); 
  4. $client->setAccessType('foo'); 
  5. $client->setDeveloperKey('foo'); 
  6. $req = new Google_Http_Request('http://foo.com'); 
  7. $client->getAuth()->sign($req); 
  8. $params = $req->getQueryParams(); 
  9. $this->assertEquals('foo', $params['key']); 
  10.  
  11. $client->setAccessToken(json_encode(array('access_token' => '1'))); 
  12. $this->assertEquals("{\"access_token\":\"1\"}", $client->getAccessToken()); 
  13.  
  14. /** 
  15. * @expectedException Google_Auth_Exception 
  16. */ 
  17. public function testPrepareInvalidScopes() { 
  18. $client = new Google_Client(); 
  19.  
  20. $scopes = $client->prepareScopes(); 
  21. $this->assertEquals("", $scopes); 
  22.  
  23. public function testNoAuthIsNull() { 
  24. $client = new Google_Client(); 
  25.  
  26. $this->assertNull($client->getAccessToken()); 
  27.  
  28. public function testPrepareService() { 
  29. $client = new Google_Client(); 
  30. $client->setScopes(array("scope1", "scope2")); 
  31. $scopes = $client->prepareScopes(); 
  32. $this->assertEquals("scope1 scope2", $scopes); 
  33.  
  34. $client->setScopes(array("", "scope2")); 
  35. $scopes = $client->prepareScopes(); 
  36. $this->assertEquals(" scope2", $scopes); 
  37.  
  38. $client->setScopes("scope2"); 
  39. $client->addScope("scope3"); 
  40. $client->addScope(array("scope4", "scope5")); 
  41. $scopes = $client->prepareScopes(); 
  42. $this->assertEquals("scope2 scope3 scope4 scope5", $scopes); 
  43.  
  44. $client->setClientId('test1'); 
  45. $client->setRedirectUri('http://localhost/'); 
  46. $client->setScopes(array("http://test.com", "scope2")); 
  47. $scopes = $client->prepareScopes(); 
  48. $this->assertEquals("http://test.com scope2", $scopes); 
  49. $this->assertEquals('' 
  50. . 'https://accounts.google.com/o/oauth2/auth' 
  51. . '?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2F' 
  52. . '&client_id=test1' 
  53. . '&scope=http%3A%2F%2Ftest.com+scope2&access_type=online' 
  54. . '&approval_prompt=auto', $client->createAuthUrl()); 
  55.  
  56. // This should not trigger a request. 
  57. $client->setDefer(true); 
  58. $dr_service = new Google_Service_Drive($client); 
  59. $this->assertInstanceOf('Google_Http_Request', $dr_service->files->listFiles()); 
  60.  
  61. public function testSettersGetters() { 
  62. $client = new Google_Client(); 
  63. $client->setClientId("client1"); 
  64. $client->setClientSecret('client1secret'); 
  65. $client->setState('1'); 
  66. $client->setApprovalPrompt('force'); 
  67. $client->setAccessType('offline'); 
  68.  
  69. $client->setRedirectUri('localhost'); 
  70. $client->setApplicationName('me'); 
  71. $this->assertEquals('object', gettype($client->getAuth())); 
  72. $this->assertEquals('object', gettype($client->getCache())); 
  73. $this->assertEquals('object', gettype($client->getIo())); 
  74.  
  75. $client->setAuth(new Google_Auth_Simple($client)); 
  76. $client->setAuth(new Google_Auth_OAuth2($client)); 
  77.  
  78. try { 
  79. $client->setAccessToken(null); 
  80. die('Should have thrown an Google_Auth_Exception.'); 
  81. } catch(Google_Auth_Exception $e) { 
  82. $this->assertEquals('Could not json decode the token', $e->getMessage()); 
  83.  
  84. $token = json_encode(array('access_token' => 'token')); 
  85. $client->setAccessToken($token); 
  86. $this->assertEquals($token, $client->getAccessToken()); 
  87.  
  88. public function testAppEngineAutoConfig() { 
  89. if (!class_exists("Memcached")) { 
  90. $this->markTestSkipped('Test requires memcache'); 
  91. $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; 
  92. $client = new Google_Client(); 
  93. $this->assertInstanceOf('Google_Cache_Memcache', $client->getCache()); 
  94. unset($_SERVER['SERVER_SOFTWARE']); 
  95.  
  96. public function testJsonConfig() { 
  97. // Device config 
  98. $config = new Google_Config(); 
  99. $client = new Google_Client($config); 
  100. $device = '{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth", "client_secret":"N0aHCBT1qX1VAcF5J1pJAn6S", "token_uri":"https://accounts.google.com/o/oauth2/token", "client_email":"", "redirect_uris":["urn:ietf:wg:oauth:2.0:oob", "oob"], "client_x509_cert_url":"", "client_id":"123456789.apps.googleusercontent.com", "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs"}}'; 
  101. $dObj = json_decode($device); 
  102. $client->setAuthConfig($device); 
  103. $cfg = $config->getClassConfig('Google_Auth_OAuth2'); 
  104. $this->assertEquals($cfg['client_id'], $dObj->installed->client_id); 
  105. $this->assertEquals($cfg['client_secret'], $dObj->installed->client_secret); 
  106. $this->assertEquals($cfg['redirect_uri'], $dObj->installed->redirect_uris[0]); 
  107.  
  108. // Web config  
  109. $config = new Google_Config(); 
  110. $client = new Google_Client($config); 
  111. $web = '{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth", "client_secret":"lpoubuib8bj-Fmke_YhhyHGgXc", "token_uri":"https://accounts.google.com/o/oauth2/token", "client_email":"123456789@developer.gserviceaccount.com", "client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/123456789@developer.gserviceaccount.com", "client_id":"123456789.apps.googleusercontent.com", "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs"}}'; 
  112. $wObj = json_decode($web); 
  113. $client->setAuthConfig($web); 
  114. $cfg = $config->getClassConfig('Google_Auth_OAuth2'); 
  115. $this->assertEquals($cfg['client_id'], $wObj->web->client_id); 
  116. $this->assertEquals($cfg['client_secret'], $wObj->web->client_secret); 
  117. $this->assertEquals($cfg['redirect_uri'], ''); 
  118.  
  119. public function testIniConfig() { 
  120. $config = new Google_Config(__DIR__ . "/testdata/test.ini"); 
  121. $this->assertEquals('My Test application', $config->getApplicationName()); 
  122. $this->assertEquals('gjfiwnGinpena3', $config->getClassConfig('Google_Auth_OAuth2', 'client_secret'));