3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\Network;
26 use Friendica\Network\CurlResult;
27 use Mockery\MockInterface;
28 use PHPUnit\Framework\TestCase;
29 use Psr\Log\LoggerInterface;
30 use Psr\Log\NullLogger;
32 class CurlResultTest extends TestCase
34 protected function setUp()
38 /** @var Dice|MockInterface $dice */
39 $dice = \Mockery::mock(Dice::class)->makePartial();
40 $dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
42 $logger = new NullLogger();
43 $dice->shouldReceive('create')
44 ->with(LoggerInterface::class)
53 public function testNormal()
55 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
56 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
59 $curlResult = new CurlResult('https://test.local', $header . $body, [
61 'content_type' => 'text/html; charset=utf-8',
62 'url' => 'https://test.local'
65 $this->assertTrue($curlResult->isSuccess());
66 $this->assertFalse($curlResult->isTimeout());
67 $this->assertFalse($curlResult->isRedirectUrl());
68 $this->assertSame($header, $curlResult->getHeader());
69 $this->assertSame($body, $curlResult->getBody());
70 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
71 $this->assertSame('https://test.local', $curlResult->getUrl());
72 $this->assertSame('https://test.local', $curlResult->getRedirectUrl());
77 * @runInSeparateProcess
78 * @preserveGlobalState disabled
80 public function testRedirect()
82 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
83 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
86 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
88 'content_type' => 'text/html; charset=utf-8',
89 'url' => 'https://test.local/test/it',
90 'redirect_url' => 'https://test.other'
93 $this->assertTrue($curlResult->isSuccess());
94 $this->assertFalse($curlResult->isTimeout());
95 $this->assertTrue($curlResult->isRedirectUrl());
96 $this->assertSame($header, $curlResult->getHeader());
97 $this->assertSame($body, $curlResult->getBody());
98 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
99 $this->assertSame('https://test.local/test/it', $curlResult->getUrl());
100 $this->assertSame('https://test.other/test/it', $curlResult->getRedirectUrl());
106 public function testTimeout()
108 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
109 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
112 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
114 'content_type' => 'text/html; charset=utf-8',
115 'url' => 'https://test.local/test/it',
116 'redirect_url' => 'https://test.other'
117 ], CURLE_OPERATION_TIMEDOUT, 'Tested error');
119 $this->assertFalse($curlResult->isSuccess());
120 $this->assertTrue($curlResult->isTimeout());
121 $this->assertFalse($curlResult->isRedirectUrl());
122 $this->assertSame($header, $curlResult->getHeader());
123 $this->assertSame($body, $curlResult->getBody());
124 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
125 $this->assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
126 $this->assertSame('Tested error', $curlResult->getError());
131 * @runInSeparateProcess
132 * @preserveGlobalState disabled
134 public function testRedirectHeader()
136 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
137 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
140 $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
142 'content_type' => 'text/html; charset=utf-8',
143 'url' => 'https://test.local/test/it?key=value',
146 $this->assertTrue($curlResult->isSuccess());
147 $this->assertFalse($curlResult->isTimeout());
148 $this->assertTrue($curlResult->isRedirectUrl());
149 $this->assertSame($header, $curlResult->getHeader());
150 $this->assertSame($body, $curlResult->getBody());
151 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
152 $this->assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
153 $this->assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl());
159 public function testInHeader()
161 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
162 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
164 $curlResult = new CurlResult('https://test.local', $header . $body, [
166 'content_type' => 'text/html; charset=utf-8',
167 'url' => 'https://test.local'
169 $this->assertTrue($curlResult->inHeader('vary'));
170 $this->assertFalse($curlResult->inHeader('wrongHeader'));
176 public function testGetHeaderArray()
178 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
179 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
181 $curlResult = new CurlResult('https://test.local', $header . $body, [
183 'content_type' => 'text/html; charset=utf-8',
184 'url' => 'https://test.local'
187 $headers = $curlResult->getHeaderArray();
189 $this->assertNotEmpty($headers);
190 $this->assertArrayHasKey('vary', $headers);
196 public function testGetHeaderWithParam()
198 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
199 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
201 $curlResult = new CurlResult('https://test.local', $header . $body, [
203 'content_type' => 'text/html; charset=utf-8',
204 'url' => 'https://test.local'
207 $this->assertNotEmpty($curlResult->getHeader());
208 $this->assertEmpty($curlResult->getHeader('wrongHeader'));