3 namespace Friendica\Test\src\Network;
6 use Friendica\BaseObject;
7 use Friendica\Network\CurlResult;
8 use Mockery\MockInterface;
9 use PHPUnit\Framework\TestCase;
10 use Psr\Log\LoggerInterface;
11 use Psr\Log\NullLogger;
13 class CurlResultTest extends TestCase
15 protected function setUp()
20 /** @var Dice|MockInterface $dice */
21 $dice = \Mockery::mock(Dice::class)->makePartial();
22 $dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
24 $logger = new NullLogger();
25 $dice->shouldReceive('create')
26 ->with(LoggerInterface::class)
29 BaseObject::setDependencyInjection($dice);
35 public function testNormal()
37 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
38 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
41 $curlResult = new CurlResult('https://test.local', $header . $body, [
43 'content_type' => 'text/html; charset=utf-8',
44 'url' => 'https://test.local'
47 $this->assertTrue($curlResult->isSuccess());
48 $this->assertFalse($curlResult->isTimeout());
49 $this->assertFalse($curlResult->isRedirectUrl());
50 $this->assertSame($header, $curlResult->getHeader());
51 $this->assertSame($body, $curlResult->getBody());
52 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
53 $this->assertSame('https://test.local', $curlResult->getUrl());
54 $this->assertSame('https://test.local', $curlResult->getRedirectUrl());
59 * @runInSeparateProcess
60 * @preserveGlobalState disabled
62 public function testRedirect()
64 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
65 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
68 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
70 'content_type' => 'text/html; charset=utf-8',
71 'url' => 'https://test.local/test/it',
72 'redirect_url' => 'https://test.other'
75 $this->assertTrue($curlResult->isSuccess());
76 $this->assertFalse($curlResult->isTimeout());
77 $this->assertTrue($curlResult->isRedirectUrl());
78 $this->assertSame($header, $curlResult->getHeader());
79 $this->assertSame($body, $curlResult->getBody());
80 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
81 $this->assertSame('https://test.local/test/it', $curlResult->getUrl());
82 $this->assertSame('https://test.other/test/it', $curlResult->getRedirectUrl());
88 public function testTimeout()
90 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
91 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
94 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
96 'content_type' => 'text/html; charset=utf-8',
97 'url' => 'https://test.local/test/it',
98 'redirect_url' => 'https://test.other'
99 ], CURLE_OPERATION_TIMEDOUT, 'Tested error');
101 $this->assertFalse($curlResult->isSuccess());
102 $this->assertTrue($curlResult->isTimeout());
103 $this->assertFalse($curlResult->isRedirectUrl());
104 $this->assertSame($header, $curlResult->getHeader());
105 $this->assertSame($body, $curlResult->getBody());
106 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
107 $this->assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
108 $this->assertSame('Tested error', $curlResult->getError());
113 * @runInSeparateProcess
114 * @preserveGlobalState disabled
116 public function testRedirectHeader()
118 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
119 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
122 $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
124 'content_type' => 'text/html; charset=utf-8',
125 'url' => 'https://test.local/test/it?key=value',
128 $this->assertTrue($curlResult->isSuccess());
129 $this->assertFalse($curlResult->isTimeout());
130 $this->assertTrue($curlResult->isRedirectUrl());
131 $this->assertSame($header, $curlResult->getHeader());
132 $this->assertSame($body, $curlResult->getBody());
133 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
134 $this->assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
135 $this->assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl());
141 public function testInHeader()
143 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
144 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
146 $curlResult = new CurlResult('https://test.local', $header . $body, [
148 'content_type' => 'text/html; charset=utf-8',
149 'url' => 'https://test.local'
151 $this->assertTrue($curlResult->inHeader('vary'));
152 $this->assertFalse($curlResult->inHeader('wrongHeader'));
158 public function testGetHeaderArray()
160 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
161 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
163 $curlResult = new CurlResult('https://test.local', $header . $body, [
165 'content_type' => 'text/html; charset=utf-8',
166 'url' => 'https://test.local'
169 $headers = $curlResult->getHeaderArray();
171 $this->assertNotEmpty($headers);
172 $this->assertArrayHasKey('vary', $headers);
178 public function testGetHeaderWithParam()
180 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
181 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
183 $curlResult = new CurlResult('https://test.local', $header . $body, [
185 'content_type' => 'text/html; charset=utf-8',
186 'url' => 'https://test.local'
189 $this->assertNotEmpty($curlResult->getHeader());
190 $this->assertEmpty('vary', $curlResult->getHeader('wrongHeader'));