scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | namespace GuzzleHttp\Tests\Adapter\Curl; |
||
4 | |||
5 | require_once __DIR__ . '/AbstractCurl.php'; |
||
6 | |||
7 | use GuzzleHttp\Adapter\Curl\CurlAdapter; |
||
8 | use GuzzleHttp\Adapter\Transaction; |
||
9 | use GuzzleHttp\Client; |
||
10 | use GuzzleHttp\Event\BeforeEvent; |
||
11 | use GuzzleHttp\Event\ErrorEvent; |
||
12 | use GuzzleHttp\Event\HeadersEvent; |
||
13 | use GuzzleHttp\Exception\ServerException; |
||
14 | use GuzzleHttp\Message\MessageFactory; |
||
15 | use GuzzleHttp\Message\Request; |
||
16 | use GuzzleHttp\Message\Response; |
||
17 | use GuzzleHttp\Tests\Server; |
||
18 | |||
19 | /** |
||
20 | * @covers GuzzleHttp\Adapter\Curl\CurlAdapter |
||
21 | */ |
||
22 | class CurlAdapterTest extends AbstractCurl |
||
23 | { |
||
24 | protected function setUp() |
||
25 | { |
||
26 | if (!function_exists('curl_reset')) { |
||
27 | $this->markTestSkipped('curl_reset() is not available'); |
||
28 | } |
||
29 | } |
||
30 | |||
31 | protected function getAdapter($factory = null, $options = []) |
||
32 | { |
||
33 | return new CurlAdapter($factory ?: new MessageFactory(), $options); |
||
34 | } |
||
35 | |||
36 | public function testCanSetMaxHandles() |
||
37 | { |
||
38 | $a = new CurlAdapter(new MessageFactory(), ['max_handles' => 10]); |
||
39 | $this->assertEquals(10, $this->readAttribute($a, 'maxHandles')); |
||
40 | } |
||
41 | |||
42 | public function testCanInterceptBeforeSending() |
||
43 | { |
||
44 | $client = new Client(); |
||
45 | $request = new Request('GET', 'http://httpbin.org/get'); |
||
46 | $response = new Response(200); |
||
47 | $request->getEmitter()->on( |
||
48 | 'before', |
||
49 | function (BeforeEvent $e) use ($response) { |
||
50 | $e->intercept($response); |
||
51 | } |
||
52 | ); |
||
53 | $transaction = new Transaction($client, $request); |
||
54 | $f = 'does_not_work'; |
||
55 | $a = new CurlAdapter(new MessageFactory(), ['handle_factory' => $f]); |
||
56 | $a->send($transaction); |
||
57 | $this->assertSame($response, $transaction->getResponse()); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @expectedException \GuzzleHttp\Exception\RequestException |
||
62 | * @expectedExceptionMessage cURL error |
||
63 | */ |
||
64 | public function testThrowsCurlErrors() |
||
65 | { |
||
66 | $client = new Client(); |
||
67 | $request = $client->createRequest('GET', 'http://localhost:123', [ |
||
68 | 'connect_timeout' => 0.001, |
||
69 | 'timeout' => 0.001, |
||
70 | ]); |
||
71 | $transaction = new Transaction($client, $request); |
||
72 | $a = new CurlAdapter(new MessageFactory()); |
||
73 | $a->send($transaction); |
||
74 | } |
||
75 | |||
76 | public function testHandlesCurlErrors() |
||
77 | { |
||
78 | $client = new Client(); |
||
79 | $request = $client->createRequest('GET', 'http://localhost:123', [ |
||
80 | 'connect_timeout' => 0.001, |
||
81 | 'timeout' => 0.001, |
||
82 | ]); |
||
83 | $r = new Response(200); |
||
84 | $request->getEmitter()->on('error', function (ErrorEvent $e) use ($r) { |
||
85 | $e->intercept($r); |
||
86 | }); |
||
87 | $transaction = new Transaction($client, $request); |
||
88 | $a = new CurlAdapter(new MessageFactory()); |
||
89 | $a->send($transaction); |
||
90 | $this->assertSame($r, $transaction->getResponse()); |
||
91 | } |
||
92 | |||
93 | public function testReleasesAdditionalEasyHandles() |
||
94 | { |
||
95 | Server::flush(); |
||
96 | Server::enqueue([ |
||
97 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", |
||
98 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", |
||
99 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", |
||
100 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" |
||
101 | ]); |
||
102 | $a = new CurlAdapter(new MessageFactory(), ['max_handles' => 2]); |
||
103 | $client = new Client(['base_url' => Server::$url, 'adapter' => $a]); |
||
104 | $request = $client->createRequest('GET', '/', [ |
||
105 | 'events' => [ |
||
106 | 'headers' => function (HeadersEvent $e) use ($client) { |
||
107 | $client->get('/', [ |
||
108 | 'events' => [ |
||
109 | 'headers' => function (HeadersEvent $e) { |
||
110 | $e->getClient()->get('/'); |
||
111 | } |
||
112 | ] |
||
113 | ]); |
||
114 | } |
||
115 | ] |
||
116 | ]); |
||
117 | $transaction = new Transaction($client, $request); |
||
118 | $a->send($transaction); |
||
119 | $this->assertCount(2, $this->readAttribute($a, 'handles')); |
||
120 | } |
||
121 | |||
122 | public function testDoesNotSaveToWhenFailed() |
||
123 | { |
||
124 | Server::flush(); |
||
125 | Server::enqueue([ |
||
126 | "HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n" |
||
127 | ]); |
||
128 | |||
129 | $tmp = tempnam('/tmp', 'test_save_to'); |
||
130 | unlink($tmp); |
||
131 | $a = new CurlAdapter(new MessageFactory()); |
||
132 | $client = new Client(['base_url' => Server::$url, 'adapter' => $a]); |
||
133 | try { |
||
134 | $client->get('/', ['save_to' => $tmp]); |
||
135 | } catch (ServerException $e) { |
||
136 | $this->assertFileNotExists($tmp); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | public function testRewindsStreamOnComplete() |
||
141 | { |
||
142 | Server::flush(); |
||
143 | Server::enqueue("HTTP/1.1 200 OK\r\nFoo: bar\r\nContent-Length: 4\r\n\r\ntest"); |
||
144 | $t = new Transaction(new Client(), new Request('GET', Server::$url)); |
||
145 | $a = new CurlAdapter(new MessageFactory()); |
||
146 | $response = $a->send($t); |
||
147 | $this->assertEquals('test', $response->getBody()->read(4)); |
||
148 | } |
||
149 | } |