]> git.mxchange.org Git - friendica.git/blob - tests/src/Network/CurlResultTest.php
Simplify Contact::addRelationship call in ActivityPub\Processor::followUser
[friendica.git] / tests / src / Network / CurlResultTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Network;
23
24 use Dice\Dice;
25 use Friendica\DI;
26 use Friendica\Network\CurlResult;
27 use Mockery\MockInterface;
28 use PHPUnit\Framework\TestCase;
29 use Psr\Log\LoggerInterface;
30 use Psr\Log\NullLogger;
31
32 class CurlResultTest extends TestCase
33 {
34         protected function setUp()
35         {
36                 parent::setUp();
37
38                 /** @var Dice|MockInterface $dice */
39                 $dice = \Mockery::mock(Dice::class)->makePartial();
40                 $dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
41
42                 $logger = new NullLogger();
43                 $dice->shouldReceive('create')
44                            ->with(LoggerInterface::class)
45                            ->andReturn($logger);
46
47                 DI::init($dice);
48         }
49
50         /**
51          * @small
52          */
53         public function testNormal()
54         {
55                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
56                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
57
58
59                 $curlResult = new CurlResult('https://test.local', $header . $body, [
60                         'http_code' => 200,
61                         'content_type' => 'text/html; charset=utf-8',
62                         'url' => 'https://test.local'
63                 ]);
64
65                 self::assertTrue($curlResult->isSuccess());
66                 self::assertFalse($curlResult->isTimeout());
67                 self::assertFalse($curlResult->isRedirectUrl());
68                 self::assertSame($header, $curlResult->getHeader());
69                 self::assertSame($body, $curlResult->getBody());
70                 self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
71                 self::assertSame('https://test.local', $curlResult->getUrl());
72                 self::assertSame('https://test.local', $curlResult->getRedirectUrl());
73         }
74
75         /**
76          * @small
77          * @runInSeparateProcess
78          * @preserveGlobalState disabled
79          */
80         public function testRedirect()
81         {
82                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
83                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
84
85
86                 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
87                         'http_code' => 301,
88                         'content_type' => 'text/html; charset=utf-8',
89                         'url' => 'https://test.local/test/it',
90                         'redirect_url' => 'https://test.other'
91                 ]);
92
93                 self::assertTrue($curlResult->isSuccess());
94                 self::assertFalse($curlResult->isTimeout());
95                 self::assertTrue($curlResult->isRedirectUrl());
96                 self::assertSame($header, $curlResult->getHeader());
97                 self::assertSame($body, $curlResult->getBody());
98                 self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
99                 self::assertSame('https://test.local/test/it', $curlResult->getUrl());
100                 self::assertSame('https://test.other/test/it', $curlResult->getRedirectUrl());
101         }
102
103         /**
104          * @small
105          */
106         public function testTimeout()
107         {
108                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
109                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
110
111
112                 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
113                         'http_code' => 500,
114                         'content_type' => 'text/html; charset=utf-8',
115                         'url' => 'https://test.local/test/it',
116                         'redirect_url' => 'https://test.other'
117                 ], CURLE_OPERATION_TIMEDOUT, 'Tested error');
118
119                 self::assertFalse($curlResult->isSuccess());
120                 self::assertTrue($curlResult->isTimeout());
121                 self::assertFalse($curlResult->isRedirectUrl());
122                 self::assertSame($header, $curlResult->getHeader());
123                 self::assertSame($body, $curlResult->getBody());
124                 self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
125                 self::assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
126                 self::assertSame('Tested error', $curlResult->getError());
127         }
128
129         /**
130          * @small
131          * @runInSeparateProcess
132          * @preserveGlobalState disabled
133          */
134         public function testRedirectHeader()
135         {
136                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
137                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
138
139
140                 $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
141                         'http_code' => 301,
142                         'content_type' => 'text/html; charset=utf-8',
143                         'url' => 'https://test.local/test/it?key=value',
144                 ]);
145
146                 self::assertTrue($curlResult->isSuccess());
147                 self::assertFalse($curlResult->isTimeout());
148                 self::assertTrue($curlResult->isRedirectUrl());
149                 self::assertSame($header, $curlResult->getHeader());
150                 self::assertSame($body, $curlResult->getBody());
151                 self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
152                 self::assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
153                 self::assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl());
154         }
155
156         /**
157          * @small
158          */
159         public function testInHeader()
160         {
161                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
162                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
163
164                 $curlResult = new CurlResult('https://test.local', $header . $body, [
165                         'http_code' => 200,
166                         'content_type' => 'text/html; charset=utf-8',
167                         'url' => 'https://test.local'
168                 ]);
169                 self::assertTrue($curlResult->inHeader('vary'));
170                 self::assertFalse($curlResult->inHeader('wrongHeader'));
171         }
172
173          /**
174          * @small
175          */
176         public function testGetHeaderArray()
177         {
178                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
179                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
180
181                 $curlResult = new CurlResult('https://test.local', $header . $body, [
182                         'http_code' => 200,
183                         'content_type' => 'text/html; charset=utf-8',
184                         'url' => 'https://test.local'
185                 ]);
186
187                 $headers = $curlResult->getHeaderArray();
188
189                 self::assertNotEmpty($headers);
190                 self::assertArrayHasKey('vary', $headers);
191         }
192
193          /**
194          * @small
195          */
196         public function testGetHeaderWithParam()
197         {
198                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
199                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
200
201                 $curlResult = new CurlResult('https://test.local', $header . $body, [
202                         'http_code' => 200,
203                         'content_type' => 'text/html; charset=utf-8',
204                         'url' => 'https://test.local'
205                 ]);
206
207                 self::assertNotEmpty($curlResult->getHeader());
208                 self::assertEmpty($curlResult->getHeader('wrongHeader'));
209         }
210 }