/** * @covers Guzzle\Guzzle::getHttpDate */ public function testGetHttpDate() { $fmt = 'D, d M Y H:i:s \\G\\M\\T'; $this->assertEquals(gmdate($fmt), Guzzle::getHttpDate('now')); $this->assertEquals(gmdate($fmt), Guzzle::getHttpDate(strtotime('now'))); $this->assertEquals(gmdate($fmt, strtotime('+1 day')), Guzzle::getHttpDate('+1 day')); }
/** * @covers Guzzle\Aws\S3\Command\Object\CopyObject */ public function testCopyObject() { $command = new \Guzzle\Aws\S3\Command\Object\CopyObject(); $command->setBucket('test')->setKey('key'); $this->assertSame($command, $command->setCopySource('source_bucket', 'source_key')); $this->assertSame($command, $command->setAcl(\Guzzle\Aws\S3\S3Client::ACL_PUBLIC_READ)); $this->assertSame($command, $command->setStorageClass('STANDARD')); $this->assertSame($command, $command->setMetadataDirective('COPY')); $this->assertSame($command, $command->setCopySourceIfMatch('match_etag')); $this->assertSame($command, $command->setCopySourceIfNoneMatch('none_match_etag')); $this->assertSame($command, $command->setCopySourceIfModifiedSince('now')); $this->assertSame($command, $command->setCopySourceIfUnmodifiedSince('now')); $client = $this->getServiceBuilder()->get('test.s3'); $this->setMockResponse($client, 'CopyObjectResponse'); $client->execute($command); $request = (string) $command->getRequest(); $this->assertEquals('http://test.s3.amazonaws.com/key', $command->getRequest()->getUrl()); $this->assertEquals('PUT', $command->getRequest()->getMethod()); $this->assertFalse($this->compareHttpHeaders(array('Host' => 'test.s3.amazonaws.com', 'Date' => '*', 'Content-Length' => '4', 'Content-MD5' => '8d777f385d3dfec8815d20f7496026dc', 'Authorization' => '*', 'x-amz-test' => '123', 'x-amz-acl' => 'public-read', 'x-amz-storage-class' => 'STANDARD', 'x-amz-copy-source' => '/source_bucket/source_key', 'x-amz-metadata-directive' => 'COPY', 'x-amz-copy-source-if-match' => 'match_etag', 'x-amz-copy-source-if-none-match' => 'none_match_etag', 'x-amz-copy-source-if-modified-since' => Guzzle::getHttpDate('now'), 'x-amz-copy-source-if-unmodified-since' => Guzzle::getHttpDate('now'), 'User-Agent' => Guzzle::getDefaultUserAgent()), $command->getRequestHeaders()->getAll())); }
/** * @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' => Guzzle::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()); }
/** * {@inheritdoc} */ public function createRequest($method = RequestInterface::GET, $uri = null, $inject = null) { $request = parent::createRequest($method, $uri, $inject); $request->setHeader('Date', Guzzle::getHttpDate('now'))->setHeader('Host', $request->getHost()); return $request; }
/** * @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')); }
/** * Copies the object if it has been modified since the specified time; * otherwise, the request returns a 412 HTTP status code error (failed * condition). * * @param string $date The HTTP date to use * * @return CopyObject */ public function setCopySourceIfModifiedSince($date) { return $this->set('copy_source_if_modified_since', Guzzle::getHttpDate($date)); }
/** * Save data to the cache adapter * * @param string $key The cache key * @param Response $response The response to cache * @param int $lifetime (optional) 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', Guzzle::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; }
/** * 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' => Guzzle::getHttpDate('+1 day')))->save(array('domain' => '.example.com', 'path' => '/test/acme/', 'cookie' => array('googoo', 'gaga'), 'max_age' => 1500, 'version' => 2)); }
/** * Return the object only if it has not been modified since the specified * time, otherwise return a 412 (precondition failed). * * @param string $since * * @return GetObject */ public function setIfUnmodifiedSince($since) { return $this->set('if_unmodified_since', Guzzle::getHttpDate($since)); }
/** * @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' => Guzzle::getHttpDate('+1 day')))->getMaxAge()); // Uses the Expires date $this->assertGreaterThanOrEqual(82400, $this->getResponse(200, array('Expires' => Guzzle::getHttpDate('+1 day')))->getMaxAge()); // Uses the Expires date $this->assertGreaterThanOrEqual(82400, $this->getResponse(200, array('Expires' => Guzzle::getHttpDate('+1 day')))->getMaxAge()); }
/** * 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: " . Guzzle::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: " . Guzzle::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: " . Guzzle::getHttpDate('now') . "\r\n\r\nDatas"), array(false, "\r\n\r\n", "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\nDate: " . Guzzle::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: " . Guzzle::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: " . Guzzle::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: " . Guzzle::getHttpDate('-10 hours') . "\r\n\r\nData", "HTTP/1.1 304 NOT MODIFIED\r\nETag: \"123456\"\r\n\r\n")); }