ComputeEngineAuthTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. class ComputeEngineAuthTest extends BaseTest
  21. {
  22. public function testTokenAcquisition()
  23. {
  24. $client = new Google_Client();
  25. /* Mock out refresh call */
  26. $response_data = json_encode(
  27. array(
  28. 'access_token' => "ACCESS_TOKEN",
  29. 'expires_in' => "12345"
  30. )
  31. );
  32. $response = $this->getMock("Google_Http_Request", array(), array(''));
  33. $response->expects($this->any())
  34. ->method('getResponseHttpCode')
  35. ->will($this->returnValue(200));
  36. $response->expects($this->any())
  37. ->method('getResponseBody')
  38. ->will($this->returnValue($response_data));
  39. $io = $this->getMock("Google_IO_Stream", array(), array($client));
  40. $client->setIo($io);$io->expects($this->any())
  41. ->method('makeRequest')
  42. ->will(
  43. $this->returnCallback(
  44. function ($request) use ($response) {
  45. return $response;
  46. }
  47. )
  48. );
  49. /* Run method */
  50. $oauth = new Google_Auth_ComputeEngine($client);
  51. $oauth->acquireAccessToken();
  52. $token = json_decode($oauth->getAccessToken(), true);
  53. /* Check results */
  54. $this->assertEquals($token['access_token'], "ACCESS_TOKEN");
  55. $this->assertEquals($token['expires_in'], "12345");
  56. $this->assertTrue($token['created'] > 0);
  57. }
  58. public function testSign()
  59. {
  60. $client = new Google_Client();
  61. $oauth = new Google_Auth_ComputeEngine($client);
  62. /* Load mock access token */
  63. $oauth->setAccessToken(
  64. json_encode(
  65. array(
  66. 'access_token' => "ACCESS_TOKEN",
  67. 'expires_in' => "12345"
  68. )
  69. )
  70. );
  71. /* Sign a URL and verify auth header is correctly set */
  72. $req = new Google_Http_Request('http://localhost');
  73. $req = $oauth->sign($req);
  74. $auth = $req->getRequestHeader('authorization');
  75. $this->assertEquals('Bearer ACCESS_TOKEN', $auth);
  76. }
  77. }