/** * @covers Guzzle\Http\Plugin\CookiePlugin */ public function testExtractsAndStoresCookies() { $cookie = array(array('domain' => '.example.com', 'path' => '/', 'max_age' => '86400', 'expires' => time() + 86400, 'version' => '1', 'secure' => true, 'port' => array('80', '8081'), 'discard' => true, 'comment' => NULL, 'comment_url' => NULL, 'http_only' => false, 'data' => array(), 'cookie' => array('a', 'b')), array('domain' => '.example.com', 'path' => '/', 'max_age' => '86400', 'expires' => time() + 86400, 'version' => '1', 'secure' => true, 'port' => array('80', '8081'), 'discard' => true, 'comment' => NULL, 'comment_url' => NULL, 'http_only' => false, 'data' => array(), 'cookie' => array('c', 'd'))); $response = Response::fromMessage("HTTP/1.1 200 OK\r\nSet-Cookie: a=b; c=d; port=\"80,8081\"; version=1; Max-Age=86400; domain=.example.com; discard; secure;\r\n\r\n"); $result = $this->plugin->extractCookies($response); $this->assertTrue(abs($result[0]['expires'] - $cookie[0]['expires']) < 10, 'Cookie #1 expires dates are too different: ' . $result[0]['expires'] . ' vs ' . $cookie[0]['expires']); $this->assertTrue(abs($result[1]['expires'] - $cookie[1]['expires']) < 10, 'Cookie #2 expires dates are too different: ' . $result[1]['expires'] . ' vs ' . $cookie[1]['expires']); unset($cookie[0]['expires']); unset($cookie[1]['expires']); unset($result[0]['expires']); unset($result[1]['expires']); $this->assertEquals($cookie, $result); $this->assertEquals(2, count($this->storage->getCookies())); $result = $this->storage->getCookies(); unset($result[0]['expires']); unset($result[1]['expires']); $this->assertEquals($cookie, $result); // Clear out the currently held cookies $this->assertEquals(2, $this->storage->clear()); $this->assertEquals(0, count($this->storage->getCookies())); // Create a new request, attach the cookie plugin, set a mock response $request = new Request('GET', 'http://www.example.com/'); $request->setClient(new Client()); $request->getEventDispatcher()->addSubscriber($this->plugin); $request->setResponse($response, true); $request->send(); // Assert that the plugin caught the cookies in the response $this->assertEquals(2, count($this->storage->getCookies())); }
/** * @covers Guzzle\Http\CookieJar\ArrayCookieJar */ public function testOverwritesCookiesThatAreOlderOrDiscardable() { $this->jar->clear(); $t = time() + 1000; $data = array('cookie' => array('foo', 'bar'), 'domain' => '.example.com', 'path' => '/', 'max_age' => '86400', 'port' => array(80, 8080), 'version' => '1', 'secure' => true, 'discard' => true, 'expires' => $t); // Make sure that the discard cookie is overridden with the non-discard $this->jar->save($data); unset($data['discard']); $this->jar->save($data); $this->assertEquals(1, count($this->jar->getCookies())); $c = $this->jar->getCookies(); $this->assertEquals(false, $c[0]['discard']); $this->assertEquals($t, $c[0]['expires']); // Make sure it doesn't duplicate the cookie $this->jar->save($data); $this->assertEquals(1, count($this->jar->getCookies())); // Make sure the more future-ful expiration date supercedes the other $data['expires'] = time() + 2000; $this->jar->save($data); $this->assertEquals(1, count($this->jar->getCookies())); $c = $this->jar->getCookies(); $this->assertNotEquals($t, $c[0]['expires']); }