3 * @copyright Copyright (C) 2010-2021, the Friendica project
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(): void
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 $headerArray = include(__DIR__ . '/../../datasets/curl/about.head.php');
57 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
60 $curlResult = new CurlResult('https://test.local', $header . $body, [
62 'content_type' => 'text/html; charset=utf-8',
63 'url' => 'https://test.local'
66 self::assertTrue($curlResult->isSuccess());
67 self::assertFalse($curlResult->isTimeout());
68 self::assertFalse($curlResult->isRedirectUrl());
69 self::assertSame($headerArray, $curlResult->getHeaders());
70 self::assertSame($body, $curlResult->getBody());
71 self::assertSame(['text/html; charset=utf-8'], $curlResult->getContentType());
72 self::assertSame('https://test.local', $curlResult->getUrl());
73 self::assertSame('https://test.local', $curlResult->getRedirectUrl());
78 * @runInSeparateProcess
79 * @preserveGlobalState disabled
81 public function testRedirect()
83 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
84 $headerArray = include(__DIR__ . '/../../datasets/curl/about.head.php');
85 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
88 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
90 'content_type' => 'text/html; charset=utf-8',
91 'url' => 'https://test.local/test/it',
92 'redirect_url' => 'https://test.other'
95 self::assertTrue($curlResult->isSuccess());
96 self::assertFalse($curlResult->isTimeout());
97 self::assertTrue($curlResult->isRedirectUrl());
98 self::assertSame($headerArray, $curlResult->getHeaders());
99 self::assertSame($body, $curlResult->getBody());
100 self::assertSame(['text/html; charset=utf-8'], $curlResult->getContentType());
101 self::assertSame('https://test.local/test/it', $curlResult->getUrl());
102 self::assertSame('https://test.other/test/it', $curlResult->getRedirectUrl());
108 public function testTimeout()
110 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
111 $headerArray = include(__DIR__ . '/../../datasets/curl/about.head.php');
112 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
115 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
117 'content_type' => 'text/html; charset=utf-8',
118 'url' => 'https://test.local/test/it',
119 'redirect_url' => 'https://test.other'
120 ], CURLE_OPERATION_TIMEDOUT, 'Tested error');
122 self::assertFalse($curlResult->isSuccess());
123 self::assertTrue($curlResult->isTimeout());
124 self::assertFalse($curlResult->isRedirectUrl());
125 self::assertSame($headerArray, $curlResult->getHeaders());
126 self::assertSame($body, $curlResult->getBody());
127 self::assertSame(['text/html; charset=utf-8'], $curlResult->getContentType());
128 self::assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
129 self::assertSame('Tested error', $curlResult->getError());
134 * @runInSeparateProcess
135 * @preserveGlobalState disabled
137 public function testRedirectHeader()
139 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
140 $headerArray = include(__DIR__ . '/../../datasets/curl/about.redirect.php');
141 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
144 $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
146 'content_type' => 'text/html; charset=utf-8',
147 'url' => 'https://test.local/test/it?key=value',
150 self::assertTrue($curlResult->isSuccess());
151 self::assertFalse($curlResult->isTimeout());
152 self::assertTrue($curlResult->isRedirectUrl());
153 self::assertSame($headerArray, $curlResult->getHeaders());
154 self::assertSame($body, $curlResult->getBody());
155 self::assertSame(['text/html; charset=utf-8'], $curlResult->getContentType());
156 self::assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
157 self::assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl());
163 public function testInHeader()
165 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
166 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
168 $curlResult = new CurlResult('https://test.local', $header . $body, [
170 'content_type' => 'text/html; charset=utf-8',
171 'url' => 'https://test.local'
173 self::assertTrue($curlResult->inHeader('vary'));
174 self::assertFalse($curlResult->inHeader('wrongHeader'));
180 public function testGetHeaderArray()
182 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
183 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
185 $curlResult = new CurlResult('https://test.local', $header . $body, [
187 'content_type' => 'text/html; charset=utf-8',
188 'url' => 'https://test.local'
191 $headers = $curlResult->getHeaderArray();
193 self::assertNotEmpty($headers);
194 self::assertArrayHasKey('vary', $headers);
200 public function testGetHeaderWithParam()
202 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
203 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
205 $curlResult = new CurlResult('https://test.local', $header . $body, [
207 'content_type' => 'text/html; charset=utf-8',
208 'url' => 'https://test.local'
211 self::assertNotEmpty($curlResult->getHeaders());
212 self::assertEmpty($curlResult->getHeader('wrongHeader'));