Skip to content

Commit

Permalink
Fix for multidimensional params support
Browse files Browse the repository at this point in the history
  • Loading branch information
SammyK committed Apr 1, 2015
1 parent b583b4a commit 2156afc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 54 deletions.
20 changes: 19 additions & 1 deletion src/Facebook/HttpClients/FacebookCurlHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function openConnection($url, $method = 'GET', array $parameters = array(
);

if ($method !== 'GET') {
$options[CURLOPT_POSTFIELDS] = $parameters;
$options[CURLOPT_POSTFIELDS] = !$this->paramsHaveFile($parameters) ? http_build_query($parameters, null, '&') : $parameters;
}
if ($method === 'DELETE' || $method === 'PUT') {
$options[CURLOPT_CUSTOMREQUEST] = $method;
Expand Down Expand Up @@ -326,4 +326,22 @@ private function needsCurlProxyFix()
return $version < self::CURL_PROXY_QUIRK_VER;
}

/**
* Detect if the params have a file to upload.
*
* @param array $params
*
* @return boolean
*/
private function paramsHaveFile(array $params)
{
foreach ($params as $value) {
if ($value instanceof \CURLFile) {
return true;
}
}

return false;
}

}
68 changes: 15 additions & 53 deletions tests/HttpClients/FacebookCurlHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public function testCanOpenGetCurlConnection()
->with(m::on(function($arg) {
$caInfo = array_diff($arg, [
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => [],
CURLOPT_URL => 'http:https://foo.com',
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 60,
Expand Down Expand Up @@ -72,11 +71,15 @@ public function testCanOpenGetCurlConnectionWithHeaders()
$this->curlMock
->shouldReceive('setopt_array')
->with(m::on(function($arg) {

// array_diff() will sometimes trigger error on multidimensional arrays
if (['X-foo: bar'] !== $arg[CURLOPT_HTTPHEADER]) {
return false;
}
unset($arg[CURLOPT_HTTPHEADER]);

$caInfo = array_diff($arg, [
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'X-foo: bar',
),
CURLOPT_URL => 'http:https://foo.com',
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 60,
Expand Down Expand Up @@ -114,17 +117,14 @@ public function testCanOpenPostCurlConnection()
->with(m::on(function($arg) {
$caInfo = array_diff($arg, [
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [],
CURLOPT_URL => 'http:https://bar.com',
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 60,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_POSTFIELDS => array(
'baz' => 'bar',
),
CURLOPT_POSTFIELDS => 'baz=bar&foo%5B0%5D=1&foo%5B1%5D=2&foo%5B2%5D=3',
]);

if (count($caInfo) !== 1) {
Expand All @@ -140,47 +140,12 @@ public function testCanOpenPostCurlConnection()
->once()
->andReturn(null);

$this->curlClient->openConnection('http:https://bar.com', 'POST', array('baz' => 'bar'));
}

public function testCanOpenPutCurlConnection()
{
$this->curlMock
->shouldReceive('init')
->once()
->andReturn(null);
$this->curlMock
->shouldReceive('setopt_array')
->with(m::on(function($arg) {
$caInfo = array_diff($arg, [
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_HTTPHEADER => [],
CURLOPT_URL => 'http:https://baz.com',
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 60,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_POSTFIELDS => array(
'baz' => 'bar',
),
]);

if (count($caInfo) !== 1) {
return false;
}

if (1 !== preg_match('/.+\/certs\/DigiCertHighAssuranceEVRootCA\.pem$/', $caInfo[CURLOPT_CAINFO])) {
return false;
}

return true;
}))
->once()
->andReturn(null);

$this->curlClient->openConnection('http:https://baz.com', 'PUT', array('baz' => 'bar'));
// Prove can support multidimensional params
$params = array(
'baz' => 'bar',
'foo' => array(1, 2, 3),
);
$this->curlClient->openConnection('http:https://bar.com', 'POST', $params);
}

public function testCanOpenDeleteCurlConnection()
Expand All @@ -194,17 +159,14 @@ public function testCanOpenDeleteCurlConnection()
->with(m::on(function($arg) {
$caInfo = array_diff($arg, [
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_HTTPHEADER => [],
CURLOPT_URL => 'http:https://faz.com',
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 60,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_POSTFIELDS => array(
'baz' => 'bar',
),
CURLOPT_POSTFIELDS => 'baz=bar',
]);

if (count($caInfo) !== 1) {
Expand Down

0 comments on commit 2156afc

Please sign in to comment.