/tests/src/Network/HTTPClient/Response/CurlResultTest.php

https://github.com/friendica/friendica · PHP · 214 lines · 138 code · 31 blank · 45 comment · 0 complexity · d5e1f14f954938887e5ad3c015770ea0 MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2010-2022, the Friendica project
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Friendica\Test\src\Network\HTTPClient\Response;
  22. use Dice\Dice;
  23. use Friendica\DI;
  24. use Friendica\Network\HTTPClient\Response\CurlResult;
  25. use Mockery\MockInterface;
  26. use PHPUnit\Framework\TestCase;
  27. use Psr\Log\LoggerInterface;
  28. use Psr\Log\NullLogger;
  29. class CurlResultTest extends TestCase
  30. {
  31. protected function setUp(): void
  32. {
  33. parent::setUp();
  34. /** @var Dice|MockInterface $dice */
  35. $dice = \Mockery::mock(Dice::class)->makePartial();
  36. $dice = $dice->addRules(include __DIR__ . '/../../../../../static/dependencies.config.php');
  37. $logger = new NullLogger();
  38. $dice->shouldReceive('create')
  39. ->with(LoggerInterface::class)
  40. ->andReturn($logger);
  41. DI::init($dice);
  42. }
  43. /**
  44. * @small
  45. */
  46. public function testNormal()
  47. {
  48. $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
  49. $headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
  50. $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
  51. $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local', $header . $body, [
  52. 'http_code' => 200,
  53. 'content_type' => 'text/html; charset=utf-8',
  54. 'url' => 'https://test.local'
  55. ]);
  56. self::assertTrue($curlResult->isSuccess());
  57. self::assertFalse($curlResult->isTimeout());
  58. self::assertFalse($curlResult->isRedirectUrl());
  59. self::assertSame($headerArray, $curlResult->getHeaders());
  60. self::assertSame($body, $curlResult->getBody());
  61. self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
  62. self::assertSame('https://test.local', $curlResult->getUrl());
  63. self::assertSame('https://test.local', $curlResult->getRedirectUrl());
  64. }
  65. /**
  66. * @small
  67. * @runInSeparateProcess
  68. * @preserveGlobalState disabled
  69. */
  70. public function testRedirect()
  71. {
  72. $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
  73. $headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
  74. $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
  75. $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local/test/it', $header . $body, [
  76. 'http_code' => 301,
  77. 'content_type' => 'text/html; charset=utf-8',
  78. 'url' => 'https://test.local/test/it',
  79. 'redirect_url' => 'https://test.other'
  80. ]);
  81. self::assertTrue($curlResult->isSuccess());
  82. self::assertFalse($curlResult->isTimeout());
  83. self::assertTrue($curlResult->isRedirectUrl());
  84. self::assertSame($headerArray, $curlResult->getHeaders());
  85. self::assertSame($body, $curlResult->getBody());
  86. self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
  87. self::assertSame('https://test.local/test/it', $curlResult->getUrl());
  88. self::assertSame('https://test.other/test/it', $curlResult->getRedirectUrl());
  89. }
  90. /**
  91. * @small
  92. */
  93. public function testTimeout()
  94. {
  95. $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
  96. $headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
  97. $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
  98. $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local/test/it', $header . $body, [
  99. 'http_code' => 500,
  100. 'content_type' => 'text/html; charset=utf-8',
  101. 'url' => 'https://test.local/test/it',
  102. 'redirect_url' => 'https://test.other'
  103. ], CURLE_OPERATION_TIMEDOUT, 'Tested error');
  104. self::assertFalse($curlResult->isSuccess());
  105. self::assertTrue($curlResult->isTimeout());
  106. self::assertFalse($curlResult->isRedirectUrl());
  107. self::assertSame($headerArray, $curlResult->getHeaders());
  108. self::assertSame($body, $curlResult->getBody());
  109. self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
  110. self::assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
  111. self::assertSame('Tested error', $curlResult->getError());
  112. }
  113. /**
  114. * @small
  115. * @runInSeparateProcess
  116. * @preserveGlobalState disabled
  117. */
  118. public function testRedirectHeader()
  119. {
  120. $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.redirect');
  121. $headerArray = include(__DIR__ . '/../../../../datasets/curl/about.redirect.php');
  122. $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
  123. $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
  124. 'http_code' => 301,
  125. 'content_type' => 'text/html; charset=utf-8',
  126. 'url' => 'https://test.local/test/it?key=value',
  127. ]);
  128. self::assertTrue($curlResult->isSuccess());
  129. self::assertFalse($curlResult->isTimeout());
  130. self::assertTrue($curlResult->isRedirectUrl());
  131. self::assertSame($headerArray, $curlResult->getHeaders());
  132. self::assertSame($body, $curlResult->getBody());
  133. self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
  134. self::assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
  135. self::assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl());
  136. }
  137. /**
  138. * @small
  139. */
  140. public function testInHeader()
  141. {
  142. $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
  143. $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
  144. $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local', $header . $body, [
  145. 'http_code' => 200,
  146. 'content_type' => 'text/html; charset=utf-8',
  147. 'url' => 'https://test.local'
  148. ]);
  149. self::assertTrue($curlResult->inHeader('vary'));
  150. self::assertFalse($curlResult->inHeader('wrongHeader'));
  151. }
  152. /**
  153. * @small
  154. */
  155. public function testGetHeaderArray()
  156. {
  157. $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
  158. $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
  159. $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local', $header . $body, [
  160. 'http_code' => 200,
  161. 'content_type' => 'text/html; charset=utf-8',
  162. 'url' => 'https://test.local'
  163. ]);
  164. $headers = $curlResult->getHeaderArray();
  165. self::assertNotEmpty($headers);
  166. self::assertArrayHasKey('vary', $headers);
  167. }
  168. /**
  169. * @small
  170. */
  171. public function testGetHeaderWithParam()
  172. {
  173. $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
  174. $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
  175. $curlResult = new CurlResult('https://test.local', $header . $body, [
  176. 'http_code' => 200,
  177. 'content_type' => 'text/html; charset=utf-8',
  178. 'url' => 'https://test.local'
  179. ]);
  180. self::assertNotEmpty($curlResult->getHeaders());
  181. self::assertEmpty($curlResult->getHeader('wrongHeader'));
  182. }
  183. }