Open Journal Systems  3.3.0
CurlHandleTest.php
1 <?php
2 
4 
13 
19 {
21 
22  protected function updateForHandle(RequestInterface $request)
23  {
24  $that = $this;
25  $request->getEventDispatcher()->addListener('request.sent', function (Event $e) use ($that) {
26  $that->requestHandle = $e['handle'];
27  });
28 
29  return $request;
30  }
31 
32  public function setUp()
33  {
34  $this->requestHandle = null;
35  }
36 
41  {
42  $h = new CurlHandle(false, array());
43  }
44 
46  {
47  $h = curl_init($this->getServer()->getUrl());
48  try {
49  $ha = new CurlHandle($h, false);
50  $this->fail('Expected InvalidArgumentException');
51  } catch (\InvalidArgumentException $e) {
52  }
53 
54  $ha = new CurlHandle($h, array(
55  CURLOPT_URL => $this->getServer()->getUrl()
56  ));
57  $this->assertEquals($this->getServer()->getUrl(), $ha->getOptions()->get(CURLOPT_URL));
58 
59  $ha = new CurlHandle($h, new Collection(array(
60  CURLOPT_URL => $this->getServer()->getUrl()
61  )));
62  $this->assertEquals($this->getServer()->getUrl(), $ha->getOptions()->get(CURLOPT_URL));
63  }
64 
66  {
67  $handle = curl_init($this->getServer()->getUrl());
68  $h = new CurlHandle($handle, array(
69  CURLOPT_URL => $this->getServer()->getUrl()
70  ));
71  $this->assertSame($handle, $h->getHandle());
72  $this->assertInstanceOf('Guzzle\\Http\\Url', $h->getUrl());
73  $this->assertEquals($this->getServer()->getUrl(), (string) $h->getUrl());
74  $this->assertEquals($this->getServer()->getUrl(), $h->getOptions()->get(CURLOPT_URL));
75  }
76 
77  public function testStoresStdErr()
78  {
79  $request = RequestFactory::getInstance()->create('GET', 'http://test.com');
80  $request->getCurlOptions()->set('debug', true);
81  $h = CurlHandle::factory($request);
82  $this->assertEquals($h->getStderr(true), $h->getOptions()->get(CURLOPT_STDERR));
83  $this->assertInternalType('resource', $h->getStderr(true));
84  $this->assertInternalType('string', $h->getStderr(false));
85  $r = $h->getStderr(true);
86  fwrite($r, 'test');
87  $this->assertEquals('test', $h->getStderr(false));
88  }
89 
90  public function testStoresCurlErrorNumber()
91  {
92  $h = new CurlHandle(curl_init('http://test.com'), array(CURLOPT_URL => 'http://test.com'));
93  $this->assertEquals(CURLE_OK, $h->getErrorNo());
94  $h->setErrorNo(CURLE_OPERATION_TIMEOUTED);
95  $this->assertEquals(CURLE_OPERATION_TIMEOUTED, $h->getErrorNo());
96  }
97 
98  public function testAccountsForMissingStdErr()
99  {
100  $handle = curl_init('http://www.test.com/');
101  $h = new CurlHandle($handle, array(
102  CURLOPT_URL => 'http://www.test.com/'
103  ));
104  $this->assertNull($h->getStderr(false));
105  }
106 
108  {
109  $handle = curl_init($this->getServer()->getUrl());
110  $h = new CurlHandle($handle, array());
111  $this->assertTrue($h->isAvailable());
112 
113  // Mess it up by closing the handle
114  curl_close($handle);
115  $this->assertFalse($h->isAvailable());
116 
117  // Mess it up by unsetting the handle
118  $handle = null;
119  $this->assertFalse($h->isAvailable());
120  }
121 
122  public function testWrapsErrorsAndInfo()
123  {
124  if (!defined('CURLOPT_TIMEOUT_MS')) {
125  $this->markTestSkipped('Update curl');
126  }
127 
128  $settings = array(
129  CURLOPT_PORT => 123,
130  CURLOPT_CONNECTTIMEOUT_MS => 1,
131  CURLOPT_TIMEOUT_MS => 1
132  );
133 
134  $handle = curl_init($this->getServer()->getUrl());
135  curl_setopt_array($handle, $settings);
136  $h = new CurlHandle($handle, $settings);
137  @curl_exec($handle);
138 
139  $errors = array(
140  "couldn't connect to host",
141  'timeout was reached',
142  'connection time-out',
143  'connect() timed out!',
144  'failed connect to 127.0.0.1:123; connection refused',
145  'failed to connect to 127.0.0.1 port 123: connection refused'
146  );
147  $this->assertTrue(in_array(strtolower($h->getError()), $errors), $h->getError() . ' was not the error');
148 
149  $this->assertTrue($h->getErrorNo() > 0);
150 
151  $this->assertEquals($this->getServer()->getUrl(), $h->getInfo(CURLINFO_EFFECTIVE_URL));
152  $this->assertInternalType('array', $h->getInfo());
153 
154  curl_close($handle);
155  $this->assertEquals(null, $h->getInfo('url'));
156  }
157 
158  public function testGetInfoWithoutDebugMode()
159  {
160  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
161  $client = new Client($this->getServer()->getUrl());
162  $request = $client->get($this->getServer()->getUrl());
163  $response = $request->send();
164 
165  $info = $response->getInfo();
166  $this->assertFalse(empty($info));
167  $this->assertEquals($this->getServer()->getUrl(), $info['url']);
168  }
169 
170  public function testWrapsCurlOptions()
171  {
172  $handle = curl_init($this->getServer()->getUrl());
173  $h = new CurlHandle($handle, array(
174  CURLOPT_AUTOREFERER => true,
175  CURLOPT_BUFFERSIZE => 1024
176  ));
177 
178  $this->assertEquals(true, $h->getOptions()->get(CURLOPT_AUTOREFERER));
179  $this->assertEquals(1024, $h->getOptions()->get(CURLOPT_BUFFERSIZE));
180  }
181 
187  public function dataProvider()
188  {
189  $testFile = __DIR__ . '/../../../../../phpunit.xml.dist';
190 
191  $postBody = new QueryString(array('file' => '@' . $testFile));
192  $qs = new QueryString(array(
193  'x' => 'y',
194  'z' => 'a'
195  ));
196 
197  $client = new Client();
198  $userAgent = $client->getDefaultUserAgent();
199  $auth = base64_encode('michael:123');
200  $testFileSize = filesize($testFile);
201 
202  $tests = array(
203  // Send a regular GET
204  array('GET', 'http://www.google.com/', null, null, array(
205  CURLOPT_RETURNTRANSFER => 0,
206  CURLOPT_HEADER => 0,
207  CURLOPT_CONNECTTIMEOUT => 150,
208  CURLOPT_WRITEFUNCTION => 'callback',
209  CURLOPT_HEADERFUNCTION => 'callback',
210  CURLOPT_HTTPHEADER => array('Accept:', 'Host: www.google.com', 'User-Agent: ' . $userAgent),
211  )),
212  // Test that custom request methods can be used
213  array('TRACE', 'http://www.google.com/', null, null, array(
214  CURLOPT_CUSTOMREQUEST => 'TRACE'
215  )),
216  // Send a GET using a port
217  array('GET', 'http://127.0.0.1:8080', null, null, array(
218  CURLOPT_RETURNTRANSFER => 0,
219  CURLOPT_HEADER => 0,
220  CURLOPT_CONNECTTIMEOUT => 150,
221  CURLOPT_WRITEFUNCTION => 'callback',
222  CURLOPT_HEADERFUNCTION => 'callback',
223  CURLOPT_PORT => 8080,
224  CURLOPT_HTTPHEADER => array('Accept:', 'Host: 127.0.0.1:8080', 'User-Agent: ' . $userAgent),
225  )),
226  // Send a HEAD request
227  array('HEAD', 'http://www.google.com/', null, null, array(
228  CURLOPT_RETURNTRANSFER => 0,
229  CURLOPT_HEADER => 0,
230  CURLOPT_CONNECTTIMEOUT => 150,
231  CURLOPT_HEADERFUNCTION => 'callback',
232  CURLOPT_HTTPHEADER => array('Accept:', 'Host: www.google.com', 'User-Agent: ' . $userAgent),
233  CURLOPT_NOBODY => 1
234  )),
235  // Send a GET using basic auth
236  array('GET', 'https://michael:123@127.0.0.1/index.html?q=2', null, null, array(
237  CURLOPT_RETURNTRANSFER => 0,
238  CURLOPT_HEADER => 0,
239  CURLOPT_CONNECTTIMEOUT => 150,
240  CURLOPT_WRITEFUNCTION => 'callback',
241  CURLOPT_HEADERFUNCTION => 'callback',
242  CURLOPT_HTTPHEADER => array(
243  'Accept:',
244  'Host: 127.0.0.1',
245  'Authorization: Basic ' . $auth,
246  'User-Agent: ' . $userAgent
247  ),
248  CURLOPT_PORT => 443
249  )),
250  // Send a GET request with custom headers
251  array('GET', 'http://127.0.0.1:8124/', array(
252  'x-test-data' => 'Guzzle'
253  ), null, array(
254  CURLOPT_PORT => 8124,
255  CURLOPT_HTTPHEADER => array(
256  'Accept:',
257  'Host: 127.0.0.1:8124',
258  'x-test-data: Guzzle',
259  'User-Agent: ' . $userAgent
260  )
261  ), array(
262  'Host' => '*',
263  'User-Agent' => '*',
264  'x-test-data' => 'Guzzle'
265  )),
266  // Send a POST using a query string
267  array('POST', 'http://127.0.0.1:8124/post.php', null, $qs, array(
268  CURLOPT_RETURNTRANSFER => 0,
269  CURLOPT_HEADER => 0,
270  CURLOPT_CONNECTTIMEOUT => 150,
271  CURLOPT_WRITEFUNCTION => 'callback',
272  CURLOPT_HEADERFUNCTION => 'callback',
273  CURLOPT_POSTFIELDS => 'x=y&z=a',
274  CURLOPT_HTTPHEADER => array (
275  'Expect:',
276  'Accept:',
277  'Host: 127.0.0.1:8124',
278  'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
279  'User-Agent: ' . $userAgent
280  )
281  ), array(
282  'Host' => '*',
283  'User-Agent' => '*',
284  'Content-Length' => '7',
285  '!Expect' => null,
286  'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
287  '!Transfer-Encoding' => null
288  )),
289  // Send a PUT using raw data
290  array('PUT', 'http://127.0.0.1:8124/put.php', null, EntityBody::factory(fopen($testFile, 'r+')), array(
291  CURLOPT_RETURNTRANSFER => 0,
292  CURLOPT_HEADER => 0,
293  CURLOPT_CONNECTTIMEOUT => 150,
294  CURLOPT_WRITEFUNCTION => 'callback',
295  CURLOPT_HEADERFUNCTION => 'callback',
296  CURLOPT_READFUNCTION => 'callback',
297  CURLOPT_INFILESIZE => filesize($testFile),
298  CURLOPT_HTTPHEADER => array (
299  'Expect:',
300  'Accept:',
301  'Host: 127.0.0.1:8124',
302  'User-Agent: ' . $userAgent
303  )
304  ), array(
305  'Host' => '*',
306  'User-Agent' => '*',
307  '!Expect' => null,
308  'Content-Length' => $testFileSize,
309  '!Transfer-Encoding' => null
310  )),
311  // Send a POST request using an array of fields
312  array('POST', 'http://127.0.0.1:8124/post.php', null, array(
313  'x' => 'y',
314  'a' => 'b'
315  ), array(
316  CURLOPT_RETURNTRANSFER => 0,
317  CURLOPT_HEADER => 0,
318  CURLOPT_CONNECTTIMEOUT => 150,
319  CURLOPT_WRITEFUNCTION => 'callback',
320  CURLOPT_HEADERFUNCTION => 'callback',
321  CURLOPT_POST => 1,
322  CURLOPT_POSTFIELDS => 'x=y&a=b',
323  CURLOPT_HTTPHEADER => array (
324  'Expect:',
325  'Accept:',
326  'Host: 127.0.0.1:8124',
327  'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
328  'User-Agent: ' . $userAgent
329  )
330  ), array(
331  'Host' => '*',
332  'User-Agent' => '*',
333  'Content-Length' => '7',
334  '!Expect' => null,
335  'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
336  '!Transfer-Encoding' => null
337  )),
338  // Send a POST request with raw POST data and a custom content-type
339  array('POST', 'http://127.0.0.1:8124/post.php', array(
340  'Content-Type' => 'application/json'
341  ), '{"hi":"there"}', array(
342  CURLOPT_RETURNTRANSFER => 0,
343  CURLOPT_HEADER => 0,
344  CURLOPT_CONNECTTIMEOUT => 150,
345  CURLOPT_WRITEFUNCTION => 'callback',
346  CURLOPT_HEADERFUNCTION => 'callback',
347  CURLOPT_CUSTOMREQUEST => 'POST',
348  CURLOPT_UPLOAD => true,
349  CURLOPT_INFILESIZE => 14,
350  CURLOPT_HTTPHEADER => array (
351  'Expect:',
352  'Accept:',
353  'Host: 127.0.0.1:8124',
354  'Content-Type: application/json',
355  'User-Agent: ' . $userAgent
356  ),
357  ), array(
358  'Host' => '*',
359  'User-Agent' => '*',
360  'Content-Type' => 'application/json',
361  '!Expect' => null,
362  'Content-Length' => '14',
363  '!Transfer-Encoding' => null
364  )),
365  // Send a POST request with raw POST data, a custom content-type, and use chunked encoding
366  array('POST', 'http://127.0.0.1:8124/post.php', array(
367  'Content-Type' => 'application/json',
368  'Transfer-Encoding' => 'chunked'
369  ), '{"hi":"there"}', array(
370  CURLOPT_RETURNTRANSFER => 0,
371  CURLOPT_HEADER => 0,
372  CURLOPT_CONNECTTIMEOUT => 150,
373  CURLOPT_WRITEFUNCTION => 'callback',
374  CURLOPT_HEADERFUNCTION => 'callback',
375  CURLOPT_CUSTOMREQUEST => 'POST',
376  CURLOPT_UPLOAD => true,
377  CURLOPT_HTTPHEADER => array (
378  'Expect:',
379  'Accept:',
380  'Host: 127.0.0.1:8124',
381  'Transfer-Encoding: chunked',
382  'Content-Type: application/json',
383  'User-Agent: ' . $userAgent
384  ),
385  ), array(
386  'Host' => '*',
387  'User-Agent' => '*',
388  'Content-Type' => 'application/json',
389  '!Expect' => null,
390  'Transfer-Encoding' => 'chunked',
391  '!Content-Length' => ''
392  )),
393  // Send a POST request with no body
394  array('POST', 'http://127.0.0.1:8124/post.php', null, '', array(
395  CURLOPT_CUSTOMREQUEST => 'POST',
396  CURLOPT_HTTPHEADER => array (
397  'Expect:',
398  'Accept:',
399  'Host: 127.0.0.1:8124',
400  'User-Agent: ' . $userAgent
401  )
402  ), array(
403  'Host' => '*',
404  'User-Agent' => '*',
405  'Content-Length' => '0',
406  '!Transfer-Encoding' => null
407  )),
408  // Send a POST request with empty post fields
409  array('POST', 'http://127.0.0.1:8124/post.php', null, array(), array(
410  CURLOPT_CUSTOMREQUEST => 'POST',
411  CURLOPT_HTTPHEADER => array (
412  'Expect:',
413  'Accept:',
414  'Host: 127.0.0.1:8124',
415  'User-Agent: ' . $userAgent
416  )
417  ), array(
418  'Host' => '*',
419  'User-Agent' => '*',
420  'Content-Length' => '0',
421  '!Transfer-Encoding' => null
422  )),
423  // Send a PATCH request
424  array('PATCH', 'http://127.0.0.1:8124/patch.php', null, 'body', array(
425  CURLOPT_INFILESIZE => 4,
426  CURLOPT_HTTPHEADER => array (
427  'Expect:',
428  'Accept:',
429  'Host: 127.0.0.1:8124',
430  'User-Agent: ' . $userAgent
431  )
432  )),
433  // Send a DELETE request with a body
434  array('DELETE', 'http://127.0.0.1:8124/delete.php', null, 'body', array(
435  CURLOPT_CUSTOMREQUEST => 'DELETE',
436  CURLOPT_INFILESIZE => 4,
437  CURLOPT_HTTPHEADER => array (
438  'Expect:',
439  'Accept:',
440  'Host: 127.0.0.1:8124',
441  'User-Agent: ' . $userAgent
442  )
443  ), array(
444  'Host' => '*',
445  'User-Agent' => '*',
446  'Content-Length' => '4',
447  '!Expect' => null,
448  '!Transfer-Encoding' => null
449  )),
450 
458  array('GET', 'http://www.google.com#head', null, null, array(
459  CURLOPT_RETURNTRANSFER => 0,
460  CURLOPT_HEADER => 0,
461  CURLOPT_CONNECTTIMEOUT => 150,
462  CURLOPT_WRITEFUNCTION => 'callback',
463  CURLOPT_HEADERFUNCTION => 'callback',
464  CURLOPT_HTTPHEADER => array('Accept:', 'Host: www.google.com', 'User-Agent: ' . $userAgent),
465  )),
466  );
467 
468  $postTest = array('POST', 'http://127.0.0.1:8124/post.php', null, $postBody, array(
469  CURLOPT_RETURNTRANSFER => 0,
470  CURLOPT_HEADER => 0,
471  CURLOPT_CONNECTTIMEOUT => 150,
472  CURLOPT_WRITEFUNCTION => 'callback',
473  CURLOPT_HEADERFUNCTION => 'callback',
474  CURLOPT_POST => 1,
475  CURLOPT_POSTFIELDS => array(
476  'file' => '@' . $testFile . ';filename=phpunit.xml.dist;type=application/octet-stream'
477  ),
478  CURLOPT_HTTPHEADER => array (
479  'Accept:',
480  'Host: 127.0.0.1:8124',
481  'Content-Type: multipart/form-data',
482  'Expect: 100-Continue',
483  'User-Agent: ' . $userAgent
484  )
485  ), array(
486  'Host' => '*',
487  'User-Agent' => '*',
488  'Content-Length' => '*',
489  'Expect' => '100-Continue',
490  'Content-Type' => 'multipart/form-data; boundary=*',
491  '!Transfer-Encoding' => null
492  ));
493 
494  if (version_compare(phpversion(), '5.5.0', '>=')) {
495  $postTest[4][CURLOPT_POSTFIELDS] = array(
496  'file' => new \CurlFile($testFile, 'application/octet-stream', 'phpunit.xml.dist')
497  );
498  }
499 
500  $tests[] = $postTest;
501 
502  return $tests;
503  }
504 
508  public function testFactoryCreatesCurlBasedOnRequest($method, $url, $headers, $body, $options, $expectedHeaders = null)
509  {
510  $client = new Client();
511  $request = $client->createRequest($method, $url, $headers, $body);
512  $request->getCurlOptions()->set('debug', true);
513 
514  $originalRequest = clone $request;
515  $curlTest = clone $request;
516  $handle = CurlHandle::factory($curlTest);
517 
518  $this->assertInstanceOf('Guzzle\\Http\\Curl\\CurlHandle', $handle);
519  $o = $handle->getOptions()->getAll();
520 
521  // Headers are case-insensitive
522  if (isset($o[CURLOPT_HTTPHEADER])) {
523  $o[CURLOPT_HTTPHEADER] = array_map('strtolower', $o[CURLOPT_HTTPHEADER]);
524  }
525  if (isset($options[CURLOPT_HTTPHEADER])) {
526  $options[CURLOPT_HTTPHEADER] = array_map('strtolower', $options[CURLOPT_HTTPHEADER]);
527  }
528 
529  $check = 0;
530  foreach ($options as $key => $value) {
531  $check++;
532  $this->assertArrayHasKey($key, $o, '-> Check number ' . $check);
533  if ($key != CURLOPT_HTTPHEADER && $key != CURLOPT_POSTFIELDS && (is_array($o[$key])) || $o[$key] instanceof \Closure) {
534  $this->assertEquals('callback', $value, '-> Check number ' . $check);
535  } else {
536  $this->assertTrue($value == $o[$key], '-> Check number ' . $check . ' - ' . var_export($value, true) . ' != ' . var_export($o[$key], true));
537  }
538  }
539 
540  // If we are testing the actual sent headers
541  if ($expectedHeaders) {
542 
543  // Send the request to the test server
544  $client = new Client($this->getServer()->getUrl());
545  $request->setClient($client);
546  $this->getServer()->flush();
547  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
548  $request->send();
549 
550  // Get the request that was sent and create a request that we expected
551  $requests = $this->getServer()->getReceivedRequests(true);
552  $this->assertEquals($method, $requests[0]->getMethod());
553 
554  $test = $this->compareHeaders($expectedHeaders, $requests[0]->getHeaders());
555  $this->assertFalse($test, $test . "\nSent: \n" . $request . "\n\n" . $requests[0]);
556 
557  // Ensure only one Content-Length header is sent
558  if ($request->getHeader('Content-Length')) {
559  $this->assertEquals((string) $request->getHeader('Content-Length'), (string) $requests[0]->getHeader('Content-Length'));
560  }
561  }
562  }
563 
565  {
566  $request = RequestFactory::getInstance()->create('GET', 'http://127.0.0.1:8124/');
567  $request->setProtocolVersion('1.1');
568  $handle = CurlHandle::factory($request);
569  $options = $handle->getOptions();
570  $this->assertEquals(CURL_HTTP_VERSION_1_1, $options[CURLOPT_HTTP_VERSION]);
571  }
572 
573  public function testUploadsPutData()
574  {
575  $this->getServer()->flush();
576  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi");
577 
578  $client = new Client($this->getServer()->getUrl());
579  $request = $client->put('/');
580  $request->getCurlOptions()->set('debug', true);
581  $request->setBody(EntityBody::factory('test'), 'text/plain', false);
582  $request->getCurlOptions()->set('progress', true);
583 
584  $o = $this->getWildcardObserver($request);
585  $request->send();
586 
587  // Make sure that the events were dispatched
588  $this->assertTrue($o->has('curl.callback.progress'));
589 
590  // Ensure that the request was received exactly as intended
591  $r = $this->getServer()->getReceivedRequests(true);
592  $this->assertFalse($r[0]->hasHeader('Transfer-Encoding'));
593  $this->assertEquals(4, (string) $r[0]->getHeader('Content-Length'));
594  $sent = strtolower($r[0]);
595  $this->assertContains('put / http/1.1', $sent);
596  $this->assertContains('host: 127.0.0.1', $sent);
597  $this->assertContains('user-agent:', $sent);
598  $this->assertContains('content-type: text/plain', $sent);
599  }
600 
602  {
603  $this->getServer()->flush();
604  $this->getServer()->enqueue(array(
605  "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
606  "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi"
607  ));
608  $client = new Client($this->getServer()->getUrl());
609  $request = $client->put('/');
610  $request->setBody(EntityBody::factory(fopen($this->getServer()->getUrl(), 'r')), 'text/plain');
611  $request->send();
612 
613  $r = $this->getServer()->getReceivedRequests(true);
614  $this->assertEquals('chunked', $r[1]->getHeader('Transfer-Encoding'));
615  $this->assertFalse($r[1]->hasHeader('Content-Length'));
616  }
617 
619  {
620  $this->getServer()->flush();
621  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi");
622 
623  $client = new Client($this->getServer()->getUrl());
624  $request = $client->put('/', array('Transfer-Encoding' => 'chunked'), 'hi!');
625  $request->send();
626 
627  $r = $this->getServer()->getReceivedRequests(true);
628  $this->assertEquals('chunked', $r[0]->getHeader('Transfer-Encoding'));
629  $this->assertFalse($r[0]->hasHeader('Content-Length'));
630  $this->assertEquals('hi!', $r[0]->getBody(true));
631  }
632 
634  {
635  $this->getServer()->flush();
636  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi");
637 
638  $request = RequestFactory::getInstance()->create('POST', $this->getServer()->getUrl());
639  $request->getCurlOptions()->set('debug', true);
640  $request->setClient(new Client());
641  $request->addPostFields(array(
642  'a' => 'b',
643  'c' => 'ay! ~This is a test, isn\'t it?'
644  ));
645  $request->send();
646 
647  // Make sure that the request was sent correctly
648  $r = $this->getServer()->getReceivedRequests(true);
649  $this->assertEquals('a=b&c=ay%21%20~This%20is%20a%20test%2C%20isn%27t%20it%3F', (string) $r[0]->getBody());
650  $this->assertFalse($r[0]->hasHeader('Transfer-Encoding'));
651  $this->assertEquals(56, (string) $r[0]->getHeader('Content-Length'));
652  $sent = strtolower($r[0]);
653  $this->assertContains('post / http/1.1', $sent);
654  $this->assertContains('content-type: application/x-www-form-urlencoded; charset=utf-8', $sent);
655  }
656 
658  {
659  $this->getServer()->flush();
660  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi");
661 
662  $request = RequestFactory::getInstance()->create('POST', $this->getServer()->getUrl());
663  $request->getCurlOptions()->set('debug', true);
664  $request->setClient(new Client());
665  $request->addPostFiles(array(
666  'foo' => __FILE__,
667  ));
668  $request->addPostFields(array(
669  'bar' => 'baz',
670  'arr' => array('a' => 1, 'b' => 2),
671  ));
672  $this->updateForHandle($request);
673  $request->send();
674 
675  // Ensure the CURLOPT_POSTFIELDS option was set properly
676  $options = $this->requestHandle->getOptions()->getAll();
677  if (version_compare(phpversion(), '5.5.0', '<')) {
678  $this->assertContains('@' . __FILE__ . ';filename=CurlHandleTest.php;type=text/x-', $options[CURLOPT_POSTFIELDS]['foo']);
679  } else{
680  $this->assertInstanceOf('CURLFile', $options[CURLOPT_POSTFIELDS]['foo']);
681  }
682  $this->assertEquals('baz', $options[CURLOPT_POSTFIELDS]['bar']);
683  $this->assertEquals('1', $options[CURLOPT_POSTFIELDS]['arr[a]']);
684  $this->assertEquals('2', $options[CURLOPT_POSTFIELDS]['arr[b]']);
685  // Ensure that a Content-Length header was sent by cURL
686  $this->assertTrue($request->hasHeader('Content-Length'));
687  }
688 
690  {
691  $request = RequestFactory::getInstance()->create('PUT', $this->getServer()->getUrl());
692  $request->setClient(new Client('http://www.example.com'));
693  $request->getCurlOptions()->set(CURLOPT_CONNECTTIMEOUT, 99);
694  $request->getCurlOptions()->set('curl.fake_opt', 99);
695  $request->getCurlOptions()->set(CURLOPT_PORT, 8181);
696  $handle = CurlHandle::factory($request);
697  $this->assertEquals(99, $handle->getOptions()->get(CURLOPT_CONNECTTIMEOUT));
698  $this->assertEquals(8181, $handle->getOptions()->get(CURLOPT_PORT));
699  $this->assertNull($handle->getOptions()->get('curl.fake_opt'));
700  $this->assertNull($handle->getOptions()->get('fake_opt'));
701  }
702 
704  {
705  $request = RequestFactory::getInstance()->create('PUT', $this->getServer()->getUrl());
706  $handle = CurlHandle::factory($request);
707  $handle->updateRequestFromTransfer($request);
708  }
709 
710  public function testCanSendBodyAsString()
711  {
712  $this->getServer()->flush();
713  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
714  $client = new Client($this->getServer()->getUrl());
715  $request = $client->put('/', null, 'foo');
716  $request->getCurlOptions()->set('body_as_string', true);
717  $request->send();
718  $requests = $this->getServer()->getReceivedRequests(false);
719  $this->assertContains('PUT /', $requests[0]);
720  $this->assertContains("\nfoo", $requests[0]);
721  $this->assertContains('content-length: 3', $requests[0]);
722  $this->assertNotContains('content-type', $requests[0]);
723  }
724 
725  public function testCanSendPostBodyAsString()
726  {
727  $this->getServer()->flush();
728  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
729  $client = new Client($this->getServer()->getUrl());
730  $request = $client->post('/', null, 'foo');
731  $request->getCurlOptions()->set('body_as_string', true);
732  $request->send();
733  $requests = $this->getServer()->getReceivedRequests(false);
734  $this->assertContains('POST /', $requests[0]);
735  $this->assertContains("\nfoo", $requests[0]);
736  $this->assertContains('content-length: 3', $requests[0]);
737  $this->assertNotContains('content-type', $requests[0]);
738  }
739 
741  {
742  $request = RequestFactory::getInstance()->create('PUT', $this->getServer()->getUrl());
743  $request->getCurlOptions()->set('debug', true);
744  $handle = CurlHandle::factory($request);
745  $this->assertNotNull($handle->getOptions()->get(CURLOPT_STDERR));
746  $this->assertNotNull($handle->getOptions()->get(CURLOPT_VERBOSE));
747  }
748 
749  public function testAddsCustomCurlOptions()
750  {
751  $request = RequestFactory::getInstance()->create('PUT', $this->getServer()->getUrl());
752  $request->getCurlOptions()->set(CURLOPT_TIMEOUT, 200);
753  $handle = CurlHandle::factory($request);
754  $this->assertEquals(200, $handle->getOptions()->get(CURLOPT_TIMEOUT));
755  }
756 
758  {
759  $this->getServer()->flush();
760  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\n\r\nContent-Length: 0\r\n\r\n");
761 
762  $fileToUpload = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'TestData' . DIRECTORY_SEPARATOR . 'test_service.json';
763 
764  $client = new Client($this->getServer()->getUrl());
765  $request = $client->post();
766  $request->addPostFile('foo', $fileToUpload, 'application/json');
767  $request->addPostFile('foo', __FILE__);
768 
769  $request->send();
770  $requests = $this->getServer()->getReceivedRequests(true);
771  $body = (string) $requests[0]->getBody();
772 
773  $this->assertContains('Content-Disposition: form-data; name="foo[0]"; filename="', $body);
774  $this->assertContains('Content-Type: application/json', $body);
775  $this->assertContains('Content-Type: text/x-', $body);
776  $this->assertContains('Content-Disposition: form-data; name="foo[1]"; filename="', $body);
777  }
778 
779  public function requestMethodProvider()
780  {
781  return array(array('POST'), array('PUT'), array('PATCH'));
782  }
783 
788  {
789  $this->getServer()->flush();
790  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
791  $client = new Client($this->getServer()->getUrl());
792  $client->createRequest($method)->send();
793  $requests = $this->getServer()->getReceivedRequests(true);
794  $this->assertFalse($requests[0]->hasHeader('Transfer-Encoding'));
795  $this->assertTrue($requests[0]->hasHeader('Content-Length'));
796  $this->assertEquals('0', (string) $requests[0]->getHeader('Content-Length'));
797  }
798 
802  public function testParseCurlConfigConvertsStringKeysToConstantKeys($options, $expected)
803  {
804  $actual = CurlHandle::parseCurlConfig($options);
805  $this->assertEquals($expected, $actual);
806  }
807 
813  public function provideCurlConfig()
814  {
815  return array(
816  // Conversion of option name to constant value
817  array(
818  array(
819  'CURLOPT_PORT' => 10,
820  'CURLOPT_TIMEOUT' => 99
821  ),
822  array(
823  CURLOPT_PORT => 10,
824  CURLOPT_TIMEOUT => 99
825  )
826  ),
827  // Keeps non constant options
828  array(
829  array('debug' => true),
830  array('debug' => true)
831  ),
832  // Conversion of constant names to constant values
833  array(
834  array('debug' => 'CURLPROXY_HTTP'),
835  array('debug' => CURLPROXY_HTTP)
836  )
837  );
838  }
839 
841  {
842  $this->getServer()->flush();
843  $this->getServer()->enqueue(array(
844  "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
845  "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"
846  ));
847 
848  $client = new Client($this->getServer()->getUrl());
849  $request = $client->put('/', null, 'test');
850  $request->send();
851  $request->send();
852 
853  $received = $this->getServer()->getReceivedRequests(true);
854  $this->assertEquals(2, count($received));
855  $this->assertEquals('test', (string) $received[0]->getBody());
856  $this->assertEquals('test', (string) $received[1]->getBody());
857  }
858 
860  {
861  $this->getServer()->flush();
862  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
863 
864  $client = new Client($this->getServer()->getUrl());
865  $request = $client->get('/', null);
866  $request->getCurlOptions()->set(CURLOPT_ENCODING, '');
867  $this->updateForHandle($request);
868  $request->send();
869  $options = $this->requestHandle->getOptions()->getAll();
870  $this->assertSame('', $options[CURLOPT_ENCODING]);
871  $received = $this->getServer()->getReceivedRequests(false);
872  $this->assertContainsIns('accept: */*', $received[0]);
873  $this->assertContainsIns('accept-encoding: ', $received[0]);
874  }
875 
877  {
878  $this->getServer()->flush();
879  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
880  $client = new Client($this->getServer()->getUrl());
881  $request = $client->put('/', null, 'test');
882  // Start sending the expect header to 2 bytes
883  $this->updateForHandle($request);
884  $request->setExpectHeaderCutoff(2)->send();
885  $options = $this->requestHandle->getOptions()->getAll();
886  $this->assertContains('Expect: 100-Continue', $options[CURLOPT_HTTPHEADER]);
887  $received = $this->getServer()->getReceivedRequests(false);
888  $this->assertContainsIns('expect: 100-continue', $received[0]);
889  }
890 
892  {
893  $this->getServer()->flush();
894  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndata");
895  $client = new Client($this->getServer()->getUrl());
896  $request = $client->get('/', array(
897  'Accept' => 'application/json',
898  'Accept-Encoding' => 'gzip, deflate',
899  ));
900  $this->updateForHandle($request);
901  $request->send();
902  $options = $this->requestHandle->getOptions()->getAll();
903  $this->assertSame('gzip, deflate', $options[CURLOPT_ENCODING]);
904  $received = $this->getServer()->getReceivedRequests(false);
905  $this->assertContainsIns('accept: application/json', $received[0]);
906  $this->assertContainsIns('accept-encoding: gzip, deflate', $received[0]);
907  }
908 
910  {
911  $this->getServer()->flush();
912  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\n\r\nContent-Length: 0\r\n\r\n");
913 
914  $client = new Client();
915  $request = $client->put($this->getServer()->getUrl(), null, array(
916  'foo' => 'baz',
917  'baz' => 'bar'
918  ));
919 
920  $request->send();
921  $requests = $this->getServer()->getReceivedRequests(true);
922  $this->assertEquals('PUT', $requests[0]->getMethod());
923  $this->assertEquals(
924  'application/x-www-form-urlencoded; charset=utf-8',
925  (string) $requests[0]->getHeader('Content-Type')
926  );
927  $this->assertEquals(15, (string) $requests[0]->getHeader('Content-Length'));
928  $this->assertEquals('foo=baz&baz=bar', (string) $requests[0]->getBody());
929  }
930 
932  {
933  $this->getServer()->flush();
934  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\n\r\nContent-Length: 0\r\n\r\n");
935 
936  $client = new Client();
937  $request = $client->put($this->getServer()->getUrl(), null, array(
938  'foo' => '@' . __FILE__
939  ));
940 
941  $request->send();
942  $requests = $this->getServer()->getReceivedRequests(true);
943  $this->assertEquals('PUT', $requests[0]->getMethod());
944  $this->assertContains('multipart/form-data', (string) $requests[0]->getHeader('Content-Type'));
945  $this->assertContains('testSendsPostFilesForNonPostRequests', (string) $requests[0]->getBody());
946  }
947 }
Guzzle\Tests\Http\Curl\CurlHandleTest\testSendsPostFieldsForNonPostRequests
testSendsPostFieldsForNonPostRequests()
Definition: CurlHandleTest.php:909
Guzzle\Tests\Http\Curl\CurlHandleTest\requestMethodProvider
requestMethodProvider()
Definition: CurlHandleTest.php:779
Guzzle\Tests\Http\Curl\CurlHandleTest\testSetsCurloptEncodingWhenAcceptEncodingHeaderIsSet
testSetsCurloptEncodingWhenAcceptEncodingHeaderIsSet()
Definition: CurlHandleTest.php:891
Guzzle\Tests\GuzzleTestCase\compareHeaders
compareHeaders($filteredHeaders, $actualHeaders)
Definition: GuzzleTestCase.php:217
Guzzle\Tests\Http\Curl\CurlHandleTest\testSendsExpectHeaderWhenSizeIsGreaterThanCutoff
testSendsExpectHeaderWhenSizeIsGreaterThanCutoff()
Definition: CurlHandleTest.php:876
Guzzle\Tests\Http\Curl\CurlHandleTest\testStoresStdErr
testStoresStdErr()
Definition: CurlHandleTest.php:77
Guzzle\Tests\Http\Curl\CurlHandleTest\testParseCurlConfigConvertsStringKeysToConstantKeys
testParseCurlConfigConvertsStringKeysToConstantKeys($options, $expected)
Definition: CurlHandleTest.php:802
Guzzle\Tests\Http\Curl\CurlHandleTest\testUploadsPutDataUsingChunkedEncodingWhenLengthCannotBeDetermined
testUploadsPutDataUsingChunkedEncodingWhenLengthCannotBeDetermined()
Definition: CurlHandleTest.php:601
Guzzle\Http\Message\RequestInterface
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestInterface.php:16
Guzzle\Tests\Http\Curl\CurlHandleTest\testSendsPostFilesForNonPostRequests
testSendsPostFilesForNonPostRequests()
Definition: CurlHandleTest.php:931
Guzzle\Tests\Http\Curl\CurlHandleTest\testConstructorInitializesObject
testConstructorInitializesObject()
Definition: CurlHandleTest.php:65
Guzzle\Tests\Http\Curl\CurlHandleTest\testUploadsPutData
testUploadsPutData()
Definition: CurlHandleTest.php:573
Guzzle\Tests\Http\Curl\CurlHandleTest\testDeterminesIfResourceIsAvailable
testDeterminesIfResourceIsAvailable()
Definition: CurlHandleTest.php:107
Guzzle\Tests\Http\Curl\CurlHandleTest\testSendsRequestsWithNoBodyUsingContentLengthZero
testSendsRequestsWithNoBodyUsingContentLengthZero($method)
Definition: CurlHandleTest.php:787
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Http\QueryString
Definition: QueryString.php:14
Guzzle\Http\Curl\CurlHandle\parseCurlConfig
static parseCurlConfig($config)
Definition: CurlHandle.php:457
Guzzle\Tests\Http\Curl\CurlHandleTest\testSendsPostUploadsWithContentDispositionHeaders
testSendsPostUploadsWithContentDispositionHeaders()
Definition: CurlHandleTest.php:757
Guzzle\Tests\Http\Curl\CurlHandleTest\$requestHandle
$requestHandle
Definition: CurlHandleTest.php:20
Guzzle\Http\EntityBody\factory
static factory($resource='', $size=null)
Definition: EntityBody.php:36
Guzzle\Tests\Http\Curl\CurlHandleTest\testWrapsErrorsAndInfo
testWrapsErrorsAndInfo()
Definition: CurlHandleTest.php:122
Guzzle\Tests\Http\Curl\CurlHandleTest\testCanSendBodyAsString
testCanSendBodyAsString()
Definition: CurlHandleTest.php:710
Guzzle\Http\EntityBody
Definition: EntityBody.php:13
Guzzle\Tests\Http\Curl
Definition: CurlHandleTest.php:3
Guzzle\Tests\Http\Curl\CurlHandleTest\testFactoryUsesSpecifiedProtocol
testFactoryUsesSpecifiedProtocol()
Definition: CurlHandleTest.php:564
Guzzle\Tests\Http\Curl\CurlHandleTest\testCanSendPostBodyAsString
testCanSendPostBodyAsString()
Definition: CurlHandleTest.php:725
Guzzle\Tests\Http\Curl\CurlHandleTest
Definition: CurlHandleTest.php:18
Guzzle\Common\Event
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Event.php:10
Guzzle\Tests\Http\Curl\CurlHandleTest\testCurlConfigurationOptionsAreSet
testCurlConfigurationOptionsAreSet()
Definition: CurlHandleTest.php:689
Guzzle\Tests\Http\Curl\CurlHandleTest\provideCurlConfig
provideCurlConfig()
Definition: CurlHandleTest.php:813
Guzzle\Tests\Http\Curl\CurlHandleTest\dataProvider
dataProvider()
Definition: CurlHandleTest.php:187
Guzzle\Tests\Http\Curl\CurlHandleTest\testAllowsWireTransferInfoToBeEnabled
testAllowsWireTransferInfoToBeEnabled()
Definition: CurlHandleTest.php:740
Guzzle\Tests\Http\Curl\CurlHandleTest\testAccountsForMissingStdErr
testAccountsForMissingStdErr()
Definition: CurlHandleTest.php:98
Guzzle\Common\HasDispatcherInterface\getEventDispatcher
getEventDispatcher()
Guzzle\Tests\Http\Curl\CurlHandleTest\testConstructorExpectsProperOptions
testConstructorExpectsProperOptions()
Definition: CurlHandleTest.php:45
Guzzle\Http\Message\RequestFactory
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestFactory.php:14
Guzzle\Tests\Http\Curl\CurlHandleTest\testWrapsCurlOptions
testWrapsCurlOptions()
Definition: CurlHandleTest.php:170
Guzzle\Tests\Http\Curl\CurlHandleTest\testGetInfoWithoutDebugMode
testGetInfoWithoutDebugMode()
Definition: CurlHandleTest.php:158
Guzzle\Tests\Http\Curl\CurlHandleTest\testEnsuresRequestsHaveResponsesWhenUpdatingFromTransfer
testEnsuresRequestsHaveResponsesWhenUpdatingFromTransfer()
Definition: CurlHandleTest.php:703
Guzzle\Tests\Http\Curl\CurlHandleTest\updateForHandle
updateForHandle(RequestInterface $request)
Definition: CurlHandleTest.php:22
Guzzle\Tests\Http\Curl\CurlHandleTest\testFactoryCreatesCurlBasedOnRequest
testFactoryCreatesCurlBasedOnRequest($method, $url, $headers, $body, $options, $expectedHeaders=null)
Definition: CurlHandleTest.php:508
Guzzle\Tests\GuzzleTestCase\assertContainsIns
assertContainsIns($needle, $haystack, $message=null)
Definition: GuzzleTestCase.php:231
Guzzle\Http\Client
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Client.php:24
Guzzle\Tests\Http\Curl\CurlHandleTest\testUploadsPutDataUsingChunkedEncodingWhenForced
testUploadsPutDataUsingChunkedEncodingWhenForced()
Definition: CurlHandleTest.php:618
Guzzle\Tests\Http\Curl\CurlHandleTest\testConstructorExpectsCurlResource
testConstructorExpectsCurlResource()
Definition: CurlHandleTest.php:40
Guzzle\Tests\Http\Curl\CurlHandleTest\testAllowsCurloptEncodingToBeSet
testAllowsCurloptEncodingToBeSet()
Definition: CurlHandleTest.php:859
Guzzle\Tests\Http\Curl\CurlHandleTest\testSendsPostRequestsWithFiles
testSendsPostRequestsWithFiles()
Definition: CurlHandleTest.php:657
Guzzle\Http\Message\RequestFactory\getInstance
static getInstance()
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestFactory.php:42
Guzzle\Http\Curl\CurlHandle
Definition: CurlHandle.php:16
Guzzle\Tests\Http\Curl\CurlHandleTest\testSendsPostRequestsWithFields
testSendsPostRequestsWithFields()
Definition: CurlHandleTest.php:633
Guzzle\Tests\GuzzleTestCase\getServer
static getServer()
Definition: GuzzleTestCase.php:36
Guzzle\Tests\Http\Curl\CurlHandleTest\setUp
setUp()
Definition: CurlHandleTest.php:32
Guzzle\Tests\Http\Curl\CurlHandleTest\testSeeksToBeginningOfStreamWhenSending
testSeeksToBeginningOfStreamWhenSending()
Definition: CurlHandleTest.php:840
Guzzle\Tests\Http\Curl\CurlHandleTest\testAddsCustomCurlOptions
testAddsCustomCurlOptions()
Definition: CurlHandleTest.php:749
Guzzle\Tests\GuzzleTestCase\getWildcardObserver
getWildcardObserver(HasDispatcherInterface $hasDispatcher)
Definition: GuzzleTestCase.php:106
Guzzle\Tests\Http\Curl\CurlHandleTest\testStoresCurlErrorNumber
testStoresCurlErrorNumber()
Definition: CurlHandleTest.php:90
Guzzle\Common\Collection
Definition: paymethod/paypal/lib/vendor/guzzle/guzzle/src/Guzzle/Common/Collection.php:11
Guzzle\Http\Curl\CurlHandle\factory
static factory(RequestInterface $request)
Definition: CurlHandle.php:52