Fix Content-Type for `CurlResult` class
[friendica.git/.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                 $headerArray = include(__DIR__ . '/../../datasets/curl/about.head.php');
57                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
58
59
60                 $curlResult = new 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                 $this->assertTrue($curlResult->isSuccess());
67                 $this->assertFalse($curlResult->isTimeout());
68                 $this->assertFalse($curlResult->isRedirectUrl());
69                 $this->assertSame($headerArray, $curlResult->getHeaders());
70                 $this->assertSame($body, $curlResult->getBody());
71                 $this->assertSame(['text/html; charset=utf-8'], $curlResult->getContentType());
72                 $this->assertSame('https://test.local', $curlResult->getUrl());
73                 $this->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 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                 $this->assertTrue($curlResult->isSuccess());
96                 $this->assertFalse($curlResult->isTimeout());
97                 $this->assertTrue($curlResult->isRedirectUrl());
98                 $this->assertSame($headerArray, $curlResult->getHeaders());
99                 $this->assertSame($body, $curlResult->getBody());
100                 $this->assertSame(['text/html; charset=utf-8'], $curlResult->getContentType());
101                 $this->assertSame('https://test.local/test/it', $curlResult->getUrl());
102                 $this->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 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                 $this->assertFalse($curlResult->isSuccess());
123                 $this->assertTrue($curlResult->isTimeout());
124                 $this->assertFalse($curlResult->isRedirectUrl());
125                 $this->assertSame($headerArray, $curlResult->getHeaders());
126                 $this->assertSame($body, $curlResult->getBody());
127                 $this->assertSame(['text/html; charset=utf-8'], $curlResult->getContentType());
128                 $this->assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
129                 $this->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                 $this->assertTrue($curlResult->isSuccess());
151                 $this->assertFalse($curlResult->isTimeout());
152                 $this->assertTrue($curlResult->isRedirectUrl());
153                 $this->assertSame($headerArray, $curlResult->getHeaders());
154                 $this->assertSame($body, $curlResult->getBody());
155                 $this->assertSame(['text/html; charset=utf-8'], $curlResult->getContentType());
156                 $this->assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
157                 $this->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 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                 $this->assertTrue($curlResult->inHeader('vary'));
174                 $this->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 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                 $this->assertNotEmpty($headers);
194                 $this->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                 $this->assertNotEmpty($curlResult->getHeaders());
212                 $this->assertEmpty($curlResult->getHeader('wrongHeader'));
213         }
214 }