]> git.mxchange.org Git - friendica.git/blob - tests/src/Network/HTTPClient/Response/CurlResultTest.php
Merge pull request #12534 from annando/activity-types
[friendica.git] / tests / src / Network / HTTPClient / Response / CurlResultTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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\HTTPClient\Response;
23
24 use Dice\Dice;
25 use Friendica\DI;
26 use Friendica\Network\HTTPClient\Response\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(): void
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                 $headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
57                 $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
58
59
60                 $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local', $header . $body, [
61                         'http_code' => 200,
62                         'content_type' => 'text/html; charset=utf-8',
63                         'url' => 'https://test.local'
64                 ]);
65
66                 self::assertTrue($curlResult->isSuccess());
67                 self::assertFalse($curlResult->isTimeout());
68                 self::assertFalse($curlResult->isRedirectUrl());
69                 self::assertSame($headerArray, $curlResult->getHeaders());
70                 self::assertSame($body, $curlResult->getBody());
71                 self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
72                 self::assertSame('https://test.local', $curlResult->getUrl());
73                 self::assertSame('https://test.local', $curlResult->getRedirectUrl());
74         }
75
76         /**
77          * @small
78          * @runInSeparateProcess
79          * @preserveGlobalState disabled
80          */
81         public function testRedirect()
82         {
83                 $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
84                 $headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
85                 $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
86
87
88                 $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local/test/it', $header . $body, [
89                         'http_code' => 301,
90                         'content_type' => 'text/html; charset=utf-8',
91                         'url' => 'https://test.local/test/it',
92                         'redirect_url' => 'https://test.other'
93                 ]);
94
95                 self::assertTrue($curlResult->isSuccess());
96                 self::assertFalse($curlResult->isTimeout());
97                 self::assertTrue($curlResult->isRedirectUrl());
98                 self::assertSame($headerArray, $curlResult->getHeaders());
99                 self::assertSame($body, $curlResult->getBody());
100                 self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
101                 self::assertSame('https://test.local/test/it', $curlResult->getUrl());
102                 self::assertSame('https://test.other/test/it', $curlResult->getRedirectUrl());
103         }
104
105         /**
106          * @small
107          */
108         public function testTimeout()
109         {
110                 $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
111                 $headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
112                 $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
113
114
115                 $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local/test/it', $header . $body, [
116                         'http_code' => 500,
117                         'content_type' => 'text/html; charset=utf-8',
118                         'url' => 'https://test.local/test/it',
119                         'redirect_url' => 'https://test.other'
120                 ], CURLE_OPERATION_TIMEDOUT, 'Tested error');
121
122                 self::assertFalse($curlResult->isSuccess());
123                 self::assertTrue($curlResult->isTimeout());
124                 self::assertFalse($curlResult->isRedirectUrl());
125                 self::assertSame($headerArray, $curlResult->getHeaders());
126                 self::assertSame($body, $curlResult->getBody());
127                 self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
128                 self::assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
129                 self::assertSame('Tested error', $curlResult->getError());
130         }
131
132         /**
133          * @small
134          * @runInSeparateProcess
135          * @preserveGlobalState disabled
136          */
137         public function testRedirectHeader()
138         {
139                 $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.redirect');
140                 $headerArray = include(__DIR__ . '/../../../../datasets/curl/about.redirect.php');
141                 $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
142
143
144                 $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
145                         'http_code' => 301,
146                         'content_type' => 'text/html; charset=utf-8',
147                         'url' => 'https://test.local/test/it?key=value',
148                 ]);
149
150                 self::assertTrue($curlResult->isSuccess());
151                 self::assertFalse($curlResult->isTimeout());
152                 self::assertTrue($curlResult->isRedirectUrl());
153                 self::assertSame($headerArray, $curlResult->getHeaders());
154                 self::assertSame($body, $curlResult->getBody());
155                 self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
156                 self::assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
157                 self::assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl());
158         }
159
160         /**
161          * @small
162          */
163         public function testInHeader()
164         {
165                 $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
166                 $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
167
168                 $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local', $header . $body, [
169                         'http_code' => 200,
170                         'content_type' => 'text/html; charset=utf-8',
171                         'url' => 'https://test.local'
172                 ]);
173                 self::assertTrue($curlResult->inHeader('vary'));
174                 self::assertFalse($curlResult->inHeader('wrongHeader'));
175         }
176
177          /**
178          * @small
179          */
180         public function testGetHeaderArray()
181         {
182                 $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
183                 $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
184
185                 $curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local', $header . $body, [
186                         'http_code' => 200,
187                         'content_type' => 'text/html; charset=utf-8',
188                         'url' => 'https://test.local'
189                 ]);
190
191                 $headers = $curlResult->getHeaderArray();
192
193                 self::assertNotEmpty($headers);
194                 self::assertArrayHasKey('vary', $headers);
195         }
196
197          /**
198          * @small
199          */
200         public function testGetHeaderWithParam()
201         {
202                 $header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
203                 $body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
204
205                 $curlResult = new CurlResult('https://test.local', $header . $body, [
206                         'http_code' => 200,
207                         'content_type' => 'text/html; charset=utf-8',
208                         'url' => 'https://test.local'
209                 ]);
210
211                 self::assertNotEmpty($curlResult->getHeaders());
212                 self::assertEmpty($curlResult->getHeader('wrongHeader'));
213         }
214 }