]> git.mxchange.org Git - friendica.git/blob - tests/src/Network/CurlResultTest.php
Tests added
[friendica.git] / tests / src / Network / CurlResultTest.php
1 <?php
2
3 namespace Friendica\Test\src\Network;
4
5 use Dice\Dice;
6 use Friendica\BaseObject;
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
20                 /** @var Dice|MockInterface $dice */
21                 $dice = \Mockery::mock(Dice::class)->makePartial();
22                 $dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
23
24                 $logger = new NullLogger();
25                 $dice->shouldReceive('create')
26                            ->with(LoggerInterface::class)
27                            ->andReturn($logger);
28
29                 BaseObject::setDependencyInjection($dice);
30         }
31
32         /**
33          * @small
34          */
35         public function testNormal()
36         {
37                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
38                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
39
40
41                 $curlResult = new CurlResult('https://test.local', $header . $body, [
42                         'http_code' => 200,
43                         'content_type' => 'text/html; charset=utf-8',
44                         'url' => 'https://test.local'
45                 ]);
46
47                 $this->assertTrue($curlResult->isSuccess());
48                 $this->assertFalse($curlResult->isTimeout());
49                 $this->assertFalse($curlResult->isRedirectUrl());
50                 $this->assertSame($header, $curlResult->getHeader());
51                 $this->assertSame($body, $curlResult->getBody());
52                 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
53                 $this->assertSame('https://test.local', $curlResult->getUrl());
54                 $this->assertSame('https://test.local', $curlResult->getRedirectUrl());
55         }
56
57         /**
58          * @small
59          * @runInSeparateProcess
60          * @preserveGlobalState disabled
61          */
62         public function testRedirect()
63         {
64                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
65                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
66
67
68                 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
69                         'http_code' => 301,
70                         'content_type' => 'text/html; charset=utf-8',
71                         'url' => 'https://test.local/test/it',
72                         'redirect_url' => 'https://test.other'
73                 ]);
74
75                 $this->assertTrue($curlResult->isSuccess());
76                 $this->assertFalse($curlResult->isTimeout());
77                 $this->assertTrue($curlResult->isRedirectUrl());
78                 $this->assertSame($header, $curlResult->getHeader());
79                 $this->assertSame($body, $curlResult->getBody());
80                 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
81                 $this->assertSame('https://test.local/test/it', $curlResult->getUrl());
82                 $this->assertSame('https://test.other/test/it', $curlResult->getRedirectUrl());
83         }
84
85         /**
86          * @small
87          */
88         public function testTimeout()
89         {
90                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
91                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
92
93
94                 $curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
95                         'http_code' => 500,
96                         'content_type' => 'text/html; charset=utf-8',
97                         'url' => 'https://test.local/test/it',
98                         'redirect_url' => 'https://test.other'
99                 ], CURLE_OPERATION_TIMEDOUT, 'Tested error');
100
101                 $this->assertFalse($curlResult->isSuccess());
102                 $this->assertTrue($curlResult->isTimeout());
103                 $this->assertFalse($curlResult->isRedirectUrl());
104                 $this->assertSame($header, $curlResult->getHeader());
105                 $this->assertSame($body, $curlResult->getBody());
106                 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
107                 $this->assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
108                 $this->assertSame('Tested error', $curlResult->getError());
109         }
110
111         /**
112          * @small
113          * @runInSeparateProcess
114          * @preserveGlobalState disabled
115          */
116         public function testRedirectHeader()
117         {
118                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
119                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
120
121
122                 $curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
123                         'http_code' => 301,
124                         'content_type' => 'text/html; charset=utf-8',
125                         'url' => 'https://test.local/test/it?key=value',
126                 ]);
127
128                 $this->assertTrue($curlResult->isSuccess());
129                 $this->assertFalse($curlResult->isTimeout());
130                 $this->assertTrue($curlResult->isRedirectUrl());
131                 $this->assertSame($header, $curlResult->getHeader());
132                 $this->assertSame($body, $curlResult->getBody());
133                 $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
134                 $this->assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
135                 $this->assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl());
136         }
137
138         /**
139          * @small
140          */
141         public function testInHeader()
142         {
143                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
144                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
145
146                 $curlResult = new CurlResult('https://test.local', $header . $body, [
147                         'http_code' => 200,
148                         'content_type' => 'text/html; charset=utf-8',
149                         'url' => 'https://test.local'
150                 ]);
151                 $this->assertTrue($curlResult->inHeader('vary'));
152                 $this->assertFalse($curlResult->inHeader('wrongHeader'));
153         }
154
155          /**
156          * @small
157          */
158         public function testGetHeaderArray()
159         {
160                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
161                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
162
163                 $curlResult = new CurlResult('https://test.local', $header . $body, [
164                         'http_code' => 200,
165                         'content_type' => 'text/html; charset=utf-8',
166                         'url' => 'https://test.local'
167                 ]);
168
169                 $headers = $curlResult->getHeaderArray();
170
171                 $this->assertNotEmpty($headers);
172                 $this->assertArrayHasKey('vary', $headers);
173         }
174
175          /**
176          * @small
177          */
178         public function testGetHeaderWithParam()
179         {
180                 $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
181                 $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
182
183                 $curlResult = new CurlResult('https://test.local', $header . $body, [
184                         'http_code' => 200,
185                         'content_type' => 'text/html; charset=utf-8',
186                         'url' => 'https://test.local'
187                 ]);
188
189                 $this->assertNotEmpty($curlResult->getHeader());
190                 $this->assertEmpty('vary', $curlResult->getHeader('wrongHeader'));
191         }
192 }