No Description

Guzzle6HttpHandlerTest.php 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 Google\Auth\HttpHandler\Guzzle6HttpHandler;
  19. use GuzzleHttp\Promise\Promise;
  20. use GuzzleHttp\Psr7\Response;
  21. class Guzzle6HttpHandlerTest extends BaseTest
  22. {
  23. public function setUp()
  24. {
  25. $this->onlyGuzzle6();
  26. $this->mockRequest =
  27. $this
  28. ->getMockBuilder('Psr\Http\Message\RequestInterface')
  29. ->getMock();
  30. $this->mockClient =
  31. $this
  32. ->getMockBuilder('GuzzleHttp\Client')
  33. ->getMock();
  34. }
  35. public function testSuccessfullySendsRequest()
  36. {
  37. $this->mockClient
  38. ->expects($this->any())
  39. ->method('send')
  40. ->will($this->returnValue(new Response(200)));
  41. $handler = new Guzzle6HttpHandler($this->mockClient);
  42. $response = $handler($this->mockRequest);
  43. $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
  44. }
  45. public function testSuccessfullySendsRequestAsync()
  46. {
  47. $this->mockClient
  48. ->expects($this->any())
  49. ->method('sendAsync')
  50. ->will($this->returnValue(new Promise(function () use (&$promise) {
  51. return $promise->resolve(new Response(200, [], 'Body Text'));
  52. })));
  53. $handler = new Guzzle6HttpHandler($this->mockClient);
  54. $promise = $handler->async($this->mockRequest);
  55. $response = $promise->wait();
  56. $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
  57. $this->assertEquals(200, $response->getStatusCode());
  58. $this->assertEquals('Body Text', (string) $response->getBody());
  59. }
  60. }