HttpClientTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Webservice;
  6. use Magento\TestFramework\ObjectManager;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. use Temando\Shipping\Webservice\Exception\HttpRequestException;
  9. use Temando\Shipping\Webservice\Exception\HttpResponseException;
  10. /**
  11. * HttpClientTest
  12. *
  13. * @package Temando\Shipping\Test\Integration
  14. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  15. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link http://www.temando.com/
  17. */
  18. class HttpClientTest extends \PHPUnit\Framework\TestCase
  19. {
  20. /**
  21. * @var ObjectManager
  22. */
  23. private $objectManager;
  24. /**
  25. * Init object manager
  26. */
  27. public function setUp()
  28. {
  29. parent::setUp();
  30. $this->objectManager = Bootstrap::getObjectManager();
  31. }
  32. /**
  33. * @test
  34. */
  35. public function setHeaders()
  36. {
  37. $zendClient = new \Zend\Http\Client();
  38. $headers = [
  39. 'foo' => 'bar',
  40. 'fox' => 'baz',
  41. ];
  42. /** @var HttpClient $httpClient */
  43. $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
  44. $httpClient->setHeaders($headers);
  45. foreach ($headers as $key => $value) {
  46. $this->assertEquals($value, $zendClient->getHeader($key));
  47. }
  48. }
  49. /**
  50. * @test
  51. */
  52. public function setUri()
  53. {
  54. $zendClient = new \Zend\Http\Client();
  55. $uri = 'https://example.org/';
  56. /** @var HttpClient $httpClient */
  57. $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
  58. $httpClient->setUri($uri);
  59. $this->assertEquals($uri, $zendClient->getUri());
  60. }
  61. /**
  62. * @test
  63. */
  64. public function setOptions()
  65. {
  66. $zendClient = new \Zend\Http\Client();
  67. $options = ['trace' => 1, 'maxredirects' => 23, 'timeout' => 42, 'useragent' => 'Foo'];
  68. /** @var HttpClient $httpClient */
  69. $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
  70. $httpClient->setOptions($options);
  71. foreach ($options as $key => $value) {
  72. $adapterConfig = $zendClient->getAdapter()->getConfig();
  73. $this->assertEquals($value, $adapterConfig[$key]);
  74. }
  75. }
  76. /**
  77. * @test
  78. */
  79. public function setBody()
  80. {
  81. $zendClient = new \Zend\Http\Client();
  82. $body = '{"error": "foo"}';
  83. /** @var HttpClient $httpClient */
  84. $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
  85. $httpClient->setRawBody($body);
  86. $this->assertEquals($body, $zendClient->getRequest()->getContent());
  87. }
  88. /**
  89. * @test
  90. */
  91. public function setQueryParams()
  92. {
  93. $zendClient = new \Zend\Http\Client();
  94. $query = [
  95. 'limit' => 23,
  96. 'offset' => 42
  97. ];
  98. /** @var HttpClient $httpClient */
  99. $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
  100. $httpClient->setParameterGet($query);
  101. foreach ($query as $key => $value) {
  102. $this->assertEquals($value, $zendClient->getRequest()->getQuery($key));
  103. }
  104. }
  105. /**
  106. * @test
  107. */
  108. public function sendRequestError()
  109. {
  110. $eMsg = 'Unknown Foo';
  111. $this->expectException(HttpRequestException::class);
  112. $this->expectExceptionMessage($eMsg);
  113. $zendClient = $this->getMockBuilder(\Zend\Http\Client::class)
  114. ->setMethods(['send', 'setMethod'])
  115. ->getMock();
  116. $zendClient
  117. ->expects($this->once())
  118. ->method('send')
  119. ->willThrowException(new \Zend\Http\Exception\RuntimeException($eMsg));
  120. /** @var HttpClient $httpClient */
  121. $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
  122. $httpClient->send('FOO');
  123. }
  124. /**
  125. * @test
  126. */
  127. public function sendResponseError()
  128. {
  129. $eMsg = 'Unknown Foo';
  130. $this->expectException(HttpResponseException::class);
  131. $this->expectExceptionMessage($eMsg);
  132. $response = $this->getMockBuilder(\Zend\Http\Response::class)
  133. ->setMethods(['isSuccess', 'getBody'])
  134. ->getMock();
  135. $response
  136. ->expects($this->once())
  137. ->method('isSuccess')
  138. ->willReturn(false);
  139. $response
  140. ->expects($this->once())
  141. ->method('getBody')
  142. ->willReturn($eMsg);
  143. $zendClient = $this->getMockBuilder(\Zend\Http\Client::class)
  144. ->setMethods(['send', 'setMethod'])
  145. ->getMock();
  146. $zendClient
  147. ->expects($this->once())
  148. ->method('send')
  149. ->willReturn($response);
  150. /** @var HttpClient $httpClient */
  151. $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
  152. $httpClient->send('FOO');
  153. }
  154. /**
  155. * @test
  156. */
  157. public function sendSuccess()
  158. {
  159. $responseBody = '{"hooray": true}';
  160. $response = $this->getMockBuilder(\Zend\Http\Response::class)
  161. ->setMethods(['isSuccess', 'getBody'])
  162. ->getMock();
  163. $response
  164. ->expects($this->once())
  165. ->method('isSuccess')
  166. ->willReturn(true);
  167. $response
  168. ->expects($this->once())
  169. ->method('getBody')
  170. ->willReturn($responseBody);
  171. $zendClient = $this->getMockBuilder(\Zend\Http\Client::class)
  172. ->setMethods(['send', 'setMethod'])
  173. ->getMock();
  174. $zendClient
  175. ->expects($this->once())
  176. ->method('send')
  177. ->willReturn($response);
  178. /** @var HttpClient $httpClient */
  179. $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
  180. $this->assertEquals($responseBody, $httpClient->send('FOO'));
  181. }
  182. }