]> git.mxchange.org Git - friendica.git/blob - tests/src/Network/CurlResultTest.php
@brief is removed completely
[friendica.git] / tests / src / Network / CurlResultTest.php
1 <?php
2
3 namespace Friendica\Test\src\Network;
4
5 use Dice\Dice;
6 use Friendica\DI;
7 use Friendica\Network\CurlResult;
8 use Mockery\MockInterface;
9 use PHPUnit\Framework\TestCase;
10 use Psr\Log\LoggerInterface;
11 use Psr\Log\NullLogger;
12
13 class CurlResultTest extends TestCase
14 {
15         protected function setUp()
16         {
17                 parent::setUp();
18
19                 /** @var Dice|MockInterface $dice */
20                 $dice = \Mockery::mock(Dice::class)->makePartial();
21                 $dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
22
23                 $logger = new NullLogger();
24                 $dice->shouldReceive('create')
25                            ->with(LoggerInterface::class)
26                            ->andReturn($logger);
27
28                 DI::init($dice);
29         }
30
31         /**
32          * @small
33          */
34         public function testNormal()
35         {
36                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
37                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
38
39
40                 $curlResult = new CurlResult('https://test.local', $header . $body, [
41                         'http_code' => 200,
42                         'content_type' => 'text/html; charset=utf-8',
43                         'url' => 'https://test.local'
44                 ]);
45
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());
54         }
55
56         /**
57          * @small
58          * @runInSeparateProcess
59          * @preserveGlobalState disabled
60          */
61         public function testRedirect()
62         {
63                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
64                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
65
66
67                 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
68                         'http_code' => 301,
69                         'content_type' => 'text/html; charset=utf-8',
70                         'url' => 'https://test.local/test/it',
71                         'redirect_url' => 'https://test.other'
72                 ]);
73
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());
82         }
83
84         /**
85          * @small
86          */
87         public function testTimeout()
88         {
89                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
90                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
91
92
93                 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
94                         'http_code' => 500,
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');
99
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());
108         }
109
110         /**
111          * @small
112          * @runInSeparateProcess
113          * @preserveGlobalState disabled
114          */
115         public function testRedirectHeader()
116         {
117                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
118                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
119
120
121                 $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
122                         'http_code' => 301,
123                         'content_type' => 'text/html; charset=utf-8',
124                         'url' => 'https://test.local/test/it?key=value',
125                 ]);
126
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());
135         }
136
137         /**
138          * @small
139          */
140         public function testInHeader()
141         {
142                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
143                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
144
145                 $curlResult = new CurlResult('https://test.local', $header . $body, [
146                         'http_code' => 200,
147                         'content_type' => 'text/html; charset=utf-8',
148                         'url' => 'https://test.local'
149                 ]);
150                 $this->assertTrue($curlResult->inHeader('vary'));
151                 $this->assertFalse($curlResult->inHeader('wrongHeader'));
152         }
153
154          /**
155          * @small
156          */
157         public function testGetHeaderArray()
158         {
159                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
160                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
161
162                 $curlResult = new CurlResult('https://test.local', $header . $body, [
163                         'http_code' => 200,
164                         'content_type' => 'text/html; charset=utf-8',
165                         'url' => 'https://test.local'
166                 ]);
167
168                 $headers = $curlResult->getHeaderArray();
169
170                 $this->assertNotEmpty($headers);
171                 $this->assertArrayHasKey('vary', $headers);
172         }
173
174          /**
175          * @small
176          */
177         public function testGetHeaderWithParam()
178         {
179                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
180                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
181
182                 $curlResult = new CurlResult('https://test.local', $header . $body, [
183                         'http_code' => 200,
184                         'content_type' => 'text/html; charset=utf-8',
185                         'url' => 'https://test.local'
186                 ]);
187
188                 $this->assertNotEmpty($curlResult->getHeader());
189                 $this->assertEmpty($curlResult->getHeader('wrongHeader'));
190         }
191 }