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