Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

193 lines
6.0 KiB

  1. <?php
  2. namespace Friendica\Test\src\Network;
  3. use Dice\Dice;
  4. use Friendica\BaseObject;
  5. use Friendica\Network\CurlResult;
  6. use Mockery\MockInterface;
  7. use PHPUnit\Framework\TestCase;
  8. use Psr\Log\LoggerInterface;
  9. use Psr\Log\NullLogger;
  10. class CurlResultTest extends TestCase
  11. {
  12. protected function setUp()
  13. {
  14. parent::setUp();
  15. /** @var Dice|MockInterface $dice */
  16. $dice = \Mockery::mock(Dice::class)->makePartial();
  17. $dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
  18. $logger = new NullLogger();
  19. $dice->shouldReceive('create')
  20. ->with(LoggerInterface::class)
  21. ->andReturn($logger);
  22. BaseObject::setDependencyInjection($dice);
  23. }
  24. /**
  25. * @small
  26. */
  27. public function testNormal()
  28. {
  29. $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
  30. $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
  31. $curlResult = new CurlResult('https://test.local', $header . $body, [
  32. 'http_code' => 200,
  33. 'content_type' => 'text/html; charset=utf-8',
  34. 'url' => 'https://test.local'
  35. ]);
  36. $this->assertTrue($curlResult->isSuccess());
  37. $this->assertFalse($curlResult->isTimeout());
  38. $this->assertFalse($curlResult->isRedirectUrl());
  39. $this->assertSame($header, $curlResult->getHeader());
  40. $this->assertSame($body, $curlResult->getBody());
  41. $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
  42. $this->assertSame('https://test.local', $curlResult->getUrl());
  43. $this->assertSame('https://test.local', $curlResult->getRedirectUrl());
  44. }
  45. /**
  46. * @small
  47. * @runInSeparateProcess
  48. * @preserveGlobalState disabled
  49. */
  50. public function testRedirect()
  51. {
  52. $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
  53. $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
  54. $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
  55. 'http_code' => 301,
  56. 'content_type' => 'text/html; charset=utf-8',
  57. 'url' => 'https://test.local/test/it',
  58. 'redirect_url' => 'https://test.other'
  59. ]);
  60. $this->assertTrue($curlResult->isSuccess());
  61. $this->assertFalse($curlResult->isTimeout());
  62. $this->assertTrue($curlResult->isRedirectUrl());
  63. $this->assertSame($header, $curlResult->getHeader());
  64. $this->assertSame($body, $curlResult->getBody());
  65. $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
  66. $this->assertSame('https://test.local/test/it', $curlResult->getUrl());
  67. $this->assertSame('https://test.other/test/it', $curlResult->getRedirectUrl());
  68. }
  69. /**
  70. * @small
  71. */
  72. public function testTimeout()
  73. {
  74. $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
  75. $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
  76. $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
  77. 'http_code' => 500,
  78. 'content_type' => 'text/html; charset=utf-8',
  79. 'url' => 'https://test.local/test/it',
  80. 'redirect_url' => 'https://test.other'
  81. ], CURLE_OPERATION_TIMEDOUT, 'Tested error');
  82. $this->assertFalse($curlResult->isSuccess());
  83. $this->assertTrue($curlResult->isTimeout());
  84. $this->assertFalse($curlResult->isRedirectUrl());
  85. $this->assertSame($header, $curlResult->getHeader());
  86. $this->assertSame($body, $curlResult->getBody());
  87. $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
  88. $this->assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
  89. $this->assertSame('Tested error', $curlResult->getError());
  90. }
  91. /**
  92. * @small
  93. * @runInSeparateProcess
  94. * @preserveGlobalState disabled
  95. */
  96. public function testRedirectHeader()
  97. {
  98. $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
  99. $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
  100. $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
  101. 'http_code' => 301,
  102. 'content_type' => 'text/html; charset=utf-8',
  103. 'url' => 'https://test.local/test/it?key=value',
  104. ]);
  105. $this->assertTrue($curlResult->isSuccess());
  106. $this->assertFalse($curlResult->isTimeout());
  107. $this->assertTrue($curlResult->isRedirectUrl());
  108. $this->assertSame($header, $curlResult->getHeader());
  109. $this->assertSame($body, $curlResult->getBody());
  110. $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
  111. $this->assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
  112. $this->assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl());
  113. }
  114. /**
  115. * @small
  116. */
  117. public function testInHeader()
  118. {
  119. $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
  120. $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
  121. $curlResult = new CurlResult('https://test.local', $header . $body, [
  122. 'http_code' => 200,
  123. 'content_type' => 'text/html; charset=utf-8',
  124. 'url' => 'https://test.local'
  125. ]);
  126. $this->assertTrue($curlResult->inHeader('vary'));
  127. $this->assertFalse($curlResult->inHeader('wrongHeader'));
  128. }
  129. /**
  130. * @small
  131. */
  132. public function testGetHeaderArray()
  133. {
  134. $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
  135. $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
  136. $curlResult = new CurlResult('https://test.local', $header . $body, [
  137. 'http_code' => 200,
  138. 'content_type' => 'text/html; charset=utf-8',
  139. 'url' => 'https://test.local'
  140. ]);
  141. $headers = $curlResult->getHeaderArray();
  142. $this->assertNotEmpty($headers);
  143. $this->assertArrayHasKey('vary', $headers);
  144. }
  145. /**
  146. * @small
  147. */
  148. public function testGetHeaderWithParam()
  149. {
  150. $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
  151. $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
  152. $curlResult = new CurlResult('https://test.local', $header . $body, [
  153. 'http_code' => 200,
  154. 'content_type' => 'text/html; charset=utf-8',
  155. 'url' => 'https://test.local'
  156. ]);
  157. $this->assertNotEmpty($curlResult->getHeader());
  158. $this->assertEmpty($curlResult->getHeader('wrongHeader'));
  159. }
  160. }