mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-24 18:13:00 +01:00
Adding helper to entity requests to enable strict redirects. More tests.
This commit is contained in:
parent
464a059d09
commit
fd8515cd92
@ -71,9 +71,7 @@ class EntityBody extends Stream implements EntityBodyInterface
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
return $this->rewindFunction
|
||||
? call_user_func($this->rewindFunction, $this)
|
||||
: parent::rewind();
|
||||
return $this->rewindFunction ? call_user_func($this->rewindFunction, $this) : parent::rewind();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -5,6 +5,7 @@ namespace Guzzle\Http\Message;
|
||||
use Guzzle\Http\EntityBody;
|
||||
use Guzzle\Http\EntityBodyInterface;
|
||||
use Guzzle\Http\QueryString;
|
||||
use Guzzle\Http\RedirectPlugin;
|
||||
use Guzzle\Http\Exception\RequestException;
|
||||
|
||||
/**
|
||||
@ -137,6 +138,16 @@ class EntityEnclosingRequest extends Request implements EntityEnclosingRequestIn
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function enableStrictRedirects($strict)
|
||||
{
|
||||
$this->getParams()->set(RedirectPlugin::STRICT_REDIRECTS, $strict);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -123,4 +123,15 @@ interface EntityEnclosingRequestInterface extends RequestInterface
|
||||
* @return EntityEnclosingRequestInterface
|
||||
*/
|
||||
public function addPostFiles(array $files);
|
||||
|
||||
/**
|
||||
* Specify whether or not to use strict redirects or to emulate browsers when redirecting a POST request. Most
|
||||
* browsers with follow a 301-302 redirect for a POST request with a GET request. This is the default behavior of
|
||||
* Guzzle. Enable strict redirects to redirect these responses with a POST rather than a GET request.
|
||||
*
|
||||
* @param bool $strict Set to true to follow strict RFC compliance when redirecting POST requests.
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function enableStrictRedirects($strict);
|
||||
}
|
||||
|
@ -176,7 +176,7 @@ class RedirectPlugin implements EventSubscriberInterface
|
||||
|
||||
// Throw an exception if the redirect count is exceeded
|
||||
if ($current > $max) {
|
||||
$this->throwTooManyRedirectsException($request);
|
||||
return $this->throwTooManyRedirectsException($request);
|
||||
}
|
||||
|
||||
return $original;
|
||||
|
@ -12,7 +12,7 @@ class EntityBodyTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
{
|
||||
/**
|
||||
* @covers Guzzle\Http\EntityBody::factory
|
||||
* @expectedException Guzzle\Common\Exception\InvalidArgumentException
|
||||
* @expectedException \Guzzle\Common\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testFactoryThrowsException()
|
||||
{
|
||||
@ -190,4 +190,31 @@ class EntityBodyTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
$body = EntityBody::factory(array('key1' => 'val1', 'key2' => 'val2'));
|
||||
$this->assertEquals('key1=val1&key2=val2', (string)$body);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\EntityBody::setRewindFunction
|
||||
* @covers Guzzle\Http\EntityBody::rewind
|
||||
*/
|
||||
public function testAllowsCustomRewind()
|
||||
{
|
||||
$body = EntityBody::factory('foo');
|
||||
$rewound = false;
|
||||
$body->setRewindFunction(function ($body) use (&$rewound) {
|
||||
$rewound = true;
|
||||
return $body->seek(0);
|
||||
});
|
||||
$body->seek(2);
|
||||
$this->assertTrue($body->rewind());
|
||||
$this->assertTrue($rewound);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\EntityBody::setRewindFunction
|
||||
* @expectedException \Guzzle\Common\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testCustomRewindFunctionMustBeCallable()
|
||||
{
|
||||
$body = EntityBody::factory();
|
||||
$body->setRewindFunction('foo');
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ use Guzzle\Http\Client;
|
||||
use Guzzle\Http\EntityBody;
|
||||
use Guzzle\Http\Message\Request;
|
||||
use Guzzle\Http\Message\RequestFactory;
|
||||
use Guzzle\Http\Exception\RequestException;
|
||||
use Guzzle\Http\RedirectPlugin;
|
||||
use Guzzle\Http\Message\EntityEnclosingRequest;
|
||||
use Guzzle\Http\Message\PostFile;
|
||||
use Guzzle\Http\QueryString;
|
||||
@ -500,4 +500,14 @@ class EntityEnclosingRequestTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
$request->setBody('foobazbarbamboo');
|
||||
$this->assertNotNull($request->getHeader('Expect'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Message\EntityEnclosingRequest::enableStrictRedirects
|
||||
*/
|
||||
public function testStrictRedirectsCanBeSpecifiedOnEntityEnclosingRequests()
|
||||
{
|
||||
$request = new EntityEnclosingRequest('PUT', 'http://test.com/');
|
||||
$request->enableStrictRedirects(true);
|
||||
$this->assertTrue($request->getParams()->get(RedirectPlugin::STRICT_REDIRECTS));
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace Guzzle\Tests\Plugin\Redirect;
|
||||
|
||||
use Guzzle\Http\Client;
|
||||
use Guzzle\Http\EntityBody;
|
||||
use Guzzle\Http\RedirectPlugin;
|
||||
use Guzzle\Http\Exception\TooManyRedirectsException;
|
||||
|
||||
@ -114,4 +115,42 @@ class RedirectPluginTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
$this->assertEquals('bar', (string) $requests[1]->getHeader('X-Baz'));
|
||||
$this->assertEquals('POST', $requests[2]->getMethod());
|
||||
}
|
||||
|
||||
public function testRewindsStreamWhenRedirectingIfNeeded()
|
||||
{
|
||||
$this->getServer()->flush();
|
||||
$this->getServer()->enqueue(array(
|
||||
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
|
||||
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
|
||||
));
|
||||
|
||||
$client = new Client($this->getServer()->getUrl());
|
||||
$request = $client->put();
|
||||
$request->enableStrictRedirects(true);
|
||||
$body = EntityBody::factory('foo');
|
||||
$body->read(1);
|
||||
$request->setBody($body);
|
||||
$request->send();
|
||||
$requests = $this->getServer()->getReceivedRequests(true);
|
||||
$this->assertEquals('foo', (string) $requests[0]->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Guzzle\Http\Exception\CouldNotRewindStreamException
|
||||
*/
|
||||
public function testThrowsExceptionWhenStreamCannotBeRewound()
|
||||
{
|
||||
$this->getServer()->flush();
|
||||
$this->getServer()->enqueue(array(
|
||||
"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi",
|
||||
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n"
|
||||
));
|
||||
|
||||
$client = new Client($this->getServer()->getUrl());
|
||||
$request = $client->put();
|
||||
$request->enableStrictRedirects(true);
|
||||
$body = EntityBody::factory(fopen($this->getServer()->getUrl(), 'r'));
|
||||
$body->read(1);
|
||||
$request->setBody($body)->send();
|
||||
}
|
||||
}
|
||||
|
@ -187,4 +187,15 @@ class StreamTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
$this->assertSame(ftell($handle), $stream->ftell());
|
||||
$this->assertEquals(2, $stream->ftell());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Stream\Stream::rewind
|
||||
*/
|
||||
public function testRewindIsSeekZero()
|
||||
{
|
||||
$stream = new Stream(fopen('php://temp', 'w+'));
|
||||
$stream->write('foobazbar');
|
||||
$this->assertTrue($stream->rewind());
|
||||
$this->assertEquals('foobazbar', $stream->read(9));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user