| 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 |
|
| 21 |
class ComputeEngineAuthTest extends BaseTest |
| 22 |
{ |
| 23 |
public function testTokenAcquisition() |
| 24 |
{ |
| 25 |
$client = new Google_Client(); |
| 26 |
|
| 27 |
/* Mock out refresh call */ |
| 28 |
$response_data = json_encode( |
| 29 |
array( |
| 30 |
'access_token' => "ACCESS_TOKEN", |
| 31 |
'expires_in' => "12345" |
| 32 |
) |
| 33 |
); |
| 34 |
$response = $this->getMock("Google_Http_Request", array(), array('')); |
| 35 |
$response->expects($this->any()) |
| 36 |
->method('getResponseHttpCode') |
| 37 |
->will($this->returnValue(200)); |
| 38 |
$response->expects($this->any()) |
| 39 |
->method('getResponseBody') |
| 40 |
->will($this->returnValue($response_data)); |
| 41 |
$io = $this->getMock("Google_IO_Stream", array(), array($client)); |
| 42 |
$client->setIo($io);$io->expects($this->any()) |
| 43 |
->method('makeRequest') |
| 44 |
->will( |
| 45 |
$this->returnCallback( |
| 46 |
function ($request) use ($response) { |
| 47 |
return $response; |
| 48 |
} |
| 49 |
) |
| 50 |
); |
| 51 |
|
| 52 |
/* Run method */ |
| 53 |
$oauth = new Google_Auth_ComputeEngine($client); |
| 54 |
$oauth->acquireAccessToken(); |
| 55 |
$token = json_decode($oauth->getAccessToken(), true); |
| 56 |
|
| 57 |
/* Check results */ |
| 58 |
$this->assertEquals($token['access_token'], "ACCESS_TOKEN"); |
| 59 |
$this->assertEquals($token['expires_in'], "12345"); |
| 60 |
$this->assertTrue($token['created'] > 0); |
| 61 |
} |
| 62 |
|
| 63 |
public function testSign() |
| 64 |
{ |
| 65 |
$client = new Google_Client(); |
| 66 |
$oauth = new Google_Auth_ComputeEngine($client); |
| 67 |
|
| 68 |
/* Load mock access token */ |
| 69 |
$oauth->setAccessToken( |
| 70 |
json_encode( |
| 71 |
array( |
| 72 |
'access_token' => "ACCESS_TOKEN", |
| 73 |
'expires_in' => "12345" |
| 74 |
) |
| 75 |
) |
| 76 |
); |
| 77 |
|
| 78 |
/* Sign a URL and verify auth header is correctly set */ |
| 79 |
$req = new Google_Http_Request('http://localhost'); |
| 80 |
$req = $oauth->sign($req); |
| 81 |
$auth = $req->getRequestHeader('authorization'); |
| 82 |
$this->assertEquals('Bearer ACCESS_TOKEN', $auth); |
| 83 |
} |
| 84 |
} |
| 85 |
|