public function testCookiesAreNotAddedWhenParamIsSet() { $jar = new ArrayCookieJar(); $plugin = new CookiePlugin($jar); $jar->add(new Cookie(array('domain' => 'example.com', 'path' => '/', 'name' => 'test', 'value' => 'hi', 'expires' => time() + 3600))); $client = new Client('http://example.com'); $client->getEventDispatcher()->addSubscriber($plugin); // Ensure that it is normally added $request = $client->get(); $request->setResponse(new Response(200), true); $request->send(); $this->assertEquals('hi', $request->getCookie('test')); // Now ensure that it is not added $request = $client->get(); $request->getParams()->set('cookies.disable', true); $request->setResponse(new Response(200), true); $request->send(); $this->assertNull($request->getCookie('test')); }
/** * @dataProvider getMatchingCookiesDataProvider */ public function testReturnsCookiesMatchingRequests($url, $cookies) { $bag = array(new Cookie(array('name' => 'foo', 'value' => 'bar', 'domain' => 'example.com', 'path' => '/', 'max_age' => '86400', 'port' => array(443, 8080), 'version' => '1', 'secure' => true)), new Cookie(array('name' => 'baz', 'value' => 'foobar', 'domain' => 'example.com', 'path' => '/', 'max_age' => '86400', 'port' => array(80, 8080), 'version' => '1', 'secure' => true)), new Cookie(array('name' => 'test', 'value' => '123', 'domain' => 'www.foobar.com', 'path' => '/path/', 'discard' => true)), new Cookie(array('name' => 'muppet', 'value' => 'cookie_monster', 'domain' => '.y.example.com', 'path' => '/acme/', 'comment' => 'Comment goes here...', 'expires' => time() + 86400)), new Cookie(array('name' => 'googoo', 'value' => 'gaga', 'domain' => '.example.com', 'path' => '/test/acme/', 'max_age' => 1500, 'version' => 2))); foreach ($bag as $cookie) { $this->jar->add($cookie); } $request = new Request('GET', $url); $results = $this->jar->getMatchingCookies($request); $this->assertEquals(count($results), count($cookies)); foreach ($cookies as $i) { $this->assertContains($bag[$i], $results); } }
/** * @covers Guzzle\Http\Plugin\CookiePlugin::onRequestBeforeSend */ public function testCookiesAreNotAddedWhenParamIsSet() { $this->storage->clear(); $this->storage->save(array('domain' => 'example.com', 'path' => '/', 'cookie' => array('test', 'hi'), 'expires' => Guzzle::getHttpDate('+1 day'))); $client = new Client('http://example.com'); $client->getEventDispatcher()->addSubscriber($this->plugin); $request = $client->get(); $request->setResponse(new Response(200), true); $request->send(); $this->assertEquals('hi', $request->getCookie()->get('test')); $request = $client->get(); $request->getParams()->set('cookies.disable', true); $request->setResponse(new Response(200), true); $request->send(); $this->assertNull($request->getCookie()->get('test')); }
/** * @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']); }