|
4 | 4 |
|
5 | 5 | use Elasticsearch;
|
6 | 6 | use Elasticsearch\ClientBuilder;
|
| 7 | +use Elasticsearch\Connections\Connection; |
7 | 8 | use Mockery as m;
|
8 | 9 |
|
9 | 10 | /**
|
@@ -266,4 +267,172 @@ public function testMaxRetriesException()
|
266 | 267 | throw $e;
|
267 | 268 | }
|
268 | 269 | }
|
| 270 | + |
| 271 | + public function testInlineHosts() |
| 272 | + { |
| 273 | + $client = Elasticsearch\ClientBuilder::create()->setHosts([ |
| 274 | + 'localhost:9200' |
| 275 | + ])->build(); |
| 276 | + |
| 277 | + // We're casting to Connection here, instead of ConnectionInterface |
| 278 | + // so we can access getHost() on Connection |
| 279 | + |
| 280 | + /** @var Connection $host */ |
| 281 | + $host = $client->transport->getConnection(); |
| 282 | + $this->assertEquals("localhost:9200", $host->getHost()); |
| 283 | + $this->assertEquals("http", $host->getTransportSchema()); |
| 284 | + |
| 285 | + |
| 286 | + $client = Elasticsearch\ClientBuilder::create()->setHosts([ |
| 287 | + 'http://localhost:9200' |
| 288 | + ])->build(); |
| 289 | + /** @var Connection $host */ |
| 290 | + $host = $client->transport->getConnection(); |
| 291 | + $this->assertEquals("localhost:9200", $host->getHost()); |
| 292 | + $this->assertEquals("http", $host->getTransportSchema()); |
| 293 | + |
| 294 | + $client = Elasticsearch\ClientBuilder::create()->setHosts([ |
| 295 | + 'http://foo.com:9200' |
| 296 | + ])->build(); |
| 297 | + /** @var Connection $host */ |
| 298 | + $host = $client->transport->getConnection(); |
| 299 | + $this->assertEquals("foo.com:9200", $host->getHost()); |
| 300 | + $this->assertEquals("http", $host->getTransportSchema()); |
| 301 | + |
| 302 | + $client = Elasticsearch\ClientBuilder::create()->setHosts([ |
| 303 | + 'https://foo.com:9200' |
| 304 | + ])->build(); |
| 305 | + /** @var Connection $host */ |
| 306 | + $host = $client->transport->getConnection(); |
| 307 | + $this->assertEquals("foo.com:9200", $host->getHost()); |
| 308 | + $this->assertEquals("https", $host->getTransportSchema()); |
| 309 | + |
| 310 | + |
| 311 | + // Note: we can't test user/pass themselves yet, need to introduce |
| 312 | + // breaking change to interface in master to do that |
| 313 | + // But we can confirm it doesn't break anything |
| 314 | + $client = Elasticsearch\ClientBuilder::create()->setHosts([ |
| 315 | + 'https://user:pass@foo.com:9200' |
| 316 | + ])->build(); |
| 317 | + /** @var Connection $host */ |
| 318 | + $host = $client->transport->getConnection(); |
| 319 | + $this->assertEquals("foo.com:9200", $host->getHost()); |
| 320 | + $this->assertEquals("https", $host->getTransportSchema()); |
| 321 | + } |
| 322 | + |
| 323 | + public function testExtendedHosts() |
| 324 | + { |
| 325 | + $client = Elasticsearch\ClientBuilder::create()->setHosts([ |
| 326 | + [ |
| 327 | + 'host' => 'localhost', |
| 328 | + 'port' => 9200, |
| 329 | + 'scheme' => 'http' |
| 330 | + ] |
| 331 | + ])->build(); |
| 332 | + /** @var Connection $host */ |
| 333 | + $host = $client->transport->getConnection(); |
| 334 | + $this->assertEquals("localhost:9200", $host->getHost()); |
| 335 | + $this->assertEquals("http", $host->getTransportSchema()); |
| 336 | + |
| 337 | + |
| 338 | + $client = Elasticsearch\ClientBuilder::create()->setHosts([ |
| 339 | + [ |
| 340 | + 'host' => 'foo.com', |
| 341 | + 'port' => 9200, |
| 342 | + 'scheme' => 'http' |
| 343 | + ] |
| 344 | + ])->build(); |
| 345 | + /** @var Connection $host */ |
| 346 | + $host = $client->transport->getConnection(); |
| 347 | + $this->assertEquals("foo.com:9200", $host->getHost()); |
| 348 | + $this->assertEquals("http", $host->getTransportSchema()); |
| 349 | + |
| 350 | + |
| 351 | + $client = Elasticsearch\ClientBuilder::create()->setHosts([ |
| 352 | + [ |
| 353 | + 'host' => 'foo.com', |
| 354 | + 'port' => 9200, |
| 355 | + 'scheme' => 'https' |
| 356 | + ] |
| 357 | + ])->build(); |
| 358 | + /** @var Connection $host */ |
| 359 | + $host = $client->transport->getConnection(); |
| 360 | + $this->assertEquals("foo.com:9200", $host->getHost()); |
| 361 | + $this->assertEquals("https", $host->getTransportSchema()); |
| 362 | + |
| 363 | + |
| 364 | + $client = Elasticsearch\ClientBuilder::create()->setHosts([ |
| 365 | + [ |
| 366 | + 'host' => 'foo.com', |
| 367 | + 'scheme' => 'http' |
| 368 | + ] |
| 369 | + ])->build(); |
| 370 | + /** @var Connection $host */ |
| 371 | + $host = $client->transport->getConnection(); |
| 372 | + $this->assertEquals("foo.com:9200", $host->getHost()); |
| 373 | + $this->assertEquals("http", $host->getTransportSchema()); |
| 374 | + |
| 375 | + |
| 376 | + $client = Elasticsearch\ClientBuilder::create()->setHosts([ |
| 377 | + [ |
| 378 | + 'host' => 'foo.com' |
| 379 | + ] |
| 380 | + ])->build(); |
| 381 | + /** @var Connection $host */ |
| 382 | + $host = $client->transport->getConnection(); |
| 383 | + $this->assertEquals("foo.com:9200", $host->getHost()); |
| 384 | + $this->assertEquals("http", $host->getTransportSchema()); |
| 385 | + |
| 386 | + |
| 387 | + $client = Elasticsearch\ClientBuilder::create()->setHosts([ |
| 388 | + [ |
| 389 | + 'host' => 'foo.com', |
| 390 | + 'port' => 9500, |
| 391 | + 'scheme' => 'https' |
| 392 | + ] |
| 393 | + ])->build(); |
| 394 | + /** @var Connection $host */ |
| 395 | + $host = $client->transport->getConnection(); |
| 396 | + $this->assertEquals("foo.com:9500", $host->getHost()); |
| 397 | + $this->assertEquals("https", $host->getTransportSchema()); |
| 398 | + |
| 399 | + |
| 400 | + try { |
| 401 | + $client = Elasticsearch\ClientBuilder::create()->setHosts([ |
| 402 | + [ |
| 403 | + 'port' => 9200, |
| 404 | + 'scheme' => 'http' |
| 405 | + ] |
| 406 | + ])->build(); |
| 407 | + $this->fail("Expected RuntimeException from missing host, none thrown"); |
| 408 | + } catch (Elasticsearch\Common\Exceptions\RuntimeException $e) { |
| 409 | + // good |
| 410 | + } |
| 411 | + |
| 412 | + // Underscore host, questionably legal, but inline method would break |
| 413 | + $client = Elasticsearch\ClientBuilder::create()->setHosts([ |
| 414 | + [ |
| 415 | + 'host' => 'the_foo.com' |
| 416 | + ] |
| 417 | + ])->build(); |
| 418 | + /** @var Connection $host */ |
| 419 | + $host = $client->transport->getConnection(); |
| 420 | + $this->assertEquals("the_foo.com:9200", $host->getHost()); |
| 421 | + $this->assertEquals("http", $host->getTransportSchema()); |
| 422 | + |
| 423 | + |
| 424 | + // Special characters in user/pass, would break inline |
| 425 | + $client = Elasticsearch\ClientBuilder::create()->setHosts([ |
| 426 | + [ |
| 427 | + 'host' => 'foo.com', |
| 428 | + 'user' => 'user', |
| 429 | + 'pass' => 'abc#$%!abc' |
| 430 | + ] |
| 431 | + ])->build(); |
| 432 | + /** @var Connection $host */ |
| 433 | + $host = $client->transport->getConnection(); |
| 434 | + $this->assertEquals("foo.com:9200", $host->getHost()); |
| 435 | + $this->assertEquals("http", $host->getTransportSchema()); |
| 436 | + |
| 437 | + } |
269 | 438 | }
|
0 commit comments