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