3 namespace Friendica\Test\src\Network;
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()
19 /** @var Dice|MockInterface $dice */
20 $dice = \Mockery::mock(Dice::class)->makePartial();
21 $dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
23 $logger = new NullLogger();
24 $dice->shouldReceive('create')
25 ->with(LoggerInterface::class)
34 public function testNormal()
36 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
37 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
40 $curlResult = new CurlResult('https://test.local', $header . $body, [
42 'content_type' => 'text/html; charset=utf-8',
43 'url' => 'https://test.local'
46 $this->assertTrue($curlResult->isSuccess());
47 $this->assertFalse($curlResult->isTimeout());
48 $this->assertFalse($curlResult->isRedirectUrl());
49 $this->assertSame($header, $curlResult->getHeader());
50 $this->assertSame($body, $curlResult->getBody());
51 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
52 $this->assertSame('https://test.local', $curlResult->getUrl());
53 $this->assertSame('https://test.local', $curlResult->getRedirectUrl());
58 * @runInSeparateProcess
59 * @preserveGlobalState disabled
61 public function testRedirect()
63 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
64 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
67 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
69 'content_type' => 'text/html; charset=utf-8',
70 'url' => 'https://test.local/test/it',
71 'redirect_url' => 'https://test.other'
74 $this->assertTrue($curlResult->isSuccess());
75 $this->assertFalse($curlResult->isTimeout());
76 $this->assertTrue($curlResult->isRedirectUrl());
77 $this->assertSame($header, $curlResult->getHeader());
78 $this->assertSame($body, $curlResult->getBody());
79 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
80 $this->assertSame('https://test.local/test/it', $curlResult->getUrl());
81 $this->assertSame('https://test.other/test/it', $curlResult->getRedirectUrl());
87 public function testTimeout()
89 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
90 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
93 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
95 'content_type' => 'text/html; charset=utf-8',
96 'url' => 'https://test.local/test/it',
97 'redirect_url' => 'https://test.other'
98 ], CURLE_OPERATION_TIMEDOUT, 'Tested error');
100 $this->assertFalse($curlResult->isSuccess());
101 $this->assertTrue($curlResult->isTimeout());
102 $this->assertFalse($curlResult->isRedirectUrl());
103 $this->assertSame($header, $curlResult->getHeader());
104 $this->assertSame($body, $curlResult->getBody());
105 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
106 $this->assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
107 $this->assertSame('Tested error', $curlResult->getError());
112 * @runInSeparateProcess
113 * @preserveGlobalState disabled
115 public function testRedirectHeader()
117 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
118 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
121 $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
123 'content_type' => 'text/html; charset=utf-8',
124 'url' => 'https://test.local/test/it?key=value',
127 $this->assertTrue($curlResult->isSuccess());
128 $this->assertFalse($curlResult->isTimeout());
129 $this->assertTrue($curlResult->isRedirectUrl());
130 $this->assertSame($header, $curlResult->getHeader());
131 $this->assertSame($body, $curlResult->getBody());
132 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
133 $this->assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
134 $this->assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl());
140 public function testInHeader()
142 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
143 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
145 $curlResult = new CurlResult('https://test.local', $header . $body, [
147 'content_type' => 'text/html; charset=utf-8',
148 'url' => 'https://test.local'
150 $this->assertTrue($curlResult->inHeader('vary'));
151 $this->assertFalse($curlResult->inHeader('wrongHeader'));
157 public function testGetHeaderArray()
159 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
160 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
162 $curlResult = new CurlResult('https://test.local', $header . $body, [
164 'content_type' => 'text/html; charset=utf-8',
165 'url' => 'https://test.local'
168 $headers = $curlResult->getHeaderArray();
170 $this->assertNotEmpty($headers);
171 $this->assertArrayHasKey('vary', $headers);
177 public function testGetHeaderWithParam()
179 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
180 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
182 $curlResult = new CurlResult('https://test.local', $header . $body, [
184 'content_type' => 'text/html; charset=utf-8',
185 'url' => 'https://test.local'
188 $this->assertNotEmpty($curlResult->getHeader());
189 $this->assertEmpty($curlResult->getHeader('wrongHeader'));