| 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\THttpClient; |
| 29 |
|
| 30 |
class THttpClientTest extends TestCase |
| 31 |
{ |
| 32 |
use PHPMock; |
| 33 |
use ReflectionHelper; |
| 34 |
|
| 35 |
public function testSetTimeoutSecs() |
| 36 |
{ |
| 37 |
$host = 'localhost'; |
| 38 |
$transport = new THttpClient($host); |
| 39 |
$transport->setTimeoutSecs(1000); |
| 40 |
|
| 41 |
$this->assertEquals(1000, $this->getPropertyValue($transport, 'timeout_')); |
| 42 |
} |
| 43 |
|
| 44 |
public function testIsOpen() |
| 45 |
{ |
| 46 |
$host = 'localhost'; |
| 47 |
$transport = new THttpClient($host); |
| 48 |
$this->assertTrue($transport->isOpen()); |
| 49 |
} |
| 50 |
|
| 51 |
public function testOpen() |
| 52 |
{ |
| 53 |
$host = 'localhost'; |
| 54 |
$transport = new THttpClient($host); |
| 55 |
$this->assertNull($transport->open()); |
| 56 |
} |
| 57 |
|
| 58 |
public function testClose() |
| 59 |
{ |
| 60 |
$handle = fopen('php://temp', 'r+'); |
| 61 |
$this->getFunctionMock('Thrift\\Transport', 'fclose') |
| 62 |
->expects($this->once()) |
| 63 |
->with($handle) |
| 64 |
->willReturn(true); |
| 65 |
|
| 66 |
$host = 'localhost'; |
| 67 |
$transport = new THttpClient($host); |
| 68 |
|
| 69 |
$this->setPropertyValue($transport, 'handle_', $handle); |
| 70 |
|
| 71 |
$this->assertNull($transport->close()); |
| 72 |
$this->assertNull($this->getPropertyValue($transport, 'handle_')); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @dataProvider readDataProvider |
| 77 |
*/ |
| 78 |
public function testRead( |
| 79 |
$readLen, |
| 80 |
$freadResult, |
| 81 |
$streamGetMetaDataResult, |
| 82 |
$expectedResult, |
| 83 |
$expectedException, |
| 84 |
$expectedExceptionMessage, |
| 85 |
$expectedExceptionCode |
| 86 |
) { |
| 87 |
$handle = fopen('php://temp', 'r+'); |
| 88 |
$this->getFunctionMock('Thrift\\Transport', 'fread') |
| 89 |
->expects($this->once()) |
| 90 |
->with($handle, $readLen) |
| 91 |
->willReturn($freadResult); |
| 92 |
|
| 93 |
$this->getFunctionMock('Thrift\\Transport', 'stream_get_meta_data') |
| 94 |
->expects(!empty($streamGetMetaDataResult) ? $this->once() : $this->never()) |
| 95 |
->with($handle) |
| 96 |
->willReturn($streamGetMetaDataResult); |
| 97 |
|
| 98 |
if ($expectedException) { |
| 99 |
$this->expectException($expectedException); |
| 100 |
$this->expectExceptionMessage($expectedExceptionMessage); |
| 101 |
$this->expectExceptionCode($expectedExceptionCode); |
| 102 |
} |
| 103 |
|
| 104 |
$host = 'localhost'; |
| 105 |
$transport = new THttpClient($host); |
| 106 |
|
| 107 |
$this->setPropertyValue($transport, 'handle_', $handle); |
| 108 |
|
| 109 |
$this->assertEquals($expectedResult, $transport->read($readLen)); |
| 110 |
} |
| 111 |
|
| 112 |
public function readDataProvider() |
| 113 |
{ |
| 114 |
yield 'read success' => [ |
| 115 |
'readLen' => 10, |
| 116 |
'freadResult' => '1234567890', |
| 117 |
'streamGetMetaDataResult' => [], |
| 118 |
'expectedResult' => '1234567890', |
| 119 |
'expectedException' => null, |
| 120 |
'expectedExceptionMessage' => null, |
| 121 |
'expectedExceptionCode' => null, |
| 122 |
]; |
| 123 |
yield 'read failed' => [ |
| 124 |
'readLen' => 10, |
| 125 |
'freadResult' => false, |
| 126 |
'streamGetMetaDataResult' => [ |
| 127 |
'timed_out' => false, |
| 128 |
], |
| 129 |
'expectedResult' => '', |
| 130 |
'expectedException' => TTransportException::class, |
| 131 |
'expectedExceptionMessage' => 'THttpClient: Could not read 10 bytes from localhost:80', |
| 132 |
'expectedExceptionCode' => TTransportException::UNKNOWN, |
| 133 |
]; |
| 134 |
yield 'read timeout' => [ |
| 135 |
'readLen' => 10, |
| 136 |
'freadResult' => '', |
| 137 |
'streamGetMetaDataResult' => [ |
| 138 |
'timed_out' => true, |
| 139 |
], |
| 140 |
'expectedResult' => '', |
| 141 |
'expectedException' => TTransportException::class, |
| 142 |
'expectedExceptionMessage' => 'THttpClient: timed out reading 10 bytes from localhost:80', |
| 143 |
'expectedExceptionCode' => TTransportException::TIMED_OUT, |
| 144 |
]; |
| 145 |
} |
| 146 |
|
| 147 |
public function testWrite() |
| 148 |
{ |
| 149 |
$host = 'localhost'; |
| 150 |
$transport = new THttpClient($host); |
| 151 |
|
| 152 |
$transport->write('1234567890'); |
| 153 |
|
| 154 |
$this->assertEquals('1234567890', $this->getPropertyValue($transport, 'buf_')); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* @dataProvider flushDataProvider |
| 159 |
*/ |
| 160 |
public function testFlush( |
| 161 |
$host, |
| 162 |
$port, |
| 163 |
$uri, |
| 164 |
$scheme, |
| 165 |
$context, |
| 166 |
$headers, |
| 167 |
$timeout, |
| 168 |
$streamContextOptions, |
| 169 |
$streamContext, |
| 170 |
$fopenResult, |
| 171 |
$expectedHost, |
| 172 |
$expectedUri, |
| 173 |
$expectedException, |
| 174 |
$expectedExceptionMessage, |
| 175 |
$expectedExceptionCode |
| 176 |
) { |
| 177 |
$this->getFunctionMock('Thrift\\Transport', 'stream_context_create') |
| 178 |
->expects($this->once()) |
| 179 |
->with($streamContextOptions) |
| 180 |
->willReturn($streamContext); |
| 181 |
|
| 182 |
$this->getFunctionMock('Thrift\\Transport', 'fopen') |
| 183 |
->expects($this->once()) |
| 184 |
->with( |
| 185 |
$scheme . '://' . $expectedHost . $expectedUri, |
| 186 |
'r', |
| 187 |
false, |
| 188 |
$streamContext |
| 189 |
)->willReturn($fopenResult); |
| 190 |
|
| 191 |
if ($expectedException) { |
| 192 |
$this->expectException($expectedException); |
| 193 |
$this->expectExceptionMessage($expectedExceptionMessage); |
| 194 |
$this->expectExceptionCode($expectedExceptionCode); |
| 195 |
} |
| 196 |
|
| 197 |
$transport = new THttpClient($host, $port, $uri, $scheme, $context); |
| 198 |
if (!empty($headers)) { |
| 199 |
$transport->addHeaders($headers); |
| 200 |
} |
| 201 |
if (!empty($timeout)) { |
| 202 |
$transport->setTimeoutSecs($timeout); |
| 203 |
} |
| 204 |
|
| 205 |
$this->assertNull($transport->flush()); |
| 206 |
} |
| 207 |
|
| 208 |
public function flushDataProvider() |
| 209 |
{ |
| 210 |
$default = [ |
| 211 |
'host' => 'localhost', |
| 212 |
'port' => '80', |
| 213 |
'uri' => '', |
| 214 |
'scheme' => 'http', |
| 215 |
'context' => [], |
| 216 |
'headers' => [], |
| 217 |
'timeout' => null, |
| 218 |
'streamContextOptions' => [ |
| 219 |
'http' => [ |
| 220 |
'method' => 'POST', |
| 221 |
'header' => "Host: localhost\r\n" . |
| 222 |
"Accept: application/x-thrift\r\n" . |
| 223 |
"User-Agent: PHP/THttpClient\r\n" . |
| 224 |
"Content-Type: application/x-thrift\r\n" . |
| 225 |
"Content-Length: 0", |
| 226 |
'content' => '', |
| 227 |
'max_redirects' => 1, |
| 228 |
], |
| 229 |
], |
| 230 |
'streamContext' => fopen('php://temp', 'r+'), |
| 231 |
'fopenResult' => fopen('php://memory', 'r+'), |
| 232 |
'expectedHost' => 'localhost', |
| 233 |
'expectedUri' => '', |
| 234 |
'expectedException' => '', |
| 235 |
'expectedExceptionMessage' => '', |
| 236 |
'expectedExceptionCode' => '', |
| 237 |
]; |
| 238 |
|
| 239 |
yield 'success' => $default; |
| 240 |
yield 'additionalHeaders' => array_merge( |
| 241 |
$default, |
| 242 |
[ |
| 243 |
'headers' => [ |
| 244 |
'X-Test-Header' => 'test', |
| 245 |
], |
| 246 |
'streamContextOptions' => [ |
| 247 |
'http' => [ |
| 248 |
'method' => 'POST', |
| 249 |
'header' => "Host: localhost\r\n" . |
| 250 |
"Accept: application/x-thrift\r\n" . |
| 251 |
"User-Agent: PHP/THttpClient\r\n" . |
| 252 |
"Content-Type: application/x-thrift\r\n" . |
| 253 |
"Content-Length: 0\r\n" . |
| 254 |
"X-Test-Header: test", |
| 255 |
'content' => '', |
| 256 |
'max_redirects' => 1, |
| 257 |
], |
| 258 |
], |
| 259 |
] |
| 260 |
); |
| 261 |
yield 'timeout' => array_merge( |
| 262 |
$default, |
| 263 |
[ |
| 264 |
'timeout' => 1000, |
| 265 |
'streamContextOptions' => [ |
| 266 |
'http' => [ |
| 267 |
'method' => 'POST', |
| 268 |
'header' => "Host: localhost\r\n" . |
| 269 |
"Accept: application/x-thrift\r\n" . |
| 270 |
"User-Agent: PHP/THttpClient\r\n" . |
| 271 |
"Content-Type: application/x-thrift\r\n" . |
| 272 |
"Content-Length: 0", |
| 273 |
'content' => '', |
| 274 |
'max_redirects' => 1, |
| 275 |
'timeout' => 1000, |
| 276 |
], |
| 277 |
], |
| 278 |
] |
| 279 |
); |
| 280 |
yield 'fopenFailed' => array_merge( |
| 281 |
$default, |
| 282 |
[ |
| 283 |
'host' => 'localhost', |
| 284 |
'port' => 8080, |
| 285 |
'uri' => 'test', |
| 286 |
'expectedHost' => 'localhost:8080', |
| 287 |
'expectedUri' => '/test', |
| 288 |
'streamContextOptions' => [ |
| 289 |
'http' => [ |
| 290 |
'method' => 'POST', |
| 291 |
'header' => "Host: localhost:8080\r\n" . |
| 292 |
"Accept: application/x-thrift\r\n" . |
| 293 |
"User-Agent: PHP/THttpClient\r\n" . |
| 294 |
"Content-Type: application/x-thrift\r\n" . |
| 295 |
"Content-Length: 0", |
| 296 |
'content' => '', |
| 297 |
'max_redirects' => 1, |
| 298 |
], |
| 299 |
], |
| 300 |
'fopenResult' => false, |
| 301 |
'expectedException' => TTransportException::class, |
| 302 |
'expectedExceptionMessage' => 'THttpClient: Could not connect to localhost:8080/test', |
| 303 |
'expectedExceptionCode' => TTransportException::NOT_OPEN, |
| 304 |
] |
| 305 |
); |
| 306 |
} |
| 307 |
|
| 308 |
public function testAddHeaders() |
| 309 |
{ |
| 310 |
$host = 'localhost'; |
| 311 |
$transport = new THttpClient($host); |
| 312 |
|
| 313 |
$this->setPropertyValue($transport, 'headers_', ['test' => '1234567890']); |
| 314 |
|
| 315 |
$transport->addHeaders(['test2' => '12345']); |
| 316 |
$this->assertEquals(['test' => '1234567890', 'test2' => '12345'], $this->getPropertyValue($transport, 'headers_')); |
| 317 |
} |
| 318 |
} |