Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

Commit

Permalink
Fixes problem with multi-dimensional files array
Browse files Browse the repository at this point in the history
  • Loading branch information
hason committed Aug 16, 2012
1 parent 5f7fd00 commit b207ffc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
30 changes: 21 additions & 9 deletions Goutte/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Guzzle\Http\Message\Response as GuzzleResponse;
use Guzzle\Http\ClientInterface as GuzzleClientInterface;
use Guzzle\Http\Client as GuzzleClient;
use Guzzle\Http\Message\EntityEnclosingRequestInterface;

/**
* Client.
Expand Down Expand Up @@ -112,15 +113,7 @@ protected function doRequest($request)
}

if ('POST' == $request->getMethod()) {
$postFiles = array();
foreach ($request->getFiles() as $name => $info) {
if (isset($info['tmp_name']) && '' !== $info['tmp_name']) {
$postFiles[$name] = $info['tmp_name'];
}
}
if (!empty($postFiles)) {
$guzzleRequest->addPostFiles($postFiles);
}
$this->addPostFiles($guzzleRequest, $request->getFiles());
}

$guzzleRequest->getCurlOptions()
Expand All @@ -144,6 +137,25 @@ protected function doRequest($request)
return $this->createResponse($response);
}

protected function addPostFiles($request, array $files, $arrayName = '')
{
if (!$request instanceof EntityEnclosingRequestInterface) {
return;
}

foreach ($files as $name => $info) {
if (!empty($arrayName)) {
$name = $arrayName . '[' . $name . ']';
}

if (isset($info['tmp_name']) && '' !== $info['tmp_name']) {
$request->addPostFile($name, $info['tmp_name']);
} elseif (is_array($info)) {
$this->addPostFiles($request, $info, $name);
}
}
}

protected function createResponse(GuzzleResponse $response)
{
return new Response($response->getBody(true), $response->getStatusCode(), $response->getHeaders()->getAll());
Expand Down
24 changes: 24 additions & 0 deletions Goutte/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ public function testUsesPostFiles()
), $request->getPostFiles());
}

public function testUsesPostFilesNestedFields()
{
$guzzle = $this->getGuzzle();
$client = new Client();
$client->setClient($guzzle);
$files = array(
'form' => array(
'test' => array(
'name' => 'test.txt',
'tmp_name' => __FILE__
),
),
);

$crawler = $client->request('POST', 'http://www.example.com/', array(), $files);
$request = $this->historyPlugin->getLastRequest();

$this->assertEquals(array(
'form[test]' => array(
new PostFile('form[test]', __FILE__, 'text/x-php')
)
), $request->getPostFiles());
}

public function testUsesCurlOptions()
{
$guzzle = $this->getGuzzle();
Expand Down

0 comments on commit b207ffc

Please sign in to comment.