Skip to content

Commit

Permalink
CurlFactory tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed Mar 26, 2015
1 parent 453a537 commit 0a3065e
Show file tree
Hide file tree
Showing 3 changed files with 545 additions and 17 deletions.
22 changes: 11 additions & 11 deletions src/Handler/CurlFactory.php
Expand Up @@ -41,7 +41,7 @@ public function __invoke(
unset($conf['_headers']);
// Add handler options from the request configuration options
if (isset($options['curl'])) {
$options = $this->applyCustomCurlOptions($options['curl'], $conf);
$conf = $this->applyCustomCurlOptions($options['curl'], $conf);
}

if (!$handle) {
Expand Down Expand Up @@ -211,7 +211,7 @@ private function applyMethod(RequestInterface $request, array $options, array &$
$body = $request->getBody();
$size = $body->getSize();

if ($size !== null && $size > 0) {
if ($size === null || $size > 0) {

This comment has been minimized.

Copy link
@jvelo

jvelo Aug 5, 2015

Hi!
Is this the expected behavior ? I find it odd that the body is being applied when its size is not set / null ; I imagine there is a reason, but couldn't figure it out from the code.
(I'm debugging an issue I have with PHP-VCR that fails when curl has the CURLOPT_READFUNCTION set without the CURLOPT_INFILESIZE)
Thanks in advance for any help/explanation.

This comment has been minimized.

Copy link
@mtdowling

mtdowling Aug 5, 2015

Author Member

When the size is null, it means that we don't know what the size is, therefore we don't know if it is 0 or > 0. This could happen for example when using a body that is streamed from another source dynamically. We still want to send the body in this case.

This comment has been minimized.

Copy link
@jvelo

jvelo Aug 6, 2015

That makes sense, thanks.

$this->applyBody($request, $options, $conf);
return;
}
Expand All @@ -237,13 +237,14 @@ private function applyMethod(RequestInterface $request, array $options, array &$

private function applyBody(RequestInterface $request, array $options, array &$conf)
{
$contentLength = $request->getHeader('Content-Length');
$size = $contentLength !== null ? (int) $contentLength : null;
$size = $request->hasHeader('Content-Length')
? (int) $request->getHeader('Content-Length')
: $request->getBody()->getSize();

// Send the body as a string if the size is less than 1MB OR if the
// [client][curl][body_as_string] request value is set.
// [curl][body_as_string] request value is set.
if (($size !== null && $size < 1000000) ||
isset($options['curl']['body_as_string'])
!empty($options['curl']['body_as_string'])
) {
$conf[CURLOPT_POSTFIELDS] = (string) $request->getBody();
// Don't duplicate the Content-Length header
Expand Down Expand Up @@ -303,7 +304,7 @@ private function applyHeaders(RequestInterface $request, array &$conf)
* Takes an array of curl options specified in the 'curl' option of a
* request's configuration array and maps them to CURLOPT_* options.
*
* This method is only called when a request has a 'curl' config setting.
* This method is only called when a request has a 'curl' config setting.
*
* @param array $options Configuration array of custom curl option
* @param array $conf Array of existing curl options
Expand Down Expand Up @@ -436,8 +437,7 @@ function ($ch, $write) use ($value) {

if (!is_array($value)) {
$conf[CURLOPT_PROXY] = $value;
} elseif (isset($request['scheme'])) {
$scheme = $request['scheme'];
} elseif ($scheme = $request->getUri()->getScheme()) {
if (isset($value[$scheme])) {
$conf[CURLOPT_PROXY] = $value[$scheme];
}
Expand Down Expand Up @@ -529,7 +529,7 @@ private static function retryFailedRewind(
array $options,
array $response
) {
if ($request->getBody()->rewind()) {
if (!$request->getBody()->rewind()) {
$response['err_message'] = 'The connection unexpectedly failed '
. 'without providing an error. The request would have been '
. 'retried, but attempting to rewind the request body failed.';
Expand All @@ -541,7 +541,7 @@ private static function retryFailedRewind(
$options['curl']['retries'] = 1;
} elseif ($options['curl']['retries'] == 2) {
$response['err_message'] = 'The cURL request was retried 3 times '
. 'and did no succeed. cURL was unable to rewind the body of '
. 'and did not succeed. cURL was unable to rewind the body of '
. 'the request and subsequent retries resulted in the same '
. 'error. Turn on the debug option to see what went wrong. '
. 'See https://bugs.php.net/bug.php?id=47204 for more information.';
Expand Down

0 comments on commit 0a3065e

Please sign in to comment.