1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-13 03:45:22 +01:00

[Http] [Service] Better handling in the RequestFactory to generate entity enclosing requests using various body variable types. Passing an API command to a concrete command class when working with service descriptions. Adding more tests.

This commit is contained in:
Michael Dowling 2011-03-22 11:23:06 -05:00
parent 43f3b213f2
commit f53d169978
3 changed files with 23 additions and 9 deletions

View File

@ -221,12 +221,12 @@ class RequestFactory
$request = new $c($method, $url, $headers);
if ($body) {
if ($method == 'POST' && is_array($body)) {
$request->addPostFields(new QueryString($body));
} else if ($method == 'POST' && $body instanceof Collection) {
$request->addPostFields($body->getAll());
} else {
if ($method == 'POST' && (is_array($body) || $body instanceof Collection)) {
$request->addPostFields($body);
} else if (is_resource($body) || $body instanceof EntityBody) {
$request->setBody($body);
} else {
$request->setBody((string) $body);
}
}
}

View File

@ -30,7 +30,7 @@ class DynamicCommandFactory implements CommandFactoryInterface
if ($command->getConcreteClass() != 'Guzzle\\Service\\Command\\ClosureCommand') {
$class = $command->getConcreteClass();
return new $class($args);
return new $class($args, $command);
}
// Build the command based on the service doc and supplied arguments

View File

@ -7,6 +7,7 @@
namespace Guzzle\Tests\Http\Message;
use Guzzle\Common\Collection;
use Guzzle\Http\Url;
use Guzzle\Http\EntityBody;
use Guzzle\Http\Message\RequestFactory;
use Guzzle\Http\QueryString;
@ -47,6 +48,7 @@ class HttpRequestFactoryTest extends \Guzzle\Tests\GuzzleTestCase
*/
public function testCreatesPutRequests()
{
// Test using a string
$request = RequestFactory::put('http://www.google.com/path?q=1&v=2', null, 'Data');
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
$this->assertEquals('PUT', $request->getMethod());
@ -56,13 +58,25 @@ class HttpRequestFactoryTest extends \Guzzle\Tests\GuzzleTestCase
$this->assertEquals('/path', $request->getPath());
$this->assertEquals('/path?q=1&v=2', $request->getResourceUri());
$this->assertInstanceOf('Guzzle\\Http\\EntityBody', $request->getBody());
$this->assertEquals('Data', (string)$request->getBody());
$this->assertEquals('Data', (string) $request->getBody());
unset($request);
// Test using an EntityBody
$request = RequestFactory::put('http://www.google.com/path?q=1&v=2', null, EntityBody::factory('Data'));
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
$this->assertEquals('Data', (string)$request->getBody());
$this->assertEquals('Data', (string) $request->getBody());
// Test using a resource
$resource = fopen('php://temp', 'w+');
fwrite($resource, 'Data');
$request = RequestFactory::put('http://www.google.com/path?q=1&v=2', null, $resource);
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
$this->assertEquals('Data', (string) $request->getBody());
// Test using an object that can be cast as a string
$request = RequestFactory::put('http://www.google.com/path?q=1&v=2', null, Url::factory('http://www.example.com/'));
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
$this->assertEquals('http://www.example.com/', (string) $request->getBody());
}
/**
@ -94,7 +108,7 @@ class HttpRequestFactoryTest extends \Guzzle\Tests\GuzzleTestCase
public function testCreatesNewPutRequestWithBody()
{
$request = RequestFactory::put('http://www.google.com/path?q=1&v=2', null, 'Data');
$this->assertEquals('Data', (string)$request->getBody());
$this->assertEquals('Data', (string) $request->getBody());
}
/**