]> git.mxchange.org Git - friendica.git/blob - tests/src/Network/CurlResultTest.php
Refactoring DBA-mocking tests
[friendica.git] / tests / src / Network / CurlResultTest.php
1 <?php
2
3 namespace Friendica\Test\src\Network;
4
5 use Friendica\Network\CurlResult;
6 use PHPUnit\Framework\TestCase;
7
8 class CurlResultTest extends TestCase
9 {
10         /**
11          * @small
12          */
13         public function testNormal()
14         {
15                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
16                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
17
18
19                 $curlResult = new CurlResult('https://test.local', $header . $body, [
20                         'http_code' => 200,
21                         'content_type' => 'text/html; charset=utf-8',
22                         'url' => 'https://test.local'
23                 ]);
24
25                 $this->assertTrue($curlResult->isSuccess());
26                 $this->assertFalse($curlResult->isTimeout());
27                 $this->assertFalse($curlResult->isRedirectUrl());
28                 $this->assertSame($header, $curlResult->getHeader());
29                 $this->assertSame($body, $curlResult->getBody());
30                 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
31                 $this->assertSame('https://test.local', $curlResult->getUrl());
32                 $this->assertSame('https://test.local', $curlResult->getRedirectUrl());
33         }
34
35         /**
36          * @small
37          * @runInSeparateProcess
38          * @preserveGlobalState disabled
39          */
40         public function testRedirect()
41         {
42                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
43                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
44
45
46                 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
47                         'http_code' => 301,
48                         'content_type' => 'text/html; charset=utf-8',
49                         'url' => 'https://test.local/test/it',
50                         'redirect_url' => 'https://test.other'
51                 ]);
52
53                 $this->assertTrue($curlResult->isSuccess());
54                 $this->assertFalse($curlResult->isTimeout());
55                 $this->assertTrue($curlResult->isRedirectUrl());
56                 $this->assertSame($header, $curlResult->getHeader());
57                 $this->assertSame($body, $curlResult->getBody());
58                 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
59                 $this->assertSame('https://test.local/test/it', $curlResult->getUrl());
60                 $this->assertSame('https://test.other/test/it', $curlResult->getRedirectUrl());
61         }
62
63         /**
64          * @small
65          */
66         public function testTimeout()
67         {
68                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
69                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
70
71
72                 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
73                         'http_code' => 500,
74                         'content_type' => 'text/html; charset=utf-8',
75                         'url' => 'https://test.local/test/it',
76                         'redirect_url' => 'https://test.other'
77                 ], CURLE_OPERATION_TIMEDOUT, 'Tested error');
78
79                 $this->assertFalse($curlResult->isSuccess());
80                 $this->assertTrue($curlResult->isTimeout());
81                 $this->assertFalse($curlResult->isRedirectUrl());
82                 $this->assertSame($header, $curlResult->getHeader());
83                 $this->assertSame($body, $curlResult->getBody());
84                 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
85                 $this->assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
86                 $this->assertSame('Tested error', $curlResult->getError());
87         }
88
89         /**
90          * @small
91          * @runInSeparateProcess
92          * @preserveGlobalState disabled
93          */
94         public function testRedirectHeader()
95         {
96                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
97                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
98
99
100                 $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
101                         'http_code' => 301,
102                         'content_type' => 'text/html; charset=utf-8',
103                         'url' => 'https://test.local/test/it?key=value',
104                 ]);
105
106                 $this->assertTrue($curlResult->isSuccess());
107                 $this->assertFalse($curlResult->isTimeout());
108                 $this->assertTrue($curlResult->isRedirectUrl());
109                 $this->assertSame($header, $curlResult->getHeader());
110                 $this->assertSame($body, $curlResult->getBody());
111                 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
112                 $this->assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
113                 $this->assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl());
114         }
115 }