No Description

Guzzle5HttpHandlerTest.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /*
  3. * Copyright 2015 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Google\Auth\Tests;
  18. use Composer\Autoload\ClassLoader;
  19. use Exception;
  20. use Google\Auth\HttpHandler\Guzzle5HttpHandler;
  21. use GuzzleHttp\Message\FutureResponse;
  22. use GuzzleHttp\Message\Response;
  23. use GuzzleHttp\Ring\Future\CompletedFutureValue;
  24. use GuzzleHttp\Stream\Stream;
  25. class Guzzle5HttpHandlerTest extends BaseTest
  26. {
  27. public function setUp()
  28. {
  29. $this->onlyGuzzle5();
  30. $this->mockPsr7Request =
  31. $this
  32. ->getMockBuilder('Psr\Http\Message\RequestInterface')
  33. ->getMock();
  34. $this->mockRequest =
  35. $this
  36. ->getMockBuilder('GuzzleHttp\Message\RequestInterface')
  37. ->getMock();
  38. $this->mockClient =
  39. $this
  40. ->getMockBuilder('GuzzleHttp\Client')
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->mockFuture =
  44. $this
  45. ->getMockBuilder('GuzzleHttp\Ring\Future\FutureInterface')
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. }
  49. public function testSuccessfullySendsRealRequest()
  50. {
  51. $request = new \GuzzleHttp\Psr7\Request('get', 'http://httpbin.org/get');
  52. $client = new \GuzzleHttp\Client();
  53. $handler = new Guzzle5HttpHandler($client);
  54. $response = $handler($request);
  55. $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
  56. $this->assertEquals(200, $response->getStatusCode());
  57. $json = json_decode((string) $response->getBody(), true);
  58. $this->assertArrayHasKey('url', $json);
  59. $this->assertEquals($request->getUri(), $json['url']);
  60. }
  61. public function testSuccessfullySendsMockRequest()
  62. {
  63. $response = new Response(
  64. 200,
  65. [],
  66. Stream::factory('Body Text')
  67. );
  68. $this->mockClient
  69. ->expects($this->any())
  70. ->method('send')
  71. ->will($this->returnValue($response));
  72. $this->mockClient
  73. ->expects($this->any())
  74. ->method('createRequest')
  75. ->will($this->returnValue($this->mockRequest));
  76. $handler = new Guzzle5HttpHandler($this->mockClient);
  77. $response = $handler($this->mockPsr7Request);
  78. $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
  79. $this->assertEquals(200, $response->getStatusCode());
  80. $this->assertEquals('Body Text', (string) $response->getBody());
  81. }
  82. public function testAsyncWithoutGuzzlePromiseThrowsException()
  83. {
  84. // Pretend the promise library doesn't exist
  85. foreach (spl_autoload_functions() as $function) {
  86. if ($function[0] instanceof ClassLoader) {
  87. $newAutoloader = clone $function[0];
  88. $newAutoloader->setPsr4('GuzzleHttp\\Promise\\', '/tmp');
  89. spl_autoload_register($newAutoloadFunc = [$newAutoloader, 'loadClass']);
  90. spl_autoload_unregister($previousAutoloadFunc = $function);
  91. }
  92. }
  93. $this->mockClient
  94. ->expects($this->any())
  95. ->method('send')
  96. ->will($this->returnValue(new FutureResponse($this->mockFuture)));
  97. $this->mockClient
  98. ->expects($this->any())
  99. ->method('createRequest')
  100. ->will($this->returnValue($this->mockRequest));
  101. $handler = new Guzzle5HttpHandler($this->mockClient);
  102. $errorThrown = false;
  103. try {
  104. $handler->async($this->mockPsr7Request);
  105. } catch (Exception $e) {
  106. $this->assertEquals(
  107. 'Install guzzlehttp/promises to use async with Guzzle 5',
  108. $e->getMessage()
  109. );
  110. $errorThrown = true;
  111. }
  112. // Restore autoloader before assertion (in case it fails)
  113. spl_autoload_register($previousAutoloadFunc);
  114. spl_autoload_unregister($newAutoloadFunc);
  115. $this->assertTrue($errorThrown);
  116. }
  117. public function testSuccessfullySendsRequestAsync()
  118. {
  119. $response = new Response(
  120. 200,
  121. [],
  122. Stream::factory('Body Text')
  123. );
  124. $this->mockClient
  125. ->expects($this->any())
  126. ->method('send')
  127. ->will($this->returnValue(new FutureResponse(
  128. new CompletedFutureValue($response)
  129. )));
  130. $this->mockClient
  131. ->expects($this->any())
  132. ->method('createRequest')
  133. ->will($this->returnValue($this->mockRequest));
  134. $handler = new Guzzle5HttpHandler($this->mockClient);
  135. $promise = $handler->async($this->mockPsr7Request);
  136. $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $promise->wait());
  137. $this->assertEquals(200, $response->getStatusCode());
  138. $this->assertEquals('Body Text', (string) $response->getBody());
  139. }
  140. /**
  141. * @expectedException Exception
  142. * @expectedExceptionMessage This is a test rejection message
  143. */
  144. public function testPromiseHandlesException()
  145. {
  146. $this->mockClient
  147. ->expects($this->any())
  148. ->method('send')
  149. ->will($this->returnValue(new FutureResponse(
  150. (new CompletedFutureValue(new Response(200)))
  151. ->then(function () {
  152. throw new Exception('This is a test rejection message');
  153. })
  154. )));
  155. $this->mockClient
  156. ->expects($this->any())
  157. ->method('createRequest')
  158. ->will($this->returnValue($this->mockRequest));
  159. $handler = new Guzzle5HttpHandler($this->mockClient);
  160. $promise = $handler->async($this->mockPsr7Request);
  161. $promise->wait();
  162. }
  163. public function testCreateGuzzle5Request()
  164. {
  165. $requestHeaders = [
  166. 'header1' => 'value1',
  167. 'header2' => 'value2',
  168. ];
  169. $this->mockPsr7Request
  170. ->expects($this->once())
  171. ->method('getHeaders')
  172. ->will($this->returnValue($requestHeaders));
  173. $mockBody = $this->getMock('Psr\Http\Message\StreamInterface');
  174. $this->mockPsr7Request
  175. ->expects($this->once())
  176. ->method('getBody')
  177. ->will($this->returnValue($mockBody));
  178. $this->mockClient
  179. ->expects($this->once())
  180. ->method('createRequest')
  181. ->with(null, null, [
  182. 'headers' => $requestHeaders + ['header3' => 'value3'],
  183. 'body' => $mockBody,
  184. ])
  185. ->will($this->returnValue(
  186. $this->getMock('GuzzleHttp\Message\RequestInterface')
  187. ));
  188. $this->mockClient
  189. ->expects($this->once())
  190. ->method('send')
  191. ->will($this->returnValue(
  192. $this->getMock('GuzzleHttp\Message\ResponseInterface')
  193. ));
  194. $handler = new Guzzle5HttpHandler($this->mockClient);
  195. $handler($this->mockPsr7Request, [
  196. 'headers' => [
  197. 'header3' => 'value3'
  198. ]
  199. ]);
  200. }
  201. }