]> git.mxchange.org Git - friendica.git/blob - tests/src/Network/CurlResultTest.php
adding annotation
[friendica.git] / tests / src / Network / CurlResultTest.php
1 <?php
2
3 namespace Friendica\Test\src\Network;
4
5 use Friendica\Core\Logger;
6 use Friendica\Network\CurlResult;
7 use Friendica\Util\Logger\VoidLogger;
8 use PHPUnit\Framework\TestCase;
9
10 class CurlResultTest extends TestCase
11 {
12         protected function setUp()
13         {
14                 parent::setUp();
15
16                 Logger::init(new VoidLogger());
17         }
18
19         /**
20          * @small
21          */
22         public function testNormal()
23         {
24                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
25                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
26
27
28                 $curlResult = new CurlResult('https://test.local', $header . $body, [
29                         'http_code' => 200,
30                         'content_type' => 'text/html; charset=utf-8',
31                         'url' => 'https://test.local'
32                 ]);
33
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());
42         }
43
44         /**
45          * @small
46          * @runInSeparateProcess
47          * @preserveGlobalState disabled
48          */
49         public function testRedirect()
50         {
51                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
52                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
53
54
55                 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
56                         'http_code' => 301,
57                         'content_type' => 'text/html; charset=utf-8',
58                         'url' => 'https://test.local/test/it',
59                         'redirect_url' => 'https://test.other'
60                 ]);
61
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());
70         }
71
72         /**
73          * @small
74          */
75         public function testTimeout()
76         {
77                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
78                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
79
80
81                 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
82                         'http_code' => 500,
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');
87
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());
96         }
97
98         /**
99          * @small
100          * @runInSeparateProcess
101          * @preserveGlobalState disabled
102          */
103         public function testRedirectHeader()
104         {
105                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
106                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
107
108
109                 $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
110                         'http_code' => 301,
111                         'content_type' => 'text/html; charset=utf-8',
112                         'url' => 'https://test.local/test/it?key=value',
113                 ]);
114
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());
123         }
124 }