Debian Codegraph @

lib/php/test/Unit/Lib/Transport/TCurlClientTest.php

raw
1 <?php
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 namespace Test\Thrift\Unit\Lib\Transport;
23
24 use phpmock\phpunit\PHPMock;
25 use PHPUnit\Framework\TestCase;
26 use Test\Thrift\Unit\Lib\ReflectionHelper;
27 use Thrift\Exception\TTransportException;
28 use Thrift\Transport\TCurlClient;
29
30 class TCurlClientTest extends TestCase
31 {
32 use PHPMock;
33 use ReflectionHelper;
34
35 public function testSetTimeoutSecs()
36 {
37 $host = 'localhost';
38 $transport = new TCurlClient($host);
39 $transport->setTimeoutSecs(1000);
40
41 $this->assertEquals(1000, $this->getPropertyValue($transport, 'timeout_'));
42 }
43
44 public function testSetConnectionTimeoutSecs()
45 {
46 $host = 'localhost';
47 $transport = new TCurlClient($host);
48 $transport->setConnectionTimeoutSecs(1000);
49
50 $this->assertEquals(1000, $this->getPropertyValue($transport, 'connectionTimeout_'));
51 }
52
53 public function testIsOpen()
54 {
55 $host = 'localhost';
56 $transport = new TCurlClient($host);
57 $this->assertTrue($transport->isOpen());
58 }
59
60 public function testOpen()
61 {
62 $host = 'localhost';
63 $transport = new TCurlClient($host);
64 $this->assertNull($transport->open());
65 }
66
67 public function testClose()
68 {
69 $host = 'localhost';
70 $transport = new TCurlClient($host);
71
72 $this->setPropertyValue($transport, 'request_', 'testRequest');
73 $this->setPropertyValue($transport, 'response_', 'testResponse');
74
75 $this->assertNull($transport->close());
76 $this->assertEmpty($this->getPropertyValue($transport, 'request_'));
77 $this->assertEmpty($this->getPropertyValue($transport, 'response_'));
78 }
79
80 public function testRead()
81 {
82 $host = 'localhost';
83 $transport = new TCurlClient($host);
84
85 $this->setPropertyValue($transport, 'response_', '1234567890');
86
87 $response = $transport->read(5);
88 $this->assertEquals('12345', $response);
89 $this->assertEquals('67890', $this->getPropertyValue($transport, 'response_'));
90
91 $response = $transport->read(5);
92 $this->assertEquals('67890', $response);
93 # The response does not cleaned after reading full answer, maybe it should be fixed
94 $this->assertEquals('67890', $this->getPropertyValue($transport, 'response_'));
95 }
96
97 public function testReadAll()
98 {
99 $host = 'localhost';
100 $transport = new TCurlClient($host);
101
102 $this->setPropertyValue($transport, 'response_', '1234567890');
103
104 $response = $transport->readAll(5);
105 $this->assertEquals('12345', $response);
106 $this->assertEquals('67890', $this->getPropertyValue($transport, 'response_'));
107 }
108
109 public function testReadAllThrift4656()
110 {
111 $host = 'localhost';
112 $transport = new TCurlClient($host);
113
114 $this->setPropertyValue($transport, 'response_', '');
115
116 $this->expectException(TTransportException::class);
117 $this->expectExceptionMessage('TCurlClient could not read 5 bytes');
118 $this->expectExceptionCode(TTransportException::UNKNOWN);
119
120 $transport->readAll(5);
121 }
122
123 public function testWrite()
124 {
125 $host = 'localhost';
126 $transport = new TCurlClient($host);
127
128 $this->setPropertyValue($transport, 'request_', '1234567890');
129
130 $transport->write('12345');
131 $this->assertEquals('123456789012345', $this->getPropertyValue($transport, 'request_'));
132 }
133
134 public function testAddHeaders()
135 {
136 $host = 'localhost';
137 $transport = new TCurlClient($host);
138
139 $this->setPropertyValue($transport, 'headers_', ['test' => '1234567890']);
140
141 $transport->addHeaders(['test2' => '12345']);
142 $this->assertEquals(['test' => '1234567890', 'test2' => '12345'], $this->getPropertyValue($transport, 'headers_'));
143 }
144
145 /**
146 * @dataProvider flushDataProvider
147 */
148 public function testFlush(
149 $host,
150 $port,
151 $uri,
152 $scheme,
153 $headers,
154 $request,
155 $timeout,
156 $connectionTimeout,
157 $curlSetOptCalls,
158 $response,
159 $responseError,
160 $responseCode,
161 $expectedException = null,
162 $expectedMessage = null,
163 $expectedCode = null
164 ) {
165 $this->getFunctionMock('Thrift\\Transport', 'register_shutdown_function')
166 ->expects($this->once())
167 ->with(
168 $this->callback(
169 function ($arg) {
170 return is_array($arg)
171 && $arg[0] === 'Thrift\\Transport\\TCurlClient'
172 && $arg[1] === 'closeCurlHandle';
173 }
174 )
175 );
176 $this->getFunctionMock('Thrift\\Transport', 'curl_init')
177 ->expects($this->once());
178
179 $this->getFunctionMock('Thrift\\Transport', 'curl_setopt')
180 ->expects($this->any())
181 ->withConsecutive(...$curlSetOptCalls)
182 ->willReturn(true);
183
184 $this->getFunctionMock('Thrift\\Transport', 'curl_exec')
185 ->expects($this->once())
186 ->with($this->anything())
187 ->willReturn($response);
188
189 $this->getFunctionMock('Thrift\\Transport', 'curl_error')
190 ->expects($this->once())
191 ->with($this->anything())
192 ->willReturn($responseError);
193
194 $this->getFunctionMock('Thrift\\Transport', 'curl_getinfo')
195 ->expects($this->once())
196 ->with($this->anything(), CURLINFO_HTTP_CODE)
197 ->willReturn($responseCode);
198
199 if (!is_null($expectedException)) {
200 $this->expectException($expectedException);
201 $this->expectExceptionMessage($expectedMessage);
202 $this->expectExceptionCode($expectedCode);
203
204 $this->getFunctionMock('Thrift\\Transport', 'curl_close')
205 ->expects($this->once())
206 ->with($this->anything());
207 }
208
209 $transport = new TCurlClient($host, $port, $uri, $scheme);
210 if (!empty($headers)) {
211 $transport->addHeaders($headers);
212 }
213 $transport->write($request);
214 if (!empty($timeout)) {
215 $transport->setTimeoutSecs($timeout);
216 }
217 if (!empty($connectionTimeout)) {
218 $transport->setConnectionTimeoutSecs($connectionTimeout);
219 }
220
221 $transport->flush();
222 }
223
224 public function flushDataProvider()
225 {
226 $request = 'request';
227
228 $default = [
229 'host' => 'localhost',
230 'port' => 80,
231 'uri' => '',
232 'scheme' => 'http',
233 'headers' => [],
234 'request' => $request,
235 'timeout' => null,
236 'connectionTimeout' => null,
237 'curlSetOptCalls' => [
238 [$this->anything(), CURLOPT_RETURNTRANSFER, true],
239 [$this->anything(), CURLOPT_USERAGENT, 'PHP/TCurlClient'],
240 [$this->anything(), CURLOPT_CUSTOMREQUEST, 'POST'],
241 [$this->anything(), CURLOPT_FOLLOWLOCATION, true],
242 [$this->anything(), CURLOPT_MAXREDIRS, 1],
243 [
244 $this->anything(),
245 CURLOPT_HTTPHEADER,
246 [
247 'Accept: application/x-thrift',
248 'Content-Type: application/x-thrift',
249 'Content-Length: ' . strlen($request),
250 ],
251 ],
252 [$this->anything(), CURLOPT_POSTFIELDS, $request],
253 [$this->anything(), CURLOPT_URL, 'http://localhost'],
254 ],
255 'response' => 'response',
256 'responseError' => '',
257 'responseCode' => 200,
258 ];
259
260 yield 'default' => $default;
261 yield 'additionalHeaders' => array_merge(
262 $default,
263 [
264 'headers' => ['test' => '1234567890'],
265 'curlSetOptCalls' => [
266 [$this->anything(), CURLOPT_RETURNTRANSFER, true],
267 [$this->anything(), CURLOPT_USERAGENT, 'PHP/TCurlClient'],
268 [$this->anything(), CURLOPT_CUSTOMREQUEST, 'POST'],
269 [$this->anything(), CURLOPT_FOLLOWLOCATION, true],
270 [$this->anything(), CURLOPT_MAXREDIRS, 1],
271 [
272 $this->anything(),
273 CURLOPT_HTTPHEADER,
274 [
275 'Accept: application/x-thrift',
276 'Content-Type: application/x-thrift',
277 'Content-Length: ' . strlen($request),
278 'test: 1234567890',
279 ],
280 ],
281 [$this->anything(), CURLOPT_POSTFIELDS, $request],
282 [$this->anything(), CURLOPT_URL, 'http://localhost'],
283 ],
284 ]
285 );
286 yield 'uri' => array_merge(
287 $default,
288 [
289 'uri' => 'test1234567890',
290 'curlSetOptCalls' => [
291 [$this->anything(), CURLOPT_RETURNTRANSFER, true],
292 [$this->anything(), CURLOPT_USERAGENT, 'PHP/TCurlClient'],
293 [$this->anything(), CURLOPT_CUSTOMREQUEST, 'POST'],
294 [$this->anything(), CURLOPT_FOLLOWLOCATION, true],
295 [$this->anything(), CURLOPT_MAXREDIRS, 1],
296 [
297 $this->anything(),
298 CURLOPT_HTTPHEADER,
299 [
300 'Accept: application/x-thrift',
301 'Content-Type: application/x-thrift',
302 'Content-Length: ' . strlen($request),
303 ],
304 ],
305 [$this->anything(), CURLOPT_POSTFIELDS, $request],
306 [$this->anything(), CURLOPT_URL, 'http://localhost/test1234567890'],
307 ],
308 ]
309 );
310 yield 'timeout' => array_merge(
311 $default,
312 [
313 'timeout' => 10,
314 'connectionTimeout' => 10,
315 'curlSetOptCalls' => [
316 [$this->anything(), CURLOPT_RETURNTRANSFER, true],
317 [$this->anything(), CURLOPT_USERAGENT, 'PHP/TCurlClient'],
318 [$this->anything(), CURLOPT_CUSTOMREQUEST, 'POST'],
319 [$this->anything(), CURLOPT_FOLLOWLOCATION, true],
320 [$this->anything(), CURLOPT_MAXREDIRS, 1],
321 [
322 $this->anything(),
323 CURLOPT_HTTPHEADER,
324 [
325 'Accept: application/x-thrift',
326 'Content-Type: application/x-thrift',
327 'Content-Length: ' . strlen($request),
328 ],
329 ],
330 [$this->anything(), CURLOPT_TIMEOUT, 10],
331 [$this->anything(), CURLOPT_CONNECTTIMEOUT, 10],
332 [$this->anything(), CURLOPT_POSTFIELDS, $request],
333 [$this->anything(), CURLOPT_URL, 'http://localhost'],
334 ],
335 ]
336 );
337 yield 'timeout msec' => array_merge(
338 $default,
339 [
340 'timeout' => 0.1,
341 'connectionTimeout' => 0.1,
342 'curlSetOptCalls' => [
343 [$this->anything(), CURLOPT_RETURNTRANSFER, true],
344 [$this->anything(), CURLOPT_USERAGENT, 'PHP/TCurlClient'],
345 [$this->anything(), CURLOPT_CUSTOMREQUEST, 'POST'],
346 [$this->anything(), CURLOPT_FOLLOWLOCATION, true],
347 [$this->anything(), CURLOPT_MAXREDIRS, 1],
348 [
349 $this->anything(),
350 CURLOPT_HTTPHEADER,
351 [
352 'Accept: application/x-thrift',
353 'Content-Type: application/x-thrift',
354 'Content-Length: ' . strlen($request),
355 ],
356 ],
357 [$this->anything(), CURLOPT_TIMEOUT_MS, 100],
358 [$this->anything(), CURLOPT_CONNECTTIMEOUT_MS, 100],
359 [$this->anything(), CURLOPT_POSTFIELDS, $request],
360 [$this->anything(), CURLOPT_URL, 'http://localhost'],
361 ],
362 ]
363 );
364 yield 'curl_exec return false' => array_merge(
365 $default,
366 [
367 'response' => false,
368 'expectedException' => TTransportException::class,
369 'expectedMessage' => 'TCurlClient: Could not connect to http://localhost',
370 'expectedCode' => TTransportException::UNKNOWN,
371 ]
372 );
373 yield 'curl_exec return response code 403' => array_merge(
374 $default,
375 [
376 'responseError' => 'Access denied',
377 'responseCode' => 403,
378 'expectedException' => TTransportException::class,
379 'expectedMessage' => 'TCurlClient: Could not connect to http://localhost, Access denied, HTTP status code: 403',
380 'expectedCode' => TTransportException::UNKNOWN,
381 ]
382 );
383 }
384
385 public function testCloseCurlHandle()
386 {
387 $this->getFunctionMock('Thrift\\Transport', 'curl_close')
388 ->expects($this->once())
389 ->with('testHandle');
390
391 $transport = new TCurlClient('localhost');
392 $this->setPropertyValue($transport, 'curlHandle', 'testHandle');
393
394 $transport::closeCurlHandle();
395 }
396 }