CurlHandlerTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace GuzzleHttp\Tests\Ring\Client;
  3. use GuzzleHttp\Ring\Client\CurlHandler;
  4. class CurlHandlerTest extends \PHPUnit_Framework_TestCase
  5. {
  6. protected function setUp()
  7. {
  8. if (!function_exists('curl_reset')) {
  9. $this->markTestSkipped('curl_reset() is not available');
  10. }
  11. }
  12. protected function getHandler($factory = null, $options = [])
  13. {
  14. return new CurlHandler($options);
  15. }
  16. public function testCanSetMaxHandles()
  17. {
  18. $a = new CurlHandler(['max_handles' => 10]);
  19. $this->assertEquals(10, $this->readAttribute($a, 'maxHandles'));
  20. }
  21. public function testCreatesCurlErrors()
  22. {
  23. $handler = new CurlHandler();
  24. $response = $handler([
  25. 'http_method' => 'GET',
  26. 'uri' => '/',
  27. 'headers' => ['host' => ['localhost:123']],
  28. 'client' => ['timeout' => 0.001, 'connect_timeout' => 0.001],
  29. ]);
  30. $this->assertNull($response['status']);
  31. $this->assertNull($response['reason']);
  32. $this->assertEquals([], $response['headers']);
  33. $this->assertInstanceOf(
  34. 'GuzzleHttp\Ring\Exception\RingException',
  35. $response['error']
  36. );
  37. $this->assertEquals(
  38. 1,
  39. preg_match('/^cURL error \d+: .*$/', $response['error']->getMessage())
  40. );
  41. }
  42. public function testReleasesAdditionalEasyHandles()
  43. {
  44. Server::flush();
  45. $response = [
  46. 'status' => 200,
  47. 'headers' => ['Content-Length' => [4]],
  48. 'body' => 'test',
  49. ];
  50. Server::enqueue([$response, $response, $response, $response]);
  51. $a = new CurlHandler(['max_handles' => 2]);
  52. $fn = function () use (&$calls, $a, &$fn) {
  53. if (++$calls < 4) {
  54. $a([
  55. 'http_method' => 'GET',
  56. 'headers' => ['host' => [Server::$host]],
  57. 'client' => ['progress' => $fn],
  58. ]);
  59. }
  60. };
  61. $request = [
  62. 'http_method' => 'GET',
  63. 'headers' => ['host' => [Server::$host]],
  64. 'client' => [
  65. 'progress' => $fn,
  66. ],
  67. ];
  68. $a($request);
  69. $this->assertCount(2, $this->readAttribute($a, 'handles'));
  70. }
  71. public function testReusesHandles()
  72. {
  73. Server::flush();
  74. $response = ['status' => 200];
  75. Server::enqueue([$response, $response]);
  76. $a = new CurlHandler();
  77. $request = [
  78. 'http_method' => 'GET',
  79. 'headers' => ['host' => [Server::$host]],
  80. ];
  81. $a($request);
  82. $a($request);
  83. }
  84. }