3 namespace Friendica\Test\src\Network;
5 use Friendica\Core\Logger;
6 use Friendica\Network\CurlResult;
7 use Friendica\Util\Logger\VoidLogger;
8 use PHPUnit\Framework\TestCase;
10 class CurlResultTest extends TestCase
12 protected function setUp()
16 Logger::init(new VoidLogger());
22 public function testNormal()
24 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
25 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
28 $curlResult = new CurlResult('https://test.local', $header . $body, [
30 'content_type' => 'text/html; charset=utf-8',
31 'url' => 'https://test.local'
34 $this->assertTrue($curlResult->isSuccess());
35 $this->assertFalse($curlResult->isTimeout());
36 $this->assertFalse($curlResult->isRedirectUrl());
37 $this->assertSame($header, $curlResult->getHeader());
38 $this->assertSame($body, $curlResult->getBody());
39 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
40 $this->assertSame('https://test.local', $curlResult->getUrl());
41 $this->assertSame('https://test.local', $curlResult->getRedirectUrl());
46 * @runInSeparateProcess
47 * @preserveGlobalState disabled
49 public function testRedirect()
51 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
52 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
55 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
57 'content_type' => 'text/html; charset=utf-8',
58 'url' => 'https://test.local/test/it',
59 'redirect_url' => 'https://test.other'
62 $this->assertTrue($curlResult->isSuccess());
63 $this->assertFalse($curlResult->isTimeout());
64 $this->assertTrue($curlResult->isRedirectUrl());
65 $this->assertSame($header, $curlResult->getHeader());
66 $this->assertSame($body, $curlResult->getBody());
67 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
68 $this->assertSame('https://test.local/test/it', $curlResult->getUrl());
69 $this->assertSame('https://test.other/test/it', $curlResult->getRedirectUrl());
75 public function testTimeout()
77 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
78 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
81 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
83 'content_type' => 'text/html; charset=utf-8',
84 'url' => 'https://test.local/test/it',
85 'redirect_url' => 'https://test.other'
86 ], CURLE_OPERATION_TIMEDOUT, 'Tested error');
88 $this->assertFalse($curlResult->isSuccess());
89 $this->assertTrue($curlResult->isTimeout());
90 $this->assertFalse($curlResult->isRedirectUrl());
91 $this->assertSame($header, $curlResult->getHeader());
92 $this->assertSame($body, $curlResult->getBody());
93 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
94 $this->assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
95 $this->assertSame('Tested error', $curlResult->getError());
100 * @runInSeparateProcess
101 * @preserveGlobalState disabled
103 public function testRedirectHeader()
105 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
106 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
109 $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
111 'content_type' => 'text/html; charset=utf-8',
112 'url' => 'https://test.local/test/it?key=value',
115 $this->assertTrue($curlResult->isSuccess());
116 $this->assertFalse($curlResult->isTimeout());
117 $this->assertTrue($curlResult->isRedirectUrl());
118 $this->assertSame($header, $curlResult->getHeader());
119 $this->assertSame($body, $curlResult->getBody());
120 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
121 $this->assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
122 $this->assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl());