1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-01-18 05:48:15 +01:00

Cleaning up and updating CookiePlugin for new redirect support. Closes #149.

This commit is contained in:
Michael Dowling 2012-10-30 21:26:48 -07:00
parent 5bb36b81c2
commit 464a059d09
5 changed files with 12 additions and 37 deletions

View File

@ -28,6 +28,8 @@
<file>./src/Guzzle/Http/Exception/HttpException.php</file>
<file>./src/Guzzle/Http/Exception/ServerErrorResponseException.php</file>
<file>./src/Guzzle/Http/Exception/ClientErrorResponseException.php</file>
<file>./src/Guzzle/Http/Exception/TooManyRedirectsException.php</file>
<file>./src/Guzzle/Http/Exception/CouldNotRewindStreamException.php</file>
<file>./src/Guzzle/Common/Exception/BadMethodCallException.php</file>
<file>./src/Guzzle/Common/Exception/InvalidArgumentException.php</file>
<file>./src/Guzzle/Common/Exception/RuntimeException.php</file>

View File

@ -495,23 +495,15 @@ class Request extends AbstractMessage implements RequestInterface
// Only download the body of the response to the specified response
// body when a successful response is received.
if ($code >= 200 && $code < 300) {
$body = $this->getResponseBody();
} else {
$body = EntityBody::factory();
}
$body = $code >= 200 && $code < 300 ? $this->getResponseBody() : EntityBody::factory();
$previousResponse = $this->response;
$this->response = new Response($code, null, $body);
if ($previousResponse) {
$this->response->setPreviousResponse($previousResponse);
}
$this->response->setStatus($code, $status)->setRequest($this);
$this->dispatch('request.receive.status_line', array(
'line' => $data,
'status_code' => $code,
'reason_phrase' => $status,
'previous_response' => $previousResponse
'request' => $this,
'line' => $data,
'status_code' => $code,
'reason_phrase' => $status
));
} elseif (strpos($data, ':') !== false) {

View File

@ -31,7 +31,7 @@ class RedirectPlugin implements EventSubscriberInterface
public static function getSubscribedEvents()
{
return array(
'request.sent' => 'onRequestSent',
'request.sent' => array('onRequestSent', 100),
'request.clone' => 'onRequestClone'
);
}

View File

@ -30,9 +30,8 @@ class CookiePlugin implements EventSubscriberInterface
public static function getSubscribedEvents()
{
return array(
'request.before_send' => array('onRequestBeforeSend', 100),
'request.sent' => array('onRequestSent', 100),
'request.receive.status_line' => 'onRequestReceiveStatusLine'
'request.before_send' => array('onRequestBeforeSend', 125),
'request.sent' => array('onRequestSent', 125)
);
}
@ -72,16 +71,4 @@ class CookiePlugin implements EventSubscriberInterface
{
$this->cookieJar->addCookiesFromResponse($event['response']);
}
/**
* Extract cookies from a redirect response
*
* @param Event $event
*/
public function onRequestReceiveStatusLine(Event $event)
{
if ($event['previous_response']) {
$this->cookieJar->addCookiesFromResponse($event['previous_response']);
}
}
}

View File

@ -63,14 +63,13 @@ class CookiePluginTest extends \Guzzle\Tests\GuzzleTestCase
public function testCookiesAreExtractedFromRedirectResponses()
{
$plugin = new CookiePlugin(new ArrayCookieJar());
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 302 Moved Temporarily\r\n" .
"Set-Cookie: test=583551; expires=Wednesday, 23-Mar-2050 19:49:45 GMT; path=/\r\n" .
"Location: /redirect\r\n\r\n",
"HTTP/1.1 200 OK\r\n" .
"Content-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\n" .
"Content-Length: 0\r\n\r\n"
));
@ -78,19 +77,14 @@ class CookiePluginTest extends \Guzzle\Tests\GuzzleTestCase
$client = new Client($this->getServer()->getUrl());
$client->getEventDispatcher()->addSubscriber($plugin);
$client->get()->send();
$request = $client->get();
$request->send();
$request = $client->get();
$request->send();
$this->assertEquals('test=583551', $request->getHeader('Cookie'));
$requests = $this->getServer()->getReceivedRequests(true);
// Confirm subsequent requests have the cookie.
$this->assertEquals('test=583551', $requests[2]->getHeader('Cookie'));
// Confirm the redirected request has the cookie.
$this->assertEquals('test=583551', $requests[1]->getHeader('Cookie'));
}