Permalink
Browse files

Cleaning up and updating CookiePlugin for new redirect support. Closes

  • Loading branch information...
1 parent 5bb36b8 commit 464a059d09c9eb00ac6a590590052966ebf19b42 @mtdowling mtdowling committed Oct 31, 2012
View
@@ -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>
@@ -495,23 +495,15 @@ public function receiveResponseHeader($data)
// 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) {
@@ -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'
);
}
@@ -30,9 +30,8 @@ public function __construct(CookieJarInterface $cookieJar)
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 @@ public function onRequestSent(Event $event)
{
$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']);
- }
- }
}
@@ -63,34 +63,28 @@ public function testAddsCookiesToRequests()
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"
));
$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'));
}

0 comments on commit 464a059

Please sign in to comment.