示例#1
0
文件: UtilsTest.php 项目: norv/guzzle
 /**
  * @covers Guzzle\Http\Utils::getHttpDate
  */
 public function testGetHttpDate()
 {
     $fmt = 'D, d M Y H:i:s \\G\\M\\T';
     $this->assertEquals(gmdate($fmt), Utils::getHttpDate('now'));
     $this->assertEquals(gmdate($fmt), Utils::getHttpDate(strtotime('now')));
     $this->assertEquals(gmdate($fmt, strtotime('+1 day')), Utils::getHttpDate('+1 day'));
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function cache($key, Response $response, $ttl = null)
 {
     if ($ttl === null) {
         $ttl = $this->defaultTtl;
     }
     if ($ttl) {
         $response->setHeader('X-Guzzle-Cache', "key={$key}, ttl={$ttl}");
         // Remove excluded headers from the response  (see RFC 2616:13.5.1)
         foreach ($this->excludeResponseHeaders as $header) {
             $response->removeHeader($header);
         }
         // Add a Date header to the response if none is set (for validation)
         if (!$response->getDate()) {
             $response->setHeader('Date', Utils::getHttpDate('now'));
         }
         $this->cache->save($key, array($response->getStatusCode(), $response->getHeaders()->getAll(), $response->getBody(true)), $ttl);
     }
 }
示例#3
0
 /**
  * Save data to the cache adapter
  *
  * @param string   $key      The cache key
  * @param Response $response The response to cache
  * @param int      $lifetime Amount of seconds to cache
  *
  * @return int Returns the lifetime of the cached data
  */
 protected function saveCache($key, Response $response, $lifetime = null)
 {
     $lifetime = $lifetime ?: $this->defaultLifetime;
     // If the data is cacheable, then save it to the cache adapter
     if ($lifetime) {
         // Remove excluded headers from the response  (see RFC 2616:13.5.1)
         foreach ($this->excludeResponseHeaders as $header) {
             $response->removeHeader($header);
         }
         // Add a Date header to the response if none is set (for validation)
         if (!$response->getDate()) {
             $response->setHeader('Date', Utils::getHttpDate('now'));
         }
         $data = array('c' => $response->getStatusCode(), 'h' => $response->getHeaders(), 'b' => $response->getBody(true));
         if ($this->serialize) {
             $data = serialize($data);
         }
         $this->getCacheAdapter()->save($key, $data, $lifetime);
     }
     return $lifetime;
 }
示例#4
0
 /**
  * @covers Guzzle\Http\Message\Response::getMaxAge
  */
 public function testDeterminesResponseMaxAge()
 {
     $this->assertEquals(null, $this->getResponse(200)->getMaxAge());
     // Uses the response's s-maxage
     $this->assertEquals(140, $this->getResponse(200, array('Cache-Control' => 's-maxage=140'))->getMaxAge());
     // Uses the response's max-age
     $this->assertEquals(120, $this->getResponse(200, array('Cache-Control' => 'max-age=120'))->getMaxAge());
     // Uses the response's max-age
     $this->assertEquals(120, $this->getResponse(200, array('Cache-Control' => 'max-age=120', 'Expires' => Utils::getHttpDate('+1 day')))->getMaxAge());
     // Uses the Expires date
     $this->assertGreaterThanOrEqual(82400, $this->getResponse(200, array('Expires' => Utils::getHttpDate('+1 day')))->getMaxAge());
     // Uses the Expires date
     $this->assertGreaterThanOrEqual(82400, $this->getResponse(200, array('Expires' => Utils::getHttpDate('+1 day')))->getMaxAge());
 }
示例#5
0
 /**
  * @covers Guzzle\Http\CookieJar\ArrayCookieJar
  */
 public function testClearsExpiredCookies()
 {
     self::addCookies($this->jar);
     $this->assertEquals(0, $this->jar->deleteExpired());
     // Add an expired cookie
     $this->jar->save(array('cookie' => array('data', 'abc'), 'expires' => Utils::getHttpDate('-1 day'), 'domain' => '.example.com'));
     // Filters out expired cookies
     $this->hasCookies($this->jar->getCookies(), array('foo', 'baz', 'test', 'muppet', 'googoo'));
     $this->assertEquals(1, $this->jar->deleteExpired());
     $this->assertEquals(0, $this->jar->deleteExpired());
 }
示例#6
0
 /**
  * Data provider to test cache revalidation
  *
  * @return array
  */
 public function cacheRevalidationDataProvider()
 {
     return array(array(true, "Pragma: no-cache\r\n\r\n", "HTTP/1.1 200 OK\r\nDate: " . Utils::getHttpDate('-100 hours') . "\r\nContent-Length: 4\r\n\r\nData", "HTTP/1.1 304 NOT MODIFIED\r\nCache-Control: max-age=2000000\r\nContent-Length: 0\r\n\r\n"), array(false, "\r\n\r\n", "HTTP/1.1 200 OK\r\nCache-Control: must-revalidate, no-cache\r\nDate: " . Utils::getHttpDate('-10 hours') . "\r\nContent-Length: 4\r\n\r\nData", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nDatas", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\nDate: " . Utils::getHttpDate('now') . "\r\n\r\nDatas"), array(false, "\r\n\r\n", "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\nDate: " . Utils::getHttpDate('-3 hours') . "\r\nContent-Length: 4\r\n\r\nData", null, null, 'never'), array(true, "\r\n\r\n", "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\nDate: " . Utils::getHttpDate('-3 hours') . "\r\nContent-Length: 4\r\n\r\nData", null, null, 'skip'), array(false, "\r\n\r\n", "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\nDate: " . Utils::getHttpDate('-3 hours') . "\r\n\r\nData", "HTTP/1.1 500 INTERNAL SERVER ERROR\r\nContent-Length: 0\r\n\r\n"), array(false, "\r\n\r\n", "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\nETag: \"123\"\r\nDate: " . Utils::getHttpDate('-10 hours') . "\r\n\r\nData", "HTTP/1.1 304 NOT MODIFIED\r\nETag: \"123456\"\r\n\r\n"));
 }
 private function setup_basic_payload()
 {
     $payload = json_encode(array('Thing' => 'Something'));
     $server = $this->getServer();
     $server->enqueue(array("HTTP/1.1 200 OK\r\n" . "Date: " . Utils::getHttpDate('now') . "\r\n" . "Last-Modified: " . Utils::getHttpDate('-1 hours') . "\r\n" . "Cache-Control: max-age=600 \r\n" . "Content-Length: " . strlen($payload) . "\r\n\r\n" . $payload));
 }
示例#8
0
 /**
  * @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' => Utils::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'));
 }
示例#9
0
 /**
  * Add values to the cookiejar
  */
 protected function addCookies()
 {
     $this->jar->save(array('cookie' => array('foo', 'bar'), 'domain' => 'example.com', 'path' => '/', 'max_age' => '86400', 'port' => array(80, 8080), 'version' => '1', 'secure' => true))->save(array('cookie' => array('test', '123'), 'domain' => 'www.foobar.com', 'path' => '/path/', 'discard' => true))->save(array('domain' => '.y.example.com', 'path' => '/acme/', 'cookie' => array('muppet', 'cookie_monster'), 'comment' => 'Comment goes here...', 'expires' => Utils::getHttpDate('+1 day')))->save(array('domain' => '.example.com', 'path' => '/test/acme/', 'cookie' => array('googoo', 'gaga'), 'max_age' => 1500, 'version' => 2));
 }