1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-24 10:03:27 +01:00

Adding test for HttpError. Moving subscribers to Subscriber dir.

This commit is contained in:
Michael Dowling 2013-10-06 12:49:43 -07:00
parent 7b5dc7c2fb
commit a18fd6f9ea
9 changed files with 178 additions and 26 deletions

View File

@ -2,10 +2,10 @@
namespace Guzzle\Http\Message;
use Guzzle\Http\HttpErrorPlugin;
use Guzzle\Http\Subscriber\HttpError;
use Guzzle\Http\Message\Post\PostBody;
use Guzzle\Http\Message\Post\PostFile;
use Guzzle\Http\RedirectPlugin;
use Guzzle\Http\Subscriber\Redirect;
use Guzzle\Stream\Stream;
use Guzzle\Url\Url;
@ -14,16 +14,16 @@ use Guzzle\Url\Url;
*/
class MessageFactory implements MessageFactoryInterface
{
/** @var HttpErrorPlugin */
/** @var HttpError */
private $errorPlugin;
/** @var RedirectPlugin */
/** @var Redirect */
private $redirectPlugin;
public function __construct()
{
$this->errorPlugin = new HttpErrorPlugin();
$this->redirectPlugin = new RedirectPlugin();
$this->errorPlugin = new HttpError();
$this->redirectPlugin = new Redirect();
}
public function createResponse($statusCode , array $headers = [], $body = null, array $options = [])
@ -132,7 +132,7 @@ class MessageFactory implements MessageFactoryInterface
static $methods;
static $map = [
'connect_timeout' => 1, 'timeout' => 1, 'verify' => 1, 'future' => 1, 'ssl_key' => 1, 'ssl_cert' => 1,
'proxy' => 1, 'debug' => 1, 'save_to' => 1, 'stream' => 1
'proxy' => 1, 'debug' => 1, 'save_to' => 1, 'stream' => 1, 'expect' => 1
];
if (!$methods) {
@ -165,9 +165,9 @@ class MessageFactory implements MessageFactoryInterface
{
if ($value !== false) {
if ($value === 'strict') {
$request->getConfig()[RedirectPlugin::STRICT_REDIRECTS] = true;
$request->getConfig()[Redirect::STRICT_REDIRECTS] = true;
} elseif (is_int($value)) {
$request->getConfig()[RedirectPlugin::MAX_REDIRECTS] = $value;
$request->getConfig()[Redirect::MAX_REDIRECTS] = $value;
}
$request->getEventDispatcher()->addSubscriber($this->redirectPlugin);
}

View File

@ -4,6 +4,7 @@ namespace Guzzle\Http\Message;
use Guzzle\Common\HasDispatcherTrait;
use Guzzle\Common\Collection;
use Guzzle\Http\Subscriber\PrepareRequestBody;
use Guzzle\Stream\StreamInterface;
use Guzzle\Url\Url;
@ -187,7 +188,7 @@ class Request implements RequestInterface
{
static $subscriber;
if (!$subscriber) {
$subscriber = new PrepareRequestBodySubscriber();
$subscriber = new PrepareRequestBody();
}
$this->getEventDispatcher()->addSubscriber($subscriber);

View File

@ -1,6 +1,6 @@
<?php
namespace Guzzle\Http;
namespace Guzzle\Http\Subscriber;
use Guzzle\Http\Event\RequestAfterSendEvent;
use Guzzle\Http\Exception\RequestException;
@ -9,7 +9,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Throws exceptions when a 4xx or 5xx response is received
*/
class HttpErrorPlugin implements EventSubscriberInterface
class HttpError implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
@ -27,7 +27,7 @@ class HttpErrorPlugin implements EventSubscriberInterface
$code = (string) $event->getResponse()->getStatusCode();
// Throw an exception for an unsuccessful response
if ($code[0] === '4' || $code[0] === '5') {
$event->intercept(RequestException::create($event->getRequest(), $event->getResponse()));
throw RequestException::create($event->getRequest(), $event->getResponse());
}
}
}

View File

@ -1,7 +1,8 @@
<?php
namespace Guzzle\Http\Message;
namespace Guzzle\Http\Subscriber;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Event\RequestBeforeSendEvent;
use Guzzle\Http\Message\Post\PostBodyInterface;
use Guzzle\Stream\StreamInterface;
@ -10,7 +11,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Prepares requests with a body before sending
*/
class PrepareRequestBodySubscriber implements EventSubscriberInterface
class PrepareRequestBody implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
@ -46,7 +47,7 @@ class PrepareRequestBodySubscriber implements EventSubscriberInterface
$addExpect = false;
if (null !== ($expect = $request->getConfig()['expect'])) {
$size = $body->getSize();
$addExpect = $size === null ? true : $size > $expect;
$addExpect = $size === null ? true : $size >= (int) $expect;
} elseif (!$body->isSeekable()) {
// Always add the Expect 100-Continue header if the body cannot be rewound
$addExpect = true;

View File

@ -1,6 +1,6 @@
<?php
namespace Guzzle\Http;
namespace Guzzle\Http\Subscriber;
use Guzzle\Http\Event\RequestAfterSendEvent;
use Guzzle\Http\Exception\TooManyRedirectsException;
@ -13,7 +13,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Plugin to implement HTTP redirects. Can redirect like a web browser or using strict RFC 2616 compliance
*/
class RedirectPlugin implements EventSubscriberInterface
class Redirect implements EventSubscriberInterface
{
const STRICT_REDIRECTS = 'strict_redirects';
const MAX_REDIRECTS = 'max_redirects';

View File

@ -6,7 +6,7 @@ use Guzzle\Http\Client;
use Guzzle\Http\Event\RequestEvents;
use Guzzle\Http\Message\Response;
use Guzzle\Http\Message\MessageFactory;
use Guzzle\Http\RedirectPlugin;
use Guzzle\Http\Subscriber\Redirect;
use Guzzle\Plugin\Mock\MockPlugin;
use Guzzle\Stream\Stream;
@ -122,13 +122,13 @@ class MessageFactoryTest extends \PHPUnit_Framework_TestCase
public function testCanEnableStrictRedirects()
{
$request = (new MessageFactory)->createRequest('GET', '/', [], null, ['allow_redirects' => 'strict']);
$this->assertTrue($request->getConfig()->get(RedirectPlugin::STRICT_REDIRECTS));
$this->assertTrue($request->getConfig()->get(Redirect::STRICT_REDIRECTS));
}
public function testCanEnableStrictRedirectsWithInt()
{
$request = (new MessageFactory)->createRequest('GET', '/', [], null, ['allow_redirects' => 10]);
$this->assertEquals(10, $request->getConfig()->get(RedirectPlugin::MAX_REDIRECTS));
$this->assertEquals(10, $request->getConfig()->get(Redirect::MAX_REDIRECTS));
}
public function testCanAddCookies()

View File

@ -0,0 +1,61 @@
<?php
namespace Guzzle\Tests\Http\Message;
use Guzzle\Http\Event\RequestAfterSendEvent;
use Guzzle\Http\Message\Response;
use Guzzle\Http\Subscriber\HttpError;
use Guzzle\Http\Adapter\Transaction;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Client;
use Guzzle\Plugin\Mock\MockPlugin;
/**
* @covers Guzzle\Http\Subscriber\HttpError
*/
class HttpErrorTest extends \PHPUnit_Framework_TestCase
{
public function testIgnoreSuccessfulRequests()
{
$event = $this->getEvent();
$event->intercept(new Response(200));
(new HttpError())->onRequestAfterSend($event);
}
/**
* @expectedException \Guzzle\Http\Exception\ClientErrorResponseException
*/
public function testThrowsClientExceptionOnFailure()
{
$event = $this->getEvent();
$event->intercept(new Response(403));
(new HttpError())->onRequestAfterSend($event);
}
/**
* @expectedException \Guzzle\Http\Exception\ServerErrorResponseException
*/
public function testThrowsServerExceptionOnFailure()
{
$event = $this->getEvent();
$event->intercept(new Response(500));
(new HttpError())->onRequestAfterSend($event);
}
private function getEvent()
{
return new RequestAfterSendEvent(new Transaction(new Client(), new Request('PUT', '/')));
}
/**
* @expectedException \Guzzle\Http\Exception\ClientErrorResponseException
*/
public function testFullTransaction()
{
$client = new Client();
$client->getEventDispatcher()->addSubscriber(new MockPlugin([
new Response(403)
]));
$client->get('/');
}
}

View File

@ -0,0 +1,92 @@
<?php
namespace Guzzle\Tests\Http\Message;
use Guzzle\Http\Adapter\Transaction;
use Guzzle\Http\Client;
use Guzzle\Http\Event\RequestBeforeSendEvent;
use Guzzle\Http\Subscriber\PrepareRequestBody;
use Guzzle\Http\Message\Request;
use Guzzle\Stream\NoSeekStream;
use Guzzle\Stream\Stream;
/**
* @covers Guzzle\Http\Subscriber\PrepareRequestBody
*/
class PrepareRequestBodyTest extends \PHPUnit_Framework_TestCase
{
public function testIgnoresRequestsWithNoBody()
{
$s = new PrepareRequestBody();
$t = $this->getTrans();
$s->onRequestBeforeSend(new RequestBeforeSendEvent($t));
$this->assertFalse($t->getRequest()->hasHeader('Expect'));
}
public function testAppliesPostBody()
{
$s = new PrepareRequestBody();
$t = $this->getTrans();
$p = $this->getMockBuilder('Guzzle\Http\Message\Post\PostBody')
->setMethods(['applyRequestHeaders'])
->getMockForAbstractClass();
$p->expects($this->once())
->method('applyRequestHeaders');
$t->getRequest()->setBody($p);
$s->onRequestBeforeSend(new RequestBeforeSendEvent($t));
}
public function testAddsExpectHeaderWithTrue()
{
$s = new PrepareRequestBody();
$t = $this->getTrans();
$t->getRequest()->getConfig()->set('expect', true);
$t->getRequest()->setBody(Stream::factory('foo', true));
$s->onRequestBeforeSend(new RequestBeforeSendEvent($t));
$this->assertEquals('100-Continue', $t->getRequest()->getHeader('Expect'));
}
public function testAddsExpectHeaderBySize()
{
$s = new PrepareRequestBody();
$t = $this->getTrans();
$t->getRequest()->getConfig()->set('expect', 2);
$t->getRequest()->setBody(Stream::factory('foo', true));
$s->onRequestBeforeSend(new RequestBeforeSendEvent($t));
$this->assertTrue($t->getRequest()->hasHeader('Expect'));
}
public function testDoesNotAddExpectHeaderBySize()
{
$s = new PrepareRequestBody();
$t = $this->getTrans();
$t->getRequest()->getConfig()->set('expect', 10);
$t->getRequest()->setBody(Stream::factory('foo', true));
$s->onRequestBeforeSend(new RequestBeforeSendEvent($t));
$this->assertFalse($t->getRequest()->hasHeader('Expect'));
}
public function testAddsExpectHeaderForNonSeekable()
{
$s = new PrepareRequestBody();
$t = $this->getTrans();
$t->getRequest()->setBody(new NoSeekStream(Stream::factory('foo', true)));
$s->onRequestBeforeSend(new RequestBeforeSendEvent($t));
$this->assertTrue($t->getRequest()->hasHeader('Expect'));
}
public function testRemovesContentLengthWhenSendingWithChunked()
{
$s = new PrepareRequestBody();
$t = $this->getTrans();
$t->getRequest()->setBody(Stream::factory('foo', true));
$t->getRequest()->setHeader('Transfer-Encoding', 'chunked');
$s->onRequestBeforeSend(new RequestBeforeSendEvent($t));
$this->assertFalse($t->getRequest()->hasHeader('Content-Length'));
}
private function getTrans()
{
return new Transaction(new Client(), new Request('PUT', '/'));
}
}

View File

@ -3,16 +3,13 @@
namespace Guzzle\Tests\Plugin\Redirect;
use Guzzle\Http\Client;
use Guzzle\Http\RedirectPlugin;
use Guzzle\Http\Exception\TooManyRedirectsException;
use Guzzle\Plugin\History\HistoryPlugin;
use Guzzle\Plugin\Mock\MockPlugin;
use Guzzle\Stream\Stream;
/**
* @covers Guzzle\Http\RedirectPlugin
* @covers Guzzle\Http\Subscriber\Redirect
*/
class RedirectPluginTest extends \PHPUnit_Framework_TestCase
class RedirectTest extends \PHPUnit_Framework_TestCase
{
public function testRedirectsRequests()
{