.gitignore000064400000000357151742734600006552 0ustar00# Ingore common cruft .DS_STORE coverage # Ignore binary files guzzle.phar guzzle-min.phar # Ignore potentially sensitive phpunit file phpunit.xml # Ignore composer generated files composer.phar composer.lock composer-test.lock vendor/ .travis.yml000064400000001321151742734600006663 0ustar00language: php php: - 5.3 - 5.4 before_script: - curl --version - echo 'Installing pecl_http' - wget --quiet http://pecl.php.net/get/pecl_http-1.7.4.tgz - tar -xzf pecl_http-1.7.4.tgz - sh -c "cd pecl_http-1.7.4 && phpize && ./configure && make && sudo make install" - echo "extension=http.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` - echo 'Installing composer' - wget --quiet http://getcomposer.org/composer.phar - php composer.phar install --dev - cp phpunit.xml.dist phpunit.xml - echo 'Ensuring the correct version of node is running' - ~/.nvm/nvm.sh install v0.6.14 - ~/.nvm/nvm.sh run v0.6.14 tests/Guzzle/Tests/Http/server.js & script: sudo phpunit --debug CHANGELOG.md000064400000010104151742734600006362 0ustar00CHANGELOG ========= * 2.6.1 (05-19-2012) * [BC] Removing 'path' support in service descriptions. Use 'uri'. * [BC] Guzzle\Service\Inspector::parseDocBlock is now protected. Adding getApiParamsForClass() with cache. * [BC] Removing Guzzle\Common\NullObject. Use https://github.com/mtdowling/NullObject if you need it. * [BC] Removing Guzzle\Common\XmlElement. * All commands, both dynamic and concrete, have ApiCommand objects. * Adding a fix for CurlMulti so that if all of the connections encounter some sort of curl error, then the loop exits. * Adding checks to EntityEnclosingRequest so that empty POST files and fields are ignored. * Making the method signature of Guzzle\Service\Builder\ServiceBuilder::factory more flexible. * 2.6.0 (05-15-2012) * [BC] Moving Guzzle\Service\Builder to Guzzle\Service\Builder\ServiceBuilder * [BC] Executing a Command returns the result of the command rather than the command * [BC] Moving all HTTP parsing logic to Guzzle\Http\Parsers. Allows for faster C implementations if needed. * [BC] Changing the Guzzle\Http\Message\Response::setProtocol() method to accept a protocol and version in separate args. * [BC] Moving ResourceIterator* to Guzzle\Service\Resource * [BC] Completely refactored ResourceIterators to iterate over a cloned command object * [BC] Moved Guzzle\Http\UriTemplate to Guzzle\Http\Parser\UriTemplate\UriTemplate * [BC] Guzzle\Guzzle is now deprecated * Moving Guzzle\Common\Guzzle::inject to Guzzle\Common\Collection::inject * Adding Guzzle\Version class to give version information about Guzzle * Adding Guzzle\Http\Utils class to provide getDefaultUserAgent() and getHttpDate() * Adding Guzzle\Curl\CurlVersion to manage caching curl_version() data * ServiceDescription and ServiceBuilder are now cacheable using similar configs * Changing the format of XML and JSON service builder configs. Backwards compatible. * Cleaned up Cookie parsing * Trimming the default Guzzle User-Agent header * Adding a setOnComplete() method to Commands that is called when a command completes * Keeping track of requests that were mocked in the MockPlugin * Fixed a caching bug in the CacheAdapterFactory * Inspector objects can be injected into a Command object * Refactoring a lot of code and tests to be case insensitive when dealing with headers * Adding Guzzle\Http\Message\HeaderComparison for easy comparison of HTTP headers using a DSL * Adding the ability to set global option overrides to service builder configs * Adding the ability to include other service builder config files from within XML and JSON files * Moving the parseQuery method out of Url and on to QueryString::fromString() as a static factory method. * 2.5.0 (05-08-2012) * Major performance improvements * [BC] Simplifying Guzzle\Common\Collection. Please check to see if you are using features that are now deprecated. * [BC] Using a custom validation system that allows a flyweight implementation for much faster validation. No longer using Symfony2 Validation component. * [BC] No longer supporting "{{ }}" for injecting into command or UriTemplates. Use "{}" * Added the ability to passed parameters to all requests created by a client * Added callback functionality to the ExponentialBackoffPlugin * Using microtime in ExponentialBackoffPlugin to allow more granular backoff stategies. * Rewinding request stream bodies when retrying requests * Exception is thrown when JSON response body cannot be decoded * Added configurable magic method calls to clients and commands. This is off by default. * Fixed a defect that added a hash to every parsed URL part * Fixed duplicate none generation for OauthPlugin. * Emitting an event each time a client is generated by a ServiceBuilder * Using an ApiParams object instead of a Collection for parameters of an ApiCommand * cache.* request parameters should be renamed to params.cache.* * Added the ability to set arbitrary curl options on requests (disable_wire, progress, etc). See CurlHandle. * Added the ability to disable type validation of service descriptions * ServiceDescriptions and ServiceBuilders are now Serializable LICENSE000064400000002127151742734600005564 0ustar00Copyright (c) 2011 Michael Dowling, https://github.com/mtdowling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. README.md000064400000014603151742734600006040 0ustar00Guzzle, PHP HTTP client and webservice framework ================================================ Guzzle is a game changer in the world of PHP HTTP clients. Guzzle allows you to truly reap the benefits of the HTTP/1.1 spec. No other library makes it easier to manage persistent connections or send requests in parallel.

In addition to taking the pain out of HTTP, Guzzle provides a lightweight framework for creating web service clients. Most web service clients follow a specific pattern: create a client class, create methods for each action, create and execute a cURL handle, parse the response, implement error handling, and return the result. Guzzle takes the redundancy out of this process and gives you the tools you need to quickly build a web service client. Start truly consuming HTTP with Guzzle. - [Download the phar](http://guzzlephp.org/guzzle.phar) and include it in your project ([minimal phar](http://guzzlephp.org/guzzle-min.phar)) - Docs: [www.guzzlephp.org](http://www.guzzlephp.org/) - Forum: https://groups.google.com/forum/?hl=en#!forum/guzzle - IRC: [#guzzlephp](irc://irc.freenode.net/#guzzlephp) channel on irc.freenode.net [![Build Status](https://secure.travis-ci.org/guzzle/guzzle.png)](http://travis-ci.org/guzzle/guzzle) Features -------- - Supports GET, HEAD, POST, DELETE, PUT, PATCH, OPTIONS, and any custom verbs - Allows full access to request and response headers - Persistent connections are implicitly managed by Guzzle, resulting in huge performance benefits - Send requests in parallel - Cookie sessions can be maintained between requests using the CookiePlugin - Allows custom entity bodies, including sending data from a PHP stream - Responses can be cached and served from cache using the caching reverse proxy plugin - Failed requests can be retried using truncated exponential backoff - Entity bodies can be validated automatically using Content-MD5 headers - All data sent over the wire can be logged using the LogPlugin - Automatically requests compressed data and automatically decompresses data - Subject/Observer signal slot system for unobtrusively modifying request behavior - Supports all of the features of libcurl including authentication, redirects, SSL, proxies, etc - Web service client framework for building future-proof interfaces to web services - Full support for [URI templates](http://tools.ietf.org/html/draft-gregorio-uritemplate-08) HTTP basics ----------- ```php '***' )); // Issue a path using a relative URL to the client's base URL // Sends to http://www.example.com/api/v1/key/***/users $request = $client->get('users'); $response = $request->send(); // Relative URL that overwrites the path of the base URL $request = $client->get('/test/123.php?a=b'); // Issue a head request on the base URL $response = $client->head()->send(); // Delete user 123 $response = $client->delete('users/123')->send(); // Send a PUT request with custom headers $response = $client->put('upload/text', array( 'X-Header' => 'My Header' ), 'body of the request')->send(); // Send a PUT request using the contents of a PHP stream as the body // Send using an absolute URL (overrides the base URL) $response = $client->put('http://www.example.com/upload', array( 'X-Header' => 'My Header' ), fopen('http://www.test.com/', 'r')); // Create a POST request with a file upload (notice the @ symbol): $request = $client->post('http://localhost:8983/solr/update', null, array ( 'custom_field' => 'my value', 'file' => '@/path/to/documents.xml' )); // Create a POST request and add the POST files manually $request = $client->post('http://localhost:8983/solr/update') ->addPostFiles(array( 'file' => '/path/to/documents.xml' )); // Responses are objects echo $response->getStatusCode() . ' ' . $response->getReasonPhrase() . "\n"; // Requests and responses can be cast to a string to show the raw HTTP message echo $request . "\n\n" . $response; // Create a request based on an HTTP message $request = RequestFactory::fromMessage( "PUT / HTTP/1.1\r\n" . "Host: test.com:8081\r\n" . "Content-Type: text/plain" "Transfer-Encoding: chunked\r\n" . "\r\n" . "this is the body" ); ``` Send requests in parallel ------------------------- ```php send(array( $client->get('users'), $client->head('messages/123'), $client->delete('orders/123') )); } catch (Guzzle\Common\Exception\ExceptionCollection $e) { echo "The following requests encountered an exception: \n"; foreach ($e as $exception) { echo $exception->getRequest() . "\n" . $exception->getMessage() . "\n"; } } ``` URI templates ------------- Guzzle supports the entire URI templates RFC. ```php '/path/to', 'a' => 'hi', 'data' => array( 'foo' => 'bar', 'mesa' => 'jarjar' ) )); $request = $client->get('http://www.test.com{+path}{?a,data*}'); ``` The generated request URL would become: ``http://www.test.com/path/to?a=hi&foo=bar&mesa=jarajar`` You can specify URI templates and an array of additional template variables to use when creating requests: ```php 'hi' )); $request = $client->get(array('/{?a,b}', array( 'b' => 'there' )); ``` The resulting URL would become ``http://test.com?a=hi&b=there`` Testing and contributing to Guzzle ---------------------------------- You will need to clone the Guzzle repository in order to be able to contribute to Guzzle: ``` git clone git@github.com:guzzle/guzzle.git cd guzzle ``` Next you will need to make sure PHPUnit is configured, Composer is installed, and you have installed Guzzle's testing dependencies. This can be acheived very simply by using the `test-init` Phing task. After running this task, simply run PHPUnit. ``` phing test-init phpunit ``` If you do not have Phing installed, you will need to perform the installation steps manually: ``` # Make sure Composer is installed curl -s http://getcomposer.org/installer | php # Install Guzzle's testing dependencies php composer.phar install --dev # Now kick off PHPUnit to run the tests cp phpunit.xml.dist phpunit.xml phpunit ``` build.xml000064400000005243151742734600006402 0ustar00 composer.json000064400000011521151742734600007277 0ustar00{ "name": "guzzle/guzzle", "type": "library", "description": "Guzzle is a PHP HTTP client library and framework for building RESTful web service clients", "keywords": ["framework", "http", "rest", "web service", "curl"], "homepage": "http://www.guzzlephp.org/", "license": "MIT", "authors": [ { "name": "Michael Dowling", "email": "mtdowling@gmail.com", "homepage": "https://github.com/mtdowling" }, { "name": "Guzzle Community", "homepage": "https://github.com/guzzle/guzzle/contributors" } ], "require": { "php": ">=5.3.2", "ext-curl": "*", "symfony/event-dispatcher": "*" }, "autoload": { "psr-0": { "Guzzle\\Tests": "tests/", "Guzzle": "src/" } }, "require-dev": { "doctrine/common": "*", "symfony/class-loader": "*", "monolog/monolog": "*", "zend/zend-cache": "2.0.0beta3", "zend/zend-log": "2.0.0beta3", "zend/zend-loader": "2.0.0beta3", "zend/zend-stdlib": "2.0.0beta3", "zend/zend-eventmanager": "2.0.0beta3", "zend/zend-log1": "1.11", "zend/zend-cache1": "1.11" }, "repositories": [ { "type": "package", "package": { "name": "zend/zend-log", "version": "2.0.0beta3", "dist": { "url": "http://packages.zendframework.com/get/Zend_Log-2.0.0beta3.tgz", "type": "pear" }, "autoload": { "psr-0": { "Zend\\Log": "php/" } } } }, { "type":"package", "package": { "name": "zend/zend-cache", "version": "2.0.0beta3", "dist": { "url": "http://packages.zendframework.com/get/Zend_Cache-2.0.0beta3.tgz", "type": "pear" }, "autoload": { "psr-0": { "Zend\\Cache": "php/" } } } }, { "type":"package", "package": { "name": "zend/zend-loader", "version": "2.0.0beta3", "dist": { "url": "http://packages.zendframework.com/get/Zend_Loader-2.0.0beta3.tgz", "type": "pear" }, "autoload": { "psr-0": { "Zend\\Loader": "php/" } } } }, { "type":"package", "package": { "name": "zend/zend-stdlib", "version": "2.0.0beta3", "dist": { "url": "http://packages.zendframework.com/get/Zend_Stdlib-2.0.0beta3.tgz", "type": "pear" }, "autoload": { "psr-0": { "Zend\\Stdlib": "php/" } } } }, { "type":"package", "package": { "name": "zend/zend-eventmanager", "version": "2.0.0beta3", "dist": { "url": "http://packages.zendframework.com/get/Zend_EventManager-2.0.0beta3.tgz", "type": "pear" }, "autoload": { "psr-0": { "Zend\\EventManager": "php/" } } } }, { "type":"package", "package": { "name": "zend/zend-log1", "version": "1.11", "source": { "url": "http://framework.zend.com/svn", "type": "svn", "reference": "framework/standard/trunk/library/Zend/Log/" }, "target-dir": "Zend/Log", "autoload": { "psr-0": { "Zend_Log": "/" } } } }, { "type":"package", "package": { "name": "zend/zend-cache1", "version": "1.11", "source": { "url": "http://framework.zend.com/svn", "type": "svn", "reference": "framework/standard/trunk/library/Zend/Cache/" }, "target-dir": "Zend/Cache", "autoload": { "psr-0": { "Zend_Cache": "/" } } } } ] } phar-stub-min.php000064400000000031151742734600007746 0ustar00registerNamespaces(array( 'Guzzle' => 'phar://guzzle.phar/src', 'Symfony\\Component\\EventDispatcher' => 'phar://guzzle.phar/vendor/symfony/event-dispatcher', 'Doctrine' => 'phar://guzzle.phar/vendor/doctrine/common/lib', 'Monolog' => 'phar://guzzle.phar/vendor/monolog/monolog/src' )); $classLoader->register(); __HALT_COMPILER(); phpunit.xml.dist000064400000004022151742734600007726 0ustar00 ./tests/Guzzle/Tests ./src/Guzzle ./src/Guzzle ./src/Guzzle/Common/GuzzleException.php ./src/Guzzle/Http/HttpException.php ./src/Guzzle/Http/Exception/ServerErrorResponseException.php ./src/Guzzle/Http/Exception/ClientErrorResponseException.php ./src/Guzzle/Common/Exception/BadMethodCallException.php ./src/Guzzle/Common/Exception/InvalidArgumentException.php ./src/Guzzle/Common/Exception/RuntimeException.php ./src/Guzzle/Common/Exception/UnexpectedValueException.php ./src/Guzzle/Service/Exception/ClientNotFoundException.php ./src/Guzzle/Service/Exception/CommandException.php ./src/Guzzle/Service/Exception/DescriptionBuilderException.php ./src/Guzzle/Service/Exception/ServiceBuilderException.php ./src/Guzzle/Service/Exception/ServiceNotFoundException.php ./src/Guzzle/Service/Exception/ValidationException.php ./src/Guzzle/Service/Exception/JsonException.php src/Guzzle/Common/AbstractHasDispatcher.php000064400000002127151742734600014775 0ustar00eventDispatcher = $eventDispatcher; return $this; } /** * {@inheritdoc} */ public function getEventDispatcher() { if (!$this->eventDispatcher) { $this->eventDispatcher = new EventDispatcher(); } return $this->eventDispatcher; } /** * {@inheritdoc} */ public function dispatch($eventName, array $context = array()) { $this->getEventDispatcher()->dispatch($eventName, new Event($context)); } } src/Guzzle/Common/Cache/AbstractCacheAdapter.php000064400000000516151742734600015562 0ustar00cache; } } src/Guzzle/Common/Cache/CacheAdapterFactory.php000064400000005647151742734600015440 0ustar00newInstanceArgs($args); } } catch (\Exception $e) { throw new RuntimeException($e->getMessage(), $e->getCode(), $e); } } } src/Guzzle/Common/Cache/CacheAdapterInterface.php000064400000003571151742734600015723 0ustar00callables = $callables; } /** * {@inheritdoc} */ public function contains($id, array $options = null) { return call_user_func($this->callables['contains'], $id, $options); } /** * {@inheritdoc} */ public function delete($id, array $options = null) { return call_user_func($this->callables['delete'], $id, $options); } /** * {@inheritdoc} */ public function fetch($id, array $options = null) { return call_user_func($this->callables['fetch'], $id, $options); } /** * {@inheritdoc} */ public function save($id, $data, $lifeTime = false, array $options = null) { return call_user_func($this->callables['save'], $id, $data, $lifeTime, $options); } } src/Guzzle/Common/Cache/DoctrineCacheAdapter.php000064400000002011151742734600015556 0ustar00cache = $cache; } /** * {@inheritdoc} */ public function contains($id, array $options = null) { return $this->cache->contains($id); } /** * {@inheritdoc} */ public function delete($id, array $options = null) { return $this->cache->delete($id); } /** * {@inheritdoc} */ public function fetch($id, array $options = null) { return $this->cache->fetch($id); } /** * {@inheritdoc} */ public function save($id, $data, $lifeTime = false, array $options = null) { return $this->cache->save($id, $data, $lifeTime); } } src/Guzzle/Common/Cache/NullCacheAdapter.php000064400000001471151742734600014732 0ustar00cache = $cache; } /** * {@inheritdoc} */ public function contains($id, array $options = null) { return $this->cache->test($id); } /** * {@inheritdoc} */ public function delete($id, array $options = null) { return $this->cache->remove($id); } /** * {@inheritdoc} */ public function fetch($id, array $options = null) { return $this->cache->load($id); } /** * {@inheritdoc} */ public function save($id, $data, $lifeTime = false, array $options = null) { return $this->cache->save($data, $id, array(), $lifeTime); } } src/Guzzle/Common/Cache/Zf2CacheAdapter.php000064400000004541151742734600014462 0ustar00cache = $cache; $this->defaultOptions = array_merge(array( 'contains' => array(), 'delete' => array(), 'fetch' => array(), 'save' => array() ), $defaultOptions); } /** * {@inheritdoc} */ public function contains($id, array $options = null) { $options = $options ? array_merge($this->defaultOptions['contains'], $options) : $this->defaultOptions['contains']; return $this->cache->hasItem($id, $options); } /** * {@inheritdoc} */ public function delete($id, array $options = null) { $options = $options ? array_merge($this->defaultOptions['delete'], $options) : $this->defaultOptions['delete']; return $this->cache->removeItem($id, $options); } /** * {@inheritdoc} */ public function fetch($id, array $options = null) { $options = $options ? array_merge($this->defaultOptions['fetch'], $options) : $this->defaultOptions['fetch']; return $this->cache->getItem($id, $options); } /** * {@inheritdoc} */ public function save($id, $data, $lifeTime = false, array $options = null) { return $this->cache->setItem($id, $data, array_merge($this->defaultOptions['save'], $options ?: array(), array( 'ttl' => $lifeTime ))); } } src/Guzzle/Common/Collection.php000064400000022530151742734600012662 0ustar00data = array(); } else { $this->data = $data; } } /** * Convert the object to a string * * @return string */ public function __toString() { return __CLASS__ . '@' . spl_object_hash($this); } /** * Add a value to a key. If a key of the same name has * already been added, the key value will be converted into an array * and the new value will be pushed to the end of the array. * * @param string $key Key to add * @param mixed $value Value to add to the key * * @return Collection Returns a reference to the object. */ public function add($key, $value) { if (!array_key_exists($key, $this->data)) { $this->data[$key] = $value; } else { if (!is_array($this->data[$key])) { $this->data[$key] = array($this->data[$key], $value); } else { $this->data[$key][] = $value; } } return $this; } /** * Removes all key value pairs * * @return Collection */ public function clear() { $this->data = array(); return $this; } /** * Return the number of keys * * @return integer */ public function count() { return count($this->data); } /** * Iterates over each key value pair in the collection passing them to the * Closure. If the Closure function returns true, the current value from * input is returned into the result Collection. The Closure must accept * three parameters: (string) $key, (string) $value and * return Boolean TRUE or FALSE for each value. * * @param Closure $closure Closure evaluation function * @param bool $static Set to TRUE to use the same class as the return * rather than returning a Collection object * * @return Collection */ public function filter(\Closure $closure, $static = true) { $collection = ($static) ? new static() : new self(); foreach ($this->data as $key => $value) { if ($closure($key, $value)) { $collection->add($key, $value); } } return $collection; } /** * Get an iterator object * * @return array */ public function getIterator() { return new \ArrayIterator($this->data); } /** * Get a specific key value. * * @param string $key Key to retrieve. * @param mixed $default If the key is not found, set this * value to specify a default * * @return mixed|null Value of the key or NULL */ public function get($key, $default = null) { if (array_key_exists($key, $this->data)) { return $this->data[$key]; } return $default; } /** * Get all or a subset of matching key value pairs * * @param array $keys Pass a single key or an array of keys to retrieve * only a particular subset of key value pair. * * @return array Returns an array of all matching key value pairs */ public function getAll(array $keys = null) { if (!$keys) { return $this->data; } $matches = array(); $allKeys = $this->getKeys(); foreach ($keys as $key) { if (in_array($key, $allKeys)) { $matches[$key] = $this->data[$key]; } } return $matches; } /** * Get all keys in the collection * * @return array */ public function getKeys() { return array_keys($this->data); } /** * Returns whether or not the specified key is present. * * @param string $key The key for which to check the existence. * * @return bool */ public function hasKey($key) { return array_key_exists($key, $this->data); } /** * Case insensitive search the keys in the collection * * @param string $key Key to search for * * @return false|string Returns false if not found, otherwise returns the key */ public function keySearch($key) { foreach (array_keys($this->data) as $k) { if (!strcasecmp($k, $key)) { return $k; } } return false; } /** * Checks if any keys contains a certain value * * @param string $value Value to search for * * @return mixed Returns the key if the value was found FALSE if the value * was not found. */ public function hasValue($value) { return array_search($value, $this->data); } /** * Returns a Collection containing all the elements of the collection after * applying the callback function to each one. The Closure should accept * three parameters: (string) $key, (string) $value, (array) $context and * return a modified value * * @param Closure $closure Closure to apply * @param array $context Context to pass to the closure * @param bool $static Set to TRUE to use the same class as the return * rather than returning a Collection object * * @return Collection */ public function map(\Closure $closure, array $context = array(), $static = true) { $collection = $static ? new static() : new self(); foreach ($this as $key => $value) { $collection->add($key, $closure($key, $value, $context)); } return $collection; } /** * Add and merge in a Collection or array of key value pair data. * * Invalid $data arguments will silently fail. * * @param Collection|array $data Associative array of key value pair data * * @return Collection Returns a reference to the object. */ public function merge($data) { if ($data instanceof self) { $data = $data->getAll(); } elseif (!is_array($data)) { return $this; } if (!empty($data)) { if (empty($this->data)) { $this->data = $data; } else { foreach ($data as $key => $value) { $this->add($key, $value); } } } return $this; } /** * ArrayAccess implementation of offsetExists() * * @param string $offset Array key * * @return bool */ public function offsetExists($offset) { return $this->hasKey($offset) !== false; } /** * ArrayAccess implementation of offsetGet() * * @param string $offset Array key * * @return null|mixed */ public function offsetGet($offset) { return $this->get($offset); } /** * ArrayAccess implementation of offsetGet() * * @param string $offset Array key * @param mixed $value Value to set */ public function offsetSet($offset, $value) { $this->set($offset, $value); } /** * ArrayAccess implementation of offsetUnset() * * @param string $offset Array key */ public function offsetUnset($offset) { $this->remove($offset); } /** * Remove a specific key value pair * * @param string $key A key to remove * * @return Collection */ public function remove($key) { unset($this->data[$key]); return $this; } /** * Replace the data of the object with the value of an array * * @param array $data Associative array of data * * @return Collection Returns a reference to the object */ public function replace(array $data) { $this->data = $data; return $this; } /** * Set a key value pair * * @param string $key Key to set * @param mixed $value Value to set * * @return Collection Returns a reference to the object */ public function set($key, $value) { $this->data[$key] = $value; return $this; } /** * Inject configuration settings into an input string * * @param string $input Input to inject * @param Collection $config Configuration data to inject into the input * * @return string */ public function inject($input) { // Only perform the preg callback if needed if (strpos($input, '{') === false) { return $input; } return preg_replace_callback('/{\s*([A-Za-z_\-\.0-9]+)\s*}/', array($this, 'getPregMatchValue'), $input); } /** * Return a collection value for a match array of a preg_replace function * * @param array $matches preg_replace* matches * * @return mixed */ public function getPregMatchValue(array $matches) { return $this->get($matches[1]); } } src/Guzzle/Common/Event.php000064400000002301151742734600011642 0ustar00context = $context; } /** * {@inheritdoc} */ public function getIterator() { return new \ArrayIterator($this->context); } /** * {@inheritdoc} */ public function offsetGet($offset) { return array_key_exists($offset, $this->context) ? $this->context[$offset] : null; } /** * {@inheritdoc} */ public function offsetSet($offset, $value) { $this->context[$offset] = $value; } /** * {@inheritdoc} */ public function offsetExists($offset) { return array_key_exists($offset, $this->context); } /** * {@inheritdoc} */ public function offsetUnset($offset) { unset($this->context[$offset]); } } src/Guzzle/Common/Exception/BadMethodCallException.php000064400000000252151742734600017024 0ustar00exceptions[] = $exception; } } elseif ($e instanceof \Exception) { $this->exceptions[] = $e; } $this->message = implode("\n", array_map(function($e) { return $e->getMessage(); }, $this->exceptions)); return $this; } /** * Get the total number of request exceptions * * @return int */ public function count() { return count($this->exceptions); } /** * Allows array-like iteration over the request exceptions * * @return ArrayIterator */ public function getIterator() { return new \ArrayIterator($this->exceptions); } } src/Guzzle/Common/Exception/InvalidArgumentException.php000064400000000256151742734600017476 0ustar00log; } } src/Guzzle/Common/Log/ClosureLogAdapter.php000064400000001350151742734600014664 0ustar00log = $logObject; } /** * {@inheritdoc} */ public function log($message, $priority = LOG_INFO, $extras = null) { call_user_func($this->log, $message, $priority, $extras); } } src/Guzzle/Common/Log/LogAdapterInterface.php000064400000001055151742734600015152 0ustar00 Logger::DEBUG, LOG_INFO => Logger::INFO, LOG_WARNING => Logger::WARNING, LOG_ERR => Logger::ERROR, LOG_CRIT => Logger::CRITICAL, LOG_ALERT => Logger::ALERT ); /** * {@inheritdoc} */ public function __construct(Logger $logObject) { $this->log = $logObject; } /** * {@inheritdoc} */ public function log($message, $priority = LOG_INFO, $extras = null) { $this->log->addRecord(self::$mapping[$priority], $message); } } src/Guzzle/Common/Log/Zf1LogAdapter.php000064400000000704151742734600013712 0ustar00log = $logObject; } /** * {@inheritdoc} */ public function log($message, $priority = LOG_INFO, $extras = null) { $this->log->log($message, $priority, $extras); } } src/Guzzle/Common/Log/Zf2LogAdapter.php000064400000000727151742734600013720 0ustar00log = $logObject; } /** * {@inheritdoc} */ public function log($message, $priority = LOG_INFO, $extras = null) { $this->log->log($message, $priority, $extras); } } src/Guzzle/Common/Stream.php000064400000017573151742734600012035 0ustar00 array( 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+', 'x+' => true, 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true, 'c+b', 'x+' => true, 'rt' => true, 'w+t' => true, 'r+t' => true, 'x+t' => true, 'c+t', 'x+' => true ), 'write' => array( 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true, 'c+', 'x+' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true, 'c+b', 'x+' => true, 'w+t' => true, 'r+t' => true, 'x+t' => true, 'c+t', 'x+' => true ) ); /** * Construct a new Stream * * @param resource $stream Stream resource to wrap * @param int $size Size of the stream in bytes. Only pass this * parameter if the size cannot be obtained from * the stream. * * @throws InvalidArgumentException if the stream is not a stream resource */ public function __construct($stream, $size = null) { if (!is_resource($stream)) { throw new InvalidArgumentException('Stream must be a resource'); } $this->size = $size; $this->stream = $stream; $this->rebuildCache(); } /** * Closes the stream when the helper is destructed */ public function __destruct() { if (is_resource($this->stream)) { fclose($this->stream); } } /** * Reprocess stream metadata */ protected function rebuildCache() { $this->cache = stream_get_meta_data($this->stream); $this->cache[self::STREAM_TYPE] = strtolower($this->cache[self::STREAM_TYPE]); $this->cache[self::WRAPPER_TYPE] = strtolower($this->cache[self::WRAPPER_TYPE]); $this->cache[self::IS_LOCAL] = stream_is_local($this->stream); $this->cache[self::IS_READABLE] = isset(self::$readWriteHash['read'][$this->cache['mode']]); $this->cache[self::IS_WRITABLE] = isset(self::$readWriteHash['write'][$this->cache['mode']]); } /** * Convert the stream to a string if the stream is readable and the stream * is seekable. This logic is enforced to ensure that outputting the stream * as a string does not affect an actual cURL request using non-repeatable * streams. * * @return string */ public function __toString() { if (!$this->isReadable() || (!$this->isSeekable() && $this->isConsumed())) { return ''; } $body = stream_get_contents($this->stream, -1, 0); $this->seek(0); return $body; } /** * Get stream metadata * * @param string $key Specific metdata to retrieve * * @return array|mixed|null */ public function getMetaData($key = null) { $meta = stream_get_meta_data($this->stream); return !$key ? $meta : (array_key_exists($key, $meta) ? $meta[$key] : null); } /** * Get the stream resource * * @return resource */ public function getStream() { return $this->stream; } /** * Get the stream wrapper type * * @return string */ public function getWrapper() { return $this->cache[self::WRAPPER_TYPE]; } /** * Wrapper specific data attached to this stream. * * @return string */ public function getWrapperData() { return $this->getMetaData('wrapper_data') ?: array(); } /** * Get a label describing the underlying implementation of the stream * * @return string */ public function getStreamType() { return $this->cache[self::STREAM_TYPE]; } /** * Get the URI/filename associated with this stream * * @return string */ public function getUri() { return $this->cache['uri']; } /** * Get the size of the stream if able * * @return int|false */ public function getSize() { if ($this->size !== null) { return $this->size; } // If the stream is a file based stream and local, then check the filesize if ($this->isLocal() && $this->getWrapper() == 'plainfile' && $this->getUri() && file_exists($this->getUri())) { return filesize($this->getUri()); } // Only get the size based on the content if the the stream is readable // and seekable so as to not interfere with actually reading the data if (!$this->cache[self::IS_READABLE] || !$this->cache[self::SEEKABLE]) { return false; } else { $this->size = strlen((string) $this); $this->seek(0); return $this->size; } } /** * Check if the stream is readable * * @return bool */ public function isReadable() { return $this->cache[self::IS_READABLE]; } /** * Check if the stream is writable * * @return bool */ public function isWritable() { return $this->cache[self::IS_WRITABLE]; } /** * Check if the stream has been consumed * * @return bool */ public function isConsumed() { return feof($this->stream); } /** * Check if the stream is a local stream vs a remote stream * * @return bool */ public function isLocal() { return $this->cache[self::IS_LOCAL]; } /** * Check if the string is repeatable * * @return bool */ public function isSeekable() { return $this->cache[self::SEEKABLE]; } /** * Specify the size of the stream in bytes * * @param int $size Size of the stream contents in bytes * * @return Stream */ public function setSize($size) { $this->size = $size; return $this; } /** * Seek to a position in the stream * * @param int $offset Stream offset * @param int $whence Where the offset is applied * * @return bool Returns TRUE on success or FALSE on failure * @link http://www.php.net/manual/en/function.fseek.php */ public function seek($offset, $whence = SEEK_SET) { return $this->cache[self::SEEKABLE] ? fseek($this->stream, $offset, $whence) === 0 : false; } /** * Read data from the stream * * @param int $length Up to length number of bytes read. * * @return string|bool Returns the data read from the stream or FALSE on * failure or EOF */ public function read($length) { return $this->cache[self::IS_READABLE] ? fread($this->stream, $length) : false; } /** * Write data to the stream * * @param string $string The string that is to be written. * * @return int|bool Returns the number of bytes written to the stream on * success or FALSE on failure. */ public function write($string) { if (!$this->cache[self::IS_WRITABLE]) { return 0; } $bytes = fwrite($this->stream, $string); $this->size += $bytes; return $bytes; } } src/Guzzle/Common/Validation/AbstractConstraint.php000064400000002367151742734600016477 0ustar00default && isset($options[0])) { // Add the default configuration option if an enumerated array // is passed $options[$this->default] = $options[0]; } // Ensure that required options are present if ($this->required && !isset($options[$this->required])) { throw new InvalidArgumentException("{$this->required} is a required validation option"); } return $this->validateValue($value, $options); } /** * Perform the actual validation in a concrete class * * @param mixed $value Value to validate * @param array $options Validation options * * @return bool|string */ abstract protected function validateValue($value, array $options = array()); } src/Guzzle/Common/Validation/AbstractType.php000064400000001544151742734600015270 0ustar00 'ctype_alnum', 'alpha' => 'ctype_alpha', 'cntrl' => 'ctype_cntrl', 'digit' => 'ctype_digit', 'graph' => 'ctype_graph', 'lower' => 'ctype_lower', 'print' => 'ctype_print', 'punct' => 'ctype_punct', 'space' => 'ctype_space', 'upper' => 'ctype_upper', 'xdigit' => 'ctype_xdigit' ); } src/Guzzle/Common/Validation/Email.php000064400000000703151742734600013706 0ustar00 'is_array', 'long' => 'is_long', 'bool' => 'is_bool', 'boolean' => 'is_bool', 'scalar' => 'is_scalar', 'string' => 'is_string', 'nan' => 'is_nan', 'float' => 'is_float', 'file' => 'is_file', 'callable' => 'is_callable', 'null' => 'is_null', 'resource' => 'is_resource', 'numeric' => 'is_numeric', 'object' => 'is_object' ); } src/Guzzle/Common/Validation/Url.php000064400000000673151742734600013427 0ustar00setConfig($config ?: new Collection()); $this->setBaseUrl($baseUrl); $this->defaultHeaders = new Collection(); $this->setRequestFactory(RequestFactory::getInstance()); } /** * Cast to a string */ public function __toString() { return spl_object_hash($this); } /** * Set the configuration object to use with the client * * @param array|Collection|string $config Parameters that define how the * client behaves and connects to a webservice. Pass an array or a * Collection object. * * @return Client */ public final function setConfig($config) { // Set the configuration object if ($config instanceof Collection) { $this->config = $config; } elseif (is_array($config)) { $this->config = new Collection($config); } else { throw new InvalidArgumentException( 'Config must be an array or Collection' ); } return $this; } /** * Get a configuration setting or all of the configuration settings * * @param bool|string $key Configuration value to retrieve. Set to FALSE * to retrieve all values of the client. The object return can be * modified, and modifications will affect the client's config. * * @return mixed|Collection */ public final function getConfig($key = false) { return $key ? $this->config->get($key) : $this->config; } /** * Get the default HTTP headers to add to each request created by the client * * @return Collection */ public function getDefaultHeaders() { return $this->defaultHeaders; } /** * Set the default HTTP headers to add to each request created by the client * * @param array|Collection $headers Default HTTP headers * * @return Client */ public function setDefaultHeaders($headers) { if ($headers instanceof Collection) { $this->defaultHeaders = $headers; } elseif (is_array($headers)) { $this->defaultHeaders = new Collection($headers); } else { throw new InvalidArgumentException('Headers must be an array or Collection'); } return $this; } /** * Expand a URI template using client configuration data * * @param string $template URI template to expand * @param array $variables Additional variables to use in the expansion * * @return string */ public function expandTemplate($template, array $variables = null) { $expansionVars = $this->getConfig()->getAll(); if ($variables) { $expansionVars = array_merge($expansionVars, $variables); } return $this->getUriTemplate()->expand($template, $expansionVars); } /** * Set the URI template expander to use with the client * * @param UriTemplateInterface $uriTemplate URI template expander * * @return Client */ public function setUriTemplate(UriTemplateInterface $uriTemplate) { $this->uriTemplate = $uriTemplate; return $this; } /** * Get the URI template expander used by the client. A default UriTemplate * object will be created if one does not exist. * * @return UriTemplateInterface */ public function getUriTemplate() { if (!$this->uriTemplate) { $this->uriTemplate = ParserRegistry::get('uri_template'); } return $this->uriTemplate; } /** * Create and return a new {@see RequestInterface} configured for the client * * @param string $method HTTP method. Defaults to GET * @param string|array $uri Resource URI. Use an absolute path * to override the base path of the client, or a relative path to * append to the base path of the client. The URI can contain the * querystring as well. Use an array to provide a URI template and * additional variables to use in the URI template expansion. * @param array|Collection $headers HTTP headers * @param string|resource|array|EntityBody $body Entity body of * request (POST/PUT) or response (GET) * * @return RequestInterface * @throws InvalidArgumentException if a URI array is passed that does not * contain exactly two elements: the URI followed by template variables */ public function createRequest($method = RequestInterface::GET, $uri = null, $headers = null, $body = null) { if (!is_array($uri)) { $templateVars = null; } else { if (count($uri) != 2 || !is_array($uri[1])) { throw new InvalidArgumentException('You must provide a URI' . ' template followed by an array of template variables' . ' when using an array for a URI template'); } list($uri, $templateVars) = $uri; } if (!$uri) { $url = $this->getBaseUrl(); } elseif (strpos($uri, 'http') === 0) { // Use absolute URLs as-is $url = $this->expandTemplate($uri, $templateVars); } else { $url = Url::factory($this->getBaseUrl())->combine($this->expandTemplate($uri, $templateVars)); } // If default headers are provided, then merge them into exising headers // If a collision occurs, the header is completely replaced if (count($this->defaultHeaders)) { if ($headers instanceof Collection) { $headers = array_merge($this->defaultHeaders->getAll(), $headers->getAll()); } elseif (is_array($headers)) { $headers = array_merge($this->defaultHeaders->getAll(), $headers); } elseif ($headers === null) { $headers = $this->defaultHeaders; } } return $this->prepareRequest( $this->requestFactory->create($method, (string) $url, $headers, $body) ); } /** * Prepare a request to be sent from the Client by adding client specific * behaviors and properties to the request. * * @param RequestInterface $request Request to prepare for the client * * @return RequestInterface */ protected function prepareRequest(RequestInterface $request) { $request->setClient($this); foreach ($this->getConfig()->getAll() as $key => $value) { if ($key == 'curl.blacklist') { continue; } // Add any curl options that might in the config to the request if (strpos($key, 'curl.') === 0) { $curlOption = substr($key, 5); // Convert constants represented as string to constant int values if (defined($curlOption)) { $value = is_string($value) && defined($value) ? constant($value) : $value; $curlOption = constant($curlOption); } $request->getCurlOptions()->set($curlOption, $value); } elseif (strpos($key, 'params.') === 0) { // Add request specific parameters to all requests (prefix with 'params.') $request->getParams()->set(substr($key, 7), $value); } } // Attach client observers to the request $request->setEventDispatcher(clone $this->getEventDispatcher()); $this->dispatch('client.create_request', array( 'client' => $this, 'request' => $request )); return $request; } /** * Get the client's base URL as either an expanded or raw URI template * * @param bool $expand Set to FALSE to get the raw base URL * without URI template expansion * * @return string|null */ public function getBaseUrl($expand = true) { return $expand ? $this->expandTemplate($this->baseUrl) : $this->baseUrl; } /** * Set the base URL of the client * * @param string $url The base service endpoint URL of the webservice * * @return Client */ public function setBaseUrl($url) { $this->baseUrl = $url; return $this; } /** * Set the name of your application and application version that will be * appended to the User-Agent header of all reqeusts. * * @param string $userAgent User agent string * @param bool $includeDefault Set to TRUE to append the default * Guzzle user agent * * @return Client */ public function setUserAgent($userAgent, $includeDefault = false) { if ($includeDefault) { $userAgent .= ' ' . Utils::getDefaultUserAgent(); } $this->defaultHeaders->set('User-Agent', $userAgent); return $this; } /** * Create a GET request for the client * * @param string|array $uri Resource URI of the request. Use an * absolute path to override the base path, or a relative path to * append. Use an array to provide a URI template and additional * variables to use in the URI template expansion. * @param array|Collection $headers HTTP headers * @param string|resource|array|EntityBody $body Where to store * the response entity body * * @return Request */ public function get($uri = null, $headers = null, $body = null) { return $this->createRequest('GET', $uri, $headers, $body); } /** * Create a HEAD request for the client * * @param string|array $uri Resource URI of the request. Use an * absolute path to override the base path, or a relative path to * append. Use an array to provide a URI template and additional * variables to use in the URI template expansion. * @param array|Collection $headers HTTP headers * * @return Request */ public function head($uri = null, $headers = null) { return $this->createRequest('HEAD', $uri, $headers); } /** * Create a DELETE request for the client * * @param string|array $uri Resource URI of the request. Use an * absolute path to override the base path, or a relative path to * append. Use an array to provide a URI template and additional * variables to use in the URI template expansion. * @param array|Collection $headers HTTP headers * * @return Request */ public function delete($uri = null, $headers = null) { return $this->createRequest('DELETE', $uri, $headers); } /** * Create a PUT request for the client * * @param string|array $uri Resource URI of the request. Use an * absolute path to override the base path, or a relative path to * append. Use an array to provide a URI template and additional * variables to use in the URI template expansion. * @param array|Collection $headers HTTP headers * @param string|resource|EntityBody $body Body to send in the request * * @return EntityEnclosingRequest */ public function put($uri = null, $headers = null, $body = null) { return $this->createRequest('PUT', $uri, $headers, $body); } /** * Create a PATCH request for the client * * @param string|array $uri Resource URI of the request. Use an * absolute path to override the base path, or a relative path to * append. Use an array to provide a URI template and additional * variables to use in the URI template expansion. * @param array|Collection $headers HTTP headers * @param string|resource|EntityBody $body Body to send in the request * * @return EntityEnclosingRequest */ public function patch($uri = null, $headers = null, $body = null) { return $this->createRequest('PATCH', $uri, $headers, $body); } /** * Create a POST request for the client * * @param string|array $uri Resource URI of the request. Use an * absolute path to override the base path, or a relative path to * append. Use an array to provide a URI template and additional * variables to use in the URI template expansion. * @param array|Collection $headers HTTP headers * @param array|Collection|string|EntityBody $postBody POST * body. Can be a string, EntityBody, or associative array of POST * fields to send in the body of the request. Prefix a value in the * array with the @ symbol reference a file. * * @return EntityEnclosingRequest */ public function post($uri = null, $headers = null, $postBody = null) { return $this->createRequest('POST', $uri, $headers, $postBody); } /** * Create an OPTIONS request for the client * * @param string|array $uri Resource URI of the request. Use an * absolute path to override the base path, or a relative path to * append. Use an array to provide a URI template and additional * variables to use in the URI template expansion. * * @return Request */ public function options($uri = null) { return $this->createRequest('OPTIONS', $uri); } /** * Sends a single request or an array of requests in parallel * * @param array $requests Request(s) to send * * @return array Returns the response(s) */ public function send($requests) { $curlMulti = $this->getCurlMulti(); $multipleRequests = is_array($requests); if (!$multipleRequests) { $requests = array($requests); } foreach ($requests as $request) { $curlMulti->add($request); } try { $curlMulti->send(); } catch (ExceptionCollection $e) { throw $multipleRequests ? $e : $e->getIterator()->offsetGet(0); } if (!$multipleRequests) { return end($requests)->getResponse(); } return array_map(function($request) { return $request->getResponse(); }, $requests); } /** * Set a curl multi object to be used internally by the client for * transferring requests. * * @param CurlMultiInterface $curlMulti Mulit object * * @return Client */ public function setCurlMulti(CurlMultiInterface $curlMulti) { $this->curlMulti = $curlMulti; return $this; } /** * Get the curl multi object used with the client * * @return CurlMultiInterface */ public function getCurlMulti() { if (!$this->curlMulti) { $this->curlMulti = CurlMulti::getInstance(); } return $this->curlMulti; } /** * Set the request factory to use with the client when creating requests * * @param RequestFactoryInterface $factory Request factory * * @return Client */ public function setRequestFactory(RequestFactoryInterface $factory) { $this->requestFactory = $factory; return $this; } } src/Guzzle/Http/ClientInterface.php000064400000020706151742734600013320 0ustar00add($key, $value); } } return new static($data->getAll()); } /** * {@inheritdoc} */ public function __construct(array $data = null) { parent::__construct($data); $this->setFieldSeparator(';') ->setPrefix('') ->setValueSeparator('=') ->setEncodeFields(false) ->setEncodeValues(false) ->setAggregateFunction(function($key, $value, $encodeFields = false, $encodeValues = false) { $value = array_unique($value); return array( (($encodeFields) ? rawurlencode($key) : $key) => (($encodeValues) ? array_map('rawurlencode', $value) : $value) ); }); } } src/Guzzle/Http/CookieJar/ArrayCookieJar.php000064400000021602151742734600014770 0ustar00getCookies($domain, $path, $name, false, false); return $this->prune(function($cookie) use ($cookies) { return !in_array($cookie, $cookies, true); }); } /** * Discard all temporary cookies. * * Scans for all cookies in the jar with either no expire field or a * true discard flag. To be called when the user agent shuts down according * to RFC 2965. * * @return int Returns the number of deleted cookies */ public function clearTemporary() { return $this->prune(function($cookie) { return (!$cookie['discard'] && $cookie['expires']); }); } /** * Delete any expired cookies * * @return int Returns the number of deleted cookies */ public function deleteExpired() { $ctime = time(); return $this->prune(function($cookie) use ($ctime) { return (!$cookie['expires'] || $ctime < $cookie['expires']); }); } /** * Get all of the matching cookies * * @param string $domain Domain of the cookie * @param string $path Path of the cookie * @param string $name Name of the cookie * @param bool $skipDiscardables Set to TRUE to skip cookies with * the Discard attribute. * @param bool $skipExpired Set to FALSE to include expired * * @return array Returns an array of arrays. Each array contains the * following keys: * * domain (string) - Domain of the cookie * path (string) - Path of the cookie * cookie (array) - Array of cookie name, value (e.g. array('name', '123') * max_age (int) - Lifetime of the cookie in seconds * expires (int) - The UNIX timestamp when the cookie expires * version (int) - Version of the cookie specification. RFC 2965 is 1 * secure (bool) - Whether or not this is a secure cookie * discard (bool) - Whether or not this is a discardable cookie * comment (string) - How the cookie is intended to be used * comment_url (str)- URL with info on how it will be used * port (string) - CSV list of ports * http_only (bool) - HTTP only cookie */ public function getCookies($domain = null, $path = null, $name = null, $skipDiscardable = false, $skipExpired = true) { $ctime = time(); $ret = array_values(array_filter($this->cookies, function($cookie) use ($domain, $path, $name, $skipDiscardable, $skipExpired, $ctime) { // Make sure the cookie is not expired if ($skipExpired && $cookie['expires'] && $ctime > $cookie['expires']) { return false; } // Normalize the domain value $domainMatch = false; if ($domain && $cookie['domain']) { if (!strcasecmp($domain, $cookie['domain'])) { $domainMatch = true; } elseif ($cookie['domain'][0] == '.') { $domainMatch = preg_match('/' . preg_quote($cookie['domain']) . '$/i', $domain); } } if (!$domain || $domainMatch) { // Check if path matches if (!$path || !strcasecmp($path, $cookie['path']) || 0 === stripos($path, $cookie['path'])) { // Check if cookie name matches if (!$name || $cookie['cookie'][0] == $name) { if (!$skipDiscardable || !$cookie['discard']) { return true; } } } } return false; })); return $ret; } /** * Save a cookie * * @parm array $cookieData Cookie information, including the following: * domain (string, required) - Domain of the cookie * path (string, required) - Path of the cookie * cookie - Array of cookie name (0) and value (1) * max_age (int, required) - Lifetime of the cookie in seconds * version (int) - Version of the cookie specification. Default is 0. RFC 2965 is 1 * secure (bool) - If it is a secure cookie * discard (bool) - If it is a discardable cookie * comment (string) - How the cookie is intended to be used * comment_url (str) - URL with info on how it will be used * port (string) - CSV list of ports * http_only (bool) - HTTP only cookie * * @return ArrayCookieJar */ public function save(array $cookieData) { if (!isset($cookieData['domain'])) { throw new InvalidArgumentException('Cookies require a domain'); } if (!isset($cookieData['cookie']) || !is_array($cookieData['cookie'])) { throw new InvalidArgumentException('Cookies require a names and values'); } $cookieData = array_merge(array( 'path' => '/', 'expires' => null, 'max_age' => 0, 'comment' => null, 'comment_url' => null, 'port' => array(), 'version' => null, 'secure' => null, 'discard' => null, 'http_only' => false ), $cookieData); // Extract the expires value and turn it into a UNIX timestamp if needed if ($cookieData['expires']) { if (!is_numeric($cookieData['expires'])) { $cookieData['expires'] = strtotime($cookieData['expires']); } } elseif ($cookieData['max_age']) { // Calculate the expires date $cookieData['expires'] = time() + (int) $cookieData['max_age']; } $keys = array('path', 'max_age', 'domain', 'http_only', 'port', 'secure'); foreach ($this->cookies as $i => $cookie) { // Check the regular comparison fields foreach ($keys as $k) { if ($cookie[$k] != $cookieData[$k]) { continue 2; } } // Is it a different cookie value name? if ($cookie['cookie'][0] != $cookieData['cookie'][0]) { continue; } // The previously set cookie is a discard cookie and this one is not // so allow the new cookie to be set if (!$cookieData['discard'] && $cookie['discard']) { unset($this->cookies[$i]); continue; } // If the new cookie's expiration is further into the future, then // replace the old cookie if ($cookieData['expires'] > $cookie['expires']) { unset($this->cookies[$i]); continue; } // The cookie exists, so no need to continue return $this; } $this->cookies[] = $cookieData; return $this; } /** * Serializes the cookie jar * * @return string */ public function serialize() { return serialize($this->cookies); } /** * Unserializes the cookie jar */ public function unserialize($data) { $this->cookies = unserialize($data); } /** * Prune the cookies using a callback function * * @param Closure $callback Callback function * * @return int Returns the number of removed cookies */ protected function prune(\Closure $callback) { $originalCount = count($this->cookies); $this->cookies = array_filter($this->cookies, $callback); return $originalCount - count($this->cookies); } } src/Guzzle/Http/CookieJar/CookieJarInterface.php000064400000007362151742734600015621 0ustar00file = $cookieFile; $this->load(); } /** * Saves the file when shutting down */ public function __destruct() { $this->persist(); } /** * Save the contents of the data array to the file * * @throws RuntimeException if the file cannot be found or created */ protected function persist() { $handle = fopen($this->file, 'w+'); // @codeCoverageIgnoreStart if ($handle === false) { throw new RuntimeException('Unable to open file ' . $this->file); } // @codeCoverageIgnoreEnd fwrite($handle, json_encode($this->getCookies(null, null, null, true, true))); fclose($handle); } /** * Load the contents of the json formatted file into the data array and * discard the unsaved state of the jar object */ protected function load() { $handle = fopen($this->file, 'c+'); // @codeCoverageIgnoreStart if ($handle === false) { throw new RuntimeException('Unable to open file ' . $this->file); } // @codeCoverageIgnoreEnd $json = ''; while ($data = fread($handle, 8096)) { $json .= $data; } fclose($handle); $this->cookies = $json ? json_decode($json, true) : array(); } } src/Guzzle/Http/Curl/CurlHandle.php000064400000032741151742734600013211 0ustar00getCurlOptions(); $tempHeaders = array(); // Array of default cURL options. $curlOptions = array( CURLOPT_URL => $request->getUrl(), CURLOPT_CUSTOMREQUEST => $request->getMethod(), CURLOPT_CONNECTTIMEOUT => 10, // Connect timeout in seconds CURLOPT_RETURNTRANSFER => false, // Streaming the return, so no need CURLOPT_HEADER => false, // Retrieve the received headers CURLOPT_USERAGENT => (string) $request->getHeader('User-Agent'), CURLOPT_ENCODING => '', // Supports all encodings CURLOPT_PORT => $request->getPort(), CURLOPT_HTTP_VERSION => $request->getProtocolVersion() === '1.0' ? CURL_HTTP_VERSION_1_0 : CURL_HTTP_VERSION_1_1, CURLOPT_HTTPHEADER => array(), CURLOPT_HEADERFUNCTION => array($mediator, 'receiveResponseHeader') ); // Enable the progress function if the 'progress' param was set if ($requestCurlOptions->get('progress')) { $curlOptions[CURLOPT_PROGRESSFUNCTION] = array($mediator, 'progress'); $curlOptions[CURLOPT_NOPROGRESS] = false; } // Enable curl debug information if the 'debug' param was set if (!$requestCurlOptions->get('disable_wire')) { $curlOptions[CURLOPT_STDERR] = fopen('php://temp', 'r+'); $curlOptions[CURLOPT_VERBOSE] = true; } // HEAD requests need no response body, everything else might if ($request->getMethod() != 'HEAD') { $curlOptions[CURLOPT_WRITEFUNCTION] = array($mediator, 'writeResponseBody'); } // Account for PHP installations with safe_mode or open_basedir enabled // @codeCoverageIgnoreStart if (CurlVersion::getInstance()->get('follow_location')) { $curlOptions[CURLOPT_FOLLOWLOCATION] = true; $curlOptions[CURLOPT_MAXREDIRS] = 5; } // @codeCoverageIgnoreEnd // Specify settings according to the HTTP method switch ($request->getMethod()) { case 'GET': $curlOptions[CURLOPT_HTTPGET] = true; break; case 'HEAD': $curlOptions[CURLOPT_NOBODY] = true; break; case 'POST': $curlOptions[CURLOPT_POST] = true; // Special handling for POST specific fields and files if (count($request->getPostFiles())) { $curlOptions[CURLOPT_POSTFIELDS] = $request->getPostFields()->getAll(); $request->removeHeader('Content-Length'); } elseif (count($request->getPostFields())) { $curlOptions[CURLOPT_POSTFIELDS] = (string) $request->getPostFields(); $request->removeHeader('Content-Length'); } break; case 'PUT': case 'PATCH': $curlOptions[CURLOPT_UPLOAD] = true; // Let cURL handle setting the Content-Length header $contentLength = $request->getHeader('Content-Length'); if ($contentLength != null) { $contentLength = (int) (string) $contentLength; $curlOptions[CURLOPT_INFILESIZE] = $contentLength; $tempHeaders['Content-Length'] = $contentLength; $request->removeHeader('Content-Length'); } break; } // Special handling for requests sending raw data if ($request instanceof EntityEnclosingRequestInterface) { // Don't modify POST requests using POST fields and files via cURL if (!isset($curlOptions[CURLOPT_POSTFIELDS])) { if ($request->getBody()) { // Add a callback for curl to read data to send with the request // only if a body was specified $curlOptions[CURLOPT_READFUNCTION] = array($mediator, 'readRequestBody'); } else { // If no body is being sent, always send Content-Length of 0 $request->setHeader('Content-Length', 0); $request->removeHeader('Transfer-Encoding'); // Need to remove CURLOPT_UPLOAD to prevent chunked encoding unset($curlOptions[CURLOPT_UPLOAD]); unset($curlOptions[CURLOPT_POST]); } } // If the Expect header is not present, prevent curl from adding it if (!$request->hasHeader('Expect')) { $curlOptions[CURLOPT_HTTPHEADER][] = 'Expect:'; } } // Set custom cURL options foreach ($requestCurlOptions as $key => $value) { if (is_numeric($key)) { $curlOptions[$key] = $value; } } // Check if any headers or cURL options are blacklisted $client = $request->getClient(); if ($client) { $blacklist = $client->getConfig('curl.blacklist'); if ($blacklist) { foreach ($blacklist as $value) { if (strpos($value, 'header.') === 0) { // Remove headers that may have previously been set // but are supposed to be blacklisted $key = substr($value, 7); $request->removeHeader($key); $curlOptions[CURLOPT_HTTPHEADER][] = $key . ':'; } else { unset($curlOptions[$value]); } } } } // Add any custom headers to the request. Emtpy headers will cause curl to // not send the header at all. foreach ($request->getHeaders() as $headerName => $values) { foreach ($values as $value) { $curlOptions[CURLOPT_HTTPHEADER][] = trim("{$headerName}: {$value}"); } } // Apply the options to a new cURL handle. $handle = curl_init(); curl_setopt_array($handle, $curlOptions); $request->getParams()->set('curl.last_options', $curlOptions); // Some fields need to be removed from the request in order to properly // send a cURL request message. The fields that were removed for this // purpose (e.g. Content-Length) should be aggregated in this array and // added back to the request. Does not apply to blacklisted headers. foreach ($tempHeaders as $key => $value) { $request->setHeader($key, $value); } return new static($handle, $curlOptions); } /** * Construct a new CurlHandle object that wraps a cURL handle * * @param resource $handle Configured cURL handle resource * @param Collection|array $options Curl options to use with the handle * * @throws InvalidArgumentException */ public function __construct($handle, $options) { if (!is_resource($handle)) { throw new InvalidArgumentException('Invalid handle provided'); } if (is_array($options)) { $this->options = new Collection($options); } elseif ($options instanceof Collection) { $this->options = $options; } else { throw new InvalidArgumentException('Expected array or Collection'); } $this->handle = $handle; } /** * Destructor */ public function __destruct() { $this->close(); } /** * Close the curl handle */ public function close() { if (is_resource($this->handle)) { curl_close($this->handle); } $this->handle = null; } /** * Check if the handle is available and still OK * * @return bool */ public function isAvailable() { return is_resource($this->handle); } /** * Get the last error that occurred on the cURL handle * * @return string */ public function getError() { return $this->isAvailable() ? curl_error($this->handle) : ''; } /** * Get the last error number that occurred on the cURL handle * * @return int */ public function getErrorNo() { if ($this->errorNo) { return $this->errorNo; } return $this->isAvailable() ? curl_errno($this->handle) : 0; } /** * Set the curl error number * * @param int $error Error number to set * * @return CurlHandle */ public function setErrorNo($error) { $this->errorNo = $error; return $this; } /** * Get cURL curl_getinfo data * * @param int $option Option to retrieve. Pass null to retrieve * retrieve all data as an array or pass a CURLINFO_* * constant * * @return array|mixed */ public function getInfo($option = null) { if (!is_resource($this->handle)) { return null; } if (null !== $option) { return curl_getinfo($this->handle, $option) ?: null; } return curl_getinfo($this->handle) ?: array(); } /** * Get the stderr output * * @param bool $asResource Set to TRUE to get an fopen resource * * @return string|resource|null */ public function getStderr($asResource = false) { $stderr = $this->getOptions()->get(CURLOPT_STDERR); if (!$stderr) { return null; } if ($asResource) { return $stderr; } fseek($stderr, 0); $e = stream_get_contents($stderr); fseek($stderr, 0, SEEK_END); return $e; } /** * Get the URL that this handle is connecting to * * @return Url */ public function getUrl() { return Url::factory($this->options->get(CURLOPT_URL)); } /** * Get the wrapped curl handle * * @return handle|null Returns the cURL handle or null if it was closed */ public function getHandle() { return $this->isAvailable() ? $this->handle : null; } /** * Get the cURL setopt options of the handle. Changing values in the return * object will have no effect on the curl handle after it is created. * * @return Collection */ public function getOptions() { return $this->options; } /** * Update a request based on the log messages of the CurlHandle * * @param RequestInterface $request Request to update */ public function updateRequestFromTransfer(RequestInterface $request) { $log = $this->getStderr(true); if (!$log || !$request->getResponse()) { return; } // Update the transfer stats of the response $request->getResponse()->setInfo($this->getInfo()); // Parse the cURL stderr output for outgoing requests $headers = ''; fseek($log, 0); while (($line = fgets($log)) !== false) { if ($line && $line[0] == '>') { $headers = substr(trim($line), 2) . "\r\n"; while (($line = fgets($log)) !== false) { if ($line[0] == '*' || $line[0] == '<') { break; } else { $headers .= trim($line) . "\r\n"; } } } } // Add request headers to the request exactly as they were sent if ($headers) { $parsed = ParserRegistry::get('message')->parseRequest($headers); if (!empty($parsed['headers'])) { $request->setHeaders(array()); foreach ($parsed['headers'] as $name => $value) { $request->setHeader($name, $value); } } if (!empty($parsed['version'])) { $request->setProtocolVersion($parsed['version']); } } } } src/Guzzle/Http/Curl/CurlMulti.php000064400000050240151742734600013102 0ustar00 array('CURLM_BAD_HANDLE', 'The passed-in handle is not a valid CURLM handle.'), CURLM_BAD_EASY_HANDLE => array('CURLM_BAD_EASY_HANDLE', "An easy handle was not good/valid. It could mean that it isn't an easy handle at all, or possibly that the handle already is in used by this or another multi handle."), CURLM_OUT_OF_MEMORY => array('CURLM_OUT_OF_MEMORY', 'You are doomed.'), CURLM_INTERNAL_ERROR => array('CURLM_INTERNAL_ERROR', 'This can only be returned if libcurl bugs. Please report it to us!') ); /** * @var CurlMulti */ private static $instance; /** * @var int */ private $scope = -1; /** * Get a cached instance of the curl mutli object * * @return CurlMulti */ public static function getInstance() { // @codeCoverageIgnoreStart if (!self::$instance) { self::$instance = new self(); } // @codeCoverageIgnoreEnd return self::$instance; } /** * {@inheritdoc} */ public static function getAllEvents() { return array( // A request was added self::ADD_REQUEST, // A request was removed self::REMOVE_REQUEST, // Requests are about to be sent self::BEFORE_SEND, // The pool finished sending the requests self::COMPLETE, // A request is still polling (sent to request's event dispatchers) self::POLLING_REQUEST, // A request exception occurred 'curl_multi.exception' ); } /** * {@inheritdoc} */ public function __construct() { // You can get some weird "Too many open files" errors when sending // a large amount of requests in parallel. These two statements // autoload classes before a system runs out of file descriptors so // that you can get back valuable error messages if you run out. class_exists('Guzzle\Http\Message\Response'); class_exists('Guzzle\Http\Exception\CurlException'); $this->createMutliHandle(); } /** * {@inheritdoc} */ public function __destruct() { $this->requestCache = null; if (is_resource($this->multiHandle)) { curl_multi_close($this->multiHandle); } } /** * {@inheritdoc} * * Adds a request to a batch of requests to be sent in parallel. * * Async requests adds a request to the current scope to be executed in * parallel with any currently executing cURL handles. You may only add an * async request while other requests are transferring. Attempting to add * an async request while no requests are transferring will add the request * normally in the next available scope (typically 0). * * @param RequestInterface $request Request to add * @param bool $async Set to TRUE to add to the current scope * * @return CurlMutli */ public function add(RequestInterface $request, $async = false) { if ($async && $this->state != self::STATE_SENDING) { $async = false; } $this->requestCache = null; $scope = $async ? $this->scope : $this->scope + 1; if (!isset($this->requests[$scope])) { $this->requests[$scope] = array(); } $this->requests[$scope][] = $request; $this->dispatch(self::ADD_REQUEST, array( 'request' => $request )); // If requests are currently transferring and this is async, then the // request must be prepared now as the send() method is not called. if ($this->state == self::STATE_SENDING && $async) { $this->beforeSend($request); } return $this; } /** * {@inheritdoc} */ public function all() { if (!$this->requestCache) { $this->requestCache = array(); foreach ($this->requests as &$scopedRequests) { foreach ($scopedRequests as $request) { $this->requestCache[] = $request; } } } return $this->requestCache; } /** * {@inheritdoc} */ public function getState() { return $this->state; } /** * {@inheritdoc} */ public function remove(RequestInterface $request) { $this->removeHandle($request); $this->requestCache = null; foreach ($this->requests as $scope => $scopedRequests) { foreach ($scopedRequests as $i => $scopedRequest) { if ($scopedRequest === $request) { unset($this->requests[$scope][$i]); break; } } } $this->dispatch(self::REMOVE_REQUEST, array( 'request' => $request )); return $this; } /** * {@inheritdoc} */ public function reset($hard = false) { // Remove each request foreach ($this->all() as $request) { $this->remove($request); } $this->requests = array(); $this->exceptions = array(); $this->state = self::STATE_IDLE; $this->scope = -1; $this->requestCache = null; // Remove any curl handles that were queued for removal if ($this->scope == -1 || $hard) { foreach ($this->removeHandles as $handle) { curl_multi_remove_handle($this->multiHandle, $handle->getHandle()); $handle->close(); } $this->removeHandles = array(); } if ($hard) { $this->createMutliHandle(); } } /** * {@inheritdoc} */ public function send() { $this->scope++; $this->state = self::STATE_SENDING; // Only prepare and send requests that are in the current recursion scope // Only enter the main perform() loop if there are requests in scope if (!empty($this->requests[$this->scope])) { // Any exceptions thrown from this event should break the entire // flow of sending requests in parallel to prevent weird errors $this->dispatch(self::BEFORE_SEND, array( 'requests' => $this->requests[$this->scope] )); foreach ($this->requests[$this->scope] as $request) { if ($request->getState() != RequestInterface::STATE_TRANSFER) { $this->beforeSend($request); } } try { $this->perform(); } catch (\Exception $e) { $this->exceptions[] = $e; } } $this->scope--; // Aggregate exceptions into an ExceptionCollection $exceptionCollection = null; if (!empty($this->exceptions)) { $exceptionCollection = new ExceptionCollection('Errors during multi transfer'); while ($e = array_shift($this->exceptions)) { $exceptionCollection->add($e); } } // Complete the transfer if this is the bottom scope and the state // of the curl multi handle is not already complete if ($this->scope == -1) { $this->state = self::STATE_COMPLETE; $this->dispatch(self::COMPLETE); $this->reset(); } // Throw any exceptions that were encountered if ($exceptionCollection) { throw $exceptionCollection; } } /** * {@inheritdoc} */ public function count() { return count($this->all()); } /** * Prepare for sending * * @param RequestInterface $request Request to prepare */ protected function beforeSend(RequestInterface $request) { try { $request->setState(RequestInterface::STATE_TRANSFER); $request->dispatch('request.before_send', array( 'request' => $request )); if ($request->getState() != RequestInterface::STATE_TRANSFER) { // Requests might decide they don't need to be sent just before // transfer (e.g. CachePlugin) $this->remove($request); } elseif ($request->getParams()->get('queued_response')) { // Queued responses do not need to be sent using curl $this->remove($request); $request->setState(RequestInterface::STATE_COMPLETE); } else { // Add the request's curl handle to the multi handle $this->checkCurlResult(curl_multi_add_handle($this->multiHandle, $this->createCurlHandle($request)->getHandle())); } } catch (\Exception $e) { $this->removeErroredRequest($request, $e); } } /** * Create a curl handle for a request * * @param RequestInterface $request Request * * @return CurlHandle */ protected function createCurlHandle(RequestInterface $request) { $wrapper = CurlHandle::factory($request); $this->handles[spl_object_hash($request)] = $wrapper; $request->getParams()->set('curl_handle', $wrapper); return $wrapper; } /** * Get the data from the multi handle */ protected function perform() { // @codeCoverageIgnoreStart // Weird things can happen when making HTTP requests in __destruct methods if (!$this->multiHandle) { return; } // @codeCoverageIgnoreEnd // If there are no requests to send, then exit from the function if ($this->scope <= 0) { if ($this->count() == 0) { return; } } elseif (empty($this->requests[$this->scope])) { return; } // Create the polling event external to the loop $event = array('curl_multi' => $this); while (1) { $active = $this->executeHandles(); // Get messages from curl handles while ($done = curl_multi_info_read($this->multiHandle)) { foreach ($this->all() as $request) { $handle = $this->getRequestHandle($request); if ($handle && $handle->getHandle() === $done['handle']) { try { $this->processResponse($request, $handle, $done); } catch (\Exception $e) { $this->removeErroredRequest($request, $e); } break; } } } // Notify each request as polling and handled queued responses if ($this->scope <= 0) { $scopedPolling = $this->all(); } else { $scopedPolling = $this->requests[$this->scope]; } // Exit the function if there are no more requests to send if (empty($scopedPolling)) { break; } // Notify all requests that requests are being polled foreach ($scopedPolling as $request) { $event['request'] = $request; $request->dispatch(self::POLLING_REQUEST, $event); } if ($active) { // Select the curl handles until there is any activity on any // of the open file descriptors. // See https://github.com/php/php-src/blob/master/ext/curl/multi.c#L170 $active = $this->executeHandles(true, 0.25); } else { // Requests are not actually pending a cURL select call, so // we need to delay in order to prevent eating too much CPU usleep(500); } } } /** * Execute and select curl handles until there is activity * * @param bool $select Set to TRUE to select the file descriptors * @param int $timeout Select timeout in seconds * * @return int Returns the number of active handles */ private function executeHandles($select = false, $timeout = 1) { $active = 0; $selectResult = 0; do { if ($select) { $selectResult = curl_multi_select($this->multiHandle, $timeout); } if ($selectResult == 0) { while ($mrc = curl_multi_exec($this->multiHandle, $active) == CURLM_CALL_MULTI_PERFORM); // Check the return value to ensure an error did not occur $this->checkCurlResult($mrc); } // Poll once if not selecing, or poll until there are no handles with activity } while ($select && $active && $selectResult == 0); return $active; } /** * Remove a request that encountered an exception * * @param RequestInterface $request Request to remove * @param Exception $e Exception encountered */ protected function removeErroredRequest(RequestInterface $request, \Exception $e) { $this->remove($request); $request->setState(RequestInterface::STATE_ERROR); $this->dispatch(self::MULTI_EXCEPTION, array( 'exception' => $e, 'all_exceptions' => $this->exceptions )); $this->exceptions[] = $e; } /** * Check for errors and fix headers of a request based on a curl response * * @param RequestInterface $request Request to process * @param CurlHandle $handle Curl handle object * @param array $curl Array returned from curl_multi_info_read * * @throws CurlException on Curl error */ protected function processResponse(RequestInterface $request, CurlHandle $handle, array $curl) { // Set the transfer stats on the response $handle->updateRequestFromTransfer($request); // Check if a cURL exception occurred, and if so, notify things $e = $this->isCurlException($request, $handle, $curl); // Always remove completed curl handles. They can be added back again // via events if needed (e.g. ExponentialBackoffPlugin) $this->removeHandle($request); if ($e) { // Set the state of the request to an error $request->setState(RequestInterface::STATE_ERROR); // Notify things that listen to the request of the failure $request->dispatch('request.exception', array( 'request' => $this, 'exception' => $e )); // Allow things to ignore the error if possible if ($request->getState() != RequestInterface::STATE_TRANSFER) { $this->remove($request); throw $e; } } else { $request->setState(RequestInterface::STATE_COMPLETE); // Allow things to ignore the error if possible if ($request->getState() != RequestInterface::STATE_TRANSFER) { $this->remove($request); } } } /** * Remove a curl handle from the curl multi object * * @param RequestInterface $request Request that owns the handle */ protected function removeHandle(RequestInterface $request) { $handle = $this->getRequestHandle($request); if ($handle) { unset($this->handles[spl_object_hash($request)]); // Nasty things (Bus errors segmentation faults) can sometimes // happen when removing cURL handles from inside the context of a // callback or in a recursive scope. Because of this, here we are // queueing all curl handles that need to be removed and closed // so that this happens only in the outermost scope when everything // has completed sending. $this->removeHandles[] = $handle; } } /** * Check if a cURL transfer resulted in what should be an exception * * @param RequestInterface $request Request to check * @param CurlHandle $handle Curl handle object * @param array $curl Array returned from curl_multi_info_read * * @return Exception|bool */ private function isCurlException(RequestInterface $request, CurlHandle $handle, array $curl) { if (CURLE_OK == $curl['result']) { return false; } $handle->setErrorNo($curl['result']); $e = new CurlException(sprintf('[curl] %s: %s [url] %s [info] %s [debug] %s', $handle->getErrorNo(), $handle->getError(), $handle->getUrl(), var_export($handle->getInfo(), true), $handle->getStderr())); $e->setRequest($request) ->setError($handle->getError(), $handle->getErrorNo()); return $e; } /** * Get the curl handle associated with a request * * @param RequestInterface $request Request * * @return CurlHandle|null */ private function getRequestHandle(RequestInterface $request) { $hash = spl_object_hash($request); return isset($this->handles[$hash]) ? $this->handles[$hash] : null; } /** * Throw an exception for a cURL multi response if needed * * @param int $code Curl response code * * @throws CurlException */ private function checkCurlResult($code) { if ($code <= 0) { return; } if (isset($this->multiErrors[$code])) { $message = "cURL error: {$code} ({$this->multiErrors[$code][0]}): cURL message: {$this->multiErrors[$code][1]}"; } else { $message = 'Unexpected cURL error: ' . $code; } throw new CurlException($message); } /** * Create the new cURL multi handle with error checking */ private function createMutliHandle() { if ($this->multiHandle && is_resource($this->multiHandle)) { curl_multi_close($this->multiHandle); } $this->multiHandle = curl_multi_init(); // @codeCoverageIgnoreStart if ($this->multiHandle === false) { throw new CurlException('Unable to create multi handle'); } // @codeCoverageIgnoreEnd } } src/Guzzle/Http/Curl/CurlMultiInterface.php000064400000004107151742734600014724 0ustar00version) { $this->version = curl_version(); // Check if CURLOPT_FOLLOWLOCATION is available $this->version['follow_location'] = !ini_get('open_basedir'); } return $this->version; } /** * Get a specific type of curl information * * @param $type Version information to retrieve. This value is one of: * * - version_number: cURL 24 bit version number * - version: cURL version number, as a string * - ssl_version_number: OpenSSL 24 bit version number * - ssl_version: OpenSSL version number, as a string * - libz_version: zlib version number, as a string * - host: Information about the host where cURL was built * - features: A bitmask of the CURL_VERSION_XXX constants * - protocols: An array of protocols names supported by cURL * * @return string|float|bool if the $type is found, and false if not found */ public function get($type) { $version = $this->getAll(); return isset($version[$type]) ? $version[$type] : false; } } src/Guzzle/Http/Curl/RequestMediator.php000064400000005420151742734600014277 0ustar00request = $request; $this->emitIo = $request->getParams()->get('curl.emit_io'); } /** * Receive a response header from curl * * @param resource $curl Curl handle * @param string $header Received header * * @return int */ public function receiveResponseHeader ($curl, $header) { return $this->request->receiveResponseHeader($header); } /** * Received a progress notification * * @param int $downloadSize Total download size * @param int $downloaded Amount of bytes downloaded * @param int $uploadSize Total upload size * @param int $uploaded Amount of bytes uploaded */ public function progress($downloadSize, $downloaded, $uploadSize, $uploaded) { $this->request->dispatch('curl.callback.progress', array( 'request' => $this->request, 'download_size' => $downloadSize, 'downloaded' => $downloaded, 'upload_size' => $uploadSize, 'uploaded' => $uploaded )); } /** * Write data to the response body of a request * * @param resource $curl Curl handle * @param string $write Data that was received * * @return int */ public function writeResponseBody($curl, $write) { if ($this->emitIo) { $this->request->dispatch('curl.callback.write', array( 'request' => $this->request, 'write' => $write )); } return $this->request->getResponse()->getBody()->write($write); } /** * Read data from the request body and send it to curl * * @param resource $ch Curl handle * @param resource $fd File descriptor * @param int $length Amount of data to read * * @return string */ public function readRequestBody($ch, $fd, $length) { $read = ''; if ($this->request->getBody()) { $read = $this->request->getBody()->read($length); if ($this->emitIo) { $this->request->dispatch('curl.callback.read', array( 'request' => $this->request, 'read' => $read )); } } return !$read ? '' : $read; } } src/Guzzle/Http/EntityBody.php000064400000014127151742734600012353 0ustar00handleCompression($filter); $this->contentEncoding = $result ? $filter : false; return $result; } /** * Uncompress a deflated string. Once uncompressed, the uncompressed * string is then used as the wrapped stream. * * @param string $filter De-compression filter * * @return bool Returns TRUE on success or FALSE on failure */ public function uncompress($filter = 'zlib.inflate') { $offsetStart = 0; // When inflating gzipped data, the first 10 bytes must be stripped // if a gzip header is present if ($filter == 'zlib.inflate') { // @codeCoverageIgnoreStart if (!$this->isReadable() || ($this->isConsumed() && !$this->isSeekable())) { return false; } // @codeCoverageIgnoreEnd $this->seek(0); if (fread($this->stream, 3) == "\x1f\x8b\x08") { $offsetStart = 10; } } $this->contentEncoding = false; return $this->handleCompression($filter, $offsetStart); } /** * Get the Content-Length of the entity body if possible (alias of getSize) * * @return int|false */ public function getContentLength() { return $this->getSize(); } /** * Guess the Content-Type or return the default application/octet-stream * * @return string * @see http://www.php.net/manual/en/function.finfo-open.php */ public function getContentType() { if (!class_exists('finfo', false) || !($this->isLocal() && $this->getWrapper() == 'plainfile' && file_exists($this->getUri()))) { return 'application/octet-stream'; } $finfo = new \finfo(FILEINFO_MIME_TYPE); return $finfo->file($this->getUri()); } /** * Get an MD5 checksum of the stream's contents * * @param bool $rawOutput Whether or not to use raw output * @param bool $base64Encode whether or not to base64 encode * raw output (only if raw output is true) * * @return bool|string Returns an MD5 string on success or FALSE on failure */ public function getContentMd5($rawOutput = false, $base64Encode = false) { if (!$this->seek(0)) { return false; } $ctx = hash_init('md5'); while ($data = $this->read(1024)) { hash_update($ctx, $data); } $out = hash_final($ctx, (bool) $rawOutput); $this->seek(0); return ((bool) $base64Encode && (bool) $rawOutput) ? base64_encode($out) : $out; } /** * Set the type of encoding stream that was used on the entity body * * @param string $streamFilterContentEncoding Stream filter used * * @return EntityBody */ public function setStreamFilterContentEncoding($streamFilterContentEncoding) { $this->contentEncoding = $streamFilterContentEncoding; return $this; } /** * Get the Content-Encoding of the EntityBody * * @return bool|string */ public function getContentEncoding() { return strtr($this->contentEncoding, array( 'zlib.deflate' => 'gzip', 'bzip2.compress' => 'compress' )) ?: false; } /** * Handles compression or uncompression of stream data * * @param $filter Name of the filter to use (zlib.deflate or zlib.inflate) * @param int $offsetStart Number of bytes to skip from start * * @return bool Returns TRUE on success or FALSE on failure */ protected function handleCompression($filter, $offsetStart = null) { // @codeCoverageIgnoreStart if (!$this->isReadable() || ($this->isConsumed() && !$this->isSeekable())) { return false; } // @codeCoverageIgnoreEnd $handle = fopen('php://temp', 'r+'); $filter = @stream_filter_append($handle, $filter, STREAM_FILTER_WRITE); if (!$filter) { return false; } // Seek to the beginning of the stream if possible $this->seek(0); if ($offsetStart) { fread($this->stream, $offsetStart); } while ($data = fread($this->stream, 8096)) { fwrite($handle, $data); } fclose($this->stream); $this->stream = $handle; stream_filter_remove($filter); $stat = fstat($this->stream); $this->size = $stat['size']; $this->rebuildCache(); $this->seek(0); return true; } } src/Guzzle/Http/Exception/BadResponseException.php000064400000003661151742734600016304 0ustar00isClientError()) { $label = 'Client error response'; $class = __NAMESPACE__ . '\\ClientErrorResponseException'; } elseif ($response->isServerError()) { $label = 'Server error response'; $class = __NAMESPACE__ . '\\ServerErrorResponseException'; } else { $label = 'Unsuccessful response'; $class = __CLASS__; $e = new self(); } $message = $label . PHP_EOL . implode(PHP_EOL, array( '[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[url] ' . $request->getUrl(), '[request] ' . (string) $request, '[response] ' . (string) $response )); $e = new $class($message); $e->setResponse($response); $e->setRequest($request); return $e; } /** * Set the response that caused the exception * * @param Response $response Response to set */ public function setResponse(Response $response) { $this->response = $response; } /** * Get the response that caused the exception * * @return Response */ public function getResponse() { return $this->response; } } src/Guzzle/Http/Exception/ClientErrorResponseException.php000064400000000260151742734600020036 0ustar00curlError = $error; $this->curlErrorNo = $number; return $this; } /** * Get the associated cURL error message * * @return string */ public function getError() { return $this->curlError; } /** * Get the associated cURL error number * * @return int */ public function getErrorNo() { return $this->curlErrorNo; } } src/Guzzle/Http/Exception/RequestException.php000064400000001461151742734600015523 0ustar00request = $request; return $this; } /** * Get the request that caused the exception * * @return RequestInterface */ public function getRequest() { return $this->request; } } src/Guzzle/Http/Exception/ServerErrorResponseException.php000064400000000260151742734600020066 0ustar00params; } /** * Add a header to an existing collection of headers. * * @param string $header Header name to add * @param string $value Value of the header * * @return AbstractMessage */ public function addHeader($header, $value) { $key = strtolower($header); if (!isset($this->headers[$key])) { $this->headers[$key] = new Header($header, $value); } else { $this->headers[$key]->add($value, $header); } $this->changedHeader('set', $key); return $this; } /** * Add and merge in an array of HTTP headers. * * @param array $headers Associative array of header data. * * @return AbstractMessage */ public function addHeaders(array $headers) { foreach ($headers as $key => $value) { $this->addHeader($key, $value); } return $this; } /** * Retrieve an HTTP header by name. Performs a case-insensitive search of * all headers. * * @param string $header Header to retrieve. * @param bool $string Set to true to get the header as a string * * @return string|Header|null Returns NULL if no matching header is found. * Returns a string if $string is set to TRUE. Returns a Header object * if a matching header is found. */ public function getHeader($header, $string = false) { $key = strtolower($header); if (!isset($this->headers[$key])) { return null; } return $string ? (string) $this->headers[$key] : $this->headers[$key]; } /** * Get all headers as a collection * * @return Collection Returns a {@see Collection} of all headers */ public function getHeaders() { $result = array(); // Convert all of the headers into a collection foreach ($this->headers as $header) { foreach ($header->raw() as $key => $value) { $result[$key] = $value; } } return new Collection($result); } /** * Set an HTTP header * * @param string $header Name of the header to set. * @param mixed $value Value to set. * * @return AbstractMessage */ public function setHeader($header, $value) { // Remove any existing header $key = strtolower($header); unset($this->headers[$key]); if ($value instanceof Header) { $this->headers[$key] = $value; } else { // Allow for 0, '', and NULL to be set if (!$value) { $value = array($value); } $this->headers[$key] = new Header($header, $value); } $this->changedHeader('set', $key); return $this; } /** * Overwrite all HTTP headers with the supplied array of headers * * @param array $headers Associative array of header data. * * @return AbstractMessage */ public function setHeaders(array $headers) { // Get the keys that are changing $changed = array_keys($this->headers); // Erase the old headers $this->headers = array(); // Add the new headers foreach ($headers as $key => $value) { $changed[] = $key; $this->addHeader($key, $value); } // Notify of the changed headers foreach (array_unique($changed) as $header) { $this->changedHeader('set', strtolower($header)); } return $this; } /** * Check if the specified header is present. * * @param string $header The header to check. * * @return bool Returns TRUE or FALSE if the header is present */ public function hasHeader($header) { return array_key_exists(strtolower($header), $this->headers); } /** * Remove a specific HTTP header. * * @param string $header HTTP header to remove. * * @return AbstractMessage */ public function removeHeader($header) { $key = strtolower($header); unset($this->headers[$key]); $this->changedHeader('remove', $key); return $this; } /** * Get a tokenized header as a Collection * * @param string $header Header to retrieve * @param string $token Token separator * * @return Collection|null Returns a Collection object containing the * tokenized values if the header was found. Returns NULL otherwise. */ public function getTokenizedHeader($header, $token = ';') { if (!$this->hasHeader($header)) { return null; } $data = new Collection(); foreach ($this->getHeader($header) as $singleValue) { foreach (explode($token, $singleValue) as $kvp) { $parts = explode('=', $kvp, 2); if (!isset($parts[1])) { $data[count($data)] = trim($parts[0]); } else { $data->add(trim($parts[0]), trim($parts[1])); } } } foreach ($data as $key => $value) { if (is_array($value)) { $data->set($key, array_unique($value)); } } return $data; } /** * Set a tokenized header on the request that implodes a Collection of data * into a string separated by a token * * @param string $header Header to set * @param array|Collection $data Header data * @param string $token Token delimiter * * @return AbstractMessage * @throws InvalidArgumentException if data is not an array or Collection */ public function setTokenizedHeader($header, $data, $token = ';') { if (!($data instanceof Collection) && !is_array($data)) { throw new InvalidArgumentException('Data must be a Collection or array'); } $values = array(); foreach ($data as $key => $value) { foreach ((array) $value as $v) { $values[] = is_int($key) ? $v : $key . '=' . $v; } } return $this->setHeader($header, implode($token, $values)); } /** * Get a Cache-Control directive from the message * * @param string $directive Directive to retrieve * * @return null|string */ public function getCacheControlDirective($directive) { return isset($this->cacheControl[$directive]) ? $this->cacheControl[$directive] : null; } /** * Check if the message has a Cache-Control directive * * @param string $directive Directive to check * * @return bool */ public function hasCacheControlDirective($directive) { return isset($this->cacheControl[$directive]); } /** * Add a Cache-Control directive on the message * * @param string $directive Directive to set * @param bool|string $value Value to set * * @return AbstractMessage */ public function addCacheControlDirective($directive, $value = true) { $this->cacheControl[$directive] = $value; $this->rebuildCacheControlDirective(); return $this; } /** * Remove a Cache-Control directive from the message * * @param string $directive Directive to remove * * @return AbstractMessage */ public function removeCacheControlDirective($directive) { if (array_key_exists($directive, $this->cacheControl)) { unset($this->cacheControl[$directive]); $this->rebuildCacheControlDirective(); } return $this; } /** * Check to see if the modified headers need to reset any of the managed * headers like cache-control * * @param string $action One of set or remove * @param string $header Header that changed */ protected function changedHeader($action, $header) { if ($header == 'cache-control') { $this->parseCacheControlDirective(); } } /** * Parse the Cache-Control HTTP header into an array */ private function parseCacheControlDirective() { $this->cacheControl = array(); $tokenized = $this->getTokenizedHeader('Cache-Control', ',') ?: array(); foreach ($tokenized as $key => $value) { if (is_numeric($key)) { $this->cacheControl[$value] = true; } else { $this->cacheControl[$key] = $value; } } } /** * Rebuild the Cache-Control HTTP header using the user-specified values */ private function rebuildCacheControlDirective() { $cacheControl = array(); foreach ($this->cacheControl as $key => $value) { if ($value === true) { $cacheControl[] = $key; } else { $cacheControl[] = $key . '=' . $value; } } $this->headers['cache-control'] = new Header('Cache-Control', implode(', ', $cacheControl)); } /** * Get headers as a string * * @return string */ protected function getHeaderString() { $headers = ''; foreach ($this->headers as $key => $value) { foreach ($value->raw() as $k => $v) { foreach ($v as $vv) { $headers .= "{$k}: {$vv}\r\n"; } } } return $headers; } } src/Guzzle/Http/Message/EntityEnclosingRequest.php000064400000014336151742734600016336 0ustar00postFields = new QueryString(); $this->postFields->setPrefix(''); parent::__construct($method, $url, $headers); } /** * Get the HTTP request as a string * * @return string */ public function __toString() { return parent::__toString() . (count($this->getPostFields()) ? $this->postFields : $this->body); } /** * Set the body of the request * * @param string|resource|EntityBody $body Body to use in the entity body * of the request * @param string $contentType Content-Type to set. Leave null * to use an existing Content-Type or to guess the Content-Type * @param bool $tryChunkedTransfer Set to TRUE to try to use * Tranfer-Encoding chunked * * @return EntityEnclosingRequest * @throws RequestException if the protocol is < 1.1 and Content-Length can * not be determined */ public function setBody($body, $contentType = null, $tryChunkedTransfer = false) { $this->body = EntityBody::factory($body); $this->removeHeader('Content-Length'); $this->setHeader('Expect', '100-Continue'); if ($contentType) { $this->setHeader('Content-Type', (string) $contentType); } if ($tryChunkedTransfer) { $this->setHeader('Transfer-Encoding', 'chunked'); } else { $this->removeHeader('Transfer-Encoding'); // Set the Content-Length header if it can be determined $size = $this->body->getContentLength(); if ($size !== null && $size !== false) { $this->setHeader('Content-Length', $size); } elseif ('1.1' == $this->protocolVersion) { $this->setHeader('Transfer-Encoding', 'chunked'); } else { throw new RequestException('Cannot determine entity body ' . 'size and cannot use chunked Transfer-Encoding when ' . 'using HTTP/' . $this->protocolVersion ); } } return $this; } /** * Get the body of the request if set * * @return EntityBody|null */ public function getBody() { return $this->body; } /** * Get a POST field from the request * * @param string $field Field to retrive * * @return mixed|null */ public function getPostField($field) { return $this->postFields->get($field); } /** * Get the post fields that will be used in the request * * @return QueryString */ public function getPostFields() { return $this->postFields; } /** * Returns an associative array of POST field names and file paths * * @return array */ public function getPostFiles() { $files = array(); foreach ($this->postFields as $key => $value) { if (is_string($value) && substr($value, 0, 1) == '@') { $files[$key] = substr($value, 1); } } return $files; } /** * Add POST fields to use in the request * * @param QueryString|array $fields POST fields * * @return EntityEnclosingRequest */ public function addPostFields($fields) { $this->postFields->merge($fields); $this->processPostFields(); return $this; } /** * Set a POST field value * * @param string $key Key to set * @param string $value Value to set * * @return EntityEnclosingRequest */ public function setPostField($key, $value) { $this->postFields->set($key, $value); $this->processPostFields(); return $this; } /** * Add POST files to use in the upload * * @param array $files An array of filenames to POST * * @return EntityEnclosingRequest * @throws RequestException if the file cannot be read */ public function addPostFiles(array $files) { foreach ((array) $files as $key => $file) { if (!is_string($file) || empty($file)) { continue; } // Convert non-associative array keys into 'file' if (is_numeric($key)) { $key = 'file'; } // PHP's curl bindings require a leading @ for POST files if ($file[0] != '@') { $file = '@' . $file; } if (!is_readable(substr($file, 1))) { throw new RequestException('File cannot be opened for reading: ' . $file); } $this->postFields->add($key, $file); } $this->processPostFields(); return $this; } /** * Remove a POST field or file by name * * @param string $field Name of the POST field or file to remove * * @return EntityEnclosingRequest */ public function removePostField($field) { $this->postFields->remove($field); $this->processPostFields(); return $this; } /** * Determine what type of request should be sent based on post fields */ protected function processPostFields() { if (0 == count($this->getPostFiles())) { $this->setHeader('Content-Type', 'application/x-www-form-urlencoded'); $this->removeHeader('Expect'); } else { $this->setHeader('Expect', '100-Continue'); $this->setHeader('Content-Type', 'multipart/form-data'); $this->postFields->setEncodeFields(false)->setEncodeValues(false); } } } src/Guzzle/Http/Message/EntityEnclosingRequestInterface.php000064400000004574151742734600020162 0ustar00header = $header; $this->glue = $glue; if (null !== $values) { foreach ((array) $values as $key => $value) { if (is_numeric($key)) { $key = $header; } if ($value === null) { $this->add($value, $key); } else { foreach ((array) $value as $v) { $this->add($v, $key); } } } } } /** * Convert the header to a string * * @return string */ public function __toString() { if (!$this->stringCache) { $this->stringCache = implode($this->glue, $this->toArray()); } return $this->stringCache; } /** * Add a value to the list of header values * * @param string $value Value to add * @param string $header The exact header casing to add with. * Defaults to the name of the header. * * @return Header */ public function add($value, $header = null) { if (!$header) { $header = $this->getName(); } if (!array_key_exists($header, $this->values)) { $this->values[$header] = array($value); } else { $this->values[$header][] = $value; } $this->stringCache = null; $this->arrayCache = null; return $this; } /** * Get the name of the header * * @return string */ public function getName() { return $this->header; } /** * Change the glue used to implode the values * * @param string $glue Glue used to implode multiple values * * @return Header */ public function setGlue($glue) { $this->glue = $glue; $this->stringCache = null; return $this; } /** * Get the glue used to implode multiple values into a string * * @return string */ public function getGlue() { return $this->glue; } /** * Normalize the header into a single standard header with an array of values * * @return Header */ public function normalize() { $this->stringCache = null; $this->arrayCache = null; $this->values = array( $this->getName() => $this->toArray() ); return $this; } /** * Check if a particular case variation is present in the header * Example: A header exists on a message for 'Foo', and 'foo'. The Header * object will contain all of the values of 'Foo' and all of the values of * 'foo'. You can use this method to check to see if a header was set * using 'foo' (true), 'Foo' (true), 'FOO' (false), etc. * * @param string $header Exact header to check for * * @return bool */ public function hasExactHeader($header) { return array_key_exists($header, $this->values); } /** * Check if the collection of headers has a particular value * * @param string $searchValue Value to search for * @param bool $caseInsensitive Set to TRUE to use a case * insensitive search * * @return bool */ public function hasValue($searchValue, $caseInsensitive = false) { foreach ($this->toArray() as $value) { if ($value == $searchValue) { return true; } elseif ($caseInsensitive && !strcasecmp($value, $searchValue)) { return true; } } return false; } /** * Get all of the header values as a flat array * * @return array */ public function toArray() { if (!$this->arrayCache) { $this->arrayCache = array(); foreach ($this->values as $values) { foreach ($values as $value) { $this->arrayCache[] = $value; } } } return $this->arrayCache; } /** * Get the raw data array of the headers. This array is represented as an * associative array of the various cases that might be stored in the * header and an array of values associated with each case variation. * * @return array */ public function raw() { return $this->values; } /** * Returns the total number of header values * * @return int */ public function count() { return count($this->toArray()); } /** * Get an iterator that can be used to easily iterate over each header value * * @return ArrayIterator */ public function getIterator() { return new \ArrayIterator($this->toArray()); } } src/Guzzle/Http/Message/HeaderComparison.php000064400000010312151742734600015060 0ustar00getAll(); } foreach ($filteredHeaders as $k => $v) { if ($k[0] == '_') { // This header should be ignored $ignore[] = str_replace('_', '', $k); } elseif ($k[0] == '!') { // This header must not be present $absent[] = str_replace('!', '', $k); } else { $expected[$k] = $v; } } return $this->compareArray($expected, $actualHeaders, $ignore, $absent); } /** * Check if an array of HTTP headers matches another array of HTTP headers * while taking * into account as a wildcard for header values * * @param array $expected Expected HTTP headers (allows wildcard values) * @param array|Collection $actual Actual HTTP header array * @param array $ignore Headers to ignore from the comparison * @param array $absent Array of headers that must not be present * * @return array|false Returns an array of the differences or FALSE if none */ public function compareArray(array $expected, $actual, array $ignore = array(), array $absent = array()) { $differences = array(); // Add information about headers that were present but weren't supposed to be foreach ($absent as $header) { if ($this->hasKey($header, $actual)) { $differences["++ {$header}"] = $actual[$header]; unset($actual[$header]); } } // Check if expected headers are missing foreach ($expected as $header => $value) { if (!$this->hasKey($header, $actual)) { $differences["- {$header}"] = $value; } } // Flip the ignore array so it works with the case insenstivie helper $ignore = array_flip($ignore); // Allow case-insenstivie comparisons in wildcards $expected = array_change_key_case($expected); // Compare the expected and actual HTTP headers in no particular order foreach ($actual as $key => $value) { // If this is to be ignored, the skip it if ($this->hasKey($key, $ignore)) { continue; } // If the header was not expected if (!$this->hasKey($key, $expected)) { $differences["+ {$key}"] = $value; continue; } // Check values and take wildcards into account $lkey = strtolower($key); $pos = is_string($expected[$lkey]) ? strpos($expected[$lkey], '*') : false; foreach ((array) $actual[$key] as $v) { if (($pos === false && $v != $expected[$lkey]) || $pos > 0 && substr($v, 0, $pos) != substr($expected[$lkey], 0, $pos)) { $differences[$key] = "{$value} != {$expected[$lkey]}"; } } } return empty($differences) ? false : $differences; } /** * Case insensitive check if an array have a key * * @param string $key Key to check * @param array $array Array to check * * @return bool */ protected function hasKey($key, $array) { foreach (array_keys($array) as $k) { if (!strcasecmp($k, $key)) { return true; } } return false; } } src/Guzzle/Http/Message/MessageInterface.php000064400000010157151742734600015051 0ustar00method = strtoupper($method); $this->curlOptions = new Collection(); $this->params = new Collection(); $this->setUrl($url); if ($headers) { // Special handling for multi-value headers foreach ($headers as $key => $value) { $lkey = strtolower($key); // Deal with collisions with Host and Authorization if ($lkey == 'host') { $this->setHeader($key, $value); } elseif ($lkey == 'authorization') { $parts = explode(' ', $value); if ($parts[0] == 'Basic' && isset($parts[1])) { list($user, $pass) = explode(':', base64_decode($parts[1])); $this->setAuth($user, $pass); } else { $this->setHeader($key, $value); } } else { foreach ((array) $value as $v) { $this->addHeader($key, $v); } } } } if (!$this->hasHeader('User-Agent', true)) { $this->setHeader('User-Agent', Utils::getDefaultUserAgent()); } $this->cookie = Cookie::factory($this->getHeader('Cookie')); $this->setState(self::STATE_NEW); } /** * Clone the request object, leaving off any response that was received */ public function __clone() { if ($this->eventDispatcher) { $this->eventDispatcher = clone $this->eventDispatcher; } $this->curlOptions = clone $this->curlOptions; $this->params = clone $this->params; $this->url = clone $this->url; // Get a clone of the headers $headers = array(); foreach ($this->headers as $k => $v) { $headers[$k] = clone $v; } $this->headers = $headers; $this->response = $this->responseBody = null; $this->params->remove('curl_handle') ->remove('queued_response') ->remove('curl_multi'); $this->setState(RequestInterface::STATE_NEW); } /** * Get the HTTP request as a string * * @return string */ public function __toString() { return $this->getRawHeaders() . "\r\n\r\n"; } /** * Default method that will throw exceptions if an unsuccessful response * is received. * * @param Event $event Received * @throws BadResponseException if the response is not successful */ public static function onRequestError(Event $event) { $e = BadResponseException::factory($event['request'], $event['response']); $event['request']->dispatch('request.exception', array( 'request' => $event['request'], 'response' => $event['response'], 'exception' => $e )); throw $e; } /** * Set the client used to transport the request * * @param ClientInterface $client * * @return Request */ public function setClient(ClientInterface $client) { $this->client = $client; return $this; } /** * Get the client used to transport the request * * @return ClientInterface $client */ public function getClient() { return $this->client; } /** * Get the raw message headers as a string * * @return string */ public function getRawHeaders() { $raw = $this->method . ' ' . $this->getResourceUri(); $protocolVersion = $this->protocolVersion ?: '1.1'; $raw = trim($raw) . ' ' . strtoupper(str_replace('https', 'http', $this->url->getScheme())) . '/' . $protocolVersion . "\r\n"; $raw .= $this->getHeaderString(); return rtrim($raw, "\r\n"); } /** * Set the URL of the request * * Warning: Calling this method will modify headers, rewrite the query * string object, and set other data associated with the request. * * @param string|Url $url Full URL to set including query string * * @return Request */ public function setUrl($url) { if ($url instanceof Url) { $this->url = $url; } else { $this->url = Url::factory($url); } // Update the port and host header $this->setPort($this->url->getPort()); if ($this->url->getUsername() || $this->url->getPassword()) { $this->setAuth($this->url->getUsername(), $this->url->getPassword()); // Remove the auth info from the URL $this->url->setUsername(null); $this->url->setPassword(null); } return $this; } /** * Send the request * * @return Response * @throws RequestException on a request error */ public function send() { if (!$this->client) { throw new RuntimeException('A client must be set on the request'); } return $this->client->send($this); } /** * Get the previously received {@see Response} or NULL if the request has * not been sent * * @return Response|null */ public function getResponse() { return $this->response; } /** * Get the collection of key value pairs that will be used as the query * string in the request * * @param bool $asString Set to TRUE to get the query as string * * @return QueryString|string */ public function getQuery($asString = false) { return $asString ? (string) $this->url->getQuery() : $this->url->getQuery(); } /** * Get the HTTP method of the request * * @return string */ public function getMethod() { return $this->method; } /** * Get the URI scheme of the request (http, https, ftp, etc) * * @return string */ public function getScheme() { return $this->url->getScheme(); } /** * Set the URI scheme of the request (http, https, ftp, etc) * * @param string $scheme Scheme to set * * @return Request */ public function setScheme($scheme) { $this->url->setScheme($scheme); return $this; } /** * Get the host of the request * * @return string */ public function getHost() { return $this->url->getHost(); } /** * Set the host of the request. * * @param string $host Host to set (e.g. www.yahoo.com, www.yahoo.com) * * @return Request */ public function setHost($host) { $this->url->setHost($host); $this->setPort($this->url->getPort()); return $this; } /** * Get the HTTP protocol version of the request * * @return string */ public function getProtocolVersion() { return $this->protocolVersion; } /** * Set the HTTP protocol version of the request (e.g. 1.1 or 1.0) * * @param string $protocol HTTP protocol version to use with the request * * @return Request */ public function setProtocolVersion($protocol) { $this->protocolVersion = $protocol; return $this; } /** * Get the path of the request (e.g. '/', '/index.html') * * @return string */ public function getPath() { return $this->url->getPath(); } /** * Set the path of the request (e.g. '/', '/index.html') * * @param string|array $path Path to set or array of segments to implode * * @return Request */ public function setPath($path) { $this->url->setPath($path); return $this; } /** * Get the port that the request will be sent on if it has been set * * @return int|null */ public function getPort() { return $this->url->getPort(); } /** * Set the port that the request will be sent on * * @param int $port Port number to set * * @return Request */ public function setPort($port) { $this->url->setPort($port); // Include the port in the Host header if it is not the default port // for the scheme of the URL $scheme = $this->url->getScheme(); if (($scheme == 'http' && $port != 80) || ($scheme == 'https' && $port != 443)) { $this->headers['host'] = new Header('Host', $this->url->getHost() . ':' . $port); } else { $this->headers['host'] = new Header('Host', $this->url->getHost()); } return $this; } /** * Get the username to pass in the URL if set * * @return string|null */ public function getUsername() { return $this->username; } /** * Get the password to pass in the URL if set * * @return string|null */ public function getPassword() { return $this->password; } /** * Set HTTP authorization parameters * * @param string|false $user User name or false disable authentication * @param string $password Password * @param string $scheme Curl authentication scheme to use * * @return Request * * @see http://www.ietf.org/rfc/rfc2617.txt * @throws RequestException */ public function setAuth($user, $password = '', $scheme = CURLAUTH_BASIC) { // If we got false or null, disable authentication if (!$user || !$password) { $this->password = $this->username = null; $this->removeHeader('Authorization'); $this->getCurlOptions()->remove(CURLOPT_HTTPAUTH); } else { $this->username = $user; $this->password = $password; // Bypass CURL when using basic auth to promote connection reuse if ($scheme == CURLAUTH_BASIC) { $this->getCurlOptions()->remove(CURLOPT_HTTPAUTH); $this->setHeader('Authorization', 'Basic ' . base64_encode($this->username . ':' . $this->password)); } else { $this->getCurlOptions()->set(CURLOPT_HTTPAUTH, $scheme) ->set(CURLOPT_USERPWD, $this->username . ':' . $this->password); } } return $this; } /** * Get the URI of the request (e.g. '/', '/index.html', '/index.html?q=1) * This is the path plus the query string, fragment * * @return string */ public function getResourceUri() { return $this->url->getPath() . (string) $this->url->getQuery(); } /** * Get the full URL of the request (e.g. 'http://www.guzzle-project.com/') * scheme://username:password@domain:port/path?query_string#fragment * * @param bool $asObject Set to TRUE to retrieve the URL as * a clone of the URL object owned by the request * * @return string|Url */ public function getUrl($asObject = false) { return $asObject ? clone $this->url : (string) $this->url; } /** * Get the state of the request. One of 'complete', 'sending', 'new' * * @return string */ public function getState() { return $this->state; } /** * Set the state of the request * * @param string $state State of the request (complete, sending, or new) * * @return Request */ public function setState($state) { $this->state = $state; if ($this->state == self::STATE_NEW) { $this->responseBody = $this->response = null; } elseif ($this->state == self::STATE_COMPLETE) { $this->processResponse(); } return $this; } /** * Get the cURL options that will be applied when the cURL handle is created * * @return Collection */ public function getCurlOptions() { return $this->curlOptions; } /** * Method to receive HTTP response headers as they are retrieved * * @param string $data Header data. * * @return integer Returns the size of the data. */ public function receiveResponseHeader($data) { $this->state = self::STATE_TRANSFER; $length = strlen($data); $data = str_replace(array("\r", "\n"), '', $data); if (strpos($data, ':') !== false) { list($header, $value) = explode(':', $data, 2); $this->response->addHeader(trim($header), trim($value)); } elseif (strlen($data) > 6) { list($dummy, $code, $status) = explode(' ', $data, 3); // 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(); } $previousResponse = $this->response; $this->response = new Response($code, null, $body); $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 )); } return $length; } /** * Manually set a response for the request. * * This method is useful for specifying a mock response for the request or * setting the response using a cache. Manually setting a response will * bypass the actual sending of a request. * * @param Response $response Response object to set * @param bool $queued Set to TRUE to keep the request in a stat * of not having been sent, but queue the response for send() * * @return Request Returns a reference to the object. */ public function setResponse(Response $response, $queued = false) { $response->setRequest($this); if ($queued) { $this->getParams()->set('queued_response', $response); } else { $this->getParams()->remove('queued_response'); $this->response = $response; $this->responseBody = $response->getBody(); $this->processResponse(); } $this->dispatch('request.set_response', $this->getEventArray()); return $this; } /** * Set the EntityBody that will hold the response message's entity body. * * This method should be invoked when you need to send the response's * entity body somewhere other than the normal php://temp buffer. For * example, you can send the entity body to a socket, file, or some other * custom stream. * * @param EntityBody $body Response body object * * @return Request */ public function setResponseBody(EntityBody $body) { $this->responseBody = $body; return $this; } /** * Determine if the response body is repeatable (readable + seekable) * * @return bool */ public function isResponseBodyRepeatable() { return !$this->responseBody ? true : $this->responseBody->isSeekable() && $this->responseBody->isReadable(); } /** * Get an array of Cookies or a specific cookie from the request * * @param string $name Cookie to retrieve * * @return null|string|Cookie Returns null if not found by name, a Cookie * object if no $name is supplied, or the cookie value by name if found * If a Cookie object is returned, changes to the cookie object does * not modify the request's cookies. You will need to set the cookie * back on the request after modifying the object. */ public function getCookie($name = null) { return !$name ? clone $this->cookie : $this->cookie->get($name); } /** * Set the Cookie header using an array or Cookie object * * @param array|Cookie $cookies Cookie data to set on the request * * @return Request */ public function setCookie($cookies) { if ($cookies instanceof Cookie) { $this->cookie = $cookies; } elseif (is_array($cookies)) { $this->cookie->replace($cookies); } else { throw new InvalidArgumentException('Invalid cookie data'); } $this->headers['cookie'] = new Header('Cookie', (string) $this->cookie); return $this; } /** * Add a Cookie value by name to the Cookie header * * @param string $name Name of the cookie to add * @param string $value Value to set * * @return Request */ public function addCookie($name, $value) { $this->cookie->add($name, $value); $this->headers['cookie'] = new Header('Cookie', (string) $this->cookie); return $this; } /** * Remove the cookie header or a specific cookie value by name * * @param string $name Cookie to remove by name. If no value is * provided, the entire Cookie header is removed from the request * * @return Request */ public function removeCookie($name = null) { $this->cookie->remove($name); $this->headers['cookie'] = new Header('Cookie', (string) $this->cookie); return $this; } /** * Returns whether or not the response served to the request can be cached * * @return bool */ public function canCache() { // Only GET and HEAD requests can be cached if ($this->method != RequestInterface::GET && $this->method != RequestInterface::HEAD) { return false; } // Never cache requests when using no-store if ($this->hasCacheControlDirective('no-store')) { return false; } return true; } /** * {@inheritdoc} */ public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) { $this->eventDispatcher = $eventDispatcher; $this->eventDispatcher->addListener('request.error', array(__CLASS__, 'onRequestError'), -255); return $this; } /** * {@inheritdoc} */ public function getEventDispatcher() { if (!$this->eventDispatcher) { $this->setEventDispatcher(new EventDispatcher()); } return $this->eventDispatcher; } /** * {@inheritdoc} */ public function dispatch($eventName, array $context = array()) { $context['request'] = $this; $this->getEventDispatcher()->dispatch($eventName, new Event($context)); } /** * {@inheritdoc} */ protected function changedHeader($action, $header) { parent::changedHeader($action, $header); if ($header === 'host') { // If the Host header was changed, be sure to update the internal URL $this->setHost((string) $this->getHeader('Host')); } elseif ($header === 'cookie') { // Be sure to get an cookie updates and update the internal Cookie if ($action === 'set') { $this->cookie = Cookie::factory($this->getHeader('Cookie')); } elseif ($this->cookie) { $this->cookie->clear(); } } } /** * Get the EntityBody that will store the received response entity body * * @return EntityBody */ protected function getResponseBody() { if ($this->responseBody === null) { $this->responseBody = EntityBody::factory(); } return $this->responseBody; } /** * Get an array containing the request and response for event notifications * * @return array */ protected function getEventArray() { return array( 'request' => $this, 'response' => $this->response ); } /** * Process a received response * * @throws BadResponseException on unsuccessful responses */ protected function processResponse() { // Use the queued response if one is set if ($this->getParams()->get('queued_response')) { $this->response = $this->getParams()->get('queued_response'); $this->responseBody = $this->response->getBody(); $this->getParams()->remove('queued_response'); } elseif (!$this->response) { // If no response, then processResponse shouldn't have been called $e = new RequestException('Error completing request'); $e->setRequest($this); throw $e; } $this->state = self::STATE_COMPLETE; // A request was sent, but we don't know if we'll send more or if the // final response will be a successful response $this->dispatch('request.sent', $this->getEventArray()); // Some response processors will remove the response or reset the state // (example: ExponentialBackoffPlugin) if ($this->state == RequestInterface::STATE_COMPLETE) { // The request completed, so the HTTP transaction is complete $this->dispatch('request.complete', $this->getEventArray()); // If the response is bad, allow listeners to modify it or throw // exceptions. You can change the response by modifying the Event // object in your listeners or calling setResponse() on the request if ($this->response->isError()) { $event = new Event($this->getEventArray()); $this->getEventDispatcher()->dispatch('request.error', $event); // Allow events of request.error to quietly change the response if ($event['response'] !== $this->response) { $this->response = $event['response']; } } // If a successful response was received, dispatch an event if ($this->response->isSuccessful()) { $this->dispatch('request.success', $this->getEventArray()); } } return $this; } } src/Guzzle/Http/Message/RequestFactory.php000064400000007171151742734600014626 0ustar00parseRequest($message); if (!$parsed) { return false; } $request = $this->fromParts($parsed['method'], $parsed['request_url'], $parsed['headers'], $parsed['body'], $parsed['protocol'], $parsed['version']); // EntityEnclosingRequest adds an "Expect: 100-Continue" header when // using a raw request body for PUT or POST requests. This factory // method should accurately reflect the message, so here we are // removing the Expect header if one was not supplied in the message. if (!isset($parsed['headers']['Expect']) && !isset($parsed['headers']['expect'])) { $request->removeHeader('Expect'); } return $request; } /** * {@inheritdoc} */ public function fromParts($method, array $urlParts, $headers = null, $body = null, $protocol = 'HTTP', $protocolVersion = '1.1') { return $this->create($method, Url::buildUrl($urlParts, true), $headers, $body) ->setProtocolVersion($protocolVersion); } /** * {@inheritdoc} */ public function create($method, $url, $headers = null, $body = null) { if ($method != 'POST' && $method != 'PUT' && $method != 'PATCH') { $c = $this->requestClass; $request = new $c($method, $url, $headers); if ($body) { $request->setResponseBody(EntityBody::factory($body)); } return $request; } $c = $this->entityEnclosingRequestClass; $request = new $c($method, $url, $headers); if ($body) { $isChunked = (string) $request->getHeader('Transfer-Encoding') == 'chunked'; if ($method == 'POST' && (is_array($body) || $body instanceof Collection)) { $request->addPostFields($body); if ($isChunked) { $request->setHeader('Transfer-Encoding', 'chunked'); } } elseif (is_resource($body) || $body instanceof EntityBody) { $request->setBody($body, (string) $request->getHeader('Content-Type'), $isChunked); } else { $request->setBody((string) $body, (string) $request->getHeader('Content-Type'), $isChunked); } } return $request; } } src/Guzzle/Http/Message/RequestFactoryInterface.php000064400000003710151742734600016442 0ustar00 'Continue', 101 => 'Switching Protocols', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 422 => 'Unprocessable Entity', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', ); /** * @var EntityBody The response body */ protected $body; /** * @var string The reason phrase of the response (human readable code) */ protected $reasonPhrase; /** * @var string The status code of the response */ protected $statusCode; /** * @var string Response protocol */ protected $protocol = 'HTTP'; /** * @var array Information about the request */ protected $info = array(); /** * @var RequestInterface Request object that may or may not be set */ protected $request = null; /** * @var array Cacheable response codes (see RFC 2616:13.4) */ protected $cacheResponseCodes = array(200, 203, 206, 300, 301, 410); /** * Create a new Response based on a raw response message * * @param string $message Response message * * @return Response|bool Returns false on error */ public static function fromMessage($message) { $data = ParserRegistry::get('message')->parseResponse($message); if (!$data) { return false; } // Always set the appropriate Content-Length unset($data['headers']['Content-Length']); unset($data['headers']['content-length']); $data['headers']['Content-Length'] = strlen($data['body']); $response = new static($data['code'], $data['headers'], $data['body']); $response->setProtocol($data['protocol'], $data['version']) ->setStatus($data['code'], $data['reason_phrase']); return $response; } /** * Construct the response * * @param string $statusCode The response status code (e.g. 200, 404, etc) * @param Collection|array $headers The response headers * @param string|resource|EntityBody $body The body of the response * * @throws BadResponseException if an invalid response code is given */ public function __construct($statusCode, $headers = null, $body = null) { if (!array_key_exists($statusCode, self::$statusTexts)) { throw new BadResponseException( 'Invalid response code: ' . $statusCode ); } $this->setStatus($statusCode); $this->params = new Collection(); $this->body = EntityBody::factory($body ?: ''); if ($headers) { if (!is_array($headers) && !($headers instanceof Collection)) { throw new BadResponseException('Invalid headers argument received'); } foreach ($headers as $key => $value) { $this->addHeaders(array($key => $value)); } } } /** * Convert the response object to a string * * @return string */ public function __toString() { return $this->getMessage(); } /** * Get the response entity body * * @param bool $asString Set to TRUE to return a string of the body rather * than a full body object * * @return EntityBody|string */ public function getBody($asString = false) { return $asString ? (string) $this->body : $this->body; } /** * Set the protocol and protocol version of the response * * @param string $protocol Response protocol * @param string $version Protocol version * * @return Response */ public function setProtocol($protocol, $version) { $this->protocol = $protocol; $this->protocolVersion = $version; return $this; } /** * Get the protocol used for the response (e.g. HTTP) * * @return string */ public function getProtocol() { return $this->protocol ?: 'HTTP'; } /** * Get the HTTP protocol version * * @return string */ public function getProtocolVersion() { return $this->protocolVersion ?: '1.1'; } /** * Get a cURL transfer information * * @param string $key A single statistic to check * * @return array|string|null Returns all stats if no key is set, a single * stat if a key is set, or null if a key is set and not found * @link http://www.php.net/manual/en/function.curl-getinfo.php */ public function getInfo($key = null) { if ($key === null) { return $this->info; } elseif (array_key_exists($key, $this->info)) { return $this->info[$key]; } else { return null; } } /** * Set the transfer information * * @param array $info Array of cURL transfer stats * * @return Response */ public function setInfo(array $info) { $this->info = $info; return $this; } /** * Set the response status * * @param int $statusCode Response status code to set * @param string $reasonPhrase Response reason phrase * * @return Response * @throws BadResponseException when an invalid response code is received */ public function setStatus($statusCode, $reasonPhrase = '') { $statusCode = (int) trim($statusCode); if (!array_key_exists($statusCode, self::$statusTexts)) { throw new BadResponseException('Invalid response code: ' . $statusCode); } $this->statusCode = $statusCode; $this->reasonPhrase = $reasonPhrase ? $reasonPhrase : self::$statusTexts[$statusCode]; return $this; } /** * Get the response status code * * @return integer */ public function getStatusCode() { return $this->statusCode; } /** * Get the entire response as a string * * @return string */ public function getMessage() { $message = $this->getRawHeaders(); // Only include the body in the message if the size is < 2MB $size = $this->body->getSize(); if ($size < 2097152) { $message .= (string) $this->body; } return $message; } /** * Get the the raw message headers as a string * * @return string */ public function getRawHeaders() { $headers = 'HTTP/1.1 ' . $this->statusCode . ' ' . $this->reasonPhrase . "\r\n"; $headers .= $this->getHeaderString(); return $headers . "\r\n"; } /** * Get the request object that is associated with this response * * @return null|Request\Request */ public function getRequest() { return $this->request; } /** * Get the response reason phrase- a human readable version of the numeric * status code * * @return string */ public function getReasonPhrase() { return $this->reasonPhrase; } /** * Get the Accept-Ranges HTTP header * * @return string Returns what partial content range types this * server supports. */ public function getAcceptRanges() { return $this->getHeader('Accept-Ranges', true); } /** * Get the Age HTTP header * * @param bool $headerOnly Set to TRUE to only retrieve the * Age header rather than calculating the age * * @return integer|null Returns the age the object has been in a proxy cache * in seconds. */ public function getAge($headerOnly = false) { $age = $this->getHeader('Age', true); if (!$headerOnly && $age === null && $this->getDate()) { $age = time() - strtotime($this->getDate()); } return $age; } /** * Get the Allow HTTP header * * @return string|null Returns valid actions for a specified resource. To * be used for a 405 Method not allowed. */ public function getAllow() { return $this->getHeader('Allow', true); } /** * Check if an HTTP method is allowed by checking the Allow response header * * @param string $method Method to check * * @return bool */ public function isMethodAllowed($method) { $allow = $this->getHeader('Allow'); if ($allow) { foreach (explode(',', $allow) as $allowable) { if (!strcasecmp(trim($allowable), $method)) { return true; } } } return false; } /** * Get the Cache-Control HTTP header * * @return Header|null Returns a Header object that tells all caching * mechanisms from server to client whether they may cache this object. */ public function getCacheControl() { return $this->getHeader('Cache-Control'); } /** * Get the Connection HTTP header * * @return string */ public function getConnection() { return $this->getHeader('Connection', true); } /** * Get the Content-Encoding HTTP header * * @return string|null Returns the type of encoding used on the data. One * of compress, deflate, gzip, identity. */ public function getContentEncoding() { return $this->getHeader('Content-Encoding', true); } /** * Get the Content-Language HTTP header * * @return string|null Returns the language the content is in. */ public function getContentLanguage() { return $this->getHeader('Content-Language', true); } /** * Get the Content-Length HTTP header * * @return integer Returns the length of the response body in bytes */ public function getContentLength() { return (int) $this->getHeader('Content-Length', true); } /** * Get the Content-Location HTTP header * * @return string|null Returns an alternate location for the returned data * (e.g /index.htm) */ public function getContentLocation() { return $this->getHeader('Content-Location', true); } /** * Get the Content-Disposition HTTP header * * @return string|null Returns the Content-Disposition header */ public function getContentDisposition() { return (string) $this->getHeader('Content-Disposition')->setGlue(';'); } /** * Get the Content-MD5 HTTP header * * @return string|null Returns a Base64-encoded binary MD5 sum of the * content of the response. */ public function getContentMd5() { return $this->getHeader('Content-MD5', true); } /** * Get the Content-Range HTTP header * * @return string Returns where in a full body message this partial message * belongs (e.g. bytes 21010-47021/47022). */ public function getContentRange() { return $this->getHeader('Content-Range', true); } /** * Get the Content-Type HTTP header * * @return string Returns the mime type of this content. */ public function getContentType() { return $this->getHeader('Content-Type', true); } /** * Checks if the Content-Type is of a certain type. This is useful if the * Content-Type header contains charset information and you need to know if * the Content-Type matches a particular type. * * @param string $type Content type to check against * * @return bool */ public function isContentType($type) { return stripos($this->getContentType(), $type) !== false; } /** * Get the Date HTTP header * * @return string|null Returns the date and time that the message was sent. */ public function getDate() { return $this->getHeader('Date', true); } /** * Get the ETag HTTP header * * @return string|null Returns an identifier for a specific version of a * resource, often a Message digest. */ public function getEtag() { $etag = $this->getHeader('ETag', true); return $etag ? str_replace('"', '', $etag) : null; } /** * Get the Expires HTTP header * * @return string|null Returns the date/time after which the response is * considered stale. */ public function getExpires() { return $this->getHeader('Expires', true); } /** * Get the Last-Modified HTTP header * * @return string|null Returns the last modified date for the requested * object, in RFC 2822 format (e.g. Tue, 15 Nov 1994 12:45:26 GMT) */ public function getLastModified() { return $this->getHeader('Last-Modified', true); } /** * Get the Location HTTP header * * @return string|null Used in redirection, or when a new resource has been * created. (e.g. http://www.w3.org/pub/WWW/People.html) */ public function getLocation() { return $this->getHeader('Location', true); } /** * Get the Pragma HTTP header * * @return Header|null Returns the implementation-specific headers that may * have various effects anywhere along the request-response chain. */ public function getPragma() { return $this->getHeader('Pragma'); } /** * Get the Proxy-Authenticate HTTP header * * @return string|null Authentication to access the proxy (e.g. Basic) */ public function getProxyAuthenticate() { return $this->getHeader('Proxy-Authenticate', true); } /** * Get the Retry-After HTTP header * * @return int|null If an entity is temporarily unavailable, this * instructs the client to try again after a specified period of time. */ public function getRetryAfter() { $time = $this->getHeader('Retry-After', true); if ($time === null) { return null; } if (!is_numeric($time)) { $time = strtotime($time) - time(); } return (int) $time; } /** * Get the Server HTTP header * * @return string|null A name for the server */ public function getServer() { return $this->getHeader('Server', true); } /** * Get the Set-Cookie HTTP header * * @return Header|null An HTTP cookie. */ public function getSetCookie() { return $this->getHeader('Set-Cookie'); } /** * Get the Trailer HTTP header * * @return string|null The Trailer general field value indicates that the * given set of header fields is present in the trailer of a message * encoded with chunked transfer-coding. */ public function getTrailer() { return $this->getHeader('Trailer', true); } /** * Get the Transfer-Encoding HTTP header * * @return string|null The form of encoding used to safely transfer the * entity to the user. Currently defined methods are: chunked */ public function getTransferEncoding() { return $this->getHeader('Transfer-Encoding', true); } /** * Get the Vary HTTP header * * @return string|null Tells downstream proxies how to match future request * headers to decide whether the cached response can be used rather * than requesting a fresh one from the origin server. */ public function getVary() { return $this->getHeader('Vary', true); } /** * Get the Via HTTP header * * @return string|null Informs the client of proxies through which the * response was sent. (e.g. 1.0 fred, 1.1 nowhere.com (Apache/1.1)) */ public function getVia() { return $this->getHeader('Via', true); } /** * Get the Warning HTTP header * * @return string|null A general warning about possible problems with the * entity body. (e.g. 199 Miscellaneous warning) */ public function getWarning() { return $this->getHeader('Warning', true); } /** * Get the WWW-Authenticate HTTP header * * @return string|null Indicates the authentication scheme that should be * used to access the requested entity (e.g. Basic) */ public function getWwwAuthenticate() { return $this->getHeader('WWW-Authenticate', true); } /** * Checks if HTTP Status code is a Client Error (4xx) * * @return bool */ public function isClientError() { return substr(strval($this->statusCode), 0, 1) == '4'; } /** * Checks if HTTP Status code is Server OR Client Error (4xx or 5xx) * * @return boolean */ public function isError() { return $this->isClientError() || $this->isServerError(); } /** * Checks if HTTP Status code is Information (1xx) * * @return bool */ public function isInformational() { return substr(strval($this->statusCode), 0, 1) == '1'; } /** * Checks if HTTP Status code is a Redirect (3xx) * * @return bool */ public function isRedirect() { return substr(strval($this->statusCode), 0, 1) == '3'; } /** * Checks if HTTP Status code is Server Error (5xx) * * @return bool */ public function isServerError() { return substr(strval($this->statusCode), 0, 1) == '5'; } /** * Checks if HTTP Status code is Successful (2xx | 304) * * @return bool */ public function isSuccessful() { return substr(strval($this->statusCode), 0, 1) == '2' || $this->statusCode == '304'; } /** * Set the request object associated with the response * * @param RequestInterface The request object used to generate the response * * @return Response */ public function setRequest(RequestInterface $request) { $this->request = $request; return $this; } /** * Check if the response can be cached * * @return bool Returns TRUE if the response can be cached or false if not */ public function canCache() { // Check if the response is cacheable based on the code if (!in_array((int) $this->getStatusCode(), $this->cacheResponseCodes)) { return false; } // Make sure a valid body was returned and can be cached if ((!$this->getBody()->isReadable() || !$this->getBody()->isSeekable()) && ($this->getContentLength() > 0 || $this->getTransferEncoding() == 'chunked')) { return false; } // Never cache no-store resources (this is a private cache, so private // can be cached) if ($this->hasCacheControlDirective('no-store')) { return false; } // HTTPS responses must send a Cache-Control: public value for caching if ($this->getRequest() && $this->getRequest()->getScheme() == 'https' && !$this->hasCacheControlDirective('public')) { return false; } return $this->isFresh() || $this->getFreshness() === null || $this->canValidate(); } /** * Gets the number of seconds from the current time in which this response * is still considered fresh as specified in RFC 2616-13 * * @return int|null Returns the number of seconds */ public function getMaxAge() { // s-max-age, then max-age, then Expires if ($age = $this->getCacheControlDirective('s-maxage')) { return $age; } if ($age = $this->getCacheControlDirective('max-age')) { return $age; } if ($this->getHeader('Expires')) { return strtotime($this->getExpires()) - time(); } return null; } /** * Check if the response is considered fresh. * * A response is considered fresh when its age is less than the freshness * lifetime (maximum age) of the response. * * @return bool|null */ public function isFresh() { $fresh = $this->getFreshness(); return $fresh === null ? null : $this->getFreshness() > 0; } /** * Check if the response can be validated against the origin server using * a conditional GET request. * * @return bool */ public function canValidate() { return $this->getEtag() || $this->getLastModified(); } /** * Get the freshness of the response by returning the difference of the * maximum lifetime of the response and the age of the response * (max-age - age). * * Freshness values less than 0 mean that the response is no longer fresh * and is ABS(freshness) seconds expired. Freshness values of greater than * zer0 is the number of seconds until the response is no longer fresh. * A NULL result means that no freshness information is available. * * @return int */ public function getFreshness() { $maxAge = $this->getMaxAge(); $age = $this->getAge(); return $maxAge && $age ? ($maxAge - $age) : null; } } src/Guzzle/Http/Parser/Cookie/CookieParser.php000064400000007653151742734600015322 0ustar00 'Domain', 'path' => 'Path', 'max_age' => 'Max-Age', 'expires' => 'Expires', 'version' => 'Version', 'secure' => 'Secure', 'port' => 'Port', 'discard' => 'Discard', 'comment' => 'Comment', 'comment_url' => 'Comment-Url', 'http_only' => 'HttpOnly' ); // Create the default return array $data = array_fill_keys(array_keys($parts), null); $data['cookies'] = array(); $data['data'] = array(); $data['path'] = '/'; $data['http_only'] = false; // Add the default domain and path // These values are overridden if using Set-Cookie2 cookie values. if ($host) { $data['domain'] = $host; } if ($path) { $data['path'] = $path; } $foundCookies = $foundNonCookies = 0; // Add the cookie pieces into the parsed data array foreach ($pieces as $part) { $avPair = array_map('trim', explode('=', $part, 2)); $key = $avPair[0]; if (count($avPair) === 1) { // Matches secure, httpOnly, etc... $value = true; } else { $value = isset($avPair[1]) ? $avPair[1] : ''; // Remove wrapping quotes if (strpos($value, '"') === 0) { $value = substr($value, 1); } if (substr($value, -1, 1) == '"') { $value = substr($value, 0, -1); } } // Check if the key is in the cookie pieces $found = false; if ($foundCookies) { foreach ($parts as $mapValue => $search) { if (!strcasecmp($search, $key)) { $key = $mapValue; $found = true; break; } } } // Decode the value if $decode is TRUE $value = ($decode && $value && !is_bool($value)) ? urldecode($value) : $value; if (0 == $foundCookies || !$found) { // If cookies have not yet been retrieved, or this value was // not found in the cookie pieces array, treat as a cookie // IF non-cookies have been parsed, then this isn't a cookie, // but it's cookie data. Cookies must be first, followed by data. if ($foundNonCookies == 0) { $data['cookies'][$key] = $value; $foundCookies++; } else { $data['data'][$key] = $value; } } else { if ($key == 'port') { $value = array_map('trim', explode(',', $value)); } $data[$key] = $value; $foundNonCookies++; } } // Calculate the expires date if (!$data['expires'] && $data['max_age']) { $data['expires'] = time() + (int)$data['max_age']; } return $data; } } src/Guzzle/Http/Parser/Cookie/CookieParserInterface.php000064400000002735151742734600017137 0ustar00 $requestUrl, 'scheme' => 'http' ); // Check for the Host header if (isset($parts['headers']['Host'])) { $urlParts['host'] = $parts['headers']['Host']; } elseif (isset($parts['headers']['host'])) { $urlParts['host'] = $parts['headers']['host']; } else { $urlParts['host'] = ''; } if (false === strpos($urlParts['host'], ':')) { $urlParts['port'] = ''; } else { $hostParts = explode(':', $urlParts['host']); $urlParts['host'] = trim($hostParts[0]); $urlParts['port'] = (int) trim($hostParts[1]); if ($urlParts['port'] == 443) { $urlParts['scheme'] = 'https'; } } // Check if a query is present $path = $urlParts['path']; $qpos = strpos($path, '?'); if ($qpos) { $urlParts['query'] = substr($path, $qpos + 1); $urlParts['path'] = substr($path, 0, $qpos); } else { $urlParts['query'] = ''; } return $urlParts; } } src/Guzzle/Http/Parser/Message/MessageParser.php000064400000006106151742734600015640 0ustar00parseMessage($message); // Parse the protocol and protocol version list($protocol, $version) = explode('/', $parts['start_line'][2]); $parsed = array( 'method' => strtoupper($parts['start_line'][0]), 'protocol' => strtoupper($protocol), 'version' => $version, 'headers' => $parts['headers'], 'body' => $parts['body'] ); $parsed['request_url'] = $this->getUrlPartsFromMessage($parts['start_line'][1], $parsed); return $parsed; } /** * {@inheritdoc} */ public function parseResponse($message) { if (!$message) { return false; } $parts = $this->parseMessage($message); list($protocol, $version) = explode('/', trim($parts['start_line'][0])); return array( 'protocol' => $protocol, 'version' => $version, 'code' => $parts['start_line'][1], 'reason_phrase' => $parts['start_line'][2], 'headers' => $parts['headers'], 'body' => $parts['body'] ); } /** * Parse a message into parts * * @param string $message Message to parse * * @return array */ protected function parseMessage($message) { $startLine = null; $headers = array(); $body = ''; // Iterate over each line in the message, accounting for line endings $lines = preg_split('/(\\r?\\n)/', $message, -1, PREG_SPLIT_DELIM_CAPTURE); for ($i = 0, $totalLines = count($lines); $i < $totalLines; $i += 2) { $line = $lines[$i]; // If two line breaks were encountered, then this is the end of body if (empty($line)) { if ($i < $totalLines - 1) { $body = implode('', array_slice($lines, $i + 2)); } break; } // Parse message headers if (!$startLine) { $startLine = explode(' ', $line, 3); } else if (strpos($line, ':')) { $parts = explode(':', $line, 2); $key = trim($parts[0]); $value = isset($parts[1]) ? trim($parts[1]) : ''; if (!isset($headers[$key])) { $headers[$key] = $value; } elseif (!is_array($headers[$key])) { $headers[$key] = array($headers[$key], $value); } else { $headers[$key][] = $value; } } } return array( 'start_line' => $startLine, 'headers' => $headers, 'body' => $body ); } } src/Guzzle/Http/Parser/Message/MessageParserInterface.php000064400000001256151742734600017462 0ustar00 $parts->requestMethod, 'protocol' => 'HTTP', 'version' => number_format($parts->httpVersion, 1), 'headers' => $parts->headers, 'body' => $parts->body ); $parsed['request_url'] = $this->getUrlPartsFromMessage($parts->requestUrl, $parsed); return $parsed; } /** * {@inheritdoc} */ public function parseResponse($message) { if (!$message) { return false; } $parts = http_parse_message($message); return array( 'protocol' => 'HTTP', 'version' => number_format($parts->httpVersion, 1), 'code' => $parts->responseCode, 'reason_phrase' => $parts->responseStatus, 'headers' => $parts->headers, 'body' => $parts->body ); } } src/Guzzle/Http/Parser/ParserRegistry.php000064400000002755151742734600014506 0ustar00 'Guzzle\\Http\\Parser\\Message\\MessageParser', 'cookie' => 'Guzzle\\Http\\Parser\\Cookie\\CookieParser', 'url' => 'Guzzle\\Http\\Parser\\Url\\UrlParser', 'uri_template' => 'Guzzle\\Http\\Parser\\UriTemplate\\UriTemplate', ); /** * Get a specific parser by handle name * * @param string $name Name of the parser to retrieve * * @return mixed|null Returns null if the parser is not found or cannot * be instantiated */ public static function get($name) { if (!isset(self::$instances[$name])) { if (!isset(self::$mapping[$name])) { return null; } $class = self::$mapping[$name]; self::$instances[$name] = new $class(); } return self::$instances[$name]; } /** * Register a custom parser by name with the register * * @param string $name Name or handle of the parser to register * @param mixed $parser Instantiated parser to register */ public static function set($name, $parser) { self::$instances[$name] = $parser; } } src/Guzzle/Http/Parser/UriTemplate/UriTemplate.php000064400000016404151742734600016203 0ustar00 true, '#' => true, '.' => true, '/' => true, ';' => true, '?' => true, '&' => true ); /** * @var array Delimiters */ private static $delims = array(':', '/', '?', '#', '[', ']', '@', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '='); /** * @var array Percent encoded delimiters */ private static $delimsPct = array('%3A', '%2F', '%3F', '%23', '%5B', '%5D', '%40', '%21', '%24', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C', '%3B', '%3D'); /** * Expand the URI template using the supplied variables * * @param string $template URI Template to expand * @param array $variables Variables to use with the expansion * * @return string Returns the expanded template */ public function expand($template, array $variables) { $this->template = $template; $this->variables = $variables; // Check to ensure that the preg_* function is needed if (false === strpos($this->template, '{')) { return $this->template; } return preg_replace_callback(self::$regex, array($this, 'expandMatch'), $this->template); } /** * Parse an expression into parts * * @param string $expression Expression to parse * * @return array Returns an associative array of parts */ private function parseExpression($expression) { // Check for URI operators $operator = ''; if (isset(self::$operatorHash[$expression[0]])) { $operator = $expression[0]; $expression = substr($expression, 1); } $values = explode(',', $expression); foreach ($values as &$value) { $value = trim($value); $varspec = array(); $substrPos = strpos($value, ':'); if ($substrPos) { $varspec['value'] = substr($value, 0, $substrPos); $varspec['modifier'] = ':'; $varspec['position'] = (int) substr($value, $substrPos + 1); } elseif (substr($value, -1) == '*') { $varspec['modifier'] = '*'; $varspec['value'] = substr($value, 0, -1); } else { $varspec['value'] = (string) $value; $varspec['modifier'] = ''; } $value = $varspec; } return array( 'operator' => $operator, 'values' => $values ); } /** * Process an expansion * * @param array $matches Matches met in the preg_replace_callback * * @return string Returns the replacement string */ private function expandMatch(array $matches) { $parsed = self::parseExpression($matches[1]); $replacements = array(); $prefix = $parsed['operator']; $joiner = $parsed['operator']; $useQueryString = false; if ($parsed['operator'] == '?') { $joiner = '&'; $useQueryString = true; } elseif ($parsed['operator'] == '&') { $useQueryString = true; } elseif ($parsed['operator'] == '#') { $joiner = ','; } elseif ($parsed['operator'] == ';') { $useQueryString = true; } elseif ($parsed['operator'] == '' || $parsed['operator'] == '+') { $joiner = ','; $prefix = ''; } foreach ($parsed['values'] as $value) { if (!array_key_exists($value['value'], $this->variables)) { continue; } $variable = $this->variables[$value['value']]; $actuallyUseQueryString = $useQueryString; $expanded = ''; if (is_array($variable)) { $isAssoc = $this->isAssoc($variable); $kvp = array(); foreach ($variable as $key => $var) { if ($isAssoc) { $key = rawurlencode($key); } $var = rawurlencode($var); if ($parsed['operator'] == '+' || $parsed['operator'] == '#') { $var = $this->decodeReserved($var); } if ($value['modifier'] == '*') { if ($isAssoc) { $var = $key . '=' . $var; } elseif ($key > 0 && $actuallyUseQueryString) { $var = $value['value'] . '=' . $var; } } $kvp[$key] = $var; } if ($value['modifier'] == '*') { $expanded = implode($joiner, $kvp); if ($isAssoc) { // Don't prepend the value name when using the explode // modifier with an associative array $actuallyUseQueryString = false; } } else { if ($isAssoc) { // When an associative array is encountered and the // explode modifier is not set, then the result must // be a comma separated list of keys followed by their // respective values. foreach ($kvp as $k => &$v) { $v = $k . ',' . $v; } } $expanded = implode(',', $kvp); } } else { if ($value['modifier'] == ':') { $variable = substr($variable, 0, $value['position']); } $expanded = rawurlencode($variable); if ($parsed['operator'] == '+' || $parsed['operator'] == '#') { $expanded = $this->decodeReserved($expanded); } } if ($actuallyUseQueryString) { if (!$expanded && $joiner != '&') { $expanded = $value['value']; } else { $expanded = $value['value'] . '=' . $expanded; } } $replacements[] = $expanded; } $ret = implode($joiner, $replacements); if ($ret && $prefix) { return $prefix . $ret; } return $ret; } /** * Determines if an array is associative * * @param array $array Array to check * * @return bool */ private function isAssoc(array $array) { return (bool) count(array_filter(array_keys($array), 'is_string')); } /** * Removes percent encoding on reserved characters (used with + and # modifiers) * * @param string $string String to fix * * @return string */ private function decodeReserved($string) { return str_replace(self::$delimsPct, self::$delims, $string); } } src/Guzzle/Http/Parser/UriTemplate/UriTemplateInterface.php000064400000000741151742734600020021 0ustar00utf8 = $utf8; } /** * {@inheritdoc} */ public function parseUrl($url) { $parts = parse_url($url); // Need to handle query parsing specially for UTF-8 requirements if ($this->utf8 && isset($parts['query'])) { $queryPos = strpos($url, '?'); if (isset($parts['fragment'])) { $parts['query'] = substr($url, $queryPos + 1, strpos($url, '#') - $queryPos - 1); } else { $parts['query'] = substr($url, $queryPos + 1); } } $parts['scheme'] = isset($parts['scheme']) ? $parts['scheme'] : null; $parts['host'] = isset($parts['host']) ? $parts['host'] : null; $parts['path'] = isset($parts['path']) ? $parts['path'] : null; $parts['port'] = isset($parts['port']) ? $parts['port'] : null; $parts['query'] = isset($parts['query']) ? $parts['query'] : null; $parts['user'] = isset($parts['user']) ? $parts['user'] : null; $parts['pass'] = isset($parts['pass']) ? $parts['pass'] : null; $parts['fragment'] = isset($parts['fragment']) ? $parts['fragment'] : null; return $parts; } } src/Guzzle/Http/Parser/Url/UrlParserInterface.php000064400000001032151742734600016006 0ustar000 to automatically flush * the queue when the number of requests is > $autoFlushCount */ public function __construct($autoFlushCount = 0) { $this->autoFlushCount = $autoFlushCount; } /** * {@inheritdoc} */ public static function getSubscribedEvents() { return array( 'client.create_request' => array('onRequestCreate', -255), 'request.before_send' => array('onRequestBeforeSend', 255), 'flush' => array('flush', -255) ); } /** * Get a count of the requests in queue * * @return int */ public function count() { return count($this->queue); } /** * Remove a request from the queue * * @param RequestInterface $request Request to remove * * @return BatchQueuePlugin */ public function removeRequest(RequestInterface $request) { $this->queue = array_filter($this->queue, function($r) use ($request) { return $r !== $request; }); return $this; } /** * Add request to the queue * * @param Event $event */ public function onRequestCreate(Event $event) { $this->addRequest($event['request']); } /** * Add request to the queue * * @param RequestInterface $request Request to add * * @return BatchQueuePlugin */ public function addRequest(RequestInterface $request) { $this->queue[] = $request; if ($this->autoFlushCount && count($this->queue) >= $this->autoFlushCount) { $this->flush(); } return $this; } /** * Ensures that queued requests that get sent outside of the context * of the batch plugin get removed from the queue * * @param Event $event */ public function onRequestBeforeSend(Event $event) { $this->removeRequest($event['request']); } /** * Flush the queue * * @param Event $event */ public function flush() { $multis = array(); // Prepare each request for their respective curl multi objects while ($request = array_shift($this->queue)) { $multi = $request->getClient()->getCurlMulti(); $multi->add($request); if (!in_array($multi, $multis)) { $multis[] = $multi; } } foreach ($multis as $multi) { $multi->send(); } } } src/Guzzle/Http/Plugin/CachePlugin.php000064400000034226151742734600013703 0ustar00adapter = $adapter; $this->serialize = (bool) $serialize; $this->defaultLifetime = (int) $defaultLifetime; } /** * {@inheritdoc} */ public static function getSubscribedEvents() { return array( 'request.before_send' => array('onRequestBeforeSend', -255), 'request.sent' => array('onRequestSent', 255) ); } /** * Get the cache adapter object * * @return CacheAdapter */ public function getCacheAdapter() { return $this->adapter; } /** * Calculate the hash key of a request object * * @param RequestInterface $request Request to hash * @param string $raw Set to TRUE to retrieve the un-encoded string for debugging * * @return string */ public function getCacheKey(RequestInterface $request, $raw = false) { // See if the key has already been calculated $key = $request->getParams()->get('cache.key'); // Always recalculate when using the raw option if (!$key || $raw) { // Generate the start of the key $key = $request->getScheme() . '_' . $request->getHost() . $request->getPath(); $filterHeaders = array('Cache-Control'); $filterQuery = array(); // Check to see how and if the key should be filtered foreach (explode(';', $request->getParams()->get('cache.key_filter')) as $part) { $pieces = array_map('trim', explode('=', $part)); if (isset($pieces[1])) { $remove = array_map('trim', explode(',', $pieces[1])); if ($pieces[0] == 'header') { $filterHeaders = array_merge($filterHeaders, $remove); } elseif ($pieces[0] == 'query') { $filterQuery = array_merge($filterQuery, $remove); } } } // Use the filtered query string $queryString = (string) $request->getQuery()->filter(function($key, $value) use ($filterQuery) { return !in_array($key, $filterQuery); }); // Use the filtered headers $headerString = http_build_query($request->getHeaders()->map(function($key, $value) { return count($value) == 1 ? $value[0] : $value; })->filter(function($key, $value) use ($filterHeaders) { return !in_array($key, $filterHeaders); })->getAll()); if ($raw) { $key = strtolower('gz_' . $key . $queryString . '_' . $headerString); } else { $key = strtolower('gz_' . md5($key . $queryString . '_' . $headerString)); $request->getParams()->set('cache.key', $key); } } return $key; } /** * Check if a response in cache will satisfy the request before sending * * @param Event $event */ public function onRequestBeforeSend(Event $event) { $request = $event['request']; // This request is being prepared $key = spl_object_hash($request); $hashKey = $this->getCacheKey($request); $this->cached[$key] = $hashKey; $cachedData = $this->getCacheAdapter()->fetch($hashKey); // If the cached data was found, then make the request into a // manually set request if ($cachedData) { if ($this->serialize) { $cachedData = unserialize($cachedData); } unset($this->cached[$key]); $response = new Response($cachedData['c'], $cachedData['h'], $cachedData['b']); $response->setHeader('Age', time() - strtotime($response->getDate() ?: 'now')); if (!$response->hasHeader('X-Guzzle-Cache')) { $response->setHeader('X-Guzzle-Cache', "key={$key}"); } // Validate that the response satisfies the request if ($this->canResponseSatisfyRequest($request, $response)) { $request->setResponse($response); } } } /** * If possible, store a response in cache after sending * * @param Event $event */ public function onRequestSent(Event $event) { $request = $event['request']; $response = $event['response']; if ($response->canCache()) { // The request is complete and now processing the response $key = spl_object_hash($request); if (isset($this->cached[$key])) { if ($response->isSuccessful()) { if ($request->getParams()->get('cache.override_ttl')) { $lifetime = $request->getParams()->get('cache.override_ttl'); $response->setHeader('X-Guzzle-Cache', "key={$key}, ttl={$lifetime}"); } else { $lifetime = $response->getMaxAge(); } $this->saveCache($this->cached[$key], $response, $lifetime); } // Remove the hashed placeholder from the parameters object unset($this->cached[$key]); } } } /** * Revalidate a cached response * * @param RequestInterface $request Request to revalidate * @param Response $response Response to revalidate * * @return bool */ public function revalidate(RequestInterface $request, Response $response) { $revalidate = clone $request; $revalidate->getEventDispatcher()->removeSubscriber($this); $revalidate->removeHeader('Pragma') ->removeHeader('Cache-Control') ->setHeader('If-Modified-Since', $response->getDate()); if ($response->getEtag()) { $revalidate->setHeader('If-None-Match', '"' . $response->getEtag() . '"'); } try { $validateResponse = $revalidate->send(); if ($validateResponse->getStatusCode() == 200) { // The server does not support validation, so use this response $request->setResponse($validateResponse); // Store this response in cache if possible if ($validateResponse->canCache()) { $this->saveCache($this->getCacheKey($request), $validateResponse, $validateResponse->getMaxAge()); } return false; } if ($validateResponse->getStatusCode() == 304) { // Make sure that this response has the same ETage if ($validateResponse->getEtag() != $response->getEtag()) { return false; } // Replace cached headers with any of these headers from the // origin server that might be more up to date $modified = false; foreach (array('Date', 'Expires', 'Cache-Control', 'ETag', 'Last-Modified') as $name) { if ($validateResponse->hasHeader($name)) { $modified = true; $response->setHeader($name, $validateResponse->getHeader($name)); } } // Store the updated response in cache if ($modified && $response->canCache()) { $this->saveCache($this->getCacheKey($request), $response, $response->getMaxAge()); } return true; } } catch (BadResponseException $e) { // 404 errors mean the resource no longer exists, so remove from // cache, and prevent an additional request by throwing the exception if ($e->getResponse()->getStatusCode() == 404) { $this->getCacheAdapter()->delete($this->getCacheKey($request)); throw $e; } } // Other exceptions encountered in the revalidation request are ignored // in hopes that sending a request to the origin server will fix it return false; } /** * Check if a cache response satisfies a request's caching constraints * * @param RequestInterface $request Request to validate * @param Response $response Response to validate * * @return bool */ public function canResponseSatisfyRequest(RequestInterface $request, Response $response) { $responseAge = $response->getAge(); // Check the request's max-age header against the age of the response if ($request->hasCacheControlDirective('max-age') && $responseAge > $request->getCacheControlDirective('max-age')) { return false; } // Check the response's max-age header if ($response->isFresh() === false) { $maxStale = $request->getCacheControlDirective('max-stale'); if (null !== $maxStale) { if ($maxStale !== true && $response->getFreshness() < (-1 * $maxStale)) { return false; } } elseif ($responseAge > $response->getCacheControlDirective('max-age')) { return false; } } // Only revalidate GET requests if ($request->getMethod() == RequestInterface::GET) { // Check if the response must be validated against the origin server if ($request->getHeader('Pragma') == 'no-cache' || $request->hasCacheControlDirective('no-cache') || $request->hasCacheControlDirective('must-revalidate') || $response->hasCacheControlDirective('must-revalidate') || $response->hasCacheControlDirective('no-cache')) { // no-cache: When no parameters are present, always revalidate // When parameters are present in no-cache and the request includes // those same parameters, then the response must re-validate // I'll need an example of what fields look like in order to // implement a smarter version of no-cache // Requests can decline to revalidate against the origin server // by setting the cache.revalidate param to one of: // never - To never revalidate and always contact the origin server // skip - To skip revalidation and just use what is in cache switch ($request->getParams()->get('cache.revalidate')) { case 'never': return false; case 'skip': return true; } return $this->revalidate($request, $response); } } return true; } /** * 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; } } src/Guzzle/Http/Plugin/CookiePlugin.php000064400000015221151742734600014103 0ustar00jar = $storage; } /** * Clears temporary cookies */ public function __destruct() { $this->clearTemporaryCookies(); } /** * {@inheritdoc} */ public static function getSubscribedEvents() { return array( 'request.before_send' => array('onRequestBeforeSend', 100), 'request.sent' => array('onRequestSent', 100), 'request.receive.status_line' => 'onRequestReceiveStatusLine' ); } /** * Add cookies to a request based on the destination of the request and * the cookies stored in the storage backend. Any previously set cookies * will be removed. * * @param RequestInterface $request Request to add cookies to. If the * request object already has a cookie header, then no further cookies * will be added. * * @return array Returns an array of the cookies that were added */ public function addCookies(RequestInterface $request) { $request->removeHeader('Cookie'); // Find cookies that match this request $cookies = $this->jar->getCookies($request->getHost(), $request->getPath()); $match = false; if ($cookies) { foreach ($cookies as $cookie) { $match = true; // If a port restriction is set, validate the port if (!empty($cookie['port'])) { if (!in_array($request->getPort(), $cookie['port'])) { $match = false; } } // Validate the secure flag if ($cookie['secure']) { if ($request->getScheme() != 'https') { $match = false; } } // If this request is eligible for the cookie, then merge it in if ($match) { $request->addCookie($cookie['cookie'][0], isset($cookie['cookie'][1]) ? $cookie['cookie'][1] : null); } } } return $match && $cookies ? $cookies : array(); } /** * Extracts cookies from an HTTP Response object, looking for Set-Cookie: * and Set-Cookie2: headers and persists them to the cookie storage. * * @param Response $response */ public function extractCookies(Response $response) { if (!$cookie = $response->getSetCookie()) { return array(); } $cookieData = array(); $parser = ParserRegistry::get('cookie'); foreach ($cookie as $c) { $request = $response->getRequest(); if ($request) { $cdata = $parser->parseCookie($c, $request->getHost(), $request->getPath()); } else { $cdata = $parser->parseCookie($c); } //@codeCoverageIgnoreStart if (!$cdata) { continue; } //@codeCoverageIgnoreEnd $cookies = array(); // Break up cookie v2 into multiple cookies if (count($cdata['cookies']) == 1) { $cdata['cookie'] = array(key($cdata['cookies']), current($cdata['cookies'])); unset($cdata['cookies']); $cookies = array($cdata); } else { foreach ($cdata['cookies'] as $key => $cookie) { $row = $cdata; unset($row['cookies']); $row['cookie'] = array($key, $cookie); $cookies[] = $row; } } if (count($cookies)) { foreach ($cookies as &$c) { $this->jar->save($c); $cookieData[] = $c; } } } return $cookieData; } /** * Clear cookies currently held in the Cookie storage. * * Invoking this method without arguments will empty the whole Cookie * storage. If given a $domain argument only cookies belonging to that * domain will be removed. If given a $domain and $path argument, cookies * belonging to the specified path within that domain are removed. If given * all three arguments, then the cookie with the specified name, path and * domain is removed. * * @param string $domain Set to clear only cookies matching a domain * @param string $path Set to clear only cookies matching a domain and path * @param string $name Set to clear only cookies matching a domain, path, and name * * @return int Returns the number of deleted cookies */ public function clearCookies($domain = null, $path = null, $name = null) { return $this->jar->clear(str_replace(array('http://', 'https://'), '', $domain), $path, $name); } /** * Discard all temporary cookies. * * Scans for all cookies in the storage with either no expire field or a * true discard flag. To be called when the user agent shuts down according * to RFC 2965. * * @return int Returns the number of deleted cookies */ public function clearTemporaryCookies() { return $this->jar->clearTemporary(); } /** * Add cookies before a request is sent * * @param Event $event */ public function onRequestBeforeSend(Event $event) { $request = $event['request']; if (!$request->getParams()->get('cookies.disable')) { $this->addCookies($request); } } /** * Extract cookies from a sent request * * @param Event $event */ public function onRequestSent(Event $event) { $this->extractCookies($event['response']); } /** * Extract cookies from a redirect response * * @param Event $event */ public function onRequestReceiveStatusLine(Event $event) { if ($event['previous_response']) { $this->extractCookies($event['previous_response']); } } } src/Guzzle/Http/Plugin/CurlAuthPlugin.php000064400000002205151742734600014417 0ustar00username = $username; $this->password = $password; $this->scheme = $scheme; } /** * {@inheritdoc} */ public static function getSubscribedEvents() { return array('client.create_request' => array('onRequestCreate', 255)); } /** * Add basic auth * * @param Event $event */ public function onRequestCreate(Event $event) { $event['request']->setAuth($this->username, $this->password, $this->scheme); } } src/Guzzle/Http/Plugin/ExponentialBackoffPlugin.php000064400000021000151742734600016424 0ustar00setMaxRetries($maxRetries); $this->setFailureCodes($failureCodes); if (!$delayFunction) { $this->delayClosure = array($this, 'calculateWait'); } else { $this->delayClosure = $delayFunction; } // @codeCoverageIgnoreStart if (!self::$defaultFailureCodesHash) { self::$defaultFailureCodesHash = array_fill_keys(self::$defaultFailureCodes, 1); } // @codeCoverageIgnoreEnd } /** * Get a default array of codes and cURL errors to retry * * @return array */ public static function getDefaultFailureCodes() { return self::$defaultFailureCodes; } /** * {@inheritdoc} */ public static function getSubscribedEvents() { return array( 'request.sent' => 'onRequestSent', 'request.exception' => 'onRequestSent', CurlMultiInterface::POLLING_REQUEST => 'onRequestPoll' ); } /** * Set the maximum number of retries the plugin should use before failing * the request * * @param integer $maxRetries The maximum number of retries. * * @return ExponentialBackoffPlugin */ public function setMaxRetries($maxRetries) { $this->maxRetries = max(0, (int) $maxRetries); return $this; } /** * Get the maximum number of retries the plugin will attempt * * @return integer */ public function getMaxRetries() { return $this->maxRetries; } /** * Get the HTTP response codes that should be retried using truncated * exponential backoff * * @return array */ public function getFailureCodes() { return array_keys($this->failureCodes); } /** * Set the HTTP response codes that should be retried using truncated * exponential backoff * * @param mixed $codes Array of HTTP response codes or PHP callable * * @return ExponentialBackoffPlugin */ public function setFailureCodes($codes = null) { // Use the default failure codes if no value was provided $this->failureCodes = $codes ?: static::getDefaultFailureCodes(); // Determine if the passed failure codes are a PHP callable $this->callableFailureCodes = is_callable($this->failureCodes); // Use a hash of codes so that it is faster to lookup errors if (!$this->callableFailureCodes) { $this->failureCodes = array_fill_keys($this->failureCodes, 1); } return $this; } /** * Determine how long to wait using truncated exponential backoff * * @param int $retries Number of retries so far * * @return int */ public function calculateWait($retries) { return (int) pow(2, $retries); } /** * Called when a request has been sent and isn't finished processing * * @param Event $event */ public function onRequestSent(Event $event) { $request = $event['request']; $response = $event['response']; $exception = $event['exception']; $retry = null; $failureCodes = $this->failureCodes; if ($this->callableFailureCodes) { // Use a callback to determine if the request should be retried $retry = call_user_func($this->failureCodes, $request, $response, $exception); // If null is returned, then use the default check if ($retry === null) { $failureCodes = self::$defaultFailureCodesHash; } } // If a retry method hasn't decided what to do yet, then use the default check if ($retry === null) { if ($exception && $exception instanceof CurlException) { // Handle cURL exceptions $retry = isset($failureCodes[$exception->getErrorNo()]); } elseif ($response) { $retry = isset($failureCodes[$response->getStatusCode()]) || isset($failureCodes[$response->getReasonPhrase()]); } } if ($retry) { $this->retryRequest($request); } } /** * Called when a request is polling in the curl multi object * * @param Event $event */ public function onRequestPoll(Event $event) { $request = $event['request']; $delay = $request->getParams()->get(self::DELAY_PARAM); // If the duration of the delay has passed, retry the request using the pool if (null !== $delay && microtime(true) >= $delay) { // Remove the request from the pool and then add it back again. // This is required for cURL to know that we want to retry sending // the easy handle. $request->getParams()->remove(self::DELAY_PARAM); // Rewind the request body if possible if ($request instanceof EntityEnclosingRequestInterface) { $request->getBody()->seek(0); } $multi = $event['curl_multi']; $multi->remove($request); $multi->add($request, true); } } /** * Trigger a request to retry * * @param RequestInterface $request Request to retry */ protected function retryRequest(RequestInterface $request) { $params = $request->getParams(); $retries = ((int) $params->get(self::RETRY_PARAM)) + 1; $params->set(self::RETRY_PARAM, $retries); // If this request has been retried too many times, then throw an exception if ($retries <= $this->maxRetries) { // Calculate how long to wait until the request should be retried $delay = call_user_func($this->delayClosure, $retries); $delayTime = microtime(true) + $delay; // Send the request again $request->setState(RequestInterface::STATE_TRANSFER); $params->set(self::DELAY_PARAM, $delay); } } } src/Guzzle/Http/Plugin/HistoryPlugin.php000064400000005154151742734600014337 0ustar00 'onRequestComplete'); } /** * Add a request to the history * * @param RequestInterface $request Request to add * * @return HistoryPlugin */ public function add(RequestInterface $request) { if ($request->getResponse()) { $this->requests[] = $request; if (count($this->requests) > $this->getlimit()) { array_shift($this->requests); } } return $this; } /** * Set the max number of requests to store * * @param int $limit Limit * * @return HistoryPlugin */ public function setLimit($limit) { $this->limit = (int) $limit; return $this; } /** * Get the request limit * * @return int */ public function getLimit() { return $this->limit; } /** * Get the requests in the history * * @return ArrayIterator */ public function getIterator() { return new \ArrayIterator($this->requests); } /** * Get the number of requests in the history * * @return int */ public function count() { return count($this->requests); } /** * Get the last request sent * * @return RequestInterface */ public function getLastRequest() { return end($this->requests); } /** * Get the last response in the history * * @return Response */ public function getLastResponse() { return $this->getLastRequest()->getResponse(); } /** * Clears the history * * @return HistoryPlugin */ public function clear() { $this->requests = array(); return $this; } /** * {@inheritdoc} */ public function onRequestComplete(Event $event) { $this->add($event['request']); } } src/Guzzle/Http/Plugin/LogPlugin.php000064400000022202151742734600013410 0ustar00logAdapter = $logAdapter; $this->hostname = gethostname(); $this->setSettings($settings); } /** * {@inheritdoc} */ public static function getSubscribedEvents() { return array( 'curl.callback.write' => array('onCurlWrite', 255), 'curl.callback.read' => array('onCurlRead', 255), 'request.before_send' => array('onRequestBeforeSend', 255), 'request.complete' => array('onRequestComplete', 255) ); } /** * Change the log settings of the plugin * * @param int $settings Bitwise settings to control what's logged * * @return LogPlugin */ public function setSettings($settings) { $this->settings = $settings; return $this; } /** * Get the log adapter object * * @return LogAdapterInterface */ public function getLogAdapter() { return $this->logAdapter; } /** * Event triggered when curl data is read from a request * * @param Event $event */ public function onCurlRead(Event $event) { // Stream the request body to the log if the body is not repeatable $request = $event['request']; if ($request->getParams()->get('request_wire')) { $request->getParams()->get('request_wire')->write($event['read']); } } /** * Event triggered when curl data is written to a response * * @param Event $event */ public function onCurlWrite(Event $event) { // Stream the response body to the log if the body is not repeatable $request = $event['request']; if ($request->getParams()->get('response_wire')) { $request->getParams()->get('response_wire')->write($event['write']); } } /** * Called before a request is sent * * @param Event $event */ public function onRequestBeforeSend(Event $event) { $request = $event['request']; // Ensure that curl IO events are emitted $request->getParams()->set('curl.emit_io', true); // We need to make special handling for content wiring and // non-repeatable streams. if ($this->settings & self::LOG_BODY) { if ($request instanceof EntityEnclosingRequestInterface) { if ($request->getBody() && (!$request->getBody()->isSeekable() || !$request->getBody()->isReadable())) { // The body of the request cannot be recalled so // logging the content of the request will need to // be streamed using updates $request->getParams()->set('request_wire', EntityBody::factory()); } } if (!$request->isResponseBodyRepeatable()) { // The body of the response cannot be recalled so // logging the content of the response will need to // be streamed using updates $request->getParams()->set('response_wire', EntityBody::factory()); } } } /** * Triggers the actual log write when a request completes * * @param Event $event */ public function onRequestComplete(Event $event) { $this->log($event['request'], $event['response']); } /** * Log a message based on a request and response * * @param RequestInterface $request Request to log * @param Response $response Response to log */ private function log(RequestInterface $request, Response $response = null) { $message = ''; if ($this->settings & self::LOG_CONTEXT) { // Log common contextual information $message = $request->getHost() . ' - "' . $request->getMethod() . ' ' . $request->getResourceUri() . ' ' . strtoupper($request->getScheme()) . '/' . $request->getProtocolVersion() . '"'; // If a response is set, then log additional contextual information if ($response) { $message .= sprintf(' - %s %s - %s %s %s', $response->getStatusCode(), $response->getContentLength() ?: 0, $response->getInfo('total_time'), $response->getInfo('speed_upload'), $response->getInfo('speed_download') ); } } // Check if we are logging anything that will come from cURL if ($request->getParams()->get('curl_handle') && ($this->settings & self::LOG_DEBUG || $this->settings & self::LOG_HEADERS || $this->settings & self::LOG_BODY)) { // If context logging too, then add a new line for cleaner messages if ($this->settings & self::LOG_CONTEXT) { $message .= "\n"; } // Filter cURL's verbose output based on config settings $message .= $this->parseCurlLog($request); // Log the response body if the response is available if ($this->settings & self::LOG_BODY && $response) { if ($request->getParams()->get('response_wire')) { $message .= (string) $request->getParams()->get('response_wire'); } else { $message .= $response->getBody(true); } } } // Send the log message to the adapter, adding a category and host $priority = $response && !$response->isSuccessful() ? LOG_ERR : LOG_DEBUG; $this->logAdapter->log(trim($message), $priority, array( 'category' => 'guzzle.request', 'host' => $this->hostname )); } /** * Parse cURL log messages * * @param RequestInterface $request Request that has a curl handle * * @return string */ protected function parseCurlLog(RequestInterface $request) { $message = ''; $handle = $request->getParams()->get('curl_handle'); $stderr = $handle->getStderr(true); if ($stderr) { rewind($stderr); $addedBody = false; while ($line = fgets($stderr)) { // * - Debug | < - Downstream | > - Upstream if ($line[0] == '*') { if ($this->settings & self::LOG_DEBUG) { $message .= $line; } } elseif ($this->settings & self::LOG_HEADERS) { $message .= $line; } // Add the request body if needed if ($this->settings & self::LOG_BODY) { if (trim($line) == '' && $request instanceof EntityEnclosingRequestInterface) { if ($request->getParams()->get('request_wire')) { $message .= (string) $request->getParams()->get('request_wire') . "\r\n"; } else { $message .= (string) $request->getBody() . "\r\n"; } $addedBody = true; } } } } return $message; } } src/Guzzle/Http/Plugin/Md5ValidatorPlugin.php000064400000006634151742734600015175 0ustar00contentLengthCutoff = $contentLengthCutoff; $this->contentEncoded = $contentEncoded; } /** * {@inheritdoc} */ public static function getSubscribedEvents() { return array('request.complete' => array('onRequestComplete', 255)); } /** * {@inheritdoc} * @throws UnexpectedValueException */ public function onRequestComplete(Event $event) { $response = $event['response']; $contentMd5 = $response->getContentMd5(); if (!$contentMd5) { return; } $contentEncoding = $response->getContentEncoding(); if ($contentEncoding && !$this->contentEncoded) { return false; } // Make sure that the request's size is under the cutoff size $size = $response->getContentLength() ?: $response->getBody()->getSize(); if (!$size || $size > $this->contentLengthCutoff) { return; } if (!$contentEncoding) { $hash = $response->getBody()->getContentMd5(); } elseif ($contentEncoding == 'gzip') { $response->getBody()->compress('zlib.deflate'); $hash = $response->getBody()->getContentMd5(); $response->getBody()->uncompress(); } elseif ($contentEncoding == 'compress') { $response->getBody()->compress('bzip2.compress'); $hash = $response->getBody()->getContentMd5(); $response->getBody()->uncompress(); } else { return; } if ($contentMd5 !== $hash) { throw new UnexpectedValueException(sprintf( 'The response entity body may have been ' . 'modified over the wire. The Content-MD5 ' . 'received (%s) did not match the calculated ' . 'MD5 hash (%s).', $contentMd5, $hash )); } } } src/Guzzle/Http/Plugin/MockPlugin.php000064400000011526151742734600013567 0ustar00temporary = $temporary; if ($responses) { foreach ($responses as $response) { $this->addResponse($response); } } } /** * {@inheritdoc} */ public static function getSubscribedEvents() { return array('client.create_request' => 'onRequestCreate'); } /** * {@inheritdoc} */ public static function getAllEvents() { return array('mock.request'); } /** * Get a mock response from a file * * @param string $file File to retreive a mock response from * * @return Response * @throws InvalidArgumentException if the file is not found */ public static function getMockFile($path) { if (!file_exists($path)) { throw new InvalidArgumentException('Unable to open mock file: ' . $path); } $parts = explode("\n\n", file_get_contents($path), 2); // Convert \n to \r\n in headers $data = isset($parts[1]) ? str_replace("\n", "\r\n", $parts[0]) . "\r\n\r\n" . $parts[1] : $parts[0]; return Response::fromMessage($data); } /** * Returns the number of remaining mock responses * * @return int */ public function count() { return count($this->queue); } /** * Add a response to the end of the queue * * @param string|Response $response Response object or path to response file * * @return MockPlugin * @throws InvalidArgumentException if a string or Response is not passed */ public function addResponse($response) { if (!($response instanceof Response)) { if (!is_string($response)) { throw new InvalidArgumentException('Invalid response'); } $response = self::getMockFile($response); } $this->queue[] = $response; return $this; } /** * Clear the queue * * @return MockPlugin */ public function clearQueue() { $this->queue = array(); return $this; } /** * Returns an array of mock responses remaining in the queue * * @return array */ public function getQueue() { return $this->queue; } /** * Check if this is a temporary plugin * * @return bool */ public function isTemporary() { return $this->temporary; } /** * Get a response from the front of the list and add it to a request * * @param RequestInterface $request Request to mock * * @return MockPlugin */ public function dequeue(RequestInterface $request) { $this->dispatch('mock.request', array( 'plugin' => $this, 'request' => $request )); $request->setResponse(array_shift($this->queue), true); return $this; } /** * Clear the array of received requests */ public function flush() { $this->received = array(); } /** * Get an array of requests that were mocked by this plugin * * @return array */ public function getReceivedRequests() { return $this->received; } /** * Called when a request completes * * @param Event $event */ public function onRequestCreate(Event $event) { if (!empty($this->queue)) { $request = $event['request']; $this->dequeue($request); $this->received[] = $request; // Detach the filter from the client so it's a one-time use if ($this->temporary && empty($this->queue) && $request->getClient()) { $request->getClient()->getEventDispatcher()->removeSubscriber($this); } } } } src/Guzzle/Http/Plugin/OauthPlugin.php000064400000013563151742734600013761 0ustar00config = Inspector::prepareConfig($config, array( 'version' => '1.0', 'consumer_key' => 'anonymous', 'consumer_secret' => 'anonymous', 'signature_method' => 'HMAC-SHA1', 'signature_callback' => function($stringToSign, $key) { return hash_hmac('sha1', $stringToSign, $key, true); } ), array( 'signature_method', 'signature_callback', 'version', 'consumer_key', 'consumer_secret', 'token', 'token_secret' )); } /** * {@inheritdoc} */ public static function getSubscribedEvents() { return array( 'request.before_send' => array('onRequestBeforeSend', -1000) ); } /** * Request before-send event handler * * @param Event $event Event received */ public function onRequestBeforeSend(Event $event) { $timestamp = $event['timestamp'] ?: time(); // Build Authorization header $authString = 'OAuth '; foreach(array( 'oauth_consumer_key' => $this->config['consumer_key'], 'oauth_nonce' => $this->generateNonce($event['request'], $timestamp), 'oauth_signature' => $this->getSignature($event['request'], $timestamp), 'oauth_signature_method' => $this->config['signature_method'], 'oauth_timestamp' => $timestamp, 'oauth_token' => $this->config['token'], 'oauth_version' => $this->config['version'], ) as $key => $val) { $authString .= $key . '="' . urlencode($val) . '", '; } // Add Authorization header $event['request']->setHeader('Authorization', substr($authString, 0, -2)); } /** * Calculate signature for request * * @param RequestInterface $request Request to generate a signature for * @param int $timestamp Timestamp to use for nonce * * @return string */ public function getSignature(RequestInterface $request, $timestamp) { $string = $this->getStringToSign($request, $timestamp); $key = urlencode($this->config['consumer_secret']) . '&' . urlencode($this->config['token_secret']); return base64_encode(call_user_func($this->config['signature_callback'], $string, $key)); } /** * Calculate string to sign * * @param RequestInterface $request Request to generate a signature for * @param int $timestamp Timestamp to use for nonce * * @return string */ public function getStringToSign(RequestInterface $request, $timestamp) { $params = new Collection(array( 'oauth_consumer_key' => $this->config['consumer_key'], 'oauth_nonce' => $this->generateNonce($request, $timestamp), 'oauth_signature_method' => $this->config['signature_method'], 'oauth_timestamp' => $timestamp, 'oauth_token' => $this->config['token'], 'oauth_version' => $this->config['version'] )); // Add query string parameters $params->merge($request->getQuery()); // Add POST fields to signing string if ($request instanceof EntityEnclosingRequestInterface && $request->getHeader('Content-Type') == 'application/x-www-form-urlencoded') { $params->merge($request->getPostFields()); } // Sort params $params = $params->getAll(); ksort($params); // Build signing string from combined params $parameterString = array(); foreach ($params as $key => $values) { $key = rawurlencode($key); $values = (array) $values; sort($values); foreach ($values as $value) { if (is_bool($value)) { $value = $value ? 'true' : 'false'; } $parameterString[] = $key . '=' . rawurlencode($value); } } $url = Url::factory($request->getUrl())->setQuery('')->setFragment(''); return strtoupper($request->getMethod()) . '&' . rawurlencode($url) . '&' . rawurlencode(implode('&', $parameterString)); } /** * Returns a Nonce Based on the Timestamp and URL. This will allow for * multiple requests in parallel with the same exact timestamp to use * separate nonce's. * * @param RequestInterface $request Request to generate a nonce for * @param int $timestamp Timestamp to use for nonce * * @return string */ protected function generateNonce(RequestInterface $request, $timestamp) { return sha1($timestamp . $request->getUrl()); } } src/Guzzle/Http/QueryString.php000064400000024154151742734600012556 0ustar00add($key, $value); } return $q; } /** * Convert the query string parameters to a query string string * * @return string */ public function __toString() { if (empty($this->data)) { return ''; } $queryString = $this->prefix; $firstValue = true; foreach ($this->encodeData($this->data, $this->encodeFields, $this->encodeValues) as $name => $value) { $value = $value !== null ? (array) $value : array(false); foreach ($value as $v) { if (!$firstValue) { $queryString .= $this->fieldSeparator; } $queryString .= $name; if (is_string($v)) { $queryString .= $this->valueSeparator . $v; } $firstValue = false; } } return $queryString; } /** * Aggregate multi-valued parameters using PHP style syntax * * @param string $key The name of the query string parameter * @param array $value The values of the parameter * @param bool $encodeFields Set to TRUE to encode field names * @param bool $encodeValues Set to TRUE to encode values * * @return array Returns an array of the combined values */ public function aggregateUsingPhp($key, array $value, $encodeFields = false, $encodeValues = false) { $ret = array(); foreach ($value as $k => $v) { $k = $key . '[' . $k . ']'; if (is_array($v)) { $ret = array_merge($ret, $this->aggregateUsingPhp($k, $v, $encodeFields, $encodeValues)); } else { if ($encodeFields) { $k = rawurlencode($k); } $v = $encodeValues ? rawurlencode($v) : $v; $ret[$k] = $v; } } return $ret; } /** * Aggregate multi-valued parameters by joining the values using a comma * * * $q = new \Guzzle\Http\QueryString(array( * 'value' => array(1, 2, 3) * )); * $q->setAggregateFunction(array($q, 'aggregateUsingComma')); * echo $q; // outputs: ?value=1,2,3 * * * @param string $key The name of the query string parameter * @param array $value The values of the parameter * @param bool $encodeFields Set to TRUE to encode field names * @param bool $encodeValues Set to TRUE to encode values * * @return array Returns an array of the combined values */ public function aggregateUsingComma($key, array $value, $encodeFields = false, $encodeValues = false) { return array( $encodeFields ? rawurlencode($key) : $key => $encodeValues ? implode(',', array_map('rawurlencode', $value)) : implode(',', $value) ); } /** * Aggregate multi-valued parameters using duplicate values in a query string * * Example: http://test.com?q=1&q=2 * * @param string $key The name of the query string parameter * @param array $value The values of the parameter * @param bool $encodeFields Set to TRUE to encode field names * @param bool $encodeValues Set to TRUE to encode values * * @return array Returns an array of the combined values */ public function aggregateUsingDuplicates($key, array $value, $encodeFields = false, $encodeValues = false) { return array( $encodeFields ? rawurlencode($key) : $key => $encodeValues ? array_map('rawurlencode', $value) : $value ); } /** * Get the query string field separator * * @return string */ public function getFieldSeparator() { return $this->fieldSeparator; } /** * Get the query string prefix * * @return string */ public function getPrefix() { return $this->prefix; } /** * Get the query string value separator * * @return string */ public function getValueSeparator() { return $this->valueSeparator; } /** * Returns whether or not field names are being urlencoded when converting * the query string object to a string * * @return bool */ public function isEncodingFields() { return $this->encodeFields; } /** * Returns whether or not values are being urlencoded when converting * the query string object to a string * * @return bool */ public function isEncodingValues() { return $this->encodeValues; } /** * Provide a function for combining multi-valued query string parameters * into a single or multiple fields * * @param callback|null $callback A function or callback array that accepts * a $key, $value, $encodeFields, and $encodeValues as arguments and * returns an associative array containing the combined values. Set * to null to remove any custom aggregator. * * @return QueryString * * @see \Guzzle\Http\QueryString::aggregateUsingComma() */ public function setAggregateFunction($callback) { $this->aggregator = $callback; } /** * Set whether or not field names should be urlencoded when converting * the query string object to a string * * @param bool $encode Set to TRUE to encode field names, FALSE to not * encode field names * * @return QueryString */ public function setEncodeFields($encode) { $this->encodeFields = $encode; return $this; } /** * Set whether or not field values should be urlencoded when converting * the query string object to a string * * @param bool $encode Set to TRUE to encode field values, FALSE to not * encode field values * * @return QueryString */ public function setEncodeValues($encode) { $this->encodeValues = $encode; return $this; } /** * Set the query string separator * * @param string $separator The query string separator that will separate * fields * * @return QueryString */ public function setFieldSeparator($separator) { $this->fieldSeparator = $separator; return $this; } /** * Set the query string prefix * * @param string $prefix Prefix to use with the query string (e.g. '?') * * @return QueryString */ public function setPrefix($prefix) { $this->prefix = $prefix; return $this; } /** * Set the query string value separator * * @param string $separator The query string separator that will separate * values from fields * * @return QueryString */ public function setValueSeparator($separator) { $this->valueSeparator = $separator; return $this; } /** * Returns an array of url encoded field names and values * * @return array */ public function urlEncode() { return $this->encodeData($this->data, $this->encodeFields, $this->encodeValues); } /** * Url encode parameter data. * * If a parameter value is an array and no aggregator has been set, the * values of the array will be converted into a PHP compatible form array. * If an aggregator is set, the values will be converted using the * aggregator function * * @param array $data The data to encode * @param bool $encodeFields Toggle URL encoding of fields * @param bool $encodeValues Toggle URL encoding of values * * @return array Returns an array of encoded values and keys */ protected function encodeData(array $data, $encodeFields = true, $encodeValues = true) { if (!$this->aggregator) { $this->aggregator = array($this, 'aggregateUsingPhp'); } $temp = array(); foreach ($data as $key => &$value) { if (is_array($value)) { $encoded = $this->encodeData($value, $encodeFields, $encodeValues); $temp = array_merge($temp, call_user_func_array($this->aggregator, array($key, $value, $encodeFields, $encodeValues))); } else { if ($encodeValues && is_string($value) || is_numeric($value)) { $value = rawurlencode($value); } if ($encodeFields) { $temp[rawurlencode($key)] = $value; } else { $temp[$key] = $value; } } } return $temp; } } src/Guzzle/Http/Url.php000064400000031265151742734600011025 0ustar00parseUrl($url); // Convert the query string into a QueryString object if ($parts['query']) { $parts['query'] = QueryString::fromString($parts['query']); } return new self($parts['scheme'], $parts['host'], $parts['user'], $parts['pass'], $parts['port'], $parts['path'], $parts['query'], $parts['fragment']); } /** * Build a URL from parse_url parts. The generated URL will be a relative * URL if a scheme or host are not provided. * * @param array $parts Array of parse_url parts * * @return string */ public static function buildUrl(array $parts) { $url = $scheme = ''; if (isset($parts['scheme'])) { $scheme = $parts['scheme']; $url .= $scheme . '://'; } if (isset($parts['host'])) { if (isset($parts['user'])) { $url .= $parts['user']; if (isset($parts['pass'])) { $url .= ':' . $parts['pass']; } $url .= '@'; } $url .= $parts['host']; // Only include the port if it is not the default port of the scheme if (isset($parts['port']) && !(($scheme == 'http' && $parts['port'] == 80) || ($scheme == 'https' && $parts['port'] == 443))) { $url .= ':' . $parts['port']; } } if (empty($parts['path'])) { $url .= '/'; } else { if ($parts['path'][0] != '/') { $url .= '/'; } $url .= $parts['path']; } // Add the query string if present if (!empty($parts['query'])) { if ($parts['query'][0] != '?') { $url .= array_key_exists('query_prefix', $parts) ? $parts['query_prefix'] : '?'; } $url .= $parts['query']; } // Ensure that # is only added to the url if fragment contains anything. if (isset($parts['fragment']) && !empty($parts['fragment'])) { $url .= '#' . $parts['fragment']; } return $url; } /** * Create a new URL from URL parts * * @param string $scheme Scheme of the URL * @param string $host Host of the URL * @param string $username Username of the URL * @param string $password Password of the URL * @param int $port Port of the URL * @param string $path Path of the URL * @param QueryString|array|string $query Query string of the URL * @param string $fragment Fragment of the URL */ public function __construct($scheme, $host, $username = null, $password = null, $port = null, $path = null, QueryString $query = null, $fragment = null) { $this->scheme = $scheme; $this->host = $host; $this->port = $port; $this->username = $username; $this->password = $password; $this->fragment = $fragment; $this->setQuery($query ?: new QueryString()); if ($path) { $this->setPath($path); } } /** * Clone the URL */ public function __clone() { $this->query = clone $this->query; } /** * Returns the URL as a URL string * * @return string */ public function __toString() { return self::buildUrl($this->getParts()); } /** * Get the parts of the URL as an array * * @return array */ public function getParts() { return array( 'scheme' => $this->scheme, 'user' => $this->username, 'pass' => $this->password, 'host' => $this->host, 'port' => $this->port, 'path' => $this->getPath(), 'query' => (string) $this->query, 'fragment' => $this->fragment, 'query_prefix' => $this->query->getPrefix() ); } /** * Set the host of the request. * * @param string $host Host to set (e.g. www.yahoo.com, yahoo.com) * * @return Url */ public function setHost($host) { if (strpos($host, ':') === false) { $this->host = $host; } else { list($host, $port) = explode(':', $host); $this->host = $host; $this->setPort($port); } return $this; } /** * Get the host part of the URL * * @return string */ public function getHost() { return $this->host; } /** * Set the scheme part of the URL (http, https, ftp, etc) * * @param string $scheme Scheme to set * * @return Url */ public function setScheme($scheme) { $this->scheme = $scheme; return $this; } /** * Get the scheme part of the URL * * @return string */ public function getScheme() { return $this->scheme; } /** * Set the port part of the URL * * @param int $port Port to set * * @return Url */ public function setPort($port) { $this->port = $port; return $this; } /** * Get the port part of the URl. Will return the default port for a given * scheme if a port has not explicitly been set. * * @return int|null */ public function getPort() { if ($this->port) { return $this->port; } elseif ($this->scheme == 'http') { return 80; } elseif ($this->scheme == 'https') { return 443; } return null; } /** * Set the path part of the URL * * @param array|string $path Path string or array of path segments * * @return Url */ public function setPath($path) { if (is_array($path)) { $this->path = '/' . implode('/', $path); } else { $this->path = $path; if ($this->path != '*' && substr($this->path, 0, 1) != '/') { $this->path = '/' . $path; } } return $this; } /** * Normalize the URL so that double slashes and relative paths are removed * * @return Url */ public function normalizePath() { if ($this->path == '*') { return $this; } if ($this->path && $this->path != '/') { // Replace // and /./ with / $this->path = str_replace(array('/./', '//'), '/', $this->path); // Remove trailing relative paths if possible $segments = $this->getPathSegments(); $totalSegments = count($segments); $last = end($segments); $trailingSlash = false; if ($last === '') { array_pop($segments); $trailingSlash = true; } while ($last == '..' || $last == '.') { if ($last == '..') { array_pop($segments); $last = array_pop($segments); } if ($last == '.' || $last == '') { $last = array_pop($segments); } } $this->path = implode('/', $segments); if ($trailingSlash) { $this->path .= '/'; } } // Must always start with a slash if (substr($this->path, 0, 1) != '/') { $this->path = '/' . $this->path; } return $this; } /** * Add a relative path to the currently set path * * @param string $relativePath Relative path to add * * @return Url */ public function addPath($relativePath) { if (!$relativePath || $relativePath == '/') { return $this; } // Add a leading slash if needed if ($relativePath[0] != '/') { $relativePath = '/' . $relativePath; } return $this->setPath(str_replace('//', '/', $this->getPath() . $relativePath)); } /** * Get the path part of the URL * * @return string */ public function getPath() { return $this->path ?: '/'; } /** * Get the path segments of the URL as an array * * @return array */ public function getPathSegments() { return array_slice(explode('/', $this->getPath()), 1); } /** * Set the password part of the URL * * @param string $password Password to set * * @return Url */ public function setPassword($password) { $this->password = $password; return $this; } /** * Get the password part of the URL * * @return null|string */ public function getPassword() { return $this->password; } /** * Set the username part of the URL * * @param string $username Username to set * * @return Url */ public function setUsername($username) { $this->username = $username; return $this; } /** * Get the username part of the URl * * @return null|string */ public function getUsername() { return $this->username; } /** * Get the query part of the URL as a QueryString object * * @return QueryString */ public function getQuery() { return $this->query; } /** * Set the query part of the URL * * @param QueryString|string|array $query Query to set * * @return Url */ public function setQuery($query) { if (is_string($query)) { $output = null; parse_str($query, $output); $this->query = new QueryString($output); } elseif (is_array($query)) { $this->query = new QueryString($query); } elseif ($query instanceof QueryString) { $this->query = $query; } return $this; } /** * Get the fragment part of the URL * * @return null|string */ public function getFragment() { return $this->fragment; } /** * Set the fragment part of the URL * * @param string $fragment Fragment to set * * @return Url */ public function setFragment($fragment) { $this->fragment = $fragment; return $this; } /** * Check if this is an absolute URL * * @return bool */ public function isAbsolute() { return $this->scheme && $this->host; } /** * Combine the URL with another URL. Every part of the second URL supersede * the current URL if that part is specified. * * @param string $url Relative URL to combine with * * @return Url * @throws InvalidArgumentException */ public function combine($url) { $absolutePath = $url[0] == '/'; $url = self::factory($url); if ($url->getScheme()) { $this->scheme = $url->getScheme(); } if ($url->getHost()) { $this->host = $url->getHost(); } if ($url->getPort()) { $this->port = $url->getPort(); } if ($url->getUsername()) { $this->username = $url->getUsername(); } if ($url->getPassword()) { $this->password = $url->getPassword(); } if ($url->getFragment()) { $this->fragment = $url->getFragment(); } if ($absolutePath) { // Replace the current URL and query if set if ($url->getPath()) { $this->path = $url->getPath(); } if (count($url->getQuery())) { $this->query = clone $url->getQuery(); } } else { // Append to the current path and query string if ($url->getPath()) { $this->addPath($url->getPath()); } if ($url->getQuery()) { $this->query->merge($url->getQuery()); } } return $this; } } src/Guzzle/Http/Utils.php000064400000002071151742734600011354 0ustar00get('version'), $curl->get('ssl_version') ); } return self::$userAgent; } } src/Guzzle/Service/AbstractFactory.php000064400000004746151742734600014043 0ustar00getCacheTtlKey($config); // Check if a cache was provided if (isset($options['cache.adapter']) && is_string($config)) { $adapter = $options['cache.adapter']; $ttl = isset($options[$cacheTtlKey]) ? $options[$cacheTtlKey] : 3600; $cacheKey = 'guzzle' . crc32($config); // Check if the instantiated data is in the cache $cached = $adapter->fetch($cacheKey); if ($cached) { return $cached; } } // Get the name of the class to instantiate for the type of data $class = $this->getClassName($config); if ($class) { $result = $this->getFactory($class)->build($config, $options); if ($adapter) { $adapter->save($cacheKey, $result, $ttl); } return $result; } $this->throwException(); } /** * Get the string used to hold cache TTL specific information for the factory * * @param mixed $config Config data being loaded * * @return string */ abstract protected function getCacheTtlKey($config); /** * Throw an exception when the abstract factory cannot instantiate anything * * @param string $message Message for the exception * * @return string * @throws Exception */ abstract protected function throwException($message = ''); /** * Get the name of a class to instantiate for the type of data provided * * @param mixed $config Data to use to determine a class name * * @return mixed */ abstract protected function getClassName($config); /** * Get a factory by object name, or retrieve previously a created factory * * @param string $class Name of the factory class to retrieve * * @return mixed */ protected function getFactory($class) { if (!isset($this->factories[$class])) { $factory = $class; $this->factories[$class] = new $factory(); } return $this->factories[$class]; } } src/Guzzle/Service/Builder/ArrayServiceBuilderFactory.php000064400000004040151742734600017557 0ustar00 &$service) { $service['params'] = isset($service['params']) ? $service['params'] : array(); // Check if this client builder extends another client if (!empty($service['extends'])) { // Make sure that the service it's extending has been defined if (!isset($services[$service['extends']])) { throw new ServiceNotFoundException($name . ' is trying to extend a non-existent service: ' . $service['extends']); } $service['class'] = empty($service['class']) ? $services[$service['extends']]['class'] : $service['class']; $extendsParams = isset($services[$service['extends']]['params']) ? $services[$service['extends']]['params'] : false; if ($extendsParams) { $service['params'] = array_merge($extendsParams, $service['params']); } } // Overwrite default values with global parameter values if (!empty($options)) { $service['params'] = array_merge($service['params'], $options); } $service['class'] = !isset($service['class']) ? '' : str_replace('.', '\\', $service['class']); } return new $class($services); } } src/Guzzle/Service/Builder/JsonServiceBuilderFactory.php000064400000001154151742734600017415 0ustar00loader) { $this->loader = new JsonLoader(); } return ServiceBuilder::factory($this->loader->parseJsonFile($config), $options); } } src/Guzzle/Service/Builder/ServiceBuilder.php000064400000013173151742734600015237 0ustar00build($config, $globalParameters); } /** * Construct a new service builder * * @param array $serviceBuilderConfig Service configuration settings: * name => Name of the service * class => Client class to instantiate using a factory method * params => array of key value pair configuration settings for the builder */ public function __construct(array $serviceBuilderConfig) { $this->builderConfig = $serviceBuilderConfig; } /** * {@inheritdoc} */ public static function getAllEvents() { return array('service_builder.create_client'); } /** * Restores the service builder from JSON * * @param string $serialized JSON data to restore from */ public function unserialize($serialized) { $this->builderConfig = json_decode($serialized, true); } /** * Represents the service builder as a string * * @return array */ public function serialize() { return json_encode($this->builderConfig); } /** * Get a client using a registered builder * * @param string $name Name of the registered client to retrieve * @param bool $throwAway Set to TRUE to not store the client * for later retrieval from the ServiceBuilder * * @return ClientInterface * @throws ServiceNotFoundException when a client cannot be found by name */ public function get($name, $throwAway = false) { if (!isset($this->builderConfig[$name])) { throw new ServiceNotFoundException('No service is registered as ' . $name); } if (!$throwAway && isset($this->clients[$name])) { return $this->clients[$name]; } // Convert references to the actual client foreach ($this->builderConfig[$name]['params'] as $k => &$v) { if (0 === strpos($v, '{') && strlen($v) - 1 == strrpos($v, '}')) { $v = $this->get(trim(str_replace(array('{', '}'), '', $v))); } } $client = call_user_func( array($this->builderConfig[$name]['class'], 'factory'), $this->builderConfig[$name]['params'] ); if (!$throwAway) { $this->clients[$name] = $client; } // Dispatch an event letting listeners know a client was created $this->dispatch('service_builder.create_client', array( 'client' => $client )); return $client; } /** * Register a client by name with the service builder * * @param string $name Name of the client to register * @param mixed $value Service to register * * @return ServiceBuilderInterface */ public function set($key, $service) { $this->builderConfig[$key] = $service; return $this; } /** * Register a client by name with the service builder * * @param string $offset Name of the client to register * @param ClientInterface $value Client to register */ public function offsetSet($offset, $value) { $this->set($offset, $value); } /** * Remove a registered client by name * * @param string $offset Client to remove by name */ public function offsetUnset($offset) { unset($this->builderConfig[$offset]); } /** * Check if a client is registered with the service builder by name * * @param string $offset Name to check to see if a client exists * * @return bool */ public function offsetExists($offset) { return isset($this->builderConfig[$offset]); } /** * Get a registered client by name * * @param string $offset Registered client name to retrieve * * @return ClientInterface */ public function offsetGet($offset) { return $this->get($offset); } } src/Guzzle/Service/Builder/ServiceBuilderAbstractFactory.php000064400000003017151742734600020247 0ustar00throwException('Must pass a file name, array, or SimpleXMLElement'); } return __NAMESPACE__ . '\\' . $class; } } src/Guzzle/Service/Builder/ServiceBuilderFactoryInterface.php000064400000000715151742734600020406 0ustar00parseXmlFile($config), $options); } /** * Parse an XML document into an array * * @param string $filename Path to the file * * @return array */ protected function parseXmlFile($filename) { if ($filename instanceof \SimpleXMLElement) { $xml = $filename; } else { $xml = new \SimpleXMLElement($filename, null, true); $result = array( 'services' => array() ); } // Account for old style service builder config files if (isset($xml->services)) { $services = $xml->services->service; } else { $services = $xml->clients->client; } foreach ($services as $service) { $row = array(); foreach ($service->param as $param) { $row[(string) $param->attributes()->name] = (string) $param->attributes()->value; } $result['services'][(string) $service->attributes()->name] = array( 'class' => (string) $service->attributes()->class, 'extends' => (string) $service->attributes()->extends, 'params' => $row ); } // Include any XML files under the includes elements if (isset($xml->includes->include)) { // You can only extend other services when using a file if ($filename instanceof \SimpleXMLElement) { throw new ServiceBuilderException('You can not extend other SimpleXMLElement services'); } foreach ($xml->includes->include as $include) { $path = (string) $include->attributes()->path; if ($path[0] != DIRECTORY_SEPARATOR) { $path = dirname($filename) . DIRECTORY_SEPARATOR . $path; } $result = array_merge_recursive($this->parseXmlFile($path), $result); } } // Grab the class name if it's set if (isset($xml->class)) { $result['class'] = str_replace('.', '\\', (string) $xml->class); } return $result; } } src/Guzzle/Service/Client.php000064400000025476151742734600012171 0ustar00magicMethodBehavior == self::MAGIC_CALL_DISABLED) { throw new BadMethodCallException("Missing method $method. Enable" . " magic calls to use magic methods with command names."); } $command = $this->getCommand(Inflector::snake($method), $args); return $this->magicMethodBehavior == self::MAGIC_CALL_RETURN ? $command : $this->execute($command); } /** * Set the behavior for missing methods * * @param int $behavior Behavior to use when a missing method is called. * Set to Client::MAGIC_CALL_DISABLED to disable magic method calls * Set to Client::MAGIC_CALL_EXECUTE to execute commands and return the result * Set to Client::MAGIC_CALL_RETURN to instantiate and return the command * * @return Client */ public function setMagicCallBehavior($behavior) { $this->magicMethodBehavior = (int) $behavior; return $this; } /** * Get a command by name. First, the client will see if it has a service * description and if the service description defines a command by the * supplied name. If no dynamic command is found, the client will look for * a concrete command class exists matching the name supplied. If neither * are found, an InvalidArgumentException is thrown. * * @param string $name Name of the command to retrieve * @param array $args Arguments to pass to the command * * @return CommandInterface * @throws InvalidArgumentException if no command can be found by name */ public function getCommand($name, array $args = array()) { // Enable magic method calls on commands if it's enabled on the client if ($this->magicMethodBehavior) { $args['command.magic_method_call'] = true; } $command = $this->getCommandFactory()->factory($name, $args); if (!$command) { throw new InvalidArgumentException("Command was not found matching {$name}"); } $command->setClient($this); $this->dispatch('client.command.create', array( 'client' => $this, 'command' => $command )); return $command; } /** * Set the command factory used to create commands by name * * @param CommandFactoryInterface $factory Command factory * * @return Client */ public function setCommandFactory(CommandFactoryInterface $factory) { $this->commandFactory = $factory; return $this; } /** * Set the resource iterator factory associated with the client * * @param ResourceIteratorFactoryInterface $factory Resource iterator factory * * @return Client */ public function setResourceIteratorFactory(ResourceIteratorFactoryInterface $factory) { $this->resourceIteratorFactory = $factory; return $this; } /** * Get a resource iterator from the client. * * @param string|CommandInterface $command Command class or command name. * Passing a command name will have the client create the command for * you using $commandOptions array. * @param array $commandOptions Command options used when * creating commands. * @param array $iteratorOptions Iterator options passed to the * iterator when it is instantiated. * * @return ResourceIteratorInterface */ public function getIterator($command, array $commandOptions = null, array $iteratorOptions = array()) { if (!($command instanceof CommandInterface)) { $command = $this->getCommand($command, $commandOptions ?: array()); } return $this->getResourceIteratorFactory()->build($command, $iteratorOptions); } /** * Execute a command and return the response * * @param CommandInterface|CommandSet|array $command Command or set to execute * * @return mixed Returns the result of the executed command's * {@see CommandInterface::getResult} method if a CommandInterface is * passed, or the CommandSet itself if a CommandSet is passed * @throws InvalidArgumentException if an invalid command is passed * @throws CommandSetException if a set contains commands associated * with other clients */ public function execute($command) { if ($command instanceof CommandInterface) { $request = $command->setClient($this)->prepare(); $this->dispatch('command.before_send', array( 'command' => $command )); // Set the state to new if the command was previously executed if ($request->getState() !== RequestInterface::STATE_NEW) { $request->setState(RequestInterface::STATE_NEW); } $request->send(); $this->dispatch('command.after_send', array( 'command' => $command )); return $command->getResult(); } elseif ($command instanceof CommandSet) { foreach ($command as $c) { if ($c->getClient() && $c->getClient() !== $this) { throw new CommandSetException( 'Attempting to run a mixed-Client CommandSet from a ' . 'Client context. Run the set using CommandSet::execute() ' ); } $c->setClient($this); } return $command->execute(); } elseif (is_array($command)) { return $this->execute(new CommandSet($command)); } throw new InvalidArgumentException('Invalid command sent to ' . __METHOD__); } /** * Set the service description of the client * * @param ServiceDescription $service Service description that describes * all of the commands and information of the client * @param bool $updateFactory Set to FALSE to not update the service * description based command factory if it is not already present on * the client * * @return Client */ public function setDescription(ServiceDescription $service, $updateFactory = true) { $this->serviceDescription = $service; // Add the service description factory to the factory chain if it is not set if ($updateFactory) { // Convert non chain factories to a chain factory if (!($this->getCommandFactory() instanceof CompositeFactory)) { $this->commandFactory = new CompositeFactory(array($this->commandFactory)); } // Add a service description factory if one does not already exist if (!$this->commandFactory->has('Guzzle\\Service\\Command\\Factory\\ServiceDescriptionFactory')) { // Add the service description factory before the concrete factory $this->commandFactory->add(new ServiceDescriptionFactory($service), 'Guzzle\\Service\\Command\\Factory\\ConcreteClassFactory'); } else { // Update an existing service description factory $factory = $this->commandFactory->find('Guzzle\\Service\\Command\\Factory\\ServiceDescriptionFactory'); $factory->setServiceDescription($service); } } return $this; } /** * Get the service description of the client * * @return ServiceDescription|null */ public function getDescription() { return $this->serviceDescription; } /** * Get the resource iterator factory associated with the client * * @return ResourceIteratorFactoryInterface */ protected function getResourceIteratorFactory() { if (!$this->resourceIteratorFactory) { // Build the default resource iterator factory if one is not set $clientClass = get_class($this); $namespace = substr($clientClass, 0, strrpos($clientClass, '\\')) . '\\Model'; $this->resourceIteratorFactory = new ResourceIteratorClassFactory($namespace); } return $this->resourceIteratorFactory; } /** * Get the command factory associated with the client * * @return CommandFactoryInterface */ protected function getCommandFactory() { if (!$this->commandFactory) { $this->commandFactory = CompositeFactory::getDefaultChain($this); } return $this->commandFactory; } } src/Guzzle/Service/ClientInterface.php000064400000007374151742734600014007 0ustar00apiCommand = $apiCommand; } else { // If this is a concrete command, then build an ApiCommand object $className = get_class($this); // Determine the name of the command based on the relation to the // client that executes the command $this->apiCommand = new ApiCommand(array( 'name' => str_replace('\\_', '.', Inflector::snake(substr($className, strpos($className, 'Command') + 8))), 'class' => $className, 'params' => $this->getInspector()->getApiParamsForClass($className) )); } // Set default values on the command $this->getInspector()->validateConfig($this->apiCommand->getParams(), $this, false, false); $headers = $this->get('headers'); if (!$headers instanceof Collection) { $this->set('headers', new Collection((array) $headers)); } // You can set a command.on_complete option in your parameters as a // convenience method for setting an onComplete function $onComplete = $this->get('command.on_complete'); if ($onComplete) { $this->remove('command.on_complete'); $this->setOnComplete($onComplete); } $this->init(); } /** * Custom clone behavior */ public function __clone() { $this->request = null; $this->result = null; } /** * Enables magic methods for setting parameters. * * @param string $method Name of the parameter to set * @param array $args Arguments to pass to the command * * @return AbstractCommand * @throws BadMethodCallException when a parameter doesn't exist */ public function __call($method, $args = null) { // Ensure magic method call behavior is enabled if (!$this->get('command.magic_method_call')) { throw new BadMethodCallException('Magic method calls are disabled ' . 'for this command. Consider enabling magic method calls by ' . 'setting the command.magic_method_call parameter to true.'); } if ($args && strpos($method, 'set') === 0) { // Convert the method into the snake cased parameter key $key = Inflector::snake(substr($method, 3)); // If the parameter exists, set it if (array_key_exists($key, $this->apiCommand->getParams())) { $this->set($key, $args[0]); return $this; } } // If the method is not a set method, or the parameter doesn't exist, fail throw new BadMethodCallException("Missing method {$method}."); } /** * Get the short form name of the command * * @return string */ public function getName() { return $this->apiCommand->getName(); } /** * Get the API command information about the command * * @return ApiCommand */ public function getApiCommand() { return $this->apiCommand; } /** * Specify a callable to execute when the command completes * * @param mixed $callable Callable to execute when the command completes. * The callable must accept a {@see CommandInterface} object as the * only argument. * * @return Command * @throws InvalidArgumentException */ public function setOnComplete($callable) { if (!is_callable($callable)) { throw new InvalidArgumentException('The onComplete function must be callable'); } $this->onComplete = $callable; return $this; } /** * Execute the command and return the result * * @return mixed Returns the result of {@see AbstractCommand::execute} * @throws CommandException if a client has not been associated with the command */ public function execute() { if (!$this->client) { throw new CommandException('A Client object must be associated with the command before it can be executed from the context of the command.'); } return $this->client->execute($this); } /** * Get the client object that will execute the command * * @return ClientInterface|null */ public function getClient() { return $this->client; } /** * Set the client objec that will execute the command * * @param ClientInterface $client The client objec that will execute the command * * @return Command */ public function setClient(ClientInterface $client) { $this->client = $client; return $this; } /** * Get the request object associated with the command * * @return RequestInterface * @throws CommandException if the command has not been executed */ public function getRequest() { if (!$this->request) { throw new CommandException('The command must be prepared before retrieving the request'); } return $this->request; } /** * Get the response object associated with the command * * @return Response * @throws CommandException if the command has not been executed */ public function getResponse() { if (!$this->isExecuted()) { throw new CommandException('The command must be executed before retrieving the response'); } return $this->request->getResponse(); } /** * Get the result of the command * * @return Response By default, commands return a Response * object unless overridden in a subclass * @throws CommandException if the command has not been executed */ public function getResult() { if (!$this->isExecuted()) { throw new CommandException('The command must be executed before retrieving the result'); } if (null === $this->result) { $this->process(); // Call the onComplete method if one is set if ($this->onComplete) { call_user_func($this->onComplete, $this); } } return $this->result; } /** * Returns TRUE if the command has been prepared for executing * * @return bool */ public function isPrepared() { return $this->request !== null; } /** * Returns TRUE if the command has been executed * * @return bool */ public function isExecuted() { return $this->request !== null && $this->request->getState() == 'complete'; } /** * Prepare the command for executing and create a request object. * * @return RequestInterface Returns the generated request * @throws CommandException if a client object has not been set previously * or in the prepare() */ public function prepare() { if (!$this->isPrepared()) { if (!$this->client) { throw new CommandException('A Client object must be associated with the command before it can be prepared.'); } // Fail on missing required arguments $this->getInspector()->validateConfig($this->apiCommand->getParams(), $this, true); $this->build(); // Add custom request headers set on the command $headers = $this->get('headers'); if ($headers && $headers instanceof Collection) { foreach ($headers as $key => $value) { $this->request->setHeader($key, $value); } } } return $this->getRequest(); } /** * Get the object that manages the request headers that will be set on any * outbound requests from the command * * @return Collection */ public function getRequestHeaders() { return $this->get('headers', new Collection()); } /** * Set the Inspector to use with the command * * @param Inspector $inspector Inspector to use for config validation * * @return AbstractCommand */ public function setInspector(Inspector $inspector) { $this->inspector = $inspector; return $this; } /** * Get the Inspector used with the Command * * @return Inspector */ protected function getInspector() { if (!$this->inspector) { $this->inspector = Inspector::getInstance(); } return $this->inspector; } /** * Initialize the command (hook to be implemented in subclasses) */ protected function init() {} /** * Create the request object that will carry out the command */ abstract protected function build(); /** * Create the result of the command after the request has been completed. * * Sets the result as the response by default. If the response is an XML * document, this will set the result as a SimpleXMLElement. If the XML * response is invalid, the result will remain the Response, not XML. * If an application/json response is received, the result will automat- * ically become an array. */ protected function process() { // Uses the response object by default $this->result = $this->getRequest()->getResponse(); $contentType = $this->result->getContentType(); // Is the body an JSON document? If so, set the result to be an array if (stripos($contentType, 'json') !== false) { $body = trim($this->result->getBody(true)); if ($body) { $decoded = json_decode($body, true); if (JSON_ERROR_NONE !== json_last_error()) { throw new JsonException('The response body can not be decoded to JSON', json_last_error()); } $this->result = $decoded; } } if (stripos($contentType, 'xml') !== false) { // Is the body an XML document? If so, set the result to be a SimpleXMLElement // If the body is available, then parse the XML $body = trim($this->result->getBody(true)); if ($body) { // Silently allow parsing the XML to fail try { $xml = new \SimpleXMLElement($body); $this->result = $xml; } catch (\Exception $e) {} } } } } src/Guzzle/Service/Command/ClosureCommand.php000064400000002425151742734600015231 0ustar00get('closure')) { throw new InvalidArgumentException('A closure must be passed in the parameters array'); } } /** * {@inheritdoc} * * @throws UnexpectedValueException If the closure does not return a request */ protected function build() { $closure = $this->get('closure'); $this->request = $closure($this, $this->apiCommand); if (!$this->request || !$this->request instanceof RequestInterface) { throw new UnexpectedValueException('Closure command did not return a RequestInterface object'); } } } src/Guzzle/Service/Command/CommandInterface.php000064400000006650151742734600015521 0ustar00addCommand($command); } } /** * Add a command to the set * * @param CommandInterface $command Command object to add to the command set * * @return CommandSet */ public function addCommand(CommandInterface $command) { $this->commands[] = $command; return $this; } /** * Implements Countable * * @return int */ public function count() { return count($this->commands); } /** * Execute the command set * * @return CommandSet * @throws CommandSetException if any of the commands do not have an associated * {@see ClientInterface} object */ public function execute() { // Keep a list of all commands with no client $invalid = array_filter($this->commands, function($command) { return !$command->getClient(); }); // If any commands do not have a client, then throw an exception if (count($invalid)) { $e = new CommandSetException('Commands found with no associated client'); $e->setCommands($invalid); throw $e; } // Execute all batched commands in parallel if (count($this->commands)) { $multis = array(); // Prepare each request and send out client notifications foreach ($this->commands as $command) { $request = $command->prepare(); $request->getParams()->set('command', $command); $request->getEventDispatcher()->addListener('request.complete', array($this, 'update'), -99999); $command->getClient()->dispatch('command.before_send', array( 'command' => $command )); $command->getClient()->getCurlMulti()->add($command->getRequest()); if (!in_array($command->getClient()->getCurlMulti(), $multis)) { $multis[] = $command->getClient()->getCurlMulti(); } } foreach ($multis as $multi) { $multi->send(); } } return $this; } /** * Implements IteratorAggregate * * @return ArrayIterator */ public function getIterator() { return new \ArrayIterator($this->commands); } /** * Get all of the attached commands * * @return array */ public function getCommands() { return $this->commands; } /** * Check if the set contains a specific command * * @param string|CommandInterface $command Command object class name or * concrete CommandInterface object * * @return bool */ public function hasCommand($command) { return (bool) (count(array_filter($this->commands, function($value) use ($command) { return is_string($command) ? ($value instanceof $command) : ($value === $command); })) > 0); } /** * Remove a command from the set * * @param string|CommandInterface $command The command object or command * class name to remove * * @return CommandSet */ public function removeCommand($command) { $this->commands = array_values(array_filter($this->commands, function($value) use ($command) { return is_string($command) ? !($value instanceof $command) : ($value !== $command); })); return $this; } /** * Trigger the result of the command to be created as commands complete and * make sure the command isn't going to send more requests * * {@inheritdoc} */ public function update(Event $event) { $request = $event['request']; $command = $request->getParams()->get('command'); if ($command && $command->isExecuted()) { $request = $event['request']; $request->getEventDispatcher()->removeListener('request.complete', $this); $request->getParams()->remove('command'); // Force the result to be processed $command->getResult(); $command->getClient()->dispatch('command.after_send', array( 'command' => $command )); } } } src/Guzzle/Service/Command/DynamicCommand.php000064400000005246151742734600015205 0ustar00apiCommand->getUri()) { $url = $this->getClient()->getBaseUrl(); } else { // Get the path values and use the client config settings $variables = $this->getClient()->getConfig()->getAll(); foreach ($this->apiCommand->getParams() as $name => $arg) { $configValue = $this->get($name); if (is_scalar($configValue)) { $variables[$name] = $arg->getPrepend() . $configValue . $arg->getAppend(); } } // Expand the URI template using the URI values $uri = ParserRegistry::get('uri_template')->expand($this->apiCommand->getUri(), $variables); // Merge the client's base URL with the URI template $url = Url::factory($this->getClient()->getBaseUrl()); $url->combine($uri); $url = (string) $url; } // Inject path and base_url values into the URL $this->request = $this->getClient()->createRequest($this->apiCommand->getMethod(), $url); // Add arguments to the request using the location attribute foreach ($this->apiCommand->getParams() as $name => $arg) { $configValue = $this->get($name); $location = $arg->getLocation(); if (!$configValue || !$location) { continue; } // Create the value based on prepend and append settings $value = $arg->getPrepend() . $configValue . $arg->getAppend(); // Determine the location and key setting location[:key] $parts = explode(':', $location); $place = $parts[0]; // If a key is specified (using location:key), use it $key = isset($parts[1]) ? $parts[1] : $name; // Add the parameter to the request switch ($place) { case 'body': $this->request->setBody(EntityBody::factory($value)); break; case 'header': $this->request->setHeader($key, $value); break; case 'query': $this->request->getQuery()->set($key, $value); break; } } } } src/Guzzle/Service/Command/Factory/AliasFactory.php000064400000002222151742734600016301 0ustar00client = $client; $this->aliases = $aliases; } /** * {@inheritdoc} */ public function factory($name, array $args = array()) { if (isset($this->aliases[$name])) { try { return $this->client->getCommand($this->aliases[$name], $args); } catch (InvalidArgumentException $e) { return null; } } } } src/Guzzle/Service/Command/Factory/CompositeFactory.php000064400000010174151742734600017217 0ustar00getDescription(); if ($description instanceof ServiceDescription) { $chain->add(new ServiceDescriptionFactory($description)); } $chain->add(new ConcreteClassFactory($client)); return $chain; } /** * @param array $factories Array of command factories */ public function __construct(array $factories = array()) { $this->factories = $factories; } /** * Add a command factory to the chain * * @param FactoryInterface $factory Factor to add * @param string|FactoryInterface $before Insert the new * command factory before a command factory class or object matching a * class name. * * @return CompositeFactory */ public function add(FactoryInterface $factory, $before = null) { $pos = null; if ($before) { foreach ($this->factories as $i => $f) { if ($before instanceof FactoryInterface) { if ($f === $before) { $pos = $i; break; } } elseif (is_string($before)) { if ($f instanceof $before) { $pos = $i; break; } } } } if ($pos === null) { $this->factories[] = $factory; } else { array_splice($this->factories, $i, 0, array($factory)); } return $this; } /** * Check if the chain contains a specific command factory * * @param FactoryInterface|string $factory Factory to check * * @return bool */ public function has($factory) { return (bool) $this->find($factory); } /** * Remove a specific command factory from the chain * * @param string|FactoryInterface $factory Factory to remove by * name or instance * * @return CompositeFactory */ public function remove($factory = null) { if (!($factory instanceof FactoryInterface)) { $factory = $this->find($factory); } $this->factories = array_values(array_filter($this->factories, function($f) use ($factory) { return $f !== $factory; })); return $this; } /** * Get a command factory by class name * * @param string|FactoryInterface $factory Command factory class or instance * * @return null|FactoryInterface */ public function find($factory) { foreach ($this->factories as $f) { if ($factory === $f || (is_string($factory) && $f instanceof $factory)) { return $f; } } } /** * Create a command using the associated command factories * * @param string $name Name of the command * @param array $args Command arguments * * @return CommandInterface */ public function factory($name, array $args = array()) { foreach ($this->factories as $factory) { $command = $factory->factory($name, $args); if ($command) { return $command; } } } /** * {@inheritdoc} */ public function count() { return count($this->factories); } /** * {@inheritdoc} */ public function getIterator() { return new \ArrayIterator($this->factories); } } src/Guzzle/Service/Command/Factory/ConcreteClassFactory.php000064400000002532151742734600020004 0ustar00client = $client; } /** * {@inheritdoc} */ public function factory($name, array $args = array()) { // Determine the class to instantiate based on the namespace of the // current client and the default location of commands $prefix = $this->client->getConfig('command.prefix'); if (!$prefix) { // The prefix can be specified in a factory method and is cached $prefix = implode('\\', array_slice(explode('\\', get_class($this->client)), 0, -1)) . '\\Command\\'; $this->client->getConfig()->set('command.prefix', $prefix); } $class = $prefix . str_replace(' ', '\\', ucwords(str_replace('.', ' ', Inflector::camel($name)))); // Create the concrete command if it exists if (class_exists($class)) { return new $class($args); } } } src/Guzzle/Service/Command/Factory/FactoryInterface.php000064400000000643151742734600017155 0ustar00map = $map; } /** * {@inheritdoc} */ public function factory($name, array $args = array()) { if (isset($this->map[$name])) { $class = $this->map[$name]; return new $class($args); } } } src/Guzzle/Service/Command/Factory/ServiceDescriptionFactory.php000064400000002604151742734600021060 0ustar00setServiceDescription($description); } /** * Change the service description used with the factory * * @param ServiceDescription $description Service description to use * * @return ServiceDescriptionFactory */ public function setServiceDescription(ServiceDescription $description) { $this->description = $description; return $this; } /** * Returns the service description * * @return ServiceDescription */ public function getServiceDescription() { return $this->description; } /** * {@inheritdoc} */ public function factory($name, array $args = array()) { if ($this->description->hasCommand($name)) { $command = $this->description->getCommand($name); $class = $command->getConcreteClass(); return new $class($args, $command); } } } src/Guzzle/Service/Description/ApiCommand.php000064400000011257151742734600015236 0ustar00name = isset($config['name']) ? trim($config['name']) : ''; $this->doc = isset($config['doc']) ? trim($config['doc']) : ''; $this->method = isset($config['method']) ? trim($config['method']) : ''; $this->uri = isset($config['uri']) ? trim($config['uri']) : ''; $this->class = isset($config['class']) ? trim($config['class']) : self::DEFAULT_COMMAND_CLASS; if (!empty($config['params'])) { foreach ($config['params'] as $name => $param) { $this->params[$name] = $param instanceof ApiParam ? $param : new ApiParam($param); } } } /** * Get as an array * * @return true */ public function toArray() { return array( 'name' => $this->name, 'doc' => $this->doc, 'method' => $this->method, 'uri' => $this->uri, 'class' => $this->class, 'params' => $this->params ); } /** * Get the params of the command * * @return array */ public function getParams() { return $this->params; } /** * Get a single parameter of the command * * @param string $param Parameter to retrieve by name * * @return ApiParam|null */ public function getParam($param) { return isset($this->params[$param]) ? $this->params[$param] : null; } /** * Get the HTTP method of the command * * @return string */ public function getMethod() { return $this->method; } /** * Get the concrete command class that implements this command * * @return string */ public function getConcreteClass() { return $this->class; } /** * Get the name of the command * * @return string */ public function getName() { return $this->name; } /** * Get the documentation for the command * * @return string */ public function getDoc() { return $this->doc; } /** * Get the URI that will be merged into the generated request * * @return string */ public function getUri() { return $this->uri; } } src/Guzzle/Service/Description/ApiParam.php000064400000011657151742734600014724 0ustar00 $value) { if ($key == 'min_length') { $key = 'minLength'; } elseif ($key == 'max_length') { $key = 'maxLength'; } $this->{$key} = $value; } if ($this->filters) { $this->filters = array_map('trim', explode(',', $this->filters)); } else { $this->filters = array(); } if ($this->required === 'false') { $this->required = false; } elseif ($this->required === 'true') { $this->required = true; } } /** * Convert the object to an array * * @return array */ public function toArray() { return array( 'name' => $this->name, 'type' => $this->type, 'required' => $this->required, 'default' => $this->default, 'doc' => $this->doc, 'min_length' => $this->minLength, 'max_length' => $this->maxLength, 'location' => $this->location, 'static' => $this->static, 'prepend' => $this->prepend, 'append' => $this->append, 'filters' => implode(',', $this->filters) ); } /** * Get the default or static value of the command based on a value * * @param string $value Value that is currently set * * @return mixed Returns the value, a static value if one is present, or a * default value if nothing is provided. */ public function getValue($value) { if ($this->static || ($this->default && !$value)) { $check = $this->static ?: $this->default; if ($check === 'true') { return true; } elseif ($check === 'false') { return false; } else { return $check; } } return $value; } /** * Filter a value * * @param mixed $value Value to filter * * @return mixed Returns the filtered value */ public function filter($value) { if ($this->filters) { foreach ($this->filters as $filter) { $value = call_user_func($filter, $value); } } return $value; } /** * Get the name of the parameter * * @return string */ public function getName() { return $this->name; } /** * Get the type * * @return string */ public function getType() { return $this->type; } /** * Get if the parameter is required * * @return bool */ public function getRequired() { return $this->required; } /** * Get the default value of the parameter * * @return string|null */ public function getDefault() { return $this->default; } /** * Get the docs for the parameter * * @return string|null */ public function getDoc() { return $this->doc; } /** * Get the minimum allowed length of the parameter * * @return int|null */ public function getMinLength() { return $this->minLength; } /** * Get the maximum allowed length of the parameter * * @return int|null */ public function getMaxLength() { return $this->maxLength; } /** * Get the location of the parameter * * @return int|null */ public function getLocation() { return $this->location; } /** * Get the static value of the parameter * * @return int|null */ public function getStatic() { return $this->static; } /** * Get the string to prepend to values * * @return string */ public function getPrepend() { return $this->prepend; } /** * Get the string to append to values * * @return string */ public function getAppend() { return $this->append; } /** * Get an array of filters used by the parameter * * @return array */ public function getFilters() { return $this->filters; } } src/Guzzle/Service/Description/ArrayDescriptionBuilder.php000064400000004210151742734600020006 0ustar00 $type) { $default = array(); if (!isset($type['class'])) { throw new DescriptionBuilderException('Custom types require a class attribute'); } foreach ($type as $key => $value) { if ($key != 'name' && $key != 'class') { $default[$key] = $value; } } Inspector::getInstance()->registerConstraint($name, $type['class'], $default); } } $commands = array(); if (!empty($config['commands'])) { foreach ($config['commands'] as $name => $command) { $name = $command['name'] = isset($command['name']) ? $command['name'] : $name; // Extend other commands if (!empty($command['extends'])) { if (empty($commands[$command['extends']])) { throw new DescriptionBuilderException($name . ' extends missing command ' . $command['extends']); } $params = array_merge($commands[$command['extends']]->getParams(), !empty($command['params']) ? $command['params'] : array()); $command = array_merge($commands[$command['extends']]->toArray(), $command); $command['params'] = $params; } // Use the default class $command['class'] = isset($command['class']) ? str_replace('.', '\\', $command['class']) : ServiceDescription::DEFAULT_COMMAND_CLASS; $commands[$name] = new ApiCommand($command); } } return new ServiceDescription($commands); } } src/Guzzle/Service/Description/DescriptionBuilderInterface.php000064400000000653151742734600020637 0ustar00loader) { $this->loader = new JsonLoader(); } return ServiceDescription::factory($this->loader->parseJsonFile($config)); } } src/Guzzle/Service/Description/ServiceDescription.php000064400000005736151742734600017037 0ustar00build($config); } /** * Create a new ServiceDescription * * @param array $commands Array of {@see ApiCommand} objects */ public function __construct(array $commands = array()) { $this->commands = $commands; } /** * Serialize the service description * * @return string */ public function serialize() { return json_encode(array_map(function($command) { // Convert ApiCommands into arrays $data = $command->toArray(); // Convert ApiParams into arrays $data['params'] = array_map(function($param) { return $param->toArray(); }, $data['params']); return $data; }, $this->commands)); } /** * Unserialize the service description * * @param string|array $json JSON data */ public function unserialize($json) { $this->commands = array_map(function($data) { // Convert params to ApiParam objects $data['params'] = array_map(function($param) { return new ApiParam($param); }, $data['params']); // Convert commands into ApiCommands return new ApiCommand($data); }, json_decode($json, true)); } /** * Get the API commands of the service * * @return array Returns an array of ApiCommand objects */ public function getCommands() { return $this->commands; } /** * Check if the service has a command by name * * @param string $name Name of the command to check * * @return bool */ public function hasCommand($name) { return array_key_exists($name, $this->commands); } /** * Get an API command by name * * @param string $name Name of the command * * @return ApiCommand|null */ public function getCommand($name) { return $this->hasCommand($name) ? $this->commands[$name] : null; } } src/Guzzle/Service/Description/ServiceDescriptionAbstractFactory.php000064400000002331151742734600022037 0ustar00parseXmlFile($config)); } /** * Convert an XML file to an array of service data * * @param string $file XML filename to parse * * @return array * @throws InvalidArgumentException */ protected function parseXmlFile($file) { if (!file_exists($file)) { throw new DescriptionBuilderException('Unable to open ' . $file . ' for reading'); } $xml = new \SimpleXMLElement($file, null, true); $config = array( 'types' => array(), 'commands' => array() ); // Register any custom type definitions $types = $xml->types->type; if ($types) { foreach ($types as $type) { $attr = $type->attributes(); $name = (string) $attr->name; $config['types'][$name] = array(); foreach ($attr as $key => $value) { $config['types'][$name][(string) $key] = (string) $value; } } } // Parse the commands in the XML doc $commands = $xml->commands->command; if ($commands) { foreach ($commands as $command) { $attr = $command->attributes(); $name = (string) $attr->name; $config['commands'][$name] = array( 'params' => array() ); foreach ($attr as $key => $value) { $config['commands'][$name][(string) $key] = (string) $value; } $config['commands'][$name]['doc'] = (string) $command->doc; foreach ($command->param as $param) { $attr = $param->attributes(); $paramName = (string) $attr['name']; $config['commands'][$name]['params'][$paramName] = array(); foreach ($attr as $pk => $pv) { $pv = (string) $pk == 'required' ? (string) $pv === 'true' : (string) $pv; $config['commands'][$name]['params'][$paramName][(string) $pk] = (string) $pv; } } } } // Handle XML includes $includes = $xml->includes->include; if ($includes) { foreach ($includes as $includeFile) { $path = (string) $includeFile->attributes()->path; if ($path[0] != DIRECTORY_SEPARATOR) { $path = dirname($file) . DIRECTORY_SEPARATOR . $path; } $config = array_merge_recursive(self::parseXmlFile($path), $config); } } return $config; } } src/Guzzle/Service/Exception/CommandException.php000064400000000216151742734600016127 0ustar00invalidCommands; } /** * Set the invalid commands in the CommandSet * * @param array $commands Array of Command objects */ public function setCommands(array $commands) { $this->invalidCommands = $commands; } } src/Guzzle/Service/Exception/DescriptionBuilderException.php000064400000000231151742734600020340 0ustar00 array(), 'camel' => array() ); /** * Converts strings from camel case to snake case * (e.g. CamelCase camel_case). * * @param string $word Word to convert to snake case * * @return string */ public static function snake($word) { if (isset(self::$cache['snake'][$word])) { return self::$cache['snake'][$word]; } self::pruneCache('snake'); return self::$cache['snake'][$word] = strtolower($word) == $word ? $word : strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $word)); } /** * Converts strings from snake_case to upper CamelCase * * @param string $word Value to convert into upper CamelCase * * @return string */ public static function camel($word) { if (isset(self::$cache['camel'][$word])) { return self::$cache['camel'][$word]; } self::pruneCache('camel'); return self::$cache['camel'][$word] = str_replace(' ', '', ucwords(strtr($word, '_-', ' '))); } /** * Prune one of the caches * * @param string $cache Name of the cache to prune */ private static function pruneCache($cache) { if (count(self::$cache[$cache]) == self::MAX_ENTRIES_PER_CACHE) { self::$cache[$cache] = array_slice(self::$cache[$cache], self::MAX_ENTRIES_PER_CACHE * 0.1); } } } src/Guzzle/Service/Inspector.php000064400000027747151742734600012724 0ustar00constraints = array( 'blank' => array($base . 'Blank', null), 'not_blank' => array($base . 'NotBlank', null), 'integer' => array($base . 'Numeric', null), 'float' => array($base . 'Numeric', null), 'string' => array($base . 'Type', array('type' => 'string')), 'file' => array($base . 'Type', array('type' => 'file')), 'array' => array($base . 'Type', array('type' => 'array')), 'bool' => array($base . 'Bool', null), 'boolean' => array($base . 'Bool', null), 'email' => array($base . 'Email', null), 'ip' => array($base . 'Ip', null), 'url' => array($base . 'Url', null), 'class' => array($base . 'IsInstanceOf', null), 'type' => array($base . 'Type', null), 'ctype' => array($base . 'Ctype', null), 'choice' => array($base . 'Choice', null), 'enum' => array($base . 'Choice', null), 'regex' => array($base . 'Regex', null), 'date' => array($base . 'Type', array('type' => 'string')), 'date_time' => array($base . 'Type', array('type' => 'string')), 'time' => array($base . 'Numeric', null) ); } /** * Get an instance of the Inspector * * @return Inspector */ public static function getInstance() { // @codeCoverageIgnoreStart if (!self::$instance) { self::$instance = new self(); } // @codeCoverageIgnoreEnd return self::$instance; } /** * Validate and prepare configuration parameters * * @param array $config Configuration values to apply. * @param array $defaults Default parameters * @param array $required Required parameter names * * @return Collection * @throws InvalidArgumentException if a parameter is missing */ public static function prepareConfig(array $config = null, array $defaults = null, array $required = null) { $collection = new Collection($defaults); foreach ((array) $config as $key => $value) { $collection->set($key, $value); } foreach ((array) $required as $key) { if ($collection->hasKey($key) === false) { throw new ValidationException( "Config must contain a '{$key}' key" ); } } return $collection; } /** * Enable/disable type validation of configuration settings. This is * useful for very high performance requirements. * * @param bool $typeValidation Set to TRUE or FALSE * * @return Inspector */ public function setTypeValidation($typeValidation) { $this->typeValidation = $typeValidation; } /** * Get an array of the registered constraints by name * * @return array */ public function getRegisteredConstraints() { return array_map(function($constraint) { return $constraint[0]; }, $this->constraints); } /** * Register a constraint class with a special name * * @param string $name Name of the constraint to register * @param string $class Name of the class or object to use when filtering by this name * @param array $default Default values to pass to the constraint * * @return Inspector */ public function registerConstraint($name, $class, array $default = array()) { $this->constraints[$name] = array($class, $default); } /** * Get a constraint by name * * @param string $name Constraint name * * @return ConstraintInterface * @throws InvalidArgumentException if the constraint is not registered */ public function getConstraint($name) { if (!isset($this->constraints[$name])) { throw new InvalidArgumentException($name . ' has not been registered'); } if (!isset($this->constraintCache[$name])) { $c = $this->constraints[$name][0]; $this->constraintCache[$name] = new $c(); } return $this->constraintCache[$name]; } /** * Validate a constraint by name: e.g. "type:Guzzle\Common\Collection"; * type:string; choice:a,b,c; choice:'a','b','c'; etc... * * @param string $name Constraint to retrieve with optional CSV args after colon * @param mixed $value Value to validate * * @return bool|string Returns TRUE if valid, or an error message if invalid */ public function validateConstraint($name, $value) { $parts = explode(':', $name, 2); $name = $parts[0]; $constraint = $this->getConstraint($name); if (empty($parts[1])) { $args = $this->constraints[$name][1]; } elseif (strpos($parts[1], ',')) { $args = str_getcsv($parts[1], ',', "'"); } else { $args = array($parts[1]); } return $constraint->validate($value, $args); } /** * Get an array of ApiParam objects for a class using @guzzle annotations * * @param string $class Name of a class to parse * * @return array Returns an array of ApiParam objects */ public function getApiParamsForClass($class) { if (!isset($this->cache[$class])) { $reflection = new \ReflectionClass($class); $this->cache[$class] = $this->parseDocBlock($reflection->getDocComment()); } return $this->cache[$class]; } /** * Validates that all required args are included in a config object, * and if not, throws an InvalidArgumentException with a helpful error message. Adds * default args to the passed config object if the parameter was not * set in the config object. * * @param array $params Params to validate * @param Collection $config Configuration settings * @param bool $strict Set to FALSE to allow missing required fields * @param bool $validate Set to TRUE or FALSE to validate data. * Set to false when you only need to add * default values and statics. * * @return array|bool Returns an array of errors or TRUE on success * * @throws InvalidArgumentException if any args are missing and $strict is TRUE */ public function validateConfig(array $params, Collection $config, $strict = true, $validate = true) { $errors = array(); foreach ($params as $name => $arg) { // Set the default or static value if it is not set $configValue = $arg->getValue($config->get($name)); // Inject configuration information into the config value if ($configValue && is_string($configValue)) { $configValue = $config->inject($configValue); } // Ensure that required arguments are set if ($validate && $arg->getRequired() && ($configValue === null || $configValue === '')) { $errors[] = 'Requires that the ' . $name . ' argument be supplied.' . ($arg->getDoc() ? ' (' . $arg->getDoc() . ').' : ''); continue; } // Ensure that the correct data type is being used if ($validate && $this->typeValidation && $configValue !== null && $argType = $arg->getType()) { $validation = $this->validateConstraint($argType, $configValue); if ($validation !== true) { $errors[] = $name . ': ' . $validation; continue; } } // Run the value through attached filters $configValue = $arg->filter($configValue); $config->set($name, $configValue); // Check the length values if validating data if ($validate) { $argMinLength = $arg->getMinLength(); if ($argMinLength && strlen($configValue) < $argMinLength) { $errors[] = 'Requires that the ' . $name . ' argument be >= ' . $arg->getMinLength() . ' characters.'; } $argMaxLength = $arg->getMaxLength(); if ($argMaxLength && strlen($configValue) > $argMaxLength) { $errors[] = 'Requires that the ' . $name . ' argument be <= ' . $arg->getMaxLength() . ' characters.'; } } } if (empty($errors)) { return true; } elseif ($strict) { throw new ValidationException('Validation errors: ' . implode("\n", $errors)); } return $errors; } /** * Get the Guzzle arguments from a DocBlock * * @param string $doc Docblock to parse * * @return array Returns an associative array of ApiParam objects */ protected function parseDocBlock($doc) { // Get all of the @guzzle annotations from the class $matches = array(); preg_match_all('/' . self::GUZZLE_ANNOTATION . '\s+([A-Za-z0-9_\-\.]+)\s*([A-Za-z0-9]+=".+")*/', $doc, $matches); $params = array(); if (!empty($matches[1])) { foreach ($matches[1] as $index => $match) { // Add the matched argument to the array keys $params[$match] = array(); if (isset($matches[2])) { // Break up the argument attributes by closing quote foreach (explode('" ', $matches[2][$index]) as $part) { $attrs = array(); // Find the attribute and attribute value preg_match('/([A-Za-z0-9]+)="(.+)"*/', $part, $attrs); if (isset($attrs[1]) && isset($attrs[0])) { // Sanitize the strings if ($attrs[2][strlen($attrs[2]) - 1] == '"') { $attrs[2] = substr($attrs[2], 0, strlen($attrs[2]) - 1); } $params[$match][$attrs[1]] = $attrs[2]; } } } $params[$match] = new ApiParam($params[$match]); } } return $params; } } src/Guzzle/Service/JsonLoader.php000064400000002634151742734600013002 0ustar00parseJsonFile($path), $data); } } return $data; } } src/Guzzle/Service/Resource/ResourceIterator.php000064400000020146151742734600016030 0ustar00originalCommand = $command; // Parse options from the array of options $this->data = $data; $this->limit = array_key_exists('limit', $data) ? $data['limit'] : 0; $this->pageSize = array_key_exists('page_size', $data) ? $data['page_size'] : false; } /** * Get all of the resources as an array (be careful as this could issue a * large number of requests if no limit is specified) * * @return array */ public function toArray() { return iterator_to_array($this, false); } /** * {@inheritdoc} */ public function setLimit($limit) { $this->limit = $limit; $this->resetState(); return $this; } /** * {@inheritdoc} */ public function setPageSize($pageSize) { $this->pageSize = $pageSize; $this->resetState(); return $this; } /** * Get an option from the iterator * * @param string $key Key of the option to retrieve * * @return mixed|null Returns NULL if not set or the value if set */ public function get($key) { return array_key_exists($key, $this->data) ? $this->data[$key] : null; } /** * Set an option on the iterator * * @param string $key Key of the option to set * @param mixed $value Value to set for the option * * @return ResourceIterator */ public function set($key, $value) { $this->data[$key] = $value; return $this; } /** * Return the current element. * * @return mixed Returns the current element. */ public function current() { return $this->resources ? current($this->resources) : false; } /** * Return the key of the current element. * * @return mixed */ public function key() { return max(0, $this->iteratedCount - 1); } /** * Return the total number of items that have been retrieved thus far. * * @return int */ public function count() { return $this->retrievedCount; } /** * Rewind the Iterator to the first element and send the original command */ public function rewind() { // Use the original command $this->command = clone $this->originalCommand; $this->resetState(); $this->next(); } /** * Check if there is a current element after calls to rewind() or next(). * * @return bool Returns TRUE if the current element is valid or FALSE */ public function valid() { return !$this->invalid && (!$this->resources || $this->current() || $this->nextToken) && (!$this->limit || $this->iteratedCount < $this->limit + 1); } /** * Move forward to next element and may trigger subsequent requests */ public function next() { $this->iteratedCount++; // Check if a new set of resources needs to be retrieved $sendRequest = false; if (!$this->resources) { $sendRequest = true; } else { // iterate over the internal array $current = next($this->resources); $sendRequest = $current === false && $this->nextToken && (!$this->limit || $this->iteratedCount < $this->limit + 1); } if ($sendRequest) { $this->dispatch('resource_iterator.before_send', array( 'iterator' => $this, 'resources' => $this->resources )); // Get a new command object from the original command $this->command = clone $this->originalCommand; // Send a request and retrieve the newly loaded resources $this->resources = $this->sendRequest(); // If no resources were found, then the last request was not needed // and iteration must stop if (empty($this->resources)) { $this->invalid = true; } else { // Add to the number of retrieved resources $this->retrievedCount += count($this->resources); // Ensure that we rewind to the beginning of the array reset($this->resources); } $this->dispatch('resource_iterator.after_send', array( 'iterator' => $this, 'resources' => $this->resources )); } } /** * Retrieve the NextToken that can be used in other iterators. * * @return string Returns a NextToken */ public function getNextToken() { return $this->nextToken; } /** * Returns the value that should be specified for the page size for * a request that will maintain any hard limits, but still honor the * specified pageSize if the number of items retrieved + pageSize < hard * limit * * @return int Returns the page size of the next request. */ protected function calculatePageSize() { if ($this->limit && $this->iteratedCount + $this->pageSize > $this->limit) { return 1 + ($this->limit - $this->iteratedCount); } return (int) $this->pageSize; } /** * Reset the internal state of the iterator without triggering a rewind() */ protected function resetState() { $this->iteratedCount = 0; $this->retrievedCount = 0; $this->nextToken = false; $this->resources = null; $this->invalid = false; } /** * Send a request to retrieve the next page of results. * Hook for sublasses to implement. * * @return array Returns the newly loaded resources */ abstract protected function sendRequest(); } src/Guzzle/Service/Resource/ResourceIteratorApplyBatched.php000064400000006712151742734600020314 0ustar00iterator = $iterator; $this->callback = $callback; } /** * Apply the callback to the contents of the resource iterator * * Calling this method dispatches four events: * before_apply -- Before adding a resource to a batch. Context is the resource * after_apply -- After adding the resource to a batch. Context is the resource * before_batch -- Before a batch request is sent if one is sent. Context is an array of resources * after_batch -- After a batch request is sent if one is sent. Context is an array of resources * * @param int $perBatch The number of records to batch before executing the callback * * @return int Returns the number of resources iterated */ public function apply($perBatch = 20) { if ($this->iterated == 0) { $batched = array(); foreach ($this->iterator as $resource) { $batched[] = $resource; if (count($batched) >= $perBatch) { $this->applyBatch($batched); $batched = array(); } $this->iterated++; } if (count($batched)) { $this->applyBatch($batched); } } return $this->iterated; } /** * Get the total number of batches sent * * @return int */ public function getBatchCount() { return $this->batches; } /** * Get the total number of iterated resources * * @return int */ public function getIteratedCount() { return $this->iterated; } /** * Apply the callback to a collection of resources * * @param array $batch */ private function applyBatch(array $batch) { $this->batches++; $this->dispatch('iterator_batch.before_batch', array( 'iterator' => $this, 'batch' => $batch )); call_user_func_array($this->callback, array( $this->iterator, $batch )); $this->dispatch('iterator_batch.after_batch', array( 'iterator' => $this, 'batch' => $batch )); } } src/Guzzle/Service/Resource/ResourceIteratorClassFactory.php000064400000003104151742734600020341 0ustar00 CamelCaseIterator). */ class ResourceIteratorClassFactory implements ResourceIteratorFactoryInterface { /** * @var string */ protected $baseNamespace; /** * @param string $baseNamespace Base namespace of all iterator object. */ public function __construct($baseNamespace) { $this->baseNamespace = $baseNamespace; } /** * Create a resource iterator * * @param CommandInterface $data Command used for building the iterator * @param array $options Iterator options. * * @return ResourceIteratorInterface */ public function build($data, array $options = null) { if (!($data instanceof CommandInterface)) { throw new InvalidArgumentException('The first argument must be an ' . 'instance of CommandInterface'); } // Determine the name of the class to load $className = $this->baseNamespace . '\\' . Inflector::camel($data->getName()) . 'Iterator'; return new $className($data, $options); } } src/Guzzle/Service/Resource/ResourceIteratorFactoryInterface.php000064400000000674151742734600021205 0ustar00assertEquals(array(), AbstractHasDispatcher::getAllEvents()); } /** * @covers Guzzle\Common\AbstractHasDispatcher::setEventDispatcher * @covers Guzzle\Common\AbstractHasDispatcher::getEventDispatcher */ public function testAllowsDispatcherToBeInjected() { $d = new EventDispatcher(); $mock = $this->getMockForAbstractClass('Guzzle\Common\AbstractHasDispatcher'); $this->assertSame($mock, $mock->setEventDispatcher($d)); $this->assertSame($d, $mock->getEventDispatcher()); } /** * @covers Guzzle\Common\AbstractHasDispatcher::getEventDispatcher */ public function testCreatesDefaultEventDispatcherIfNeeded() { $mock = $this->getMockForAbstractClass('Guzzle\Common\AbstractHasDispatcher'); $this->assertInstanceOf('Symfony\Component\EventDispatcher\EventDispatcher', $mock->getEventDispatcher()); } /** * @covers Guzzle\Common\AbstractHasDispatcher::dispatch */ public function testHelperDispatchesEvents() { $data = array(); $mock = $this->getMockForAbstractClass('Guzzle\Common\AbstractHasDispatcher'); $mock->getEventDispatcher()->addListener('test', function(Event $e) use (&$data) { $data = $e->getIterator()->getArrayCopy(); }); $mock->dispatch('test', array( 'param' => 'abc' )); $this->assertEquals(array( 'param' => 'abc', ), $data); } } tests/Guzzle/Tests/Common/Cache/CacheAdapterFactoryTest.php000064400000007745151742734600017756 0ustar00cache = new ArrayCache(); $this->adapter = new DoctrineCacheAdapter($this->cache); } /** * @covers Guzzle\Common\Cache\CacheAdapterFactory::factory * @expectedException InvalidArgumentException */ public function testEnsuresConfigIsArray() { CacheAdapterFactory::factory(new \stdClass()); } /** * @covers Guzzle\Common\Cache\CacheAdapterFactory::factory * @expectedException InvalidArgumentException * @expectedExceptionMessage cache.provider is a required CacheAdapterFactory option */ public function testEnsuresRequiredProviderOption() { CacheAdapterFactory::factory(array( 'cache.adapter' => $this->adapter )); } /** * @covers Guzzle\Common\Cache\CacheAdapterFactory::factory * @expectedException Guzzle\Common\Exception\InvalidArgumentException * @expectedExceptionMessage cache.adapter is a required CacheAdapterFactory option */ public function testEnsuresRequiredAdapterOption() { CacheAdapterFactory::factory(array( 'cache.provider' => $this->cache )); } /** * @covers Guzzle\Common\Cache\CacheAdapterFactory::factory * @expectedException InvalidArgumentException * @expectedExceptionMessage foo is not a valid class for cache.adapter */ public function testEnsuresClassesExist() { CacheAdapterFactory::factory(array( 'cache.provider' => 'abc', 'cache.adapter' => 'foo' )); } /** * @covers Guzzle\Common\Cache\CacheAdapterFactory::factory * @covers Guzzle\Common\Cache\CacheAdapterFactory::createObject */ public function testCreatesProviderFromConfig() { $cache = CacheAdapterFactory::factory(array( 'cache.provider' => 'Doctrine\Common\Cache\ApcCache', 'cache.adapter' => 'Guzzle\Common\Cache\DoctrineCacheAdapter' )); $this->assertInstanceOf('Guzzle\Common\Cache\DoctrineCacheAdapter', $cache); $this->assertInstanceOf('Doctrine\Common\Cache\ApcCache', $cache->getCacheObject()); } /** * @covers Guzzle\Common\Cache\CacheAdapterFactory::factory * @covers Guzzle\Common\Cache\CacheAdapterFactory::createObject */ public function testCreatesProviderFromConfigWithArguments() { $cache = CacheAdapterFactory::factory(array( 'cache.provider' => 'Doctrine\Common\Cache\ApcCache', 'cache.provider.args' => array(), 'cache.adapter' => 'Guzzle\Common\Cache\DoctrineCacheAdapter', 'cache.adapter.args' => array() )); $this->assertInstanceOf('Guzzle\Common\Cache\DoctrineCacheAdapter', $cache); $this->assertInstanceOf('Doctrine\Common\Cache\ApcCache', $cache->getCacheObject()); } /** * @covers Guzzle\Common\Cache\CacheAdapterFactory * @expectedException Guzzle\Common\Exception\RuntimeException */ public function testWrapsExceptionsOnObjectCreation() { CacheAdapterFactory::factory(array( 'cache.provider' => 'Guzzle\Tests\Mock\ExceptionMock', 'cache.adapter' => 'Guzzle\Tests\Mock\ExceptionMock' )); } /** * @covers Guzzle\Common\Cache\CacheAdapterFactory */ public function testCreatesNullCacheAdapterByDefault() { $adapter = CacheAdapterFactory::factory(array()); $this->assertInstanceOf('Guzzle\\Common\\Cache\\NullCacheAdapter', $adapter); } } tests/Guzzle/Tests/Common/Cache/CacheAdapterTest.php000064400000004061151742734600016412 0ustar00cache = new ArrayCache(); $this->adapter = new DoctrineCacheAdapter($this->cache); } /** * Cleans up the environment after running a test. */ protected function tearDown() { $this->adapter = null; $this->cache = null; parent::tearDown(); } /** * @covers \Guzzle\Common\Cache\AbstractCacheAdapter::getCacheObject */ public function testGetCacheObject() { $this->assertEquals($this->cache, $this->adapter->getCacheObject()); } /** * @covers \Guzzle\Common\Cache\DoctrineCacheAdapter::save */ public function testSave() { $this->assertTrue($this->adapter->save('test', 'data', 1000)); } /** * @covers \Guzzle\Common\Cache\DoctrineCacheAdapter::fetch */ public function testFetch() { $this->assertTrue($this->adapter->save('test', 'data', 1000)); $this->assertEquals('data', $this->adapter->fetch('test')); } /** * @covers \Guzzle\Common\Cache\DoctrineCacheAdapter::contains */ public function testContains() { $this->assertTrue($this->adapter->save('test', 'data', 1000)); $this->assertTrue($this->adapter->contains('test')); } /** * @covers \Guzzle\Common\Cache\DoctrineCacheAdapter::delete */ public function testDelete() { $this->assertTrue($this->adapter->save('test', 'data', 1000)); $this->assertTrue($this->adapter->delete('test')); $this->assertFalse($this->adapter->contains('test')); } } tests/Guzzle/Tests/Common/Cache/ClosureCacheAdapterTest.php000064400000006110151742734600017744 0ustar00callables = array( 'contains' => function($id, $options = array()) use ($that) { return array_key_exists($id, $that->data); }, 'delete' => function($id, $options = array()) use ($that) { unset($that->data[$id]); return true; }, 'fetch' => function($id, $options = array()) use ($that) { return array_key_exists($id, $that->data) ? $that->data[$id] : null; }, 'save' => function($id, $data, $lifeTime, $options = array()) use ($that) { $that->data[$id] = $data; return true; } ); $this->adapter = new ClosureCacheAdapter($this->callables); } /** * Cleans up the environment after running a test. */ protected function tearDown() { $this->cache = null; $this->callables = null; parent::tearDown(); } /** * @covers Guzzle\Common\Cache\ClosureCacheAdapter::__construct * @expectedException InvalidArgumentException */ public function testEnsuresCallablesArePresent() { $callables = $this->callables; unset($callables['delete']); $cache = new ClosureCacheAdapter($callables); } /** * @covers Guzzle\Common\Cache\ClosureCacheAdapter::__construct */ public function testAllCallablesMustBePresent() { $cache = new ClosureCacheAdapter($this->callables); } /** * @covers Guzzle\Common\Cache\ClosureCacheAdapter::save * @covers Guzzle\Common\Cache\ClosureCacheAdapter::fetch */ public function testCachesDataUsingCallables() { $this->assertTrue($this->adapter->save('test', 'data', 1000)); $this->assertEquals('data', $this->adapter->fetch('test')); } /** * @covers Guzzle\Common\Cache\ClosureCacheAdapter::contains */ public function testChecksIfCacheContainsKeys() { $this->adapter->save('test', 'data', 1000); $this->assertTrue($this->adapter->contains('test')); $this->assertFalse($this->adapter->contains('foo')); } /** * @covers Guzzle\Common\Cache\ClosureCacheAdapter::delete */ public function testDeletesFromCacheByKey() { $this->adapter->save('test', 'data', 1000); $this->assertTrue($this->adapter->contains('test')); $this->adapter->delete('test'); $this->assertFalse($this->adapter->contains('test')); } } tests/Guzzle/Tests/Common/Cache/NullCacheAdapterTest.php000064400000001020151742734600017235 0ustar00assertEquals(false, $c->contains('foo')); $this->assertEquals(true, $c->delete('foo')); $this->assertEquals(false, $c->fetch('foo')); $this->assertEquals(true, $c->save('foo', 'bar')); } } tests/Guzzle/Tests/Common/Cache/Zf1CacheAdapterTest.php000064400000001104151742734600016766 0ustar00assertTrue($adapter->save('id', 'data')); $this->assertTrue($adapter->delete('id')); $this->assertEquals('foo', $adapter->fetch('id')); $this->assertEquals('123456', $adapter->contains('id')); } } tests/Guzzle/Tests/Common/Cache/Zf2CacheAdapterTest.php000064400000005134151742734600016776 0ustar00cache = StorageFactory::factory(array( 'adapter' => 'memory' )); $this->adapter = new Zf2CacheAdapter($this->cache); } /** * Cleans up the environment after running a test. */ protected function tearDown() { $this->adapter = null; $this->cache = null; parent::tearDown(); } /** * @covers Guzzle\Common\Cache\Zf2CacheAdapter::__construct */ public function testConstructorAddsDefaultOptions() { $default = array( 'contains' => array( 'namespace' => 'foo' ) ); $adapter = new Zf2CacheAdapter(StorageFactory::factory(array( 'adapter' => 'memory' )), $default); // Access the protected property $class = new \ReflectionClass($adapter); $property = $class->getProperty('defaultOptions'); $property->setAccessible(true); $defaultOptions = $property->getValue($adapter); $this->assertEquals(array_merge($default, array( 'delete' => array(), 'fetch' => array(), 'save' => array() )), $defaultOptions); } /** * @covers Guzzle\Common\Cache\Zf2CacheAdapter::save * @covers Guzzle\Common\Cache\Zf2CacheAdapter::fetch */ public function testCachesDataUsingCallables() { $this->assertTrue($this->adapter->save('test', 'data', 1000)); $this->assertEquals('data', $this->adapter->fetch('test')); } /** * @covers Guzzle\Common\Cache\Zf2CacheAdapter::contains */ public function testChecksIfCacheContainsKeys() { $this->adapter->save('test', 'data', 1000); $this->assertTrue($this->adapter->contains('test')); $this->assertFalse($this->adapter->contains('foo')); } /** * @covers Guzzle\Common\Cache\Zf2CacheAdapter::delete */ public function testDeletesFromCacheByKey() { $this->adapter->save('test', 'data', 1000); $this->assertTrue($this->adapter->contains('test')); $this->adapter->delete('test'); $this->assertFalse($this->adapter->contains('test')); } } tests/Guzzle/Tests/Common/CollectionTest.php000064400000031125151742734600015177 0ustar00coll = new Collection(); } /** * @covers Guzzle\Common\Collection::__construct */ public function testConstructorCanBeCalledWithNoParams() { $this->coll = new Collection(); $p = $this->coll->getAll(); $this->assertEmpty($p, '-> Collection must be empty when no data is passed'); } /** * @covers Guzzle\Common\Collection::__construct * @covers Guzzle\Common\Collection::getAll */ public function testConstructorCanBeCalledWithParams() { $testData = array( 'test' => 'value', 'test_2' => 'value2' ); $this->coll = new Collection($testData); $this->assertEquals($this->coll->getAll(), $testData, '-> getAll() must return the data passed in the constructor'); } /** * @covers Guzzle\Common\Collection::__toString */ public function testCollectionCanBeConvertingIntoString() { $data = new Collection(); $this->assertEquals('Guzzle\Common\Collection@' . spl_object_hash($data), (string) $data, '-> __toString() must represent the collection'); } /** * Test the IteratorAggregate implementation of theCollection object * * @covers Guzzle\Common\Collection::getIterator */ public function testImplementsIteratorAggregate() { $this->coll->set('key', 'value'); $this->assertInstanceOf('ArrayIterator', $this->coll->getIterator()); $this->assertEquals(1, count($this->coll)); $total = 0; foreach ($this->coll as $key => $value) { $this->assertEquals('key', $key); $this->assertEquals('value', $value); $total++; } $this->assertEquals(1, $total); } /** * @covers Guzzle\Common\Collection::add */ public function testCanAddValuesToExistingKeysByUsingArray() { $this->coll->add('test', 'value1'); $this->assertEquals($this->coll->getAll(), array('test' => 'value1')); $this->coll->add('test', 'value2'); $this->assertEquals($this->coll->getAll(), array('test' => array('value1', 'value2'))); $this->coll->add('test', 'value3'); $this->assertEquals($this->coll->getAll(), array('test' => array('value1', 'value2', 'value3'))); } /** * @covers Guzzle\Common\Collection::merge * @covers Guzzle\Common\Collection::getAll */ public function testHandlesMergingInDisparateDataSources() { $params = array( 'test' => 'value1', 'test2' => 'value2', 'test3' => array('value3', 'value4') ); $this->coll->merge($params); $this->assertEquals($this->coll->getAll(), $params); // Pass an invalid value and expect the same unaltered object $this->assertEquals($this->coll->merge(false), $this->coll); // Pass the same object to itself $this->assertEquals($this->coll->merge($this->coll), $this->coll); } /** * @covers Guzzle\Common\Collection::clear * @covers Guzzle\Common\Collection::remove */ public function testCanClearAllDataOrSpecificKeys() { $this->coll->merge(array( 'test' => 'value1', 'test2' => 'value2' )); // Clear a specific parameter by name $this->coll->remove('test'); $this->assertEquals($this->coll->getAll(), array( 'test2' => 'value2' )); // Clear all parameters $this->coll->clear(); $this->assertEquals($this->coll->getAll(), array()); } /** * @covers Guzzle\Common\Collection::get * @covers Guzzle\Common\Collection::getAll */ public function testGetsValuesByKey() { $this->assertNull($this->coll->get('test')); $this->coll->add('test', 'value'); $this->assertEquals('value', $this->coll->get('test')); $this->coll->set('test2', 'v2'); $this->coll->set('test3', 'v3'); $this->assertEquals(array( 'test' => 'value', 'test2' => 'v2' ), $this->coll->getAll(array('test', 'test2'))); } /** * @covers Guzzle\Common\Collection::getKeys * @covers Guzzle\Common\Collection::remove */ public function testProvidesKeys() { $this->assertEquals(array(), $this->coll->getKeys()); $this->coll->merge(array( 'test1' => 'value1', 'test2' => 'value2' )); $this->assertEquals(array('test1', 'test2'), $this->coll->getKeys()); // Returns the cached array previously returned $this->assertEquals(array('test1', 'test2'), $this->coll->getKeys()); $this->coll->remove('test1'); $this->assertEquals(array('test2'), $this->coll->getKeys()); $this->coll->add('test3', 'value3'); $this->assertEquals(array('test2', 'test3'), $this->coll->getKeys()); } /** * @covers Guzzle\Common\Collection::hasKey */ public function testChecksIfHasKey() { $this->assertFalse($this->coll->hasKey('test')); $this->coll->add('test', 'value'); $this->assertEquals(true, $this->coll->hasKey('test')); $this->coll->add('test2', 'value2'); $this->assertEquals(true, $this->coll->hasKey('test')); $this->assertEquals(true, $this->coll->hasKey('test2')); $this->assertFalse($this->coll->hasKey('testing')); $this->assertEquals(false, $this->coll->hasKey('AB-C', 'junk')); } /** * @covers Guzzle\Common\Collection::hasValue */ public function testChecksIfHasValue() { $this->assertFalse($this->coll->hasValue('value')); $this->coll->add('test', 'value'); $this->assertEquals('test', $this->coll->hasValue('value')); $this->coll->add('test2', 'value2'); $this->assertEquals('test', $this->coll->hasValue('value')); $this->assertEquals('test2', $this->coll->hasValue('value2')); $this->assertFalse($this->coll->hasValue('val')); } /** * @covers Guzzle\Common\Collection::getAll */ public function testCanGetAllValuesByArray() { $this->coll->add('foo', 'bar'); $this->coll->add('tEsT', 'value'); $this->coll->add('tesTing', 'v2'); $this->coll->add('key', 'v3'); $this->assertNull($this->coll->get('test')); $this->assertEquals(array( 'foo' => 'bar', 'tEsT' => 'value', 'tesTing' => 'v2' ), $this->coll->getAll(array( 'foo', 'tesTing', 'tEsT' ))); } /** * @covers Guzzle\Common\Collection::count */ public function testImplementsCount() { $data = new Collection(); $this->assertEquals(0, $data->count()); $data->add('key', 'value'); $this->assertEquals(1, count($data)); $data->add('key', 'value2'); $this->assertEquals(1, count($data)); $data->add('key_2', 'value3'); $this->assertEquals(2, count($data)); } /** * @covers Guzzle\Common\Collection::merge */ public function testAddParamsByMerging() { $params = array( 'test' => 'value1', 'test2' => 'value2', 'test3' => array('value3', 'value4') ); // Add some parameters $this->coll->merge($params); // Add more parameters by merging them in $this->coll->merge(array( 'test' => 'another', 'different_key' => 'new value' )); $this->assertEquals(array( 'test' => array('value1', 'another'), 'test2' => 'value2', 'test3' => array('value3', 'value4'), 'different_key' => 'new value' ), $this->coll->getAll()); } /** * @covers Guzzle\Common\Collection::filter */ public function testAllowsFunctionalFilter() { $this->coll->merge(array( 'fruit' => 'apple', 'number' => 'ten', 'prepositions' => array('about', 'above', 'across', 'after'), 'same_number' => 'ten' )); $filtered = $this->coll->filter(function($key, $value) { return $value == 'ten'; }); $this->assertNotEquals($filtered, $this->coll); $this->assertEquals(array( 'number' => 'ten', 'same_number' => 'ten' ), $filtered->getAll()); } /** * @covers Guzzle\Common\Collection::map */ public function testAllowsFunctionalMapping() { $this->coll->merge(array( 'number_1' => 1, 'number_2' => 2, 'number_3' => 3 )); $mapped = $this->coll->map(function($key, $value) { return $value * $value; }); $this->assertNotEquals($mapped, $this->coll); $this->assertEquals(array( 'number_1' => 1, 'number_2' => 4, 'number_3' => 9 ), $mapped->getAll()); } /** * @covers Guzzle\Common\Collection::offsetGet * @covers Guzzle\Common\Collection::offsetSet * @covers Guzzle\Common\Collection::offsetUnset * @covers Guzzle\Common\Collection::offsetExists */ public function testImplementsArrayAccess() { $this->coll->merge(array( 'k1' => 'v1', 'k2' => 'v2' )); $this->assertTrue($this->coll->offsetExists('k1')); $this->assertFalse($this->coll->offsetExists('Krull')); $this->coll->offsetSet('k3', 'v3'); $this->assertEquals('v3', $this->coll->offsetGet('k3')); $this->assertEquals('v3', $this->coll->get('k3')); $this->coll->offsetUnset('k1'); $this->assertFalse($this->coll->offsetExists('k1')); } /** * @covers Guzzle\Common\Collection::filter * @covers Guzzle\Common\Collection::map */ public function testUsesStaticWhenCreatingNew() { $qs = new QueryString(array( 'a' => 'b', 'c' => 'd' )); $this->assertInstanceOf('Guzzle\\Http\\QueryString', $qs->map(function($a, $b) {})); $this->assertInstanceOf('Guzzle\\Common\\Collection', $qs->map(function($a, $b) {}, array(), false)); $this->assertInstanceOf('Guzzle\\Http\\QueryString', $qs->filter(function($a, $b) {})); $this->assertInstanceOf('Guzzle\\Common\\Collection', $qs->filter(function($a, $b) {}, false)); } /** * @covers Guzzle\Common\Collection::replace */ public function testCanReplaceAllData() { $this->assertSame($this->coll, $this->coll->replace(array( 'a' => '123' ))); $this->assertEquals(array( 'a' => '123' ), $this->coll->getAll()); } /** * @covers Guzzle\Common\Collection::getPregMatchValue */ public function testReturnsValuesForPregMatch() { $c = new Collection(array('foo' => 'bar')); $this->assertEquals('bar', $c->getPregMatchValue(array(1 => 'foo'))); } public function dataProvider() { return array( array('this_is_a_test', '{ a }_is_a_{ b }', array( 'a' => 'this', 'b' => 'test' )), array('this_is_a_test', '{abc}_is_a_{ 0 }', array( 'abc' => 'this', 0 => 'test' )), array('this_is_a_test', '{ abc }_is_{ not_found }a_{ 0 }', array( 'abc' => 'this', 0 => 'test' )), array('this_is_a_test', 'this_is_a_test', array( 'abc' => 'this' )), array('_is_a_', '{ abc }_is_{ not_found }a_{ 0 }', array()), array('_is_a_', '{abc}_is_{not_found}a_{0}', array()), ); } /** * @covers Guzzle\Common\Collection::inject * @dataProvider dataProvider */ public function testInjectsConfigData($output, $input, $config) { $collection = new Collection($config); $this->assertEquals($output, $collection->inject($input)); } /** * @covers Guzzle\Common\Collection::keySearch */ public function testCanSearchByKey() { $collection = new Collection(array( 'foo' => 'bar', 'BaZ' => 'pho' )); $this->assertEquals('foo', $collection->keySearch('FOO')); $this->assertEquals('BaZ', $collection->keySearch('baz')); $this->assertEquals(false, $collection->keySearch('Bar')); } } tests/Guzzle/Tests/Common/EventTest.php000064400000002772151742734600014173 0ustar00 '123', 'other' => '456', 'event' => 'test.notify' )); } /** * @covers Guzzle\Common\Event::__construct */ public function testAllowsParameterInjection() { $event = new Event(array( 'test' => '123' )); $this->assertEquals('123', $event['test']); } /** * @covers Guzzle\Common\Event::offsetGet * @covers Guzzle\Common\Event::offsetSet * @covers Guzzle\Common\Event::offsetUnset * @covers Guzzle\Common\Event::offsetExists */ public function testImplementsArrayAccess() { $event = $this->getEvent(); $this->assertEquals('123', $event['test']); $this->assertNull($event['foobar']); $this->assertTrue($event->offsetExists('test')); $this->assertFalse($event->offsetExists('foobar')); unset($event['test']); $this->assertFalse($event->offsetExists('test')); $event['test'] = 'new'; $this->assertEquals('new', $event['test']); } /** * @covers Guzzle\Common\Event::getIterator */ public function testImplementsIteratorAggregate() { $event = $this->getEvent(); $this->assertInstanceOf('ArrayIterator', $event->getIterator()); } } tests/Guzzle/Tests/Common/Exception/ExceptionCollectionTest.php000064400000002317151742734600021015 0ustar00getExceptions(); $e->add($exceptions[0]); $e->add($exceptions[1]); $this->assertEquals("Test\nTesting", $e->getMessage()); } public function testActsAsArray() { $e = new ExceptionCollection(); $exceptions = $this->getExceptions(); $e->add($exceptions[0]); $e->add($exceptions[1]); $this->assertEquals(2, count($e)); $this->assertEquals($exceptions, $e->getIterator()->getArrayCopy()); } public function testCanAddSelf() { $e1 = new ExceptionCollection(); $e1->add(new \Exception("Test")); $e2 = new ExceptionCollection(); $e2->add(new \Exception("Test 2")); $e1->add($e2); $this->assertEquals("Test\nTest 2", $e1->getMessage()); } } tests/Guzzle/Tests/Common/Log/ClosureLogAdapterTest.php000064400000002010151742734600017173 0ustar00adapter = new ClosureLogAdapter(function($message, $priority, $extras = null) use ($that) { $that->modified = array($message, $priority, $extras); }); $this->adapter->log('test', \LOG_NOTICE, 'localhost'); $this->assertEquals(array('test', \LOG_NOTICE, 'localhost'), $this->modified); } /** * @covers \Guzzle\Common\Log\ClosureLogAdapter * @expectedException InvalidArgumentException */ public function testThrowsExceptionWhenNotCallable() { $abc = 123; $this->adapter = new ClosureLogAdapter($abc); } } tests/Guzzle/Tests/Common/Log/MonologLogAdapterTest.php000064400000001221151742734600017174 0ustar00pushHandler($handler); $adapter = new MonologLogAdapter($log); $adapter->log('test!', LOG_INFO); $this->assertTrue($handler->hasInfoRecords()); } } tests/Guzzle/Tests/Common/Log/Zend_Log.php000064400000047040151742734600014471 0ustar00_priorities = array_flip($r->getConstants()); if ($writer !== null) { $this->addWriter($writer); } } /** * Factory to construct the logger and one or more writers * based on the configuration array * * @param array|Zend_Config Array or instance of Zend_Config * @return Zend_Log * @throws Zend_Log_Exception */ static public function factory($config = array()) { if ($config instanceof Zend_Config) { $config = $config->toArray(); } if (!is_array($config) || empty($config)) { /** @see Zend_Log_Exception */ require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception('Configuration must be an array or instance of Zend_Config'); } $log = new self; if (array_key_exists('timestampFormat', $config)) { if (null != $config['timestampFormat'] && '' != $config['timestampFormat']) { $log->setTimestampFormat($config['timestampFormat']); } unset($config['timestampFormat']); } if (!is_array(current($config))) { $log->addWriter(current($config)); } else { foreach($config as $writer) { $log->addWriter($writer); } } return $log; } /** * Construct a writer object based on a configuration array * * @param array $spec config array with writer spec * @return Zend_Log_Writer_Abstract * @throws Zend_Log_Exception */ protected function _constructWriterFromConfig($config) { $writer = $this->_constructFromConfig('writer', $config, $this->_defaultWriterNamespace); if (!$writer instanceof Zend_Log_Writer_Abstract) { $writerName = is_object($writer) ? get_class($writer) : 'The specified writer'; /** @see Zend_Log_Exception */ require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception("{$writerName} does not extend Zend_Log_Writer_Abstract!"); } if (isset($config['filterName'])) { $filter = $this->_constructFilterFromConfig($config); $writer->addFilter($filter); } if (isset($config['formatterName'])) { $formatter = $this->_constructFormatterFromConfig($config); $writer->setFormatter($formatter); } return $writer; } /** * Construct filter object from configuration array or Zend_Config object * * @param array|Zend_Config $config Zend_Config or Array * @return Zend_Log_Filter_Interface * @throws Zend_Log_Exception */ protected function _constructFilterFromConfig($config) { $filter = $this->_constructFromConfig('filter', $config, $this->_defaultFilterNamespace); if (!$filter instanceof Zend_Log_Filter_Interface) { $filterName = is_object($filter) ? get_class($filter) : 'The specified filter'; /** @see Zend_Log_Exception */ require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception("{$filterName} does not implement Zend_Log_Filter_Interface"); } return $filter; } /** * Construct formatter object from configuration array or Zend_Config object * * @param array|Zend_Config $config Zend_Config or Array * @return Zend_Log_Formatter_Interface * @throws Zend_Log_Exception */ protected function _constructFormatterFromConfig($config) { $formatter = $this->_constructFromConfig('formatter', $config, $this->_defaultFormatterNamespace); if (!$formatter instanceof Zend_Log_Formatter_Interface) { $formatterName = is_object($formatter) ? get_class($formatter) : 'The specified formatter'; /** @see Zend_Log_Exception */ require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception($formatterName . ' does not implement Zend_Log_Formatter_Interface'); } return $formatter; } /** * Construct a filter or writer from config * * @param string $type 'writer' of 'filter' * @param mixed $config Zend_Config or Array * @param string $namespace * @return object * @throws Zend_Log_Exception */ protected function _constructFromConfig($type, $config, $namespace) { if ($config instanceof Zend_Config) { $config = $config->toArray(); } if (!is_array($config) || empty($config)) { require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception( 'Configuration must be an array or instance of Zend_Config' ); } $params = isset($config[ $type .'Params' ]) ? $config[ $type .'Params' ] : array(); $className = $this->getClassName($config, $type, $namespace); if (!class_exists($className)) { require_once 'Zend/Loader.php'; Zend_Loader::loadClass($className); } $reflection = new ReflectionClass($className); if (!$reflection->implementsInterface('Zend_Log_FactoryInterface')) { require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception( $className . ' does not implement Zend_Log_FactoryInterface and can not be constructed from config.' ); } return call_user_func(array($className, 'factory'), $params); } /** * Get the writer or filter full classname * * @param array $config * @param string $type filter|writer * @param string $defaultNamespace * @return string full classname * @throws Zend_Log_Exception */ protected function getClassName($config, $type, $defaultNamespace) { if (!isset($config[$type . 'Name'])) { require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception("Specify {$type}Name in the configuration array"); } $className = $config[$type . 'Name']; $namespace = $defaultNamespace; if (isset($config[$type . 'Namespace'])) { $namespace = $config[$type . 'Namespace']; } // PHP >= 5.3.0 namespace given? if (substr($namespace, -1) == '\\') { return $namespace . $className; } // emtpy namespace given? if (strlen($namespace) === 0) { return $className; } return $namespace . '_' . $className; } /** * Packs message and priority into Event array * * @param string $message Message to log * @param integer $priority Priority of message * @return array Event array */ protected function _packEvent($message, $priority) { return array_merge(array( 'timestamp' => date($this->_timestampFormat), 'message' => $message, 'priority' => $priority, 'priorityName' => $this->_priorities[$priority] ), $this->_extras ); } /** * Class destructor. Shutdown log writers * * @return void */ public function __destruct() { foreach($this->_writers as $writer) { $writer->shutdown(); } } /** * Undefined method handler allows a shortcut: * $log->priorityName('message') * instead of * $log->log('message', Zend_Log::PRIORITY_NAME) * * @param string $method priority name * @param string $params message to log * @return void * @throws Zend_Log_Exception */ public function __call($method, $params) { $priority = strtoupper($method); if (($priority = array_search($priority, $this->_priorities)) !== false) { switch (count($params)) { case 0: /** @see Zend_Log_Exception */ require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception('Missing log message'); case 1: $message = array_shift($params); $extras = null; break; default: $message = array_shift($params); $extras = array_shift($params); break; } $this->log($message, $priority, $extras); } else { /** @see Zend_Log_Exception */ require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception('Bad log priority'); } } /** * Log a message at a priority * * @param string $message Message to log * @param integer $priority Priority of message * @param mixed $extras Extra information to log in event * @return void * @throws Zend_Log_Exception */ public function log($message, $priority, $extras = null) { // sanity checks if (empty($this->_writers)) { /** @see Zend_Log_Exception */ require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception('No writers were added'); } if (! isset($this->_priorities[$priority])) { /** @see Zend_Log_Exception */ require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception('Bad log priority'); } // pack into event required by filters and writers $event = $this->_packEvent($message, $priority); // Check to see if any extra information was passed if (!empty($extras)) { $info = array(); if (is_array($extras)) { foreach ($extras as $key => $value) { if (is_string($key)) { $event[$key] = $value; } else { $info[] = $value; } } } else { $info = $extras; } if (!empty($info)) { $event['info'] = $info; } } // abort if rejected by the global filters foreach ($this->_filters as $filter) { if (! $filter->accept($event)) { return; } } // send to each writer foreach ($this->_writers as $writer) { $writer->write($event); } } /** * Add a custom priority * * @param string $name Name of priority * @param integer $priority Numeric priority * @throws Zend_Log_Exception */ public function addPriority($name, $priority) { // Priority names must be uppercase for predictability. $name = strtoupper($name); if (isset($this->_priorities[$priority]) || false !== array_search($name, $this->_priorities)) { /** @see Zend_Log_Exception */ require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception('Existing priorities cannot be overwritten'); } $this->_priorities[$priority] = $name; return $this; } /** * Add a filter that will be applied before all log writers. * Before a message will be received by any of the writers, it * must be accepted by all filters added with this method. * * @param int|Zend_Config|array|Zend_Log_Filter_Interface $filter * @return Zend_Log * @throws Zend_Log_Exception */ public function addFilter($filter) { if (is_int($filter)) { /** @see Zend_Log_Filter_Priority */ require_once 'Zend/Log/Filter/Priority.php'; $filter = new Zend_Log_Filter_Priority($filter); } elseif ($filter instanceof Zend_Config || is_array($filter)) { $filter = $this->_constructFilterFromConfig($filter); } elseif(! $filter instanceof Zend_Log_Filter_Interface) { /** @see Zend_Log_Exception */ require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception('Invalid filter provided'); } $this->_filters[] = $filter; return $this; } /** * Add a writer. A writer is responsible for taking a log * message and writing it out to storage. * * @param mixed $writer Zend_Log_Writer_Abstract or Config array * @return Zend_Log */ public function addWriter($writer) { if (is_array($writer) || $writer instanceof Zend_Config) { $writer = $this->_constructWriterFromConfig($writer); } if (!$writer instanceof Zend_Log_Writer_Abstract) { /** @see Zend_Log_Exception */ require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception( 'Writer must be an instance of Zend_Log_Writer_Abstract' . ' or you should pass a configuration array' ); } $this->_writers[] = $writer; return $this; } /** * Set an extra item to pass to the log writers. * * @param string $name Name of the field * @param string $value Value of the field * @return Zend_Log */ public function setEventItem($name, $value) { $this->_extras = array_merge($this->_extras, array($name => $value)); return $this; } /** * Register Logging system as an error handler to log php errors * Note: it still calls the original error handler if set_error_handler is able to return it. * * Errors will be mapped as: * E_NOTICE, E_USER_NOTICE => NOTICE * E_WARNING, E_CORE_WARNING, E_USER_WARNING => WARN * E_ERROR, E_USER_ERROR, E_CORE_ERROR, E_RECOVERABLE_ERROR => ERR * E_DEPRECATED, E_STRICT, E_USER_DEPRECATED => DEBUG * (unknown/other) => INFO * * @link http://www.php.net/manual/en/function.set-error-handler.php Custom error handler * * @return Zend_Log */ public function registerErrorHandler() { // Only register once. Avoids loop issues if it gets registered twice. if ($this->_registeredErrorHandler) { return $this; } $this->_origErrorHandler = set_error_handler(array($this, 'errorHandler')); // Contruct a default map of phpErrors to Zend_Log priorities. // Some of the errors are uncatchable, but are included for completeness $this->_errorHandlerMap = array( E_NOTICE => Zend_Log::NOTICE, E_USER_NOTICE => Zend_Log::NOTICE, E_WARNING => Zend_Log::WARN, E_CORE_WARNING => Zend_Log::WARN, E_USER_WARNING => Zend_Log::WARN, E_ERROR => Zend_Log::ERR, E_USER_ERROR => Zend_Log::ERR, E_CORE_ERROR => Zend_Log::ERR, E_RECOVERABLE_ERROR => Zend_Log::ERR, E_STRICT => Zend_Log::DEBUG, ); // PHP 5.3.0+ if (defined('E_DEPRECATED')) { $this->_errorHandlerMap['E_DEPRECATED'] = Zend_Log::DEBUG; } if (defined('E_USER_DEPRECATED')) { $this->_errorHandlerMap['E_USER_DEPRECATED'] = Zend_Log::DEBUG; } $this->_registeredErrorHandler = true; return $this; } /** * Error Handler will convert error into log message, and then call the original error handler * * @link http://www.php.net/manual/en/function.set-error-handler.php Custom error handler * @param int $errno * @param string $errstr * @param string $errfile * @param int $errline * @param array $errcontext * @return boolean */ public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) { $errorLevel = error_reporting(); if ($errorLevel && $errno) { if (isset($this->_errorHandlerMap[$errno])) { $priority = $this->_errorHandlerMap[$errno]; } else { $priority = Zend_Log::INFO; } $this->log($errstr, $priority, array('errno'=>$errno, 'file'=>$errfile, 'line'=>$errline, 'context'=>$errcontext)); } if ($this->_origErrorHandler !== null) { return call_user_func($this->_origErrorHandler, $errno, $errstr, $errfile, $errline, $errcontext); } return false; } /** * Set timestamp format for log entries. * * @param string $format * @return Zend_Log */ public function setTimestampFormat($format) { $this->_timestampFormat = $format; return $this; } /** * Get timestamp format used for log entries. * * @return string */ public function getTimestampFormat() { return $this->_timestampFormat; } } tests/Guzzle/Tests/Common/Log/Zf1LogAdapterTest.php000064400000003452151742734600016232 0ustar00stream = fopen('php://temp', 'r+'); $this->log = new \Zend_Log(new \Zend_Log_Writer_Stream($this->stream)); $this->adapter = new Zf1LogAdapter($this->log); } /** * @covers Guzzle\Common\Log\Zf1LogAdapter::log */ public function testLogsMessagesToAdaptedObject() { // Test without a priority $this->adapter->log('test', \LOG_NOTICE, 'guzzle.common.log.adapter.zend_log_adapter', 'localhost'); rewind($this->stream); $this->assertEquals(1, substr_count(stream_get_contents($this->stream), 'test')); // Test with a priority $this->adapter->log('test', \LOG_ALERT); rewind($this->stream); $this->assertEquals(2, substr_count(stream_get_contents($this->stream), 'test')); } /** * @covers Guzzle\Common\Log\AbstractLogAdapter::getLogObject */ public function testExposesAdaptedLogObject() { $this->assertEquals($this->log, $this->adapter->getLogObject()); } } tests/Guzzle/Tests/Common/Log/Zf2LogAdapterTest.php000064400000003135151742734600016231 0ustar00stream = fopen('php://temp', 'r+'); $this->log = new Logger(new Stream($this->stream)); $this->adapter = new Zf2LogAdapter($this->log); } /** * @covers Guzzle\Common\Log\Zf2LogAdapter::log */ public function testLogsMessagesToAdaptedObject() { // Test without a priority $this->adapter->log('Zend_Test!', \LOG_NOTICE); rewind($this->stream); $contents = stream_get_contents($this->stream); $this->assertEquals(1, substr_count($contents, 'Zend_Test!')); // Test with a priority $this->adapter->log('Zend_Test!', \LOG_ALERT); rewind($this->stream); $contents = stream_get_contents($this->stream); $this->assertEquals(2, substr_count($contents, 'Zend_Test!')); } /** * @covers Guzzle\Common\Log\AbstractLogAdapter::getLogObject */ public function testExposesAdaptedLogObject() { $this->assertEquals($this->log, $this->adapter->getLogObject()); } } tests/Guzzle/Tests/Common/StreamTest.php000064400000012256151742734600014343 0ustar00assertEquals($handle, $stream->getStream()); $this->assertTrue($stream->isReadable()); $this->assertTrue($stream->isWritable()); $this->assertTrue($stream->isLocal()); $this->assertTrue($stream->isSeekable()); $this->assertEquals('php', $stream->getWrapper()); $this->assertEquals('temp', $stream->getStreamType()); $this->assertEquals(4, $stream->getSize()); $this->assertEquals('php://temp', $stream->getUri()); $this->assertEquals(array(), $stream->getWrapperData()); $this->assertFalse($stream->isConsumed()); unset($stream); } /** * @covers Guzzle\Common\Stream::__destruct */ public function testStreamClosesHandleOnDestruct() { $handle = fopen('php://temp', 'r'); $stream = new Stream($handle); unset($stream); $this->assertFalse(is_resource($handle)); } /** * @covers Guzzle\Common\Stream::__toString */ public function testConvertsToString() { $handle = fopen('php://temp', 'w+'); fwrite($handle, 'data'); $stream = new Stream($handle); $this->assertEquals('data', (string)$stream); unset($stream); $handle = fopen(__DIR__ . '/../TestData/FileBody.txt', 'w'); $stream = new Stream($handle); $this->assertEquals('', (string)$stream); unset($stream); } /** * @covers Guzzle\Common\Stream::isConsumed */ public function testIsConsumed() { $handle = fopen('php://temp', 'w+'); fwrite($handle, 'data'); $stream = new Stream($handle); $this->assertFalse($stream->isConsumed()); $stream->read(4); $this->assertTrue($stream->isConsumed()); } /** * @covers Guzzle\Common\Stream::setSize * @covers Guzzle\Common\Stream::getSize */ public function testAllowsSettingManualSize() { $handle = fopen('php://temp', 'w+'); fwrite($handle, 'data'); $stream = new Stream($handle); $stream->setSize(10); $this->assertEquals(10, $stream->getSize()); unset($stream); } /** * @covers Guzzle\Common\Stream::read * @covers Guzzle\Common\Stream::write * @covers Guzzle\Common\Stream::seek * @covers Guzzle\Common\Stream::isReadable * @covers Guzzle\Common\Stream::isSeekable */ public function testWrapsStream() { $handle = fopen('php://temp', 'w+'); fwrite($handle, 'data'); $stream = new Stream($handle); $this->assertTrue($stream->isSeekable()); $this->assertTrue($stream->isReadable()); $this->assertTrue($stream->seek(0)); $this->assertEquals('da', $stream->read(2)); $this->assertEquals('ta', $stream->read(2)); $this->assertTrue($stream->seek(0)); $this->assertEquals('data', $stream->read(4)); $stream->write('_appended'); $stream->seek(0); $this->assertEquals('data_appended', $stream->read(13)); } /** * @covers Guzzle\Common\Stream::getSize * @covers Guzzle\Common\Stream::__construct */ public function testGetSize() { $size = filesize(__DIR__ . '/../../../bootstrap.php'); $handle = fopen(__DIR__ . '/../../../bootstrap.php', 'r'); $stream = new Stream($handle); $this->assertEquals($handle, $stream->getStream()); $this->assertEquals($size, $stream->getSize()); $this->assertEquals($size, $stream->getSize()); unset($stream); // Make sure that false is returned when the size cannot be determined $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Lenght: 0\r\n\r\n"); $handle = fopen('http://localhost:' . $this->getServer()->getPort(), 'r'); $stream = new Stream($handle); $this->assertEquals(false, $stream->getSize()); unset($stream); } /** * @covers Guzzle\Common\Stream::getMetaData */ public function testAbstractsMetaData() { $handle = fopen(__DIR__ . '/../../../bootstrap.php', 'r'); $stream = new Stream($handle); $this->assertEquals('plainfile', $stream->getMetaData('wrapper_type')); $this->assertEquals(null, $stream->getMetaData('wrapper_data')); $this->assertInternalType('array', $stream->getMetaData()); } /** * @covers Guzzle\Common\Stream::write */ public function testDoesNotAttempToWriteToReadonlyStream() { $handle = fopen(__DIR__ . '/../../../bootstrap.php', 'r'); $stream = new Stream($handle); $this->assertEquals(0, $stream->write('foo')); } } tests/Guzzle/Tests/Common/Validation/BlankTest.php000064400000000743151742734600016227 0ustar00 array('foo', 'bar')), true, null), array($c, 'baz', array('options' => array('foo', 'bar')), 'Value must be one of: foo, bar', null) ); } } tests/Guzzle/Tests/Common/Validation/CtypeTest.php000064400000001362151742734600016262 0ustar00 'alpha'), true, null), array($c, 'a', array('alpha'), true, null), array($c, '2', array('type' => 'alpha'), 'Value must be of type alpha', null), array($c, ' ', array('type' => 'space'), true, null), array($c, 'a', array('type' => 'foo'), null, 'Guzzle\Common\Exception\InvalidArgumentException') ); } } tests/Guzzle/Tests/Common/Validation/EmailTest.php000064400000000615151742734600016225 0ustar00 'stdClass'), true, null), array($c, new \DateTime(), array('class' => 'stdClass'), 'Value must be an instance of stdClass', null), array($c, 'a', null, true, 'Guzzle\Common\Exception\InvalidArgumentException'), array($c, new \stdClass(), null, true, 'Guzzle\Common\Exception\InvalidArgumentException') ); } } tests/Guzzle/Tests/Common/Validation/NotBlankTest.php000064400000000704151742734600016705 0ustar00 '/[a-z]+/'), true, null), array($c, 'foo', array('/[a-z]+/'), true, null), array($c, 'foo', array('pattern' => '/[0-9]+/'), 'foo does not match the regular expression', null), array($c, 'baz', null, null, 'Guzzle\Common\Exception\InvalidArgumentException') ); } } tests/Guzzle/Tests/Common/Validation/StringTest.php000064400000000712151742734600016442 0ustar00 'string'), true, null), array($c, 'a', array('string'), true, null), array($c, '2', array('type' => 'array'), 'Value must be of type array', null) ); } } tests/Guzzle/Tests/Common/Validation/UrlTest.php000064400000000743151742734600015742 0ustar00validate($value, $options); $this->assertEquals($result, $r); } catch (\Exception $e) { if (!$exception) { throw $e; } if (!($e instanceof $exception)) { throw $e; } } } } tests/Guzzle/Tests/GuzzleTestCase.php000064400000014612151742734600013732 0ustar00isRunning()) { self::$server->flush(); } else { self::$server->start(); } } catch (\Exception $e) { fwrite(STDERR, $e->getMessage()); } } return self::$server; } /** * Set the service builder to use for tests * * @param ServiceBuilder $builder Service builder */ public static function setServiceBuilder(ServiceBuilderInterface $builder) { self::$serviceBuilder = $builder; } /** * Get a service builder object that can be used throughout the service tests * * @return ServiceBuilder */ public function getServiceBuilder() { if (!self::$serviceBuilder) { throw new RuntimeException('No service builder has been set via setServiceBuilder()'); } return self::$serviceBuilder; } /** * Check if an event dispatcher has a subscriber * * @param HasDispatcherInterface $dispatcher * @param EventSubscriberInterface $subscriber * * @return bool */ protected function hasSubscriber(HasDispatcherInterface $dispatcher, EventSubscriberInterface $subscriber) { $class = get_class($subscriber); $all = array_keys(call_user_func(array($class, 'getSubscribedEvents'))); foreach ($all as $i => $event) { foreach ($dispatcher->getEventDispatcher()->getListeners($event) as $e) { if ($e[0] === $subscriber) { unset($all[$i]); break; } } } return count($all) == 0; } /** * Get a wildcard observer for an event dispatcher * * @param HasEventDispatcherInterface $hasEvent * * @return MockObserver */ public function getWildcardObserver(HasDispatcherInterface $hasDispatcher) { $class = get_class($hasDispatcher); $o = new MockObserver(); $events = call_user_func(array($class, 'getAllEvents')); foreach ($events as $event) { $hasDispatcher->getEventDispatcher()->addListener($event, array($o, 'update')); } return $o; } /** * Set the mock response base path * * @param string $path Path to mock response folder * * @return GuzzleTestCase */ public static function setMockBasePath($path) { self::$mockBasePath = $path; } /** * Mark a request as being mocked * * @param RequestInterface $request */ public function addMockedRequest(RequestInterface $request) { $this->requests[] = $request; return $this; } /** * Get all of the mocked requests * * @return array */ public function getMockedRequests() { return $this->requests; } /** * Get a mock response for a client by mock file name * * @param string $path Relative path to the mock response file * * @return Response */ public function getMockResponse($path) { return MockPlugin::getMockFile(self::$mockBasePath . DIRECTORY_SEPARATOR . $path); } /** * Set a mock response from a mock file on the next client request. * * This method assumes that mock response files are located under the * Command/Mock/ directory of the Service being tested * (e.g. Unfuddle/Command/Mock/). A mock response is added to the next * request sent by the client. * * @param Client $client Client object to modify * @param string $paths Path to files within the Mock folder of the service * * @return MockPlugin returns the created mock plugin */ public function setMockResponse(Client $client, $paths) { $this->requests = array(); $that = $this; $mock = new MockPlugin(null, true); $mock->getEventDispatcher()->addListener('mock.request', function(Event $event) use ($that) { $that->addMockedRequest($event['request']); }); foreach ((array) $paths as $path) { $mock->addResponse($this->getMockResponse($path)); } $client->getEventDispatcher()->addSubscriber($mock); return $mock; } /** * Compare HTTP headers and use special markup to filter values * A header prefixed with '!' means it must not exist * A header prefixed with '_' means it must be ignored * A header value of '*' means anything after the * will be ignored * * @param array $filteredHeaders Array of special headers * @param array $actualHeaders Array of headers to check against * * @return array|false Returns an array of the differences or FALSE if none */ public function compareHeaders($filteredHeaders, $actualHeaders) { $comparison = new HeaderComparison(); return $comparison->compare($filteredHeaders, $actualHeaders); } /** * Case insensitive assertContains * * @param string $needle Search string * @param string $haystack Search this * @param string $message Optional failure message */ public function assertContainsIns($needle, $haystack, $message = null) { $this->assertContains(strtolower($needle), strtolower($haystack), $message); } } tests/Guzzle/Tests/Http/ClientTest.php000064400000050163151742734600014014 0ustar00assertEquals('http://www.google.com/', $client->getBaseUrl()); $this->assertSame($client, $client->setConfig(array( 'test' => '123' ))); $this->assertEquals(array('test' => '123'), $client->getConfig()->getAll()); $this->assertEquals('123', $client->getConfig('test')); $this->assertSame($client, $client->setBaseUrl('http://www.test.com/{{test}}')); $this->assertEquals('http://www.test.com/123', $client->getBaseUrl()); $this->assertEquals('http://www.test.com/{{test}}', $client->getBaseUrl(false)); try { $client->setConfig(false); } catch (\InvalidArgumentException $e) { } } /** * @covers Guzzle\Http\Client::getAllEvents */ public function testDescribesEvents() { $this->assertEquals(array('client.create_request'), Client::getAllEvents()); } /** * @covers Guzzle\Http\Client::__toString */ public function testConvertsToString() { $client = new Client(); $hash = spl_object_hash($client); $this->assertEquals($hash, (string) $client); } /** * @covers Guzzle\Http\Client::__construct */ public function testConstructorCanAcceptConfig() { $client = new Client('http://www.test.com/', array( 'data' => '123' )); $this->assertEquals('123', $client->getConfig('data')); } /** * @covers Guzzle\Http\Client::setConfig */ public function testCanUseCollectionAsConfig() { $client = new Client('http://www.google.com/'); $client->setConfig(new Collection(array( 'api' => 'v1', 'key' => 'value', 'base_url' => 'http://www.google.com/' ))); $this->assertEquals('v1', $client->getConfig('api')); } /** * @covers Guzzle\Http\Client */ public function testExpandsUriTemplatesUsingConfig() { $client = new Client('http://www.google.com/'); $client->setConfig(array( 'api' => 'v1', 'key' => 'value', 'foo' => 'bar' )); $this->assertEquals('Testing...api/v1/key/value', $client->expandTemplate('Testing...api/{api}/key/{{key}}')); // Make sure that the client properly validates and injects config $this->assertEquals('bar', $client->getConfig('foo')); } /** * @covers Guzzle\Http\Client::__construct * @covers Guzzle\Http\Client::createRequest */ public function testClientAttachersObserversToRequests() { $this->getServer()->flush(); $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); $client = new Client($this->getServer()->getUrl()); $logPlugin = $this->getLogPlugin(); $client->getEventDispatcher()->addSubscriber($logPlugin); // Get a request from the client and ensure the the observer was // attached to the new request $request = $client->createRequest(); $this->assertTrue($this->hasSubscriber($request, $logPlugin)); } /** * @covers Guzzle\Http\Client::getBaseUrl * @covers Guzzle\Http\Client::setBaseUrl */ public function testClientReturnsValidBaseUrls() { $client = new Client('http://www.{{foo}}.{{data}}/', array( 'data' => '123', 'foo' => 'bar' )); $this->assertEquals('http://www.bar.123/', $client->getBaseUrl()); $client->setBaseUrl('http://www.google.com/'); $this->assertEquals('http://www.google.com/', $client->getBaseUrl()); } /** * @covers Guzzle\Http\Client::setUserAgent * @covers Guzzle\Http\Client::createRequest * @covers Guzzle\Http\Client::prepareRequest */ public function testSetsUserAgent() { $client = new Client('http://www.test.com/', array( 'api' => 'v1' )); $this->assertSame($client, $client->setUserAgent('Test/1.0Ab', true)); $this->assertEquals('Test/1.0Ab ' . Utils::getDefaultUserAgent(), $client->get()->getHeader('User-Agent')); $client->setUserAgent('Test/1.0Ab'); $this->assertEquals('Test/1.0Ab', $client->get()->getHeader('User-Agent')); } /** * @covers Guzzle\Http\Client::createRequest * @covers Guzzle\Http\Client::prepareRequest */ public function testClientAddsCurlOptionsToRequests() { $client = new Client('http://www.test.com/', array( 'api' => 'v1', // Adds the option using the curl values 'curl.CURLOPT_HTTPAUTH' => 'CURLAUTH_DIGEST', 'curl.abc' => 'not added', 'curl.blacklist' => 'abc' )); $request = $client->createRequest(); $options = $request->getCurlOptions(); $this->assertEquals(CURLAUTH_DIGEST, $options->get(CURLOPT_HTTPAUTH)); $this->assertNull($options->get('curl.abc')); $this->assertNull($options->get('curl.blacklist')); } /** * @covers Guzzle\Http\Client::createRequest * @covers Guzzle\Http\Client::prepareRequest */ public function testClientAddsCustomCurlOptionsToRequests() { $client = new Client('http://www.test.com/', array( 'api' => 'v1', 'curl.disable_wire' => true, 'curl.foo' => 'bar' )); $request = $client->createRequest(); $this->assertTrue($request->getCurlOptions()->get('disable_wire')); $this->assertEquals('bar', $request->getCurlOptions()->get('foo')); } /** * @covers Guzzle\Http\Client::prepareRequest */ public function testClientAddsParamsToRequests() { $client = new Client('http://www.example.com', array( 'api' => 'v1', 'params.foo' => 'bar', 'params.baz' => 'jar', )); $request = $client->createRequest(); $this->assertEquals('bar', $request->getParams()->get('foo')); $this->assertEquals('jar', $request->getParams()->get('baz')); } public function urlProvider() { $u = $this->getServer()->getUrl() . 'base/'; $u2 = $this->getServer()->getUrl() . 'base?z=1'; return array( array($u, '', $u), array($u, 'relative/path/to/resource', $u . 'relative/path/to/resource'), array($u, 'relative/path/to/resource?a=b&c=d', $u . 'relative/path/to/resource?a=b&c=d'), array($u, '/absolute/path/to/resource', $this->getServer()->getUrl() . 'absolute/path/to/resource'), array($u, '/absolute/path/to/resource?a=b&c=d', $this->getServer()->getUrl() . 'absolute/path/to/resource?a=b&c=d'), array($u2, '/absolute/path/to/resource?a=b&c=d', $this->getServer()->getUrl() . 'absolute/path/to/resource?a=b&c=d'), array($u2, 'relative/path/to/resource', $this->getServer()->getUrl() . 'base/relative/path/to/resource?z=1'), array($u2, 'relative/path/to/resource?another=query', $this->getServer()->getUrl() . 'base/relative/path/to/resource?z=1&another=query') ); } /** * @dataProvider urlProvider * @covers Guzzle\Http\Client::createRequest */ public function testBuildsRelativeUrls($baseUrl, $url, $result) { $client = new Client($baseUrl); $this->assertEquals($client->get($url)->getUrl(), $result); } /** * @covers Guzzle\Http\Client */ public function testAllowsConfigsToBeChangedAndInjectedInBaseUrl() { $client = new Client('http://{{a}}/{{b}}'); $this->assertEquals('http:///', $client->getBaseUrl()); $this->assertEquals('http://{{a}}/{{b}}', $client->getBaseUrl(false)); $client->setConfig(array( 'a' => 'test.com', 'b' => 'index.html' )); $this->assertEquals('http://test.com/index.html', $client->getBaseUrl()); } /** * @covers Guzzle\Http\Client::createRequest */ public function testCreatesRequestsWithDefaultValues() { $client = new Client($this->getServer()->getUrl() . 'base'); // Create a GET request $request = $client->createRequest(); $this->assertEquals('GET', $request->getMethod()); $this->assertEquals($client->getBaseUrl(), $request->getUrl()); // Create a DELETE request $request = $client->createRequest('DELETE'); $this->assertEquals('DELETE', $request->getMethod()); $this->assertEquals($client->getBaseUrl(), $request->getUrl()); // Create a HEAD request with custom headers $request = $client->createRequest('HEAD', 'http://www.test.com/'); $this->assertEquals('HEAD', $request->getMethod()); $this->assertEquals('http://www.test.com/', $request->getUrl()); // Create a PUT request $request = $client->createRequest('PUT'); $this->assertEquals('PUT', $request->getMethod()); // Create a PUT request with injected config $client->getConfig()->set('a', 1)->set('b', 2); $request = $client->createRequest('PUT', '/path/{{a}}?q={{b}}'); $this->assertEquals($request->getUrl(), $this->getServer()->getUrl() . 'path/1?q=2'); } /** * @covers Guzzle\Http\Client::get * @covers Guzzle\Http\Client::delete * @covers Guzzle\Http\Client::head * @covers Guzzle\Http\Client::put * @covers Guzzle\Http\Client::post * @covers Guzzle\Http\Client::options * @covers Guzzle\Http\Client::patch */ public function testClientHasHelperMethodsForCreatingRequests() { $url = $this->getServer()->getUrl(); $client = new Client($url . 'base'); $this->assertEquals('GET', $client->get()->getMethod()); $this->assertEquals('PUT', $client->put()->getMethod()); $this->assertEquals('POST', $client->post()->getMethod()); $this->assertEquals('HEAD', $client->head()->getMethod()); $this->assertEquals('DELETE', $client->delete()->getMethod()); $this->assertEquals('OPTIONS', $client->options()->getMethod()); $this->assertEquals('PATCH', $client->patch()->getMethod()); $this->assertEquals($url . 'base/abc', $client->get('abc')->getUrl()); $this->assertEquals($url . 'zxy', $client->put('/zxy')->getUrl()); $this->assertEquals($url . 'zxy?a=b', $client->post('/zxy?a=b')->getUrl()); $this->assertEquals($url . 'base?a=b', $client->head('?a=b')->getUrl()); $this->assertEquals($url . 'base?a=b', $client->delete('/base?a=b')->getUrl()); } /** * @covers Guzzle\Http\Client::createRequest */ public function testClientInjectsConfigsIntoUrls() { $client = new Client('http://www.test.com/api/v1', array( 'test' => '123' )); $request = $client->get('relative/{{test}}'); $this->assertEquals('http://www.test.com/api/v1/relative/123', $request->getUrl()); } /** * @covers Guzzle\Http\Client */ public function testAllowsEmptyBaseUrl() { $client = new Client(); $request = $client->get('http://www.google.com/'); $this->assertEquals('http://www.google.com/', $request->getUrl()); $request->setResponse(new Response(200), true); $request->send(); } /** * @covers Guzzle\Http\Client::send * @covers Guzzle\Http\Client::setCurlMulti * @covers Guzzle\Http\Client::getCurlMulti */ public function testAllowsCustomCurlMultiObjects() { $mock = $this->getMock('Guzzle\\Http\\Curl\\CurlMulti', array('add', 'send')); $mock->expects($this->once()) ->method('add'); $mock->expects($this->once()) ->method('send'); $client = new Client(); $client->setCurlMulti($mock); $request = $client->get(); $request->setResponse(new Response(200), true); $client->send($request); } /** * @covers Guzzle\Http\Client::send */ public function testClientSendsMultipleRequests() { $client = new Client($this->getServer()->getUrl()); $mock = new MockPlugin(); $responses = array( new Response(200), new Response(201), new Response(202) ); $mock->addResponse($responses[0]); $mock->addResponse($responses[1]); $mock->addResponse($responses[2]); $client->getEventDispatcher()->addSubscriber($mock); $requests = array( $client->get(), $client->head(), $client->put('/', null, 'test') ); $this->assertEquals(array( $responses[0], $responses[1], $responses[2] ), $client->send($requests)); } /** * @covers Guzzle\Http\Client::send */ public function testClientSendsSingleRequest() { $client = new Client($this->getServer()->getUrl()); $mock = new MockPlugin(); $response = new Response(200); $mock->addResponse($response); $client->getEventDispatcher()->addSubscriber($mock); $this->assertEquals($response, $client->send($client->get())); } /** * @covers Guzzle\Http\Client::send * @expectedException Guzzle\Http\Exception\BadResponseException */ public function testClientThrowsExceptionForSingleRequest() { $client = new Client($this->getServer()->getUrl()); $mock = new MockPlugin(); $response = new Response(404); $mock->addResponse($response); $client->getEventDispatcher()->addSubscriber($mock); $client->send($client->get()); } /** * @covers Guzzle\Http\Client::send * @expectedException Guzzle\Common\Exception\ExceptionCollection */ public function testClientThrowsExceptionForMultipleRequests() { $client = new Client($this->getServer()->getUrl()); $mock = new MockPlugin(); $mock->addResponse(new Response(200)); $mock->addResponse(new Response(404)); $client->getEventDispatcher()->addSubscriber($mock); $client->send(array($client->get(), $client->head())); } /** * @covers Guzzle\Http\Client */ public function testQueryStringsAreNotDoubleEncoded() { $client = new Client('http://test.com', array( 'path' => array('foo', 'bar'), 'query' => 'hi there', 'data' => array( 'test' => 'a&b' ) )); $request = $client->get('{/path*}{?query,data*}'); $this->assertEquals('http://test.com/foo/bar?query=hi%20there&test=a%26b', $request->getUrl()); $this->assertEquals('hi there', $request->getQuery()->get('query')); $this->assertEquals('a&b', $request->getQuery()->get('test')); } /** * @covers Guzzle\Http\Client */ public function testQueryStringsAreNotDoubleEncodedUsingAbsolutePaths() { $client = new Client('http://test.com', array( 'path' => array('foo', 'bar'), 'query' => 'hi there', )); $request = $client->get('http://test.com{?query}'); $this->assertEquals('http://test.com/?query=hi%20there', $request->getUrl()); $this->assertEquals('hi there', $request->getQuery()->get('query')); } /** * @covers Guzzle\Http\Client::setUriTemplate * @covers Guzzle\Http\Client::getUriTemplate */ public function testAllowsUriTemplateInjection() { $client = new Client('http://test.com', array( 'path' => array('foo', 'bar'), 'query' => 'hi there', )); $a = $client->getUriTemplate(); $this->assertSame($a, $client->getUriTemplate()); $client->setUriTemplate(new UriTemplate()); $this->assertNotSame($a, $client->getUriTemplate()); } /** * @covers Guzzle\Http\Client::expandTemplate */ public function testAllowsCustomVariablesWhenExpandingTemplates() { $client = new Client('http://test.com', array( 'test' => 'hi', )); $uri = $client->expandTemplate('http://{test}{?query*}', array( 'query' => array( 'han' => 'solo' ) )); $this->assertEquals('http://hi?han=solo', $uri); } /** * @covers Guzzle\Http\Client::createRequest * @expectedException InvalidArgumentException */ public function testUriArrayMustContainExactlyTwoElements() { $client = new Client(); $client->createRequest('GET', array('haha!')); } /** * @covers Guzzle\Http\Client::createRequest * @expectedException InvalidArgumentException */ public function testUriArrayMustContainAnArray() { $client = new Client(); $client->createRequest('GET', array('haha!', 'test')); } /** * @covers Guzzle\Http\Client::createRequest * @covers Guzzle\Http\Client::get * @covers Guzzle\Http\Client::put * @covers Guzzle\Http\Client::post * @covers Guzzle\Http\Client::head * @covers Guzzle\Http\Client::options */ public function testUriArrayAllowsCustomTemplateVariables() { $client = new Client(); $vars = array( 'var' => 'hi' ); $this->assertEquals('/hi', (string) $client->createRequest('GET', array('/{var}', $vars))->getUrl()); $this->assertEquals('/hi', (string) $client->get(array('/{var}', $vars))->getUrl()); $this->assertEquals('/hi', (string) $client->put(array('/{var}', $vars))->getUrl()); $this->assertEquals('/hi', (string) $client->post(array('/{var}', $vars))->getUrl()); $this->assertEquals('/hi', (string) $client->head(array('/{var}', $vars))->getUrl()); $this->assertEquals('/hi', (string) $client->options(array('/{var}', $vars))->getUrl()); } /** * @covers Guzzle\Http\Client::setDefaultHeaders * @covers Guzzle\Http\Client::getDefaultHeaders * @covers Guzzle\Http\Client::createRequest */ public function testAllowsDefaultHeaders() { $default = array( 'X-Test' => 'Hi!' ); $other = array( 'X-Other' => 'Foo' ); $client = new Client(); $client->setDefaultHeaders($default); $this->assertEquals($default, $client->getDefaultHeaders()->getAll()); $client->setDefaultHeaders(new Collection($default)); $this->assertEquals($default, $client->getDefaultHeaders()->getAll()); $request = $client->createRequest('GET', null, $other); $this->assertEquals('Hi!', $request->getHeader('X-Test')); $this->assertEquals('Foo', $request->getHeader('X-Other')); $request = $client->createRequest('GET', null, new Collection($other)); $this->assertEquals('Hi!', $request->getHeader('X-Test')); $this->assertEquals('Foo', $request->getHeader('X-Other')); $request = $client->createRequest('GET'); $this->assertEquals('Hi!', $request->getHeader('X-Test')); } /** * @covers Guzzle\Http\Client::setDefaultHeaders * @expectedException InvalidArgumentException */ public function testValidatesDefaultHeaders() { $client = new Client(); $client->setDefaultHeaders('foo'); } } tests/Guzzle/Tests/Http/CookieJar/ArrayCookieJarTest.php000064400000030154151742734600017307 0ustar00jar = new ArrayCookieJar(); } /** * Add values to the cookiejar */ public static function addCookies(CookieJarInterface $jar) { $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('baz', 'foobar'), '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 )); } /** * Provides test data for cookie jar retrieval */ public function getCookiesDataProvider() { return array( array(array('foo', 'baz', 'test', 'muppet', 'googoo'), '', '', '', false), array(array('foo', 'baz', 'muppet', 'googoo'), '', '', '', true), array(array('googoo'), 'www.example.com', '', '', false), array(array('muppet', 'googoo'), 'test.y.example.com', '', '', false), array(array('foo', 'baz'), 'example.com', '', '', false), array(array('muppet'), 'x.y.example.com', '/acme/', '', false), array(array('muppet'), 'x.y.example.com', '/acme/test/', '', false), array(array('googoo'), 'x.y.example.com', '/test/acme/test/', '', false), array(array('foo', 'baz'), 'example.com', '', '', false), array(array('baz'), 'example.com', '', 'baz', false), ); } /** * @covers Guzzle\Http\CookieJar\ArrayCookieJar */ public function testStoresAndRetrievesCookies() { $j = $this->jar; $this->assertSame($j, $j->save(array( 'cookie' => array('foo', 'bar'), 'domain' => '.example.com', 'path' => '/', 'max_age' => '86400', 'port' => array(80, 8080), 'version' => '1', 'secure' => true ))); $this->assertSame($j, $j->save(array( 'cookie' => array('baz', 'foobar'), 'domain' => '.example.com', 'path' => '/', 'max_age' => '86400', 'port' => array(80, 8080), 'version' => '1', 'secure' => true ))); $this->assertSame($j, $j->save(array( 'cookie' => array('test', '123'), 'domain' => 'www.foobar.com', 'path' => '/path/' ))); $this->assertEquals(array( array ( 'cookie' => array('foo', 'bar'), 'domain' => '.example.com', 'path' => '/', 'max_age' => '86400', 'port' => array(80, 8080), 'version' => '1', 'secure' => true, 'expires' => time() + 86400, 'comment' => NULL, 'comment_url' => NULL, 'discard' => NULL, 'http_only' => false ), array ( 'cookie' => array('baz', 'foobar'), 'domain' => '.example.com', 'path' => '/', 'max_age' => '86400', 'port' => array( 0 => 80, 1 => 8080, ), 'version' => '1', 'secure' => true, 'expires' => time() + 86400, 'comment' => NULL, 'comment_url' => NULL, 'discard' => NULL, 'http_only' => false ), array ( 'cookie' => array('test' , '123'), 'domain' => 'www.foobar.com', 'path' => '/path/', 'max_age' => 0, 'comment' => NULL, 'comment_url' => NULL, 'port' => array (), 'secure' => NULL, 'expires' => null, 'discard' => NULL, 'version' => null, 'http_only' => false ), ), $j->getCookies()); } /** * Checks if cookies (by name) are present in a cookieJar * * @param array $cookies Cookies from the cookie jar * @param array $names Names to check for * * @return true */ protected function hasCookies(array $cookies, array $names) { $remaining = $names; $extra = array(); foreach ($cookies as $cookie) { $match = false; foreach ($remaining as $ri => $c) { if ($cookie['cookie'][0] == $c) { unset($remaining[$ri]); $match = true; } } if (!$match) { $extra[] = $cookie['cookie'][0] . '=' . $cookie['cookie'][1]; } } $this->assertTrue(count($remaining) == 0 && count($extra) == 0, 'Unmatched: ' . implode(', ', $remaining) . ' | Extra: ' . implode(', ', $extra)); } /** * @covers Guzzle\Http\CookieJar\ArrayCookieJar * @dataProvider getCookiesDataProvider */ public function testGetCookies(array $matches, $domain = null, $path = null, $name = null, $skipDiscardable = false) { self::addCookies($this->jar); $cookies = $this->jar->getCookies($domain, $path, $name, $skipDiscardable); $this->hasCookies($cookies, $matches); } /** * @covers Guzzle\Http\CookieJar\ArrayCookieJar */ public function testClearsTemporaryCookies() { self::addCookies($this->jar); $this->assertEquals(1, $this->jar->clearTemporary()); $this->hasCookies($this->jar->getCookies(), array('foo', 'baz', 'muppet', 'googoo')); // Doesn't clear anything out because nothing is temporary $this->assertEquals(0, $this->jar->clearTemporary()); // Add an expired cookie $this->jar->save(array( 'cookie' => array('data', 'abc'), 'domain' => '.example.com' )); // Filters out expired cookies $this->hasCookies($this->jar->getCookies(), array('foo', 'baz', 'muppet', 'googoo', 'data')); // Removes the expired cookie $this->assertEquals(1, $this->jar->clearTemporary()); $this->hasCookies($this->jar->getCookies(), array('foo', 'baz', 'muppet', 'googoo')); } /** * @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()); } /** * @covers Guzzle\Http\CookieJar\ArrayCookieJar * @expectedException InvalidArgumentException * @expectedExceptionMessage Cookies require a domain */ public function testValidatesDomainWhenSaving() { $this->jar->save(array()); } /** * @covers Guzzle\Http\CookieJar\ArrayCookieJar * @expectedException InvalidArgumentException * @expectedExceptionMessage Cookies require a names and values */ public function testValidatesCookiesArePresentWhenSaving() { $this->jar->save(array( 'domain' => '.test.com' )); } /** * Provides test data for cookie jar clearing */ public function clearCookiesDataProvider() { return array( array(array(), 4, '', '', '', ''), array(array('test', 'muppet', 'googoo'), 1, 'example.com', '', ''), array(array('foo', 'baz', 'test'), 2, 'a.y.example.com', '', ''), array(array('foo', 'baz', 'test', 'googoo'), 1, 'a.y.example.com', '/acme/', ''), // Removes only baz from the cookie that contains two values array(array('test', 'muppet', 'googoo', 'foo'), 1, 'example.com', '/', 'baz'), array(array('foo', 'baz', 'test', 'muppet'), 1, 'www.example.com', '/test/acme/', '') ); } /** * @covers Guzzle\Http\CookieJar\ArrayCookieJar * @dataProvider clearCookiesDataProvider */ public function testClearsCookies($matches, $total, $domain, $path, $name) { self::addCookies($this->jar); $this->jar->clear($domain, $path, $name); $this->hasCookies($this->jar->getCookies(null, null, null, null, false, true), $matches); } /** * @covers Guzzle\Http\CookieJar\ArrayCookieJar */ public function testDoesNotAddDuplicates() { $this->jar->clear(); $this->assertEquals(0, count($this->jar->getCookies())); $data = array( 'cookie' => array('foo', 'bar'), 'domain' => '.example.com', 'path' => '/', 'max_age' => '86400', 'port' => array(80, 8080), 'version' => '1', 'secure' => true ); $this->jar->save($data); $this->jar->save($data); $this->assertEquals(1, count($this->jar->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']); } /** * @covers Guzzle\Http\CookieJar\ArrayCookieJar::serialize * @covers Guzzle\Http\CookieJar\ArrayCookieJar::unserialize */ public function testSerialization() { $j = $this->jar; static::addCookies($j); $c = count($j->getCookies()); $deflate = serialize($j); $inflate = unserialize($deflate); $this->assertEquals($c, count($inflate->getCookies())); } } tests/Guzzle/Tests/Http/CookieJar/FileCookieJarTest.php000064400000004716151742734600017115 0ustar00file = tempnam('/tmp', 'file-cookies'); $this->jar = new FileCookieJar($this->file); } /** * 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 )); } /** * @covers Guzzle\Http\CookieJar\FileCookieJar */ public function testLoadsFromFileFile() { unset($this->jar); $this->jar = new FileCookieJar($this->file); $this->assertEquals(array(), $this->jar->getCookies()); unlink($this->file); } /** * @covers Guzzle\Http\CookieJar\FileCookieJar */ public function testPersistsToFileFile() { unset($this->jar); $this->jar = new FileCookieJar($this->file); $this->addCookies(); $this->assertEquals(4, count($this->jar->getCookies())); unset($this->jar); // Make sure it wrote to the file $contents = file_get_contents($this->file); $this->assertNotEmpty($contents); // Load the jar from the file $jar = new FileCookieJar($this->file); // Weeds out temporary and session cookies $this->assertEquals(3, count($jar->getCookies())); unset($jar); unlink($this->file); } } tests/Guzzle/Tests/Http/CookieTest.php000064400000003727151742734600014013 0ustar00 'value' )), array('name=value;name2=value 2', array( 'name' => 'value', 'name2' => 'value 2' )), array('name=value;name2=x=y&a=b', array( 'name' => 'value', 'name2' => 'x=y&a=b' )), ); } /** * @covers Guzzle\Http\Cookie::factory * @dataProvider provider */ public function testFactoryBuildsCookiesFromCookieStrings($cookieString, array $data) { $jar = Cookie::factory($cookieString); $this->assertEquals($data, $jar->getAll()); } /** * @covers Guzzle\Http\Cookie::__construct */ public function testConstructorSetsDefaults() { $jar = new Cookie(); $this->assertEquals(';', $jar->getFieldSeparator()); $this->assertEquals('=', $jar->getValueSeparator()); $this->assertEquals(false, $jar->isEncodingFields()); $this->assertEquals(false, $jar->isEncodingValues()); $this->assertEquals('', $jar->getPrefix()); } /** * @covers Guzzle\Http\QueryString::__toString * @dataProvider provider */ public function testConvertsToString($cookieString) { $cookie = Cookie::factory($cookieString); $this->assertEquals($cookieString, (string) $cookie); } /** * @covers Guzzle\Http\QueryString * @covers Guzzle\Http\Cookie */ public function testAggregatesMultipleCookieValues() { $cookie = Cookie::factory('name=a;name=b'); $this->assertEquals(array('a', 'b'), $cookie->get('name')); $this->assertEquals('name=a;name=b', (string) $cookie); } } tests/Guzzle/Tests/Http/Curl/CurlHandleTest.php000064400000075620151742734600015531 0ustar00getServer()->getUrl()); try { $ha = new CurlHandle($h, false); $this->fail('Expected InvalidArgumentException'); } catch (\InvalidArgumentException $e) { } $ha = new CurlHandle($h, array( CURLOPT_URL => $this->getServer()->getUrl() )); $this->assertEquals($this->getServer()->getUrl(), $ha->getOptions()->get(CURLOPT_URL)); $ha = new CurlHandle($h, new Collection(array( CURLOPT_URL => $this->getServer()->getUrl() ))); $this->assertEquals($this->getServer()->getUrl(), $ha->getOptions()->get(CURLOPT_URL)); } /** * @covers Guzzle\Http\Curl\CurlHandle::__construct * @covers Guzzle\Http\Curl\CurlHandle::getHandle * @covers Guzzle\Http\Curl\CurlHandle::getUrl * @covers Guzzle\Http\Curl\CurlHandle::getOptions */ public function testConstructorInitializesObject() { $handle = curl_init($this->getServer()->getUrl()); $h = new CurlHandle($handle, array( CURLOPT_URL => $this->getServer()->getUrl() )); $this->assertSame($handle, $h->getHandle()); $this->assertInstanceOf('Guzzle\\Http\\Url', $h->getUrl()); $this->assertEquals($this->getServer()->getUrl(), (string) $h->getUrl()); $this->assertEquals($this->getServer()->getUrl(), $h->getOptions()->get(CURLOPT_URL)); } /** * @covers Guzzle\Http\Curl\CurlHandle::getStderr */ public function testStoresStdErr() { $h = CurlHandle::factory(RequestFactory::getInstance()->create('GET', 'http://test.com')); $this->assertEquals($h->getStderr(true), $h->getOptions()->get(CURLOPT_STDERR)); $this->assertInternalType('resource', $h->getStderr(true)); $this->assertInternalType('string', $h->getStderr(false)); $r = $h->getStderr(true); fwrite($r, 'test'); $this->assertEquals('test', $h->getStderr(false)); } /** * @covers Guzzle\Http\Curl\CurlHandle::setErrorNo * @covers Guzzle\Http\Curl\CurlHandle::getErrorNo */ public function testStoresCurlErrorNumber() { $h = new CurlHandle(curl_init('http://test.com'), array(CURLOPT_URL => 'http://test.com')); $this->assertEquals(CURLE_OK, $h->getErrorNo()); $h->setErrorNo(CURLE_OPERATION_TIMEOUTED); $this->assertEquals(CURLE_OPERATION_TIMEOUTED, $h->getErrorNo()); } /** * @covers Guzzle\Http\Curl\CurlHandle::getStderr */ public function testAccountsForMissingStdErr() { $handle = curl_init('http://www.test.com/'); $h = new CurlHandle($handle, array( CURLOPT_URL => 'http://www.test.com/' )); $this->assertNull($h->getStderr(false)); } /** * @covers Guzzle\Http\Curl\CurlHandle::isAvailable */ public function testDeterminesIfResourceIsAvailable() { $handle = curl_init($this->getServer()->getUrl()); $h = new CurlHandle($handle, array()); $this->assertTrue($h->isAvailable()); // Mess it up by closing the handle curl_close($handle); $this->assertFalse($h->isAvailable()); // Mess it up by unsetting the handle $handle = null; $this->assertFalse($h->isAvailable()); } /** * @covers Guzzle\Http\Curl\CurlHandle::getError * @covers Guzzle\Http\Curl\CurlHandle::getErrorNo * @covers Guzzle\Http\Curl\CurlHandle::getInfo */ public function testWrapsErrorsAndInfo() { if (!defined('CURLOPT_TIMEOUT_MS')) { $this->markTestSkipped('Update curl'); } $settings = array( CURLOPT_PORT => 123, CURLOPT_CONNECTTIMEOUT_MS => 1 ); $handle = curl_init($this->getServer()->getUrl()); curl_setopt_array($handle, $settings); $h = new CurlHandle($handle, $settings); @curl_exec($handle); $errors = array( CURLE_COULDNT_CONNECT => "couldn't connect to host", CURLE_OPERATION_TIMEOUTED => 'timeout was reached' ); $this->assertTrue(in_array(strtolower($h->getError()), $errors)); $this->assertTrue($h->getErrorNo() > 0); $this->assertEquals($this->getServer()->getUrl(), $h->getInfo(CURLINFO_EFFECTIVE_URL)); $this->assertInternalType('array', $h->getInfo()); curl_close($handle); $this->assertEquals(null, $h->getInfo('url')); } /** * @covers Guzzle\Http\Curl\CurlHandle::getOptions */ public function testWrapsCurlOptions() { $handle = curl_init($this->getServer()->getUrl()); $h = new CurlHandle($handle, array( CURLOPT_AUTOREFERER => true, CURLOPT_BUFFERSIZE => 1024 )); $this->assertEquals(true, $h->getOptions()->get(CURLOPT_AUTOREFERER)); $this->assertEquals(1024, $h->getOptions()->get(CURLOPT_BUFFERSIZE)); } /** * Data provider for factory tests * * @return array */ public function dataProvider() { $testFile = __DIR__ . '/../../../../../phpunit.xml'; $postBody = new QueryString(array( 'file' => '@' . $testFile )); $qs = new QueryString(array( 'x' => 'y', 'z' => 'a' )); $userAgent = Utils::getDefaultUserAgent(); $auth = base64_encode('michael:123'); $testFileSize = filesize($testFile); return array( // Send a regular GET array('GET', 'http://www.google.com/', null, null, array( CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_HTTPHEADER => array('Host: www.google.com', 'User-Agent: ' . $userAgent), )), // Test that custom request methods can be used array('TRACE', 'http://www.google.com/', null, null, array( CURLOPT_CUSTOMREQUEST => 'TRACE' )), // Send a GET using a port array('GET', 'http://127.0.0.1:8080', null, null, array( CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_PORT => 8080, CURLOPT_HTTPHEADER => array('Host: 127.0.0.1:8080', 'User-Agent: ' . $userAgent), )), // Send a HEAD request array('HEAD', 'http://www.google.com/', null, null, array( CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_HTTPHEADER => array('Host: www.google.com', 'User-Agent: ' . $userAgent), CURLOPT_NOBODY => 1 )), // Send a GET using basic auth array('GET', 'https://michael:123@localhost/index.html?q=2', null, null, array( CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_HTTPHEADER => array( 'Host: localhost', 'Authorization: Basic ' . $auth, 'User-Agent: ' . $userAgent ), CURLOPT_PORT => 443 )), // Send a GET request with custom headers array('GET', 'http://localhost:8124/', array( 'x-test-data' => 'Guzzle' ), null, array( CURLOPT_PORT => 8124, CURLOPT_HTTPHEADER => array( 'Host: localhost:8124', 'x-test-data: Guzzle', 'User-Agent: ' . $userAgent ) ), array( '_Accept' => '*', '_Accept-Encoding' => '*', 'Host' => '*', 'User-Agent' => '*', 'x-test-data' => 'Guzzle' )), // Send a POST using a query string array('POST', 'http://localhost:8124/post.php', null, $qs, array( CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_POSTFIELDS => 'x=y&z=a', CURLOPT_HTTPHEADER => array ( 'Expect:', 'Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Content-Type: application/x-www-form-urlencoded' ) ), array( '_Accept' => '*', '_Accept-Encoding' => '*', 'Host' => '*', 'User-Agent' => '*', 'Content-Length' => '7', '!Expect' => null, 'Content-Type' => 'application/x-www-form-urlencoded', '!Transfer-Encoding' => null )), // Send a PUT using raw data array('PUT', 'http://localhost:8124/put.php', null, EntityBody::factory(fopen($testFile, 'r+')), array( CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_READFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_INFILESIZE => filesize($testFile), CURLOPT_HTTPHEADER => array ( 'Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Expect: 100-Continue' ) ), array( '_Accept' => '*', '_Accept-Encoding' => '*', 'Host' => '*', 'User-Agent' => '*', 'Expect' => '100-Continue', 'Content-Length' => $testFileSize, '!Transfer-Encoding' => null )), // Send a POST request using an array of fields array('POST', 'http://localhost:8124/post.php', null, array( 'x' => 'y', 'a' => 'b' ), array( CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_POST => 1, CURLOPT_POSTFIELDS => 'x=y&a=b', CURLOPT_HTTPHEADER => array ( 'Expect:', 'Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Content-Type: application/x-www-form-urlencoded' ) ), array( '_Accept' => '*', '_Accept-Encoding' => '*', 'Host' => '*', 'User-Agent' => '*', 'Content-Length' => '7', '!Expect' => null, 'Content-Type' => 'application/x-www-form-urlencoded', '!Transfer-Encoding' => null )), // Send a POST request using a POST file array('POST', 'http://localhost:8124/post.php', null, $postBody, array( CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_POST => 1, CURLOPT_POSTFIELDS => array( 'file' => '@' . $testFile ), CURLOPT_HTTPHEADER => array ( 'Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Expect: 100-Continue', 'Content-Type: multipart/form-data' ) ), array( '_Accept' => '*', '_Accept-Encoding' => '*', 'Host' => '*', 'User-Agent' => '*', 'Content-Length' => '*', 'Expect' => '100-Continue', 'Content-Type' => 'multipart/form-data; boundary=*', '!Transfer-Encoding' => null )), // Send a POST request with raw POST data and a custom content-type array('POST', 'http://localhost:8124/post.php', array( 'Content-Type' => 'application/json' ), '{"hi":"there"}', array( CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_POST => 1, CURLOPT_HTTPHEADER => array ( 'Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Expect: 100-Continue', 'Content-Type: application/json', 'Content-Length: 14' ), ), array( '_Accept-Encoding' => '*', '_Accept' => '*', 'Host' => '*', 'User-Agent' => '*', 'Content-Type' => 'application/json', 'Expect' => '100-Continue', 'Content-Length' => '14', '!Transfer-Encoding' => null )), // Send a POST request with raw POST data, a custom content-type, and use chunked encoding array('POST', 'http://localhost:8124/post.php', array( 'Content-Type' => 'application/json', 'Transfer-Encoding' => 'chunked' ), '{"hi":"there"}', array( CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_POST => 1, CURLOPT_HTTPHEADER => array ( 'Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Expect: 100-Continue', 'Content-Type: application/json', 'Transfer-Encoding: chunked' ), ), array( '_Accept-Encoding' => '*', '_Accept' => '*', 'Host' => '*', 'User-Agent' => '*', 'Content-Type' => 'application/json', 'Expect' => '100-Continue', 'Transfer-Encoding' => 'chunked', '!Content-Length' => '' )), // Send a POST that does not have a body defined array('POST', 'http://localhost:8124/foo.php', null, null, array( CURLOPT_HTTPHEADER => array ( 'Expect:', 'Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Content-Length: 0' ) ), array( '_Accept' => '*', '_Accept-Encoding' => '*', 'Host' => '*', 'User-Agent' => '*', 'Content-Length' => '0', '!Expect' => null, '!Content-Type' => '*', '!Transfer-Encoding' => null )), // Send a PUT that does not have a body defined array('PUT', 'http://localhost:8124/empty-put.php', null, null, array( CURLOPT_HTTPHEADER => array ( 'Expect:', 'Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Content-Length: 0' ) ), array( '_Accept' => '*', '_Accept-Encoding' => '*', 'Host' => '*', 'User-Agent' => '*', 'Content-Length' => '0', '!Expect' => null, '!Content-Type' => null, '!Transfer-Encoding' => null )), // Send a PATCH request array('PATCH', 'http://localhost:8124/patch.php', null, 'body', array( CURLOPT_INFILESIZE => 4, CURLOPT_HTTPHEADER => array ( 'Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Expect: 100-Continue' ) ) /*, array( '_Accept' => '*', '_Accept-Encoding' => '*', 'Host' => '*', 'User-Agent' => '*', 'Content-Length' => '4', 'Expect' => '100-Continue', '!Content-Type' => null, '!Transfer-Encoding' => null )*/ ), ); } /** * @covers Guzzle\Http\Curl\CurlHandle::factory * @covers Guzzle\Http\Curl\CurlHandle::updateRequestFromTransfer * @covers Guzzle\Http\Curl\RequestMediator * @dataProvider dataProvider */ public function testFactoryCreatesCurlBasedOnRequest($method, $url, $headers, $body, $options, $expectedHeaders = null) { $request = RequestFactory::getInstance()->create($method, $url, $headers, $body); $originalRequest = clone $request; $curlTest = clone $request; $handle = CurlHandle::factory($curlTest); $this->assertInstanceOf('Guzzle\\Http\\Curl\\CurlHandle', $handle); $o = $curlTest->getParams()->get('curl.last_options'); // Headers are case-insensitive if (isset($o[CURLOPT_HTTPHEADER])) { $o[CURLOPT_HTTPHEADER] = array_map('strtolower', $o[CURLOPT_HTTPHEADER]); } if (isset($options[CURLOPT_HTTPHEADER])) { $options[CURLOPT_HTTPHEADER] = array_map('strtolower', $options[CURLOPT_HTTPHEADER]); } $check = 0; foreach ($options as $key => $value) { $check++; $this->assertArrayHasKey($key, $o, '-> Check number ' . $check); if ($key != CURLOPT_HTTPHEADER && $key != CURLOPT_POSTFIELDS && (is_array($o[$key])) || $o[$key] instanceof \Closure) { $this->assertEquals('callback', $value, '-> Check number ' . $check); } else { $this->assertTrue($value == $o[$key], '-> Check number ' . $check . ' - ' . var_export($value, true) . ' != ' . var_export($o[$key], true)); } } // If we are testing the actual sent headers if ($expectedHeaders) { // Send the request to the test server $client = new Client($this->getServer()->getUrl()); $request->setClient($client); $this->getServer()->flush(); $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); $request->send(); // Get the request that was sent and create a request that we expected $requests = $this->getServer()->getReceivedRequests(true); $this->assertEquals($method, $requests[0]->getMethod()); $test = $this->compareHeaders($expectedHeaders, $requests[0]->getHeaders()); $this->assertFalse($test, $test . "\nSent: \n" . $request . "\n\n" . $requests[0]); // Ensure only one Content-Length header is sent if ($request->getHeader('Content-Length')) { $this->assertEquals((string) $request->getHeader('Content-Length'), (string) $requests[0]->getHeader('Content-Length')); } } } /** * @covers Guzzle\Http\Curl\CurlHandle */ public function testFactoryUsesSpecifiedProtocol() { $request = RequestFactory::getInstance()->create('GET', 'http://localhost:8124/'); $request->setProtocolVersion('1.1'); $handle = CurlHandle::factory($request); $options = $request->getParams()->get('curl.last_options'); $this->assertEquals(CURL_HTTP_VERSION_1_1, $options[CURLOPT_HTTP_VERSION]); } /** * @covers Guzzle\Http\Curl\CurlHandle */ public function testUploadsPutData() { $this->getServer()->flush(); $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi"); $client = new Client($this->getServer()->getUrl()); $request = $client->put('/'); $request->setBody(EntityBody::factory('test'), 'text/plain', false); $request->getCurlOptions()->set('progress', true); $o = $this->getWildcardObserver($request); $request->send(); // Make sure that the events were dispatched $this->assertTrue($o->has('curl.callback.progress')); // Ensure that the request was received exactly as intended $r = $this->getServer()->getReceivedRequests(true); $this->assertEquals(strtolower($request), strtolower($r[0])); $this->assertFalse($r[0]->hasHeader('Transfer-Encoding')); $this->assertEquals(4, (string) $r[0]->getHeader('Content-Length')); } /** * @covers Guzzle\Http\Curl\CurlHandle */ public function testUploadsPutDataUsingChunkedEncodingWhenLengthCannotBeDetermined() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi" )); $client = new Client($this->getServer()->getUrl()); $request = $client->put('/'); $request->setBody(EntityBody::factory(fopen($this->getServer()->getUrl(), 'r')), 'text/plain'); $request->send(); $r = $this->getServer()->getReceivedRequests(true); $this->assertEquals('chunked', $r[1]->getHeader('Transfer-Encoding')); $this->assertFalse($r[1]->hasHeader('Content-Length')); } /** * @covers Guzzle\Http\Curl\CurlHandle */ public function testUploadsPutDataUsingChunkedEncodingWhenForced() { $this->getServer()->flush(); $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi"); $client = new Client($this->getServer()->getUrl()); $request = $client->put('/'); $request->setBody(EntityBody::factory('hi!'), 'text/plain', true); $request->send(); $r = $this->getServer()->getReceivedRequests(true); $this->assertEquals('chunked', $r[0]->getHeader('Transfer-Encoding')); $this->assertFalse($r[0]->hasHeader('Content-Length')); $this->assertEquals('hi!', $r[0]->getBody(true)); } /** * @covers Guzzle\Http\Curl\CurlHandle */ public function testSendsPostRequestsWithFields() { $this->getServer()->flush(); $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi"); $request = RequestFactory::getInstance()->create('POST', $this->getServer()->getUrl()); $request->setClient(new Client()); $request->addPostFields(array( 'a' => 'b', 'c' => 'ay! ~This is a test, isn\'t it?' )); $request->send(); // Ensure the CURLOPT_POSTFIELDS option was set properly $options = $request->getParams()->get('curl.last_options'); $this->assertEquals('a=b&c=ay%21%20~This%20is%20a%20test%2C%20isn%27t%20it%3F', $options[CURLOPT_POSTFIELDS]); // Make sure that the request was sent correctly $r = $this->getServer()->getReceivedRequests(true); $this->assertEquals(strtolower($request), strtolower($r[0])); } /** * @covers Guzzle\Http\Curl\CurlHandle */ public function testSendsPostRequestsWithFiles() { $this->getServer()->flush(); $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi"); $request = RequestFactory::getInstance()->create('POST', $this->getServer()->getUrl()); $request->setClient(new Client()); $request->addPostFiles(array( 'foo' => __FILE__, )); $request->addPostFields(array( 'bar' => 'baz' )); $request->send(); // Ensure the CURLOPT_POSTFIELDS option was set properly $options = $request->getParams()->get('curl.last_options'); $this->assertEquals(array( 'foo' => '@' . __FILE__, 'bar' => 'baz' ), $options[CURLOPT_POSTFIELDS]); // Ensure that a Content-Length header was sent by cURL $this->assertTrue($request->hasHeader('Content-Length')); } /** * @covers Guzzle\Http\Curl\CurlHandle::factory */ public function testHeadersCanBeBlacklisted() { $request = RequestFactory::getInstance()->create('PUT', $this->getServer()->getUrl()); $request->setClient(new Client('http://www.example.com', array( 'curl.blacklist' => array('header.Accept', 'header.Foo', CURLOPT_ENCODING) ))); $request->setHeader('Foo', 'Bar'); $handle = CurlHandle::factory($request); $headers = $handle->getOptions()->get(CURLOPT_HTTPHEADER); $this->assertTrue(in_array('Accept:', $headers)); $this->assertTrue(in_array('Foo:', $headers)); $this->assertFalse($handle->getOptions()->hasKey(CURLOPT_ENCODING)); $this->getServer()->flush(); $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi"); $request->send(); $r = $this->getServer()->getReceivedRequests(true); $this->assertFalse($r[0]->hasHeader('Accept')); $this->assertFalse($r[0]->hasHeader('Foo')); } /** * @covers Guzzle\Http\Curl\CurlHandle::factory */ public function testAllowsHeadersSetToNull() { $request = RequestFactory::getInstance()->create('PUT', $this->getServer()->getUrl()); $request->setClient(new Client()); $request->setBody('test'); $request->setHeader('Expect', null); $request->setHeader('Accept', null); $handle = CurlHandle::factory($request); $headers = $handle->getOptions()->get(CURLOPT_HTTPHEADER); $this->assertTrue(in_array('Expect:', $headers)); $this->assertTrue(in_array('Accept:', $headers)); } /** * @covers Guzzle\Http\Curl\CurlHandle::updateRequestFromTransfer */ public function testEnsuresRequestsHaveResponsesWhenUpdatingFromTransfer() { $request = RequestFactory::getInstance()->create('PUT', $this->getServer()->getUrl()); $handle = CurlHandle::factory($request); $handle->updateRequestFromTransfer($request); } public function testCurlFollowsRedirectsUsingPost() { $this->markTestIncomplete('This is a bug with PHP: https://bugs.php.net/bug.php?id=47204'); $url = $this->getServer()->getUrl(); $this->getServer()->enqueue(array( "HTTP/1.1 303 See Other\r\nServer: Apache-Coyote/1.1\r\nLocation: {$url}\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" )); $client = new Client($url); $request = $client->post('/', null, '{}'); $request->removeHeader('Expect'); $request->send(); $received = $this->getServer()->getReceivedRequests(true); $this->assertEquals(2, count($received)); } public function testAllowsWireTransferInfoToBeDisabled() { $request = RequestFactory::getInstance()->create('PUT', $this->getServer()->getUrl()); $request->getCurlOptions()->set('disable_wire', true); $handle = CurlHandle::factory($request); $this->assertNull($handle->getOptions()->get(CURLOPT_STDERR)); $this->assertNull($handle->getOptions()->get(CURLOPT_VERBOSE)); } public function testAddsCustomCurlOptions() { $request = RequestFactory::getInstance()->create('PUT', $this->getServer()->getUrl()); $request->getCurlOptions()->set(CURLOPT_TIMEOUT, 200); $handle = CurlHandle::factory($request); $this->assertEquals(200, $handle->getOptions()->get(CURLOPT_TIMEOUT)); } } tests/Guzzle/Tests/Http/Curl/CurlMultiTest.php000064400000050667151742734600015434 0ustar00updates = new Collection(); $this->multi = new MockMulti(); $this->mock = $this->getWildcardObserver($this->multi); } /** * @covers Guzzle\Http\Curl\CurlMulti::getInstance */ public function testReturnsCachedInstance() { $c = CurlMulti::getInstance(); $this->assertInstanceOf('Guzzle\\Http\\Curl\\CurlMultiInterface', $c); $this->assertSame($c, CurlMulti::getInstance()); } /** * @covers Guzzle\Http\Curl\CurlMulti::__construct * @covers Guzzle\Http\Curl\CurlMulti::__destruct */ public function testConstructorCreateMultiHandle() { $this->assertInternalType('resource', $this->multi->getHandle()); $this->assertEquals('curl_multi', get_resource_type($this->multi->getHandle())); } /** * @covers Guzzle\Http\Curl\CurlMulti::__destruct */ public function testDestructorClosesMultiHandle() { $handle = $this->multi->getHandle(); $this->multi->__destruct(); $this->assertFalse(is_resource($handle)); } /** * @covers Guzzle\Http\Curl\curlMulti::add * @covers Guzzle\Http\Curl\curlMulti::all * @covers Guzzle\Http\Curl\curlMulti::count */ public function testRequestsCanBeAddedAndCounted() { $multi = new CurlMulti(); $mock = $this->getWildcardObserver($multi); $request1 = new Request('GET', 'http://www.google.com/'); $multi->add($request1); $this->assertEquals(array($request1), $multi->all()); $request2 = new Request('POST', 'http://www.google.com/'); $multi->add($request2); $this->assertEquals(array($request1, $request2), $multi->all()); $this->assertEquals(2, count($multi)); $this->assertTrue($mock->has(CurlMulti::ADD_REQUEST)); $this->assertFalse($mock->has(CurlMulti::REMOVE_REQUEST)); $this->assertFalse($mock->has(CurlMulti::POLLING)); $this->assertFalse($mock->has(CurlMulti::COMPLETE)); } /** * @covers Guzzle\Http\Curl\CurlMulti::remove * @covers Guzzle\Http\Curl\CurlMulti::all */ public function testRequestsCanBeRemoved() { $request1 = new Request('GET', 'http://www.google.com/'); $this->multi->add($request1); $request2 = new Request('PUT', 'http://www.google.com/'); $this->multi->add($request2); $this->assertEquals(array($request1, $request2), $this->multi->all()); $this->assertSame($this->multi, $this->multi->remove($request1)); $this->assertEquals(array($request2), $this->multi->all()); } /** * @covers Guzzle\Http\Curl\CurlMulti::reset */ public function testsResetRemovesRequestsAndResetsState() { $request1 = new Request('GET', 'http://www.google.com/'); $this->multi->add($request1); $this->multi->reset(); $this->assertEquals(array(), $this->multi->all()); $this->assertEquals('idle', $this->multi->getState()); } /** * @covers Guzzle\Http\Curl\CurlMulti::send * @covers Guzzle\Http\Curl\CurlMulti::getState */ public function testSendsRequestsInParallel() { $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nBody"); $this->assertEquals('idle', $this->multi->getState()); $request = new Request('GET', $this->getServer()->getUrl()); $this->multi->add($request); $this->multi->send(); $this->assertEquals('idle', $this->multi->getState()); $this->assertTrue($this->mock->has(CurlMulti::ADD_REQUEST)); $this->assertTrue($this->mock->has(CurlMulti::COMPLETE)); $this->assertEquals('Body', $request->getResponse()->getBody()->__toString()); // Sending it again will not do anything because there are no requests $this->multi->send(); } /** * @covers Guzzle\Http\Curl\CurlMulti::send */ public function testSendsRequestsThroughCurl() { $this->getServer()->enqueue(array( "HTTP/1.1 204 No content\r\n" . "Content-Length: 0\r\n" . "Server: Jetty(6.1.3)\r\n\r\n", "HTTP/1.1 200 OK\r\n" . "Content-Type: text/html; charset=utf-8\r\n" . "Content-Length: 4\r\n" . "Server: Jetty(6.1.3)\r\n\r\n" . "data" )); $request1 = new Request('GET', $this->getServer()->getUrl()); $mock1 = $this->getWildcardObserver($request1); $request2 = new Request('GET', $this->getServer()->getUrl()); $mock2 = $this->getWildcardObserver($request2); $this->multi->add($request1); $this->multi->add($request2); $this->multi->send(); $response1 = $request1->getResponse(); $response2 = $request2->getResponse(); $this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $response1); $this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $response2); $this->assertTrue($response1->getBody(true) == 'data' || $response2->getBody(true) == 'data'); $this->assertTrue($response1->getBody(true) == '' || $response2->getBody(true) == ''); $this->assertTrue($response1->getStatusCode() == '204' || $response2->getStatusCode() == '204'); $this->assertNotEquals((string) $response1, (string) $response2); $this->assertTrue($mock1->has('request.before_send')); $this->assertTrue($mock2->has('request.before_send')); } /** * @covers Guzzle\Http\Curl\CurlMulti::send */ public function testSendsThroughCurlAndAggregatesRequestExceptions() { $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\n" . "Content-Type: text/html; charset=utf-8\r\n" . "Content-Length: 4\r\n" . "Server: Jetty(6.1.3)\r\n" . "\r\n" . "data", "HTTP/1.1 204 No content\r\n" . "Content-Length: 0\r\n" . "Server: Jetty(6.1.3)\r\n" . "\r\n", "HTTP/1.1 404 Not Found\r\n" . "Content-Length: 0\r\n" . "\r\n" )); $request1 = new Request('GET', $this->getServer()->getUrl()); $request2 = new Request('HEAD', $this->getServer()->getUrl()); $request3 = new Request('GET', $this->getServer()->getUrl()); $this->multi->add($request1); $this->multi->add($request2); $this->multi->add($request3); try { $this->multi->send(); $this->fail('ExceptionCollection not thrown when aggregating request exceptions'); } catch (ExceptionCollection $e) { $this->assertInstanceOf('ArrayIterator', $e->getIterator()); $this->assertEquals(1, count($e)); $exceptions = $e->getIterator(); $response1 = $request1->getResponse(); $response2 = $request2->getResponse(); $response3 = $request3->getResponse(); $this->assertNotEquals((string) $response1, (string) $response2); $this->assertNotEquals((string) $response3, (string) $response1); $this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $response1); $this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $response2); $this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $response3); $failed = $exceptions[0]->getResponse(); $this->assertEquals(404, $failed->getStatusCode()); $this->assertEquals(1, count($e)); // Test the IteratorAggregate functionality foreach ($e as $excep) { $this->assertEquals($failed, $excep->getResponse()); } } } /** * @covers Guzzle\Http\Curl\CurlMulti::send * @covers Guzzle\Http\Curl\CurlMulti::processResponse */ public function testCurlErrorsAreCaught() { $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); try { $request = RequestFactory::getInstance()->create('GET', 'http://127.0.0.1:9876/'); $request->setClient(new Client()); $request->getCurlOptions()->set(CURLOPT_FRESH_CONNECT, true); $request->getCurlOptions()->set(CURLOPT_FORBID_REUSE, true); $request->getCurlOptions()->set(CURLOPT_CONNECTTIMEOUT_MS, 5); $request->send(); $this->fail('CurlException not thrown'); } catch (CurlException $e) { $m = $e->getMessage(); $this->assertContains('[curl] ', $m); $this->assertContains('[url] http://127.0.0.1:9876/', $m); $this->assertContains('[debug] ', $m); $this->assertContains('[info] array (', $m); } } /** * @covers Guzzle\Http\Curl\CurlMulti */ public function testRemovesQueuedRequests() { $request = RequestFactory::getInstance()->create('GET', 'http://127.0.0.1:9876/'); $request->setClient(new Client()); $request->setResponse(new Response(200), true); $this->multi->add($request); $this->multi->send(); $this->assertTrue($this->mock->has(CurlMulti::ADD_REQUEST)); $this->assertTrue($this->mock->has(CurlMulti::POLLING) === false); $this->assertTrue($this->mock->has(CurlMulti::COMPLETE) !== false); } /** * @covers Guzzle\Http\Curl\CurlMulti */ public function testRemovesQueuedRequestsAddedInTransit() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" )); $client = new Client($this->getServer()->getUrl()); $r = $client->get(); $r->getEventDispatcher()->addListener('request.receive.status_line', function(Event $event) use ($client) { // Create a request using a queued response $request = $client->get()->setResponse(new Response(200), true); $request->send(); }); $r->send(); $this->assertEquals(1, count($this->getServer()->getReceivedRequests(false))); } /** * @covers Guzzle\Http\Curl\CurlMulti */ public function testProperlyBlocksBasedOnRequestsInScope() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest1", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest2", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest3", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest4", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest5", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest6", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" )); $client = new Client($this->getServer()->getUrl()); $requests = array( $client->get(), $client->get() ); $sendHeadFunction = function($event) use ($client) { $client->head()->send(); }; // Sends 2 new requests in the middle of a CurlMulti loop while other requests // are completing. This causes the scope of the multi handle to go up. $callback = function(Event $event) use ($client, $sendHeadFunction) { $client->getConfig()->set('called', $client->getConfig('called') + 1); if ($client->getConfig('called') <= 2) { $request = $client->get(); $request->getEventDispatcher()->addListener('request.complete', $sendHeadFunction); $request->send(); } }; $requests[0]->getEventDispatcher()->addListener('request.complete', $callback); $client->send($requests); $this->assertEquals(4, count($this->getServer()->getReceivedRequests(false))); } /** * @covers Guzzle\Http\Curl\CurlMulti * @expectedException RuntimeException * @expectedExceptionMessage Testing! */ public function testCatchesExceptionsBeforeSendingCurlMulti() { $client = new Client($this->getServer()->getUrl()); $multi = new CurlMulti(); $client->setCurlMulti($multi); $multi->getEventDispatcher()->addListener(CurlMulti::BEFORE_SEND, function() { throw new \RuntimeException('Testing!'); }); $client->get()->send(); } /** * @covers Guzzle\Http\Curl\CurlMulti * @covers Guzzle\Http\Curl\CurlMulti::removeErroredRequest * @expectedException Guzzle\Common\Exception\ExceptionCollection * @expectedExceptionMessage Thrown before sending! */ public function testCatchesExceptionsBeforeSendingRequests() { $client = new Client($this->getServer()->getUrl()); $request = $client->get(); $request->getEventDispatcher()->addListener('request.before_send', function() { throw new \RuntimeException('Thrown before sending!'); }); $client->send(array($request)); } /** * @covers Guzzle\Http\Curl\CurlMulti * @covers Guzzle\Http\Curl\CurlMulti::removeErroredRequest * @expectedException Guzzle\Http\Exception\BadResponseException */ public function testCatchesExceptionsWhenRemovingQueuedRequests() { $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); $client = new Client($this->getServer()->getUrl()); $r = $client->get(); $r->getEventDispatcher()->addListener('request.sent', function() use ($client) { // Create a request using a queued response $client->get()->setResponse(new Response(404), true)->send(); }); $r->send(); } /** * @covers Guzzle\Http\Curl\CurlMulti * @covers Guzzle\Http\Curl\CurlMulti::removeErroredRequest * @expectedException Guzzle\Http\Exception\BadResponseException */ public function testCatchesExceptionsWhenRemovingQueuedRequestsBeforeSending() { $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); $client = new Client($this->getServer()->getUrl()); $r = $client->get(); $r->getEventDispatcher()->addListener('request.before_send', function() use ($client) { // Create a request using a queued response $client->get()->setResponse(new Response(404), true)->send(); }); $r->send(); } /** * @covers Guzzle\Http\Curl\CurlMulti::send * @covers Guzzle\Http\Curl\CurlMulti::removeErroredRequest * @expectedException Guzzle\Common\Exception\ExceptionCollection * @expectedExceptionMessage test */ public function testCatchesRandomExceptionsThrownDuringPerform() { $client = new Client($this->getServer()->getUrl()); $multi = $this->getMock('Guzzle\\Http\\Curl\\CurlMulti', array('perform')); $multi->expects($this->once()) ->method('perform') ->will($this->throwException(new \Exception('test'))); $multi->add($client->get()); $multi->send(); } /** * @covers Guzzle\Http\Curl\CurlMulti::send */ public function testDoesNotSendRequestsDecliningToBeSent() { if (!defined('CURLOPT_TIMEOUT_MS')) { $this->markTestSkipped('Update curl'); } // Create a client that is bound to fail connecting $client = new Client('http://localhost:123', array( 'curl.CURLOPT_PORT' => 123, 'curl.CURLOPT_CONNECTTIMEOUT_MS' => 1, )); $request = $client->get(); $multi = new CurlMulti(); $multi->add($request); // Listen for request exceptions, and when they occur, first change the // state of the request back to transferring, and then just allow it to // exception out $request->getEventDispatcher()->addListener('request.exception', function(Event $event) use ($multi) { $retries = $event['request']->getParams()->get('retries'); // Allow the first failure to retry if ($retries == 0) { $event['request']->setState('transfer'); $event['request']->getParams()->set('retries', 1); // Remove the request to try again $multi->remove($event['request']); $multi->add($event['request'], true); } }); try { $multi->send(); $this->fail('Did not throw an exception at all!?!'); } catch (\Exception $e) { $this->assertEquals(1, $request->getParams()->get('retries')); } } /** * @covers Guzzle\Http\Curl\CurlMulti::send */ public function testDoesNotThrowExceptionsWhenRequestsRecover() { $this->getServer()->flush(); $client = new Client($this->getServer()->getUrl()); $request = $client->get(); $request->getEventDispatcher()->addListener('request.before_send', function(Event $event) { $event['request']->setResponse(new Response(200)); }); $multi = new CurlMulti(); $multi->add($request); $multi->send(); $this->assertEquals(0, count($this->getServer()->getReceivedRequests(false))); } /** * @covers Guzzle\Http\Curl\CurlMulti::reset */ public function testHardResetReopensMultiHandle() { $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" )); $client = new Client($this->getServer()->getUrl()); $message = ''; $plugin = new LogPlugin(new ClosureLogAdapter(function($msg) use (&$message) { $message .= $msg . "\n"; }), LogPlugin::LOG_VERBOSE); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get(); $multi = new CurlMulti(); $multi->add($request); $multi->send(); $multi->reset(true); $multi->add($request); $multi->send(); $this->assertNotContains('Re-using existing connection', $message); } /** * @covers Guzzle\Http\Curl\CurlMulti::checkCurlResult */ public function testThrowsMeaningfulExceptionsForCurlMultiErrors() { $multi = new CurlMulti(); // Set the state of the multi object to sending to trigger the exception $reflector = new \ReflectionMethod('Guzzle\Http\Curl\CurlMulti', 'checkCurlResult'); $reflector->setAccessible(true); // Successful $reflector->invoke($multi, 0); // Known error try { $reflector->invoke($multi, CURLM_BAD_HANDLE); $this->fail('Expected an exception here'); } catch (CurlException $e) { $this->assertContains('The passed-in handle is not a valid CURLM handle.', $e->getMessage()); $this->assertContains('CURLM_BAD_HANDLE', $e->getMessage()); $this->assertContains(strval(CURLM_BAD_HANDLE), $e->getMessage()); } // Unknown error try { $reflector->invoke($multi, 255); $this->fail('Expected an exception here'); } catch (CurlException $e) { $this->assertEquals('Unexpected cURL error: 255', $e->getMessage()); } } /** * @covers Guzzle\Http\Curl\curlMulti::add */ public function testAddsAsyncRequestsNormallyWhenNotSending() { $multi = new CurlMulti(); $request = new Request('GET', 'http://www.google.com/'); $multi->add($request, true); // Ensure that the request was added at the correct next scope $requests = $this->readAttribute($multi, 'requests'); $this->assertEquals(array($request), $requests[0]); } } tests/Guzzle/Tests/Http/Curl/CurlVersionTest.php000064400000003007151742734600015751 0ustar00getProperty('version'); $refProperty->setAccessible(true); $refProperty->setValue($instance, array()); $this->assertEquals($info, $instance->getAll()); $this->assertEquals($info, $instance->getAll()); $this->assertEquals($info['version'], $instance->get('version')); $this->assertFalse($instance->get('foo')); } public function testDeterminesIfCurlCanFollowLocation() { if (!ini_get('open_basedir')) { $this->assertTrue(CurlVersion::getInstance()->get('follow_location')); } else { $this->assertFalse(CurlVersion::getInstance()->get('follow_location')); } } public function testIsSingleton() { $refObject = new \ReflectionClass('Guzzle\Http\Curl\CurlVersion'); $refProperty = $refObject->getProperty('instance'); $refProperty->setAccessible(true); $refProperty->setValue(null, null); $this->assertInstanceOf('Guzzle\Http\Curl\CurlVersion', CurlVersion::getInstance()); } } tests/Guzzle/Tests/Http/Curl/RequestMediatorTest.php000064400000003703151742734600016616 0ustar00events[] = $event; } public function testEmitsEvents() { $request = new EntityEnclosingRequest('PUT', 'http://www.example.com'); $request->setBody('foo'); $request->setResponse(new Response(200)); // Ensure that IO events are emitted $request->getParams()->set('curl.emit_io', true); // Attach listeners for each event type $request->getEventDispatcher()->addListener('curl.callback.progress', array($this, 'event')); $request->getEventDispatcher()->addListener('curl.callback.read', array($this, 'event')); $request->getEventDispatcher()->addListener('curl.callback.write', array($this, 'event')); $mediator = new RequestMediator($request, true); $mediator->progress('a', 'b', 'c', 'd'); $this->assertEquals(1, count($this->events)); $this->assertEquals('curl.callback.progress', $this->events[0]->getName()); $this->assertEquals(3, $mediator->writeResponseBody('foo', 'bar')); $this->assertEquals(2, count($this->events)); $this->assertEquals('curl.callback.write', $this->events[1]->getName()); $this->assertEquals('bar', $this->events[1]['write']); $this->assertSame($request, $this->events[1]['request']); $this->assertEquals('foo', $mediator->readRequestBody('a', 'b', 3)); $this->assertEquals(3, count($this->events)); $this->assertEquals('curl.callback.read', $this->events[2]->getName()); $this->assertEquals('foo', $this->events[2]['read']); $this->assertSame($request, $this->events[2]['request']); } } tests/Guzzle/Tests/Http/EntityBodyTest.php000064400000013277151742734600014675 0ustar00assertEquals('data', (string)$body); $this->assertEquals(4, $body->getContentLength()); $this->assertEquals('php', $body->getWrapper()); $this->assertEquals('temp', $body->getStreamType()); $handle = fopen(__DIR__ . '/../../../../phpunit.xml', 'r'); if (!$handle) { $this->fail('Could not open test file'); } $body = EntityBody::factory($handle); $this->assertEquals(__DIR__ . '/../../../../phpunit.xml', $body->getUri()); $this->assertTrue($body->isLocal()); $this->assertEquals(__DIR__ . '/../../../../phpunit.xml', $body->getUri()); $this->assertEquals(filesize(__DIR__ . '/../../../../phpunit.xml'), $body->getContentLength()); // make sure that a body will return as the same object $this->assertTrue($body === EntityBody::factory($body)); } /** * @covers Guzzle\Http\EntityBody::factory */ public function testFactoryCreatesTempStreamByDefault() { $body = EntityBody::factory(''); $this->assertEquals('php', $body->getWrapper()); $this->assertEquals('temp', $body->getStreamType()); $body = EntityBody::factory(); $this->assertEquals('php', $body->getWrapper()); $this->assertEquals('temp', $body->getStreamType()); } /** * @covers Guzzle\Http\EntityBody::compress * @covers Guzzle\Http\EntityBody::uncompress * @covers Guzzle\Http\EntityBody::getContentEncoding * @covers Guzzle\Http\EntityBody::setStreamFilterContentEncoding * @covers Guzzle\Http\EntityBody::handleCompression * @covers Guzzle\Http\EntityBody::getContentLength */ public function testHandlesCompression() { $body = EntityBody::factory('testing 123...testing 123'); $this->assertFalse($body->getContentEncoding(), '-> getContentEncoding() must initially return FALSE'); $size = $body->getContentLength(); $body->compress(); $this->assertEquals('gzip', $body->getContentEncoding(), '-> getContentEncoding() must return the correct encoding after compressing'); $this->assertEquals(gzdeflate('testing 123...testing 123'), (string) $body); $this->assertTrue($body->getContentLength() < $size); $this->assertTrue($body->uncompress()); $this->assertEquals('testing 123...testing 123', (string) $body); $this->assertFalse($body->getContentEncoding(), '-> getContentEncoding() must reset to FALSE'); if (in_array('bzip2.*', stream_get_filters())) { $this->assertTrue($body->compress('bzip2.compress')); $this->assertEquals('compress', $body->getContentEncoding(), '-> compress() must set \'compress\' as the Content-Encoding'); } $this->assertFalse($body->compress('non-existent'), '-> compress() must return false when a non-existent stream filter is used'); // Release the body unset($body); // Use gzip compression on the initial content. This will include a // gzip header which will need to be stripped when deflating the stream $body = EntityBody::factory(gzencode('test')); $this->assertSame($body, $body->setStreamFilterContentEncoding('zlib.deflate')); $this->assertTrue($body->uncompress('zlib.inflate')); $this->assertEquals('test', (string)$body); unset($body); // Test using a very long string $largeString = ''; for ($i = 0; $i < 25000; $i++) { $largeString .= chr(rand(33, 126)); } $body = EntityBody::factory($largeString); $this->assertEquals($largeString, (string)$body); $this->assertTrue($body->compress()); $this->assertNotEquals($largeString, (string)$body); $compressed = (string)$body; $this->assertTrue($body->uncompress()); $this->assertEquals($largeString, (string)$body); $this->assertEquals($compressed, gzdeflate($largeString)); $body = EntityBody::factory(fopen(__DIR__ . '/../TestData/compress_test', 'w')); $this->assertFalse($body->compress()); unset($body); unlink(__DIR__ . '/../TestData/compress_test'); } /** * @covers Guzzle\Http\EntityBody::getContentType */ public function testDeterminesContentType() { // Test using a string/temp stream $body = EntityBody::factory('testing 123...testing 123'); $this->assertEquals('application/octet-stream', $body->getContentType()); // Use a local file $body = EntityBody::factory(fopen(__FILE__, 'r')); $this->assertEquals('text/x-php', $body->getContentType()); } /** * @covers Guzzle\Http\EntityBody::getContentMd5 */ public function testCreatesMd5Checksum() { $body = EntityBody::factory('testing 123...testing 123'); $this->assertEquals(md5('testing 123...testing 123'), $body->getContentMd5()); $server = $this->getServer()->enqueue( "HTTP/1.1 200 OK" . "\r\n" . "Content-Length: 3" . "\r\n\r\n" . "abc" ); $body = EntityBody::factory(fopen($this->getServer()->getUrl(), 'r')); $this->assertFalse($body->getContentMd5()); } } tests/Guzzle/Tests/Http/Exception/CurlExceptionTest.php000064400000001042151742734600017310 0ustar00assertNull($e->getError()); $this->assertNull($e->getErrorNo()); $this->assertSame($e, $e->setError('test', 12)); $this->assertEquals('test', $e->getError()); $this->assertEquals(12, $e->getErrorNo()); } } tests/Guzzle/Tests/Http/Exception/ExceptionTest.php000064400000004201151742734600016462 0ustar00setRequest($request); $this->assertEquals($request, $e->getRequest()); } /** * @covers Guzzle\Http\Exception\BadResponseException */ public function testBadResponseException() { $e = new BadResponseException('Message'); $response = new Response(200); $e->setResponse($response); $this->assertEquals($response, $e->getResponse()); } /** * @covers Guzzle\Http\Exception\BadResponseException::factory */ public function testCreatesGenericErrorExceptionOnError() { $request = new Request('GET', 'http://www.example.com'); $response = new Response(307); $e = BadResponseException::factory($request, $response); $this->assertInstanceOf('Guzzle\Http\Exception\BadResponseException', $e); } /** * @covers Guzzle\Http\Exception\BadResponseException::factory */ public function testCreatesClientErrorExceptionOnClientError() { $request = new Request('GET', 'http://www.example.com'); $response = new Response(404); $e = BadResponseException::factory($request, $response); $this->assertInstanceOf('Guzzle\Http\Exception\ClientErrorResponseException', $e); } /** * @covers Guzzle\Http\Exception\BadResponseException::factory */ public function testCreatesServerErrorExceptionOnServerError() { $request = new Request('GET', 'http://www.example.com'); $response = new Response(503); $e = BadResponseException::factory($request, $response); $this->assertInstanceOf('Guzzle\Http\Exception\ServerErrorResponseException', $e); } } tests/Guzzle/Tests/Http/Message/AbstractMessageTest.php000064400000023224151742734600017230 0ustar00mock = $this->getMockForAbstractClass('Guzzle\Http\Message\AbstractMessage'); } /** * @covers Guzzle\Http\Message\AbstractMessage::getParams */ public function testGetParams() { $request = new Request('GET', 'http://example.com'); $this->assertInstanceOf('Guzzle\\Common\\Collection', $request->getParams()); } /** * @covers Guzzle\Http\Message\AbstractMessage::addHeaders */ public function testAddHeaders() { $this->mock->setHeader('A', 'B'); $this->assertEquals($this->mock, $this->mock->addHeaders(array( 'X-Data' => '123' ))); $this->assertTrue($this->mock->hasHeader('X-Data') !== false); $this->assertTrue($this->mock->hasHeader('A') !== false); } /** * @covers Guzzle\Http\Message\AbstractMessage::setHeader */ public function testAllowsHeaderToSetAsHeader() { $h = new Header('A', 'B'); $this->mock->setHeader('A', $h); $this->assertSame($h, $this->mock->getHeader('A')); } /** * @covers Guzzle\Http\Message\AbstractMessage::getHeaders */ public function testGetHeader() { $this->mock->setHeader('Test', '123'); $this->assertEquals('123', $this->mock->getHeader('Test')); } /** * @covers Guzzle\Http\Message\AbstractMessage::getHeaders * @covers Guzzle\Http\Message\AbstractMessage::getHeader * @covers Guzzle\Http\Message\AbstractMessage::setHeaders */ public function testGetHeaders() { $this->assertSame($this->mock, $this->mock->setHeaders(array( 'a' => 'b', 'c' => 'd' ))); $this->assertEquals(array( 'a' => array('b'), 'c' => array('d') ), $this->mock->getHeaders()->getAll()); } /** * @covers Guzzle\Http\Message\AbstractMessage::hasHeader */ public function testHasHeader() { $this->assertFalse($this->mock->hasHeader('Foo')); $this->mock->setHeader('Foo', 'Bar'); $this->assertEquals(true, $this->mock->hasHeader('Foo')); $this->mock->setHeader('foo', 'yoo'); $this->assertEquals(true, $this->mock->hasHeader('Foo')); $this->assertEquals(true, $this->mock->hasHeader('foo')); $this->assertEquals(false, $this->mock->hasHeader('bar')); } /** * @covers Guzzle\Http\Message\AbstractMessage::removeHeader * @covers Guzzle\Http\Message\AbstractMessage::setHeader */ public function testRemoveHeader() { $this->mock->setHeader('Foo', 'Bar'); $this->assertEquals(true, $this->mock->hasHeader('Foo')); $this->mock->removeHeader('Foo'); $this->assertFalse($this->mock->hasHeader('Foo')); } /** * @covers Guzzle\Http\Message\AbstractMessage */ public function testHoldsCacheControlDirectives() { $mock = $this->mock; // Set a directive using a header $mock->setHeader('Cache-Control', 'max-age=100'); $this->assertEquals(100, $mock->getCacheControlDirective('max-age')); // Set a header using the directive and check that the header was updated $this->assertSame($mock, $mock->addCacheControlDirective('max-age', 80)); $this->assertEquals(80, $mock->getCacheControlDirective('max-age')); $this->assertEquals('max-age=80', $mock->getHeader('Cache-Control')); // Remove the directive $this->assertEquals($mock, $mock->removeCacheControlDirective('max-age')); $this->assertEquals('', $mock->getHeader('Cache-Control')); $this->assertEquals(null, $mock->getCacheControlDirective('max-age')); // Remove a non-existent directive $this->assertEquals($mock, $mock->removeCacheControlDirective('max-age')); // Has directive $this->assertFalse($mock->hasCacheControlDirective('max-age')); $mock->addCacheControlDirective('must-revalidate'); $this->assertTrue($mock->hasCacheControlDirective('must-revalidate')); // Make sure that it works with multiple Cache-Control headers $mock->setHeader('Cache-Control', 'must-revalidate, max-age=100'); $mock->addHeaders(array( 'Cache-Control' => 'no-cache' )); $this->assertEquals(true, $mock->getCacheControlDirective('no-cache')); $this->assertEquals(true, $mock->getCacheControlDirective('must-revalidate')); $this->assertEquals(100, $mock->getCacheControlDirective('max-age')); } public function tokenizedHeaderProvider() { return array( array('ISO-8859-1,utf-8;q=0.7,*;q=0.7"', ';', array( 'ISO-8859-1,utf-8', 'q' => array('0.7,*', '0.7"') )), array('gzip,deflate', ',', array('gzip', 'deflate')), array('en-us,en;q=0.5', ';', array( 'en-us,en', 'q' => '0.5' )) ); } /** * @covers Guzzle\Http\Message\AbstractMessage::getTokenizedHeader * @dataProvider tokenizedHeaderProvider */ public function testConvertsTokenizedHeadersToArray($string, $token, $result) { $this->mock->setHeader('test', $string); $this->assertEquals($result, $this->mock->getTokenizedHeader('test', $token)->getAll()); } /** * @covers Guzzle\Http\Message\AbstractMessage::setTokenizedHeader * @dataProvider tokenizedHeaderProvider */ public function testConvertsArrayToTokenizedHeader($string, $token, $result) { $this->mock->setTokenizedHeader('test', $result, $token); $this->assertEquals($string, $this->mock->getHeader('test')); } /** * @covers Guzzle\Http\Message\AbstractMessage::setTokenizedHeader * @expectedException InvalidArgumentException */ public function testTokenizedHeaderMustBeArrayToSet() { $this->mock->setTokenizedHeader('test', false); } /** * @covers Guzzle\Http\Message\AbstractMessage::getTokenizedHeader */ public function testReturnsNullWhenTokenizedHeaderNotFound() { $this->assertNull($this->mock->getTokenizedHeader('foo')); } /** * @covers Guzzle\Http\Message\AbstractMessage::getTokenizedHeader */ public function testMultipleTokenizedHeadersAreCombined() { $this->mock->addHeader('test', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'); $this->mock->addHeader('test', 'foo;q=123,*;q=456;q=0.7'); $this->mock->addHeader('Content-Length', 0); $this->assertEquals(array( 0 => 'ISO-8859-1,utf-8', 'q' => array('0.7,*', '0.7', '123,*', '456'), 2 => 'foo', ), $this->mock->getTokenizedHeader('test')->getAll()); } /** * @covers Guzzle\Http\Message\AbstractMessage::getHeader */ public function testReturnsNullWhenHeaderIsNotFound() { $this->assertNull($this->mock->getHeader('foo')); } /** * @covers Guzzle\Http\Message\AbstractMessage::addHeaders * @covers Guzzle\Http\Message\AbstractMessage::addHeader * @covers Guzzle\Http\Message\AbstractMessage::getHeader */ public function testAddingHeadersWithMultipleValuesUsesCaseInsensitiveKey() { $this->mock->addHeaders(array( 'test' => '123', 'Test' => '456' )); $this->mock->addHeader('TEST', '789'); $headers = array( 'test' => array('123'), 'Test' => array('456'), 'TEST' => array('789'), ); $header = $this->mock->getHeader('test'); $this->assertInstanceOf('Guzzle\Http\Message\Header', $header); $this->assertSame($header, $this->mock->getHeader('TEST')); $this->assertSame($header, $this->mock->getHeader('TeSt')); $this->assertEquals($headers, $header->raw()); } /** * @covers Guzzle\Http\Message\AbstractMessage::setHeader */ public function testSettingHeadersUsesCaseInsensitiveKey() { $this->mock->setHeader('test', '123'); $this->mock->setHeader('TEST', '456'); $this->assertEquals('456', $this->mock->getHeader('test')); $this->assertEquals('456', $this->mock->getHeader('TEST')); } /** * @covers Guzzle\Http\Message\AbstractMessage::addHeaders */ public function testAddingHeadersPreservesOriginalHeaderCase() { $this->mock->addHeaders(array( 'test' => '123', 'Test' => 'abc' )); $this->mock->addHeader('test', '456'); $this->mock->addHeader('test', '789'); $header = $this->mock->getHeader('test'); $this->assertEquals(array('123', '456', '789', 'abc'), $header->toArray()); $this->mock->addHeader('Test', 'abc'); // Add a header of a different name $this->assertEquals(array( 'test' => array('123', '456', '789'), 'Test' => array('abc', 'abc') ), $this->mock->getHeader('test')->raw()); } /** * @covers Guzzle\Http\Message\AbstractMessage */ public function testCanStoreEmptyHeaders() { $this->mock->setHeader('Content-Length', 0); $this->assertTrue($this->mock->hasHeader('Content-Length')); $this->assertEquals(0, $this->mock->getHeader('Content-Length', true)); } } tests/Guzzle/Tests/Http/Message/EntityEnclosingRequestTest.php000064400000032743151742734600020655 0ustar00 '123' )); $request->setBody('Test'); $this->assertEquals('123', $request->getHeader('X-Test')); $this->assertEquals('100-Continue', $request->getHeader('Expect')); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::setBody */ public function testCanSetBodyWithoutOverridingContentType() { $request = new EntityEnclosingRequest('PUT', 'http://test.com', array( 'Content-Type' => 'application/json' )); $request->setBody('{"a":"b"}'); $this->assertEquals('application/json', $request->getHeader('Content-Type')); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::__toString * @covers Guzzle\Http\Message\EntityEnclosingRequest::addPostFields */ public function testRequestIncludesBodyInMessage() { $request = RequestFactory::getInstance()->create('PUT', 'http://www.guzzle-project.com/', null, 'data'); $this->assertEquals("PUT / HTTP/1.1\r\n" . "Host: www.guzzle-project.com\r\n" . "User-Agent: " . Utils::getDefaultUserAgent() . "\r\n" . "Expect: 100-Continue\r\n" . "Content-Length: 4\r\n\r\n" . "data", (string) $request); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::__toString * @covers Guzzle\Http\Message\EntityEnclosingRequest::addPostFields */ public function testAddsPostFieldsAndSetsContentLength() { $request = RequestFactory::getInstance()->create('POST', 'http://www.guzzle-project.com/', null, array( 'data' => '123' )); $this->assertEquals("POST / HTTP/1.1\r\n" . "Host: www.guzzle-project.com\r\n" . "User-Agent: " . Utils::getDefaultUserAgent() . "\r\n" . "Content-Type: application/x-www-form-urlencoded\r\n\r\n" . "data=123", (string) $request); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::__toString * @covers Guzzle\Http\Message\EntityEnclosingRequest::addPostFiles * @covers Guzzle\Http\Message\EntityEnclosingRequest::addPostFields */ public function testAddsPostFilesAndSetsContentType() { $request = RequestFactory::getInstance()->create('POST', 'http://www.test.com/') ->addPostFiles(array( 'file' => __FILE__ ))->addPostFields(array( 'a' => 'b' )); $message = (string) $request; $this->assertEquals('multipart/form-data', $request->getHeader('Content-Type')); $this->assertEquals('100-Continue', $request->getHeader('Expect')); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::processPostFields */ public function testRequestBodyContainsPostFiles() { $request = RequestFactory::getInstance()->create('POST', 'http://www.test.com/'); $request->addPostFields(array( 'test' => '123' )); $this->assertContains("\r\n\r\ntest=123", (string) $request); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::processPostFields */ public function testRequestBodyAddsContentLength() { $request = RequestFactory::getInstance()->create('PUT', 'http://www.test.com/'); $request->setBody(EntityBody::factory('test')); $this->assertEquals(4, (string) $request->getHeader('Content-Length')); $this->assertFalse($request->hasHeader('Transfer-Encoding')); } /** * Tests using a Transfer-Encoding chunked entity body already set * @covers Guzzle\Http\Message\EntityEnclosingRequest::processPostFields * @covers Guzzle\Http\Message\EntityEnclosingRequest::setBody */ public function testRequestBodyDoesNotUseContentLengthWhenChunked() { $request = RequestFactory::getInstance()->create('PUT', 'http://www.test.com/'); $request->setBody(EntityBody::factory('test'), null, true); $this->assertNull($request->getHeader('Content-Length')); $this->assertTrue($request->hasHeader('Transfer-Encoding')); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::getBody * @covers Guzzle\Http\Message\EntityEnclosingRequest::setBody */ public function testRequestHasMutableBody() { $request = RequestFactory::getInstance()->create('PUT', 'http://www.guzzle-project.com/', null, 'data'); $body = $request->getBody(); $this->assertInstanceOf('Guzzle\\Http\\EntityBody', $body); $this->assertSame($body, $request->getBody()); $newBody = EntityBody::factory('foobar'); $request->setBody($newBody); $this->assertEquals('foobar', (string) $request->getBody()); $this->assertSame($newBody, $request->getBody()); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::addPostFields * @covers Guzzle\Http\Message\EntityEnclosingRequest::getPostFields * @covers Guzzle\Http\Message\EntityEnclosingRequest::getPostFiles */ public function testSetPostFields() { $request = RequestFactory::getInstance()->create('POST', 'http://www.guzzle-project.com/'); $this->assertInstanceOf('Guzzle\\Http\\QueryString', $request->getPostFields()); $fields = new QueryString(array( 'a' => 'b' )); $request->addPostFields($fields); $this->assertEquals($fields->getAll(), $request->getPostFields()->getAll()); $this->assertEquals(array(), $request->getPostFiles()); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::getPostFiles * @covers Guzzle\Http\Message\EntityEnclosingRequest::addPostFiles */ public function testSetPostFiles() { $request = RequestFactory::getInstance()->create('POST', $this->getServer()->getUrl()) ->setClient(new Client()) ->addPostFiles(array(__FILE__)) ->addPostFields(array( 'test' => 'abc' )); $this->assertEquals(array( 'file' => '@' . __FILE__, 'test' => 'abc' ), $request->getPostFields()->getAll()); $this->assertEquals(array( 'file' => __FILE__ ), $request->getPostFiles()); $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); $request->send(); $this->assertNotNull($request->getHeader('Content-Length')); $this->assertContains('multipart/form-data; boundary=', (string) $request->getHeader('Content-Type'), '-> cURL must add the boundary'); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::addPostFiles * @expectedException Guzzle\Http\Exception\RequestException */ public function testSetPostFilesThrowsExceptionWhenFileIsNotFound() { $request = RequestFactory::getInstance()->create('POST', 'http://www.guzzle-project.com/') ->addPostFiles(array( 'file' => 'filenotfound.ini' )); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::setPostField * @covers Guzzle\Http\Message\EntityEnclosingRequest::processPostFields */ public function testPostRequestsUseApplicationXwwwForUrlEncodedForArrays() { $request = RequestFactory::getInstance()->create('POST', 'http://www.guzzle-project.com/'); $request->setPostField('a', 'b'); $this->assertContains("\r\n\r\na=b", (string) $request); $this->assertEquals('application/x-www-form-urlencoded', $request->getHeader('Content-Type')); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::processPostFields */ public function testProcessMethodAddsContentType() { $request = RequestFactory::getInstance()->create('POST', 'http://www.guzzle-project.com/'); $request->setPostField('a', 'b'); $this->assertEquals('application/x-www-form-urlencoded', $request->getHeader('Content-Type')); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::processPostFields */ public function testPostRequestsUseMultipartFormDataWithFiles() { $request = RequestFactory::getInstance()->create('POST', 'http://www.guzzle-project.com/'); $request->addPostFiles(array('file' => __FILE__)); $this->assertEquals('multipart/form-data', $request->getHeader('Content-Type')); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::setBody * @covers Guzzle\Http\Message\EntityEnclosingRequest::processPostFields */ public function testCanSendMultipleRequestsUsingASingleRequestObject() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 201 Created\r\nContent-Length: 0\r\n\r\n", )); // Send the first request $request = RequestFactory::getInstance()->create('PUT', $this->getServer()->getUrl()) ->setBody('test') ->setClient(new Client()); $request->send(); $this->assertEquals(200, $request->getResponse()->getStatusCode()); // Send the second request $request->setBody('abcdefg', 'application/json', false); $request->send(); $this->assertEquals(201, $request->getResponse()->getStatusCode()); // Ensure that the same request was sent twice with different bodies $requests = $this->getServer()->getReceivedRequests(true); $this->assertEquals(2, count($requests)); $this->assertEquals(4, $requests[0]->getHeader('Content-Length', true)); $this->assertEquals(7, $requests[1]->getHeader('Content-Length', true)); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::getPostField * @covers Guzzle\Http\Message\EntityEnclosingRequest::removePostField */ public function testRemovingPostFieldRebuildsPostFields() { $request = new EntityEnclosingRequest('POST', 'http://test.com'); $request->setPostField('test', 'value'); $request->removePostField('test'); $this->assertNull($request->getPostField('test')); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::setBody */ public function testUsesChunkedTransferWhenBodyLengthCannotBeDetermined() { $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); $request = new EntityEnclosingRequest('PUT', 'http://test.com/'); $request->setBody(fopen($this->getServer()->getUrl(), 'r')); $this->assertEquals('chunked', $request->getHeader('Transfer-Encoding')); $this->assertFalse($request->hasHeader('Content-Length')); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::setBody * @expectedException Guzzle\Http\Exception\RequestException */ public function testThrowsExceptionWhenContentLengthCannotBeDeterminedAndUsingHttp1() { $request = new EntityEnclosingRequest('PUT', 'http://test.com/'); $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); $request->setProtocolVersion('1.0'); $request->setBody(fopen($this->getServer()->getUrl(), 'r')); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::getPostFiles */ public function testAllowsNestedPostData() { $request = new EntityEnclosingRequest('POST', 'http://test.com/'); $request->addPostFields(array( 'a' => array('b', 'c') )); $this->assertEquals(array( 'a' => array('b', 'c') ), $request->getPostFields()->getAll()); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::addPostFiles */ public function testIgnoresEmptyFiles() { $request = new EntityEnclosingRequest('POST', 'http://test.com/'); $request->addPostFiles(array( 'a' => '', 'b' => null, 'c' => new \stdClass() )); $this->assertEquals(array(), $request->getPostFiles()); $this->assertEquals(array(), $request->getPostFields()->getAll()); } /** * @covers Guzzle\Http\Message\EntityEnclosingRequest::addPostFields * @covers Guzzle\Http\Message\EntityEnclosingRequest::getPostFiles * @covers Guzzle\Http\Message\EntityEnclosingRequest::getPostFields */ public function testHandlesEmptyStrings() { $request = new EntityEnclosingRequest('POST', 'http://test.com/'); $request->addPostFields(array( 'a' => '', 'b' => null, 'c' => 'Foo', 'd' => '@' . __FILE__ )); $this->assertEquals(array( 'a' => '', 'b' => null, 'c' => 'Foo', 'd' => '@' . __FILE__ ), $request->getPostFields()->getAll()); $this->assertEquals(array( 'd' => __FILE__ ), $request->getPostFiles()); } } tests/Guzzle/Tests/Http/Message/HeaderComparisonTest.php000064400000005640151742734600017405 0ustar00 'Foo' ), array( 'Content-Length' => 'Foo' ), false), // Missing header array(array( 'X-Foo' => 'Bar' ), array(), array( '- X-Foo' => 'Bar' )), // Extra headers is present array(array( 'X-Foo' => 'Bar' ), array( 'X-Foo' => 'Bar', 'X-Baz' => 'Jar' ), array( '+ X-Baz' => 'Jar' )), // Header is present but must be absent array(array( '!X-Foo' => '*' ), array( 'X-Foo' => 'Bar' ), array( '++ X-Foo' => 'Bar' )), // Different values array(array( 'X-Foo' => 'Bar' ), array( 'X-Foo' => 'Baz' ), array( 'X-Foo' => 'Baz != Bar' )), // Wildcard search passes array(array( 'X-Foo' => '*' ), array( 'X-Foo' => 'Bar' ), false), // Wildcard search fails array(array( 'X-Foo' => '*' ), array(), array( '- X-Foo' => '*' )), // Ignore extra header if present array(array( 'X-Foo' => '*', '_X-Bar' => '*', ), array( 'X-Foo' => 'Baz', 'X-Bar' => 'Jar' ), false), // Ignore extra header if present and is not array(array( 'X-Foo' => '*', '_X-Bar' => '*', ), array( 'X-Foo' => 'Baz' ), false), // Case insensitive array(array( 'X-Foo' => '*', '_X-Bar' => '*', ), array( 'x-foo' => 'Baz', 'x-BAR' => 'baz' ), false), // Case insensitive with collection array(array( 'X-Foo' => '*', '_X-Bar' => '*', ), new Collection(array( 'x-foo' => 'Baz', 'x-BAR' => 'baz' )), false), ); } /** * @dataProvider filterProvider */ public function testComparesHeaders($filters, $headers, $result) { $compare = new HeaderComparison(); $this->assertEquals($result, $compare->compare($filters, $headers)); } } tests/Guzzle/Tests/Http/Message/HeaderTest.php000064400000006172151742734600015353 0ustar00 array('foo', 'Foo'), 'Zoo' => 'bar', ); public function testRawReturnsRawArray() { $i = new Header('Zoo', $this->test); $this->assertEquals(array( 'zoo' => array('foo', 'Foo'), 'Zoo' => array('bar') ), $i->raw()); } public function testStoresHeaderName() { $i = new Header('Zoo', $this->test); $this->assertEquals('Zoo', $i->getName()); } public function testConvertsToString() { $i = new Header('Zoo', $this->test); $this->assertEquals('foo, Foo, bar', (string) $i); $i->setGlue('; '); $this->assertEquals('foo; Foo; bar', (string) $i); } public function testNormalizesCases() { $h = new Header('Zoo', $this->test); $h->normalize(); $this->assertEquals(array( 'Zoo' => array('foo', 'Foo', 'bar') ), $h->raw()); } public function testCanSearchForValues() { $h = new Header('Zoo', $this->test); $this->assertTrue($h->hasValue('foo')); $this->assertTrue($h->hasValue('Foo')); $this->assertTrue($h->hasValue('bar')); $this->assertFalse($h->hasValue('moo')); $this->assertFalse($h->hasValue('FoO')); $this->assertTrue($h->hasValue('FoO', true)); } public function testIsCountable() { $h = new Header('Zoo', $this->test); $this->assertEquals(3, count($h)); } public function testCanBeIterated() { $h = new Header('Zoo', $this->test); $results = array(); foreach ($h as $key => $value) { $results[$key] = $value; } $this->assertEquals(array( 'foo', 'Foo', 'bar' ), $results); } public function testAllowsFalseyValues() { // Allows 0 $h = new Header('Foo', 0, ';'); $this->assertEquals('0', (string) $h); $this->assertEquals(1, count($h)); $this->assertEquals(';', $h->getGlue()); // Does not add a null header by default $h = new Header('Foo'); $this->assertEquals('', (string) $h); $this->assertEquals(0, count($h)); // Allows null array for a single null header $h = new Header('Foo', array(null)); $this->assertEquals('', (string) $h); $this->assertEquals(1, count($h)); // Allows empty string $h = new Header('Foo', ''); $this->assertEquals('', (string) $h); $this->assertEquals(1, count($h)); } public function testUsesHeaderNameWhenNoneIsSupplied() { $h = new Header('Foo', 'bar', ';'); $h->add('baz'); $this->assertEquals(array('Foo'), array_keys($h->raw())); } public function testCanCheckForExactHeaderValues() { $h = new Header('Foo', 'bar', ';'); $this->assertTrue($h->hasExactHeader('Foo')); $this->assertFalse($h->hasExactHeader('foo')); } } tests/Guzzle/Tests/Http/Message/RequestFactoryTest.php000064400000034676151742734600017155 0ustar00assertSame($factory, RequestFactory::getInstance()); } /** * @covers Guzzle\Http\Message\RequestFactory::create * @covers Guzzle\Http\Message\RequestFactory::getInstance */ public function testCreatesNewGetRequests() { $request = RequestFactory::getInstance()->create('GET', 'http://www.google.com/'); $this->assertInstanceOf('Guzzle\\Http\\Message\\MessageInterface', $request); $this->assertInstanceOf('Guzzle\\Http\\Message\\RequestInterface', $request); $this->assertInstanceOf('Guzzle\\Http\\Message\\Request', $request); $this->assertEquals('GET', $request->getMethod()); $this->assertEquals('http', $request->getScheme()); $this->assertEquals('http://www.google.com/', $request->getUrl()); $this->assertEquals('www.google.com', $request->getHost()); $this->assertEquals('/', $request->getPath()); $this->assertEquals('/', $request->getResourceUri()); // Create a GET request with a custom receiving body $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); $b = EntityBody::factory(); $request = RequestFactory::getInstance()->create('GET', $this->getServer()->getUrl(), null, $b); $request->setClient(new Client()); $response = $request->send(); $this->assertSame($b, $response->getBody()); } /** * @covers Guzzle\Http\Message\RequestFactory::create */ public function testCreatesPutRequests() { // Test using a string $request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/path?q=1&v=2', null, 'Data'); $this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request); $this->assertEquals('PUT', $request->getMethod()); $this->assertEquals('http', $request->getScheme()); $this->assertEquals('http://www.google.com/path?q=1&v=2', $request->getUrl()); $this->assertEquals('www.google.com', $request->getHost()); $this->assertEquals('/path', $request->getPath()); $this->assertEquals('/path?q=1&v=2', $request->getResourceUri()); $this->assertInstanceOf('Guzzle\\Http\\EntityBody', $request->getBody()); $this->assertEquals('Data', (string) $request->getBody()); unset($request); // Test using an EntityBody $request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/path?q=1&v=2', null, EntityBody::factory('Data')); $this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request); $this->assertEquals('Data', (string) $request->getBody()); // Test using a resource $resource = fopen('php://temp', 'w+'); fwrite($resource, 'Data'); $request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/path?q=1&v=2', null, $resource); $this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request); $this->assertEquals('Data', (string) $request->getBody()); // Test using an object that can be cast as a string $request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/path?q=1&v=2', null, Url::factory('http://www.example.com/')); $this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request); $this->assertEquals('http://www.example.com/', (string) $request->getBody()); } /** * @covers Guzzle\Http\Message\RequestFactory::create */ public function testCreatesHeadAndDeleteRequests() { $request = RequestFactory::getInstance()->create('DELETE', 'http://www.test.com/'); $this->assertEquals('DELETE', $request->getMethod()); $request = RequestFactory::getInstance()->create('HEAD', 'http://www.test.com/'); $this->assertEquals('HEAD', $request->getMethod()); } /** * @covers Guzzle\Http\Message\RequestFactory::create */ public function testCreatesOptionsRequests() { $request = RequestFactory::getInstance()->create('OPTIONS', 'http://www.example.com/'); $this->assertEquals('OPTIONS', $request->getMethod()); $this->assertInstanceOf('Guzzle\\Http\\Message\\Request', $request); } /** * @covers Guzzle\Http\Message\RequestFactory::create */ public function testCreatesNewPutRequestWithBody() { $request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/path?q=1&v=2', null, 'Data'); $this->assertEquals('Data', (string) $request->getBody()); } /** * @covers Guzzle\Http\Message\RequestFactory::create */ public function testCreatesNewPostRequestWithFields() { // Use an array $request = RequestFactory::getInstance()->create('POST', 'http://www.google.com/path?q=1&v=2', null, array( 'a' => 'b' )); $this->assertEquals(array('a' => 'b'), $request->getPostFields()->getAll()); unset($request); // Use a collection $request = RequestFactory::getInstance()->create('POST', 'http://www.google.com/path?q=1&v=2', null, new Collection(array( 'a' => 'b' ))); $this->assertEquals(array('a' => 'b'), $request->getPostFields()->getAll()); // Use a QueryString $request = RequestFactory::getInstance()->create('POST', 'http://www.google.com/path?q=1&v=2', null, new QueryString(array( 'a' => 'b' ))); $this->assertEquals(array('a' => 'b'), $request->getPostFields()->getAll()); $request = RequestFactory::getInstance()->create('POST', 'http://www.test.com/', null, array( 'a' => 'b', 'file' => '@' . __FILE__ )); $this->assertEquals(array( 'a' => 'b', 'file' => '@' . __FILE__ ), $request->getPostFields()->getAll()); $this->assertEquals(array( 'file' => __FILE__ ), $request->getPostFiles()); } /** * @covers Guzzle\Http\Message\RequestFactory::fromParts * @covers Guzzle\Http\Message\RequestFactory::create */ public function testCreatesFromParts() { $parts = parse_url('http://michael:123@www.google.com:8080/path?q=1&v=2'); $request = RequestFactory::getInstance()->fromParts('PUT', $parts, null, 'Data'); $this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request); $this->assertEquals('PUT', $request->getMethod()); $this->assertEquals('http', $request->getScheme()); $this->assertEquals('http://www.google.com:8080/path?q=1&v=2', $request->getUrl()); $this->assertEquals('www.google.com', $request->getHost()); $this->assertEquals('www.google.com:8080', $request->getHeader('Host')); $this->assertEquals('/path', $request->getPath()); $this->assertEquals('/path?q=1&v=2', $request->getResourceUri()); $this->assertInstanceOf('Guzzle\\Http\\EntityBody', $request->getBody()); $this->assertEquals('Data', (string)$request->getBody()); $this->assertEquals('michael', $request->getUsername()); $this->assertEquals('123', $request->getPassword()); $this->assertEquals('8080', $request->getPort()); $this->assertEquals(array( 'scheme' => 'http', 'host' => 'www.google.com', 'port' => 8080, 'path' => '/path', 'query' => 'q=1&v=2', ), parse_url($request->getUrl())); } /** * @covers Guzzle\Http\Message\RequestFactory::fromMessage * @covers Guzzle\Http\Message\RequestFactory::create */ public function testCreatesFromMessage() { $auth = base64_encode('michael:123'); $message = "PUT /path?q=1&v=2 HTTP/1.1\r\nHost: www.google.com:8080\r\nContent-Length: 4\r\nAuthorization: Basic {$auth}\r\n\r\nData"; $request = RequestFactory::getInstance()->fromMessage($message); $this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request); $this->assertEquals('PUT', $request->getMethod()); $this->assertEquals('http', $request->getScheme()); $this->assertEquals('http://www.google.com:8080/path?q=1&v=2', $request->getUrl()); $this->assertEquals('www.google.com', $request->getHost()); $this->assertEquals('www.google.com:8080', $request->getHeader('Host')); $this->assertEquals('/path', $request->getPath()); $this->assertEquals('/path?q=1&v=2', $request->getResourceUri()); $this->assertInstanceOf('Guzzle\\Http\\EntityBody', $request->getBody()); $this->assertEquals('Data', (string) $request->getBody()); $this->assertEquals('michael', $request->getUsername()); $this->assertEquals('123', $request->getPassword()); $this->assertEquals('8080', $request->getPort()); // Test passing a blank message returns false $this->assertFalse($request = RequestFactory::getInstance()->fromMessage('')); // Test passing a url with no port $message = "PUT /path?q=1&v=2 HTTP/1.1\r\nHost: www.google.com\r\nContent-Length: 4\r\nAuthorization: Basic {$auth}\r\n\r\nData"; $request = RequestFactory::getInstance()->fromMessage($message); $this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request); $this->assertEquals('PUT', $request->getMethod()); $this->assertEquals('http', $request->getScheme()); $this->assertEquals('http://www.google.com/path?q=1&v=2', $request->getUrl()); $this->assertEquals('www.google.com', $request->getHost()); $this->assertEquals('/path', $request->getPath()); $this->assertEquals('/path?q=1&v=2', $request->getResourceUri()); $this->assertInstanceOf('Guzzle\\Http\\EntityBody', $request->getBody()); $this->assertEquals('Data', (string)$request->getBody()); $this->assertEquals('michael', $request->getUsername()); $this->assertEquals('123', $request->getPassword()); $this->assertEquals(80, $request->getPort()); } /** * @covers Guzzle\Http\Message\RequestFactory::create */ public function testCreatesNewTraceRequest() { $request = RequestFactory::getInstance()->create('TRACE', 'http://www.google.com/'); $this->assertFalse($request instanceof \Guzzle\Http\Message\EntityEnclosingRequest); $this->assertEquals('TRACE', $request->getMethod()); } /** * @covers Guzzle\Http\Message\RequestFactory::create */ public function testCreatesProperTransferEncodingRequests() { $request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/', array( 'Transfer-Encoding' => 'chunked' ), 'hello'); $this->assertEquals('chunked', $request->getHeader('Transfer-Encoding')); $this->assertFalse($request->hasHeader('Content-Length')); } /** * @covers Guzzle\Http\Message\RequestFactory::fromMessage */ public function testProperlyDealsWithDuplicateHeaders() { $parser = new MessageParser(); $message = "POST / http/1.1\r\n" . "DATE:Mon, 09 Sep 2011 23:36:00 GMT\r\n" . "host:host.foo.com\r\n" . "ZOO:abc\r\n" . "ZOO:123\r\n" . "ZOO:HI\r\n" . "zoo:456\r\n\r\n"; $parts = $parser->parseRequest($message); $this->assertEquals(array ( 'DATE' => 'Mon, 09 Sep 2011 23:36:00 GMT', 'host' => 'host.foo.com', 'ZOO' => array('abc', '123', 'HI'), 'zoo' => '456', ), $parts['headers']); $request = RequestFactory::getInstance()->fromMessage($message); $this->assertEquals(array( 'ZOO' => array('abc', '123', 'HI'), 'zoo' => array('456') ), $request->getHeader('zoo')->raw()); } /** * @covers Guzzle\Http\Message\RequestFactory::fromMessage * @covers Guzzle\Http\Message\RequestFactory::create */ public function testCreatesHttpMessagesWithBodiesAndNormalizesLineEndings() { $message = "POST / http/1.1\r\n" . "Content-Type:application/x-www-form-urlencoded; charset=utf8\r\n" . "Date:Mon, 09 Sep 2011 23:36:00 GMT\r\n" . "Host:host.foo.com\r\n\r\n" . "foo=bar"; $request = RequestFactory::getInstance()->fromMessage($message); $this->assertEquals('application/x-www-form-urlencoded; charset=utf8', (string) $request->getHeader('Content-Type')); $this->assertEquals('foo=bar', (string) $request->getBody()); $message = "POST / http/1.1\n" . "Content-Type:application/x-www-form-urlencoded; charset=utf8\n" . "Date:Mon, 09 Sep 2011 23:36:00 GMT\n" . "Host:host.foo.com\n\n" . "foo=bar"; $request = RequestFactory::getInstance()->fromMessage($message); $this->assertEquals('foo=bar', (string) $request->getBody()); $message = "PUT / HTTP/1.1\r\nContent-Length: 0\r\n\r\n"; $request = RequestFactory::getInstance()->fromMessage($message); $this->assertTrue($request->hasHeader('Content-Length')); $this->assertEquals(0, (string) $request->getHeader('Content-Length')); } /** * @covers Guzzle\Http\Message\RequestFactory::create */ public function testHandlesChunkedTransferEncoding() { $request = RequestFactory::getInstance()->create('PUT', 'http://www.foo.com/', array( 'Transfer-Encoding' => 'chunked' ), 'Test'); $this->assertFalse($request->hasHeader('Content-Length')); $this->assertEquals('chunked', $request->getHeader('Transfer-Encoding')); $request = RequestFactory::getInstance()->create('POST', 'http://www.foo.com/', array( 'transfer-encoding' => 'chunked' ), array( 'foo' => 'bar' )); $this->assertFalse($request->hasHeader('Content-Length')); $this->assertEquals('chunked', $request->getHeader('Transfer-Encoding')); } } tests/Guzzle/Tests/Http/Message/RequestTest.php000064400000076021151742734600015613 0ustar00client = new Client($this->getServer()->getUrl()); $this->request = $this->client->get(); } public function tearDown() { unset($this->request); unset($this->client); } /** * @covers Guzzle\Http\Message\Request::__construct */ public function testConstructorBuildsRequestWithArrayHeaders() { // Test passing an array of headers $request = new Request('GET', 'http://www.guzzle-project.com/', array( 'foo' => 'bar' )); $this->assertEquals('GET', $request->getMethod()); $this->assertEquals('http://www.guzzle-project.com/', $request->getUrl()); $this->assertEquals('bar', $request->getHeader('foo')); } /** * @covers Guzzle\Http\Message\Request::getAllEvents */ public function testDescribesEvents() { $this->assertInternalType('array', Request::getAllEvents()); } /** * @covers Guzzle\Http\Message\Request::__construct */ public function testConstructorBuildsRequestWithCollectionHeaders() { $request = new Request('GET', 'http://www.guzzle-project.com/', new Collection(array( 'foo' => 'bar' ))); $this->assertEquals('bar', $request->getHeader('foo')); } /** * @covers Guzzle\Http\Message\Request::__construct */ public function testConstructorBuildsRequestWithNoHeaders() { $request = new Request('GET', 'http://www.guzzle-project.com/', null); $this->assertFalse($request->hasHeader('foo')); } /** * @covers Guzzle\Http\Message\Request::__construct */ public function testConstructorHandlesBasicAuth() { $auth = base64_encode('michael:foo'); $request = new Request('GET', 'http://www.guzzle-project.com/', array( 'Authorization' => 'Basic ' . $auth )); $this->assertEquals('michael', $request->getUserName()); $this->assertEquals('foo', $request->getPassword()); $this->assertEquals('Basic ' . $auth, (string) $request->getHeader('Authorization')); } /** * @covers Guzzle\Http\Message\Request::__construct */ public function testConstructorHandlesNonBasicAuth() { $request = new Request('GET', 'http://www.guzzle-project.com/', array( 'Authorization' => 'Foo bar' )); $this->assertNull($request->getUserName()); $this->assertNull($request->getPassword()); $this->assertEquals('Foo bar', (string) $request->getHeader('Authorization')); } /** * @covers Guzzle\Http\Message\Request::__toString * @covers Guzzle\Http\Message\Request::getRawHeaders * @covers Guzzle\Http\Message\AbstractMessage::getHeaderString */ public function testRequestsCanBeConvertedToRawMessageStrings() { $auth = base64_encode('michael:123'); $message = "PUT /path?q=1&v=2 HTTP/1.1\r\n" . "Host: www.google.com\r\n" . "Authorization: Basic {$auth}\r\n" . "User-Agent: " . Utils::getDefaultUserAgent() . "\r\n" . "Expect: 100-Continue\r\n" . "Content-Length: 4\r\n\r\nData"; $request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/path?q=1&v=2', array( 'Authorization' => 'Basic ' . $auth ), 'Data'); $this->assertEquals($message, $request->__toString()); } /** * Add authorization after the fact and see that it was put in the message * * @covers Guzzle\Http\Message\Request::__toString * @covers Guzzle\Http\Message\Request::getRawHeaders */ public function testRequestStringsIncludeAuth() { $auth = base64_encode('michael:123'); $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); $request = RequestFactory::getInstance()->create('PUT', $this->getServer()->getUrl(), null, 'Data') ->setClient($this->client) ->setAuth('michael', '123', CURLAUTH_BASIC); $request->send(); $this->assertContains('Authorization: Basic ' . $auth, (string) $request); } /** * @covers Guzzle\Http\Message\Request::getEventDispatcher */ public function testGetEventDispatcher() { $d = $this->request->getEventDispatcher(); $this->assertInstanceOf('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface', $d); $this->assertEquals($d, $this->request->getEventDispatcher()); } /** * @covers Guzzle\Http\Message\Request::getClient * @covers Guzzle\Http\Message\Request::setClient */ public function testRequestsManageClients() { $request = new Request('GET', 'http://test.com'); $this->assertNull($request->getClient()); $request->setClient($this->client); $this->assertSame($this->client, $request->getClient()); } /** * @covers Guzzle\Http\Message\Request::send * @expectedException RuntimeException * @expectedExceptionMessage A client must be set on the request */ public function testRequestsRequireClients() { $request = new Request('GET', 'http://test.com'); $request->send(); } /** * @covers Guzzle\Http\Message\Request::send * @covers Guzzle\Http\Message\Request::getResponse * @covers Guzzle\Http\Message\Request::setResponse * @covers Guzzle\Http\Message\Request::processResponse * @covers Guzzle\Http\Message\Request::getResponseBody */ public function testSend() { $response = new Response(200, array( 'Content-Length' => 3 ), 'abc'); $this->request->setResponse($response, true); $r = $this->request->send(); $this->assertSame($response, $r); $this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $this->request->getResponse()); $this->assertSame($r, $this->request->getResponse()); $this->assertEquals('complete', $this->request->getState()); } /** * @covers Guzzle\Http\Message\Request::getResponse * @covers Guzzle\Http\Message\Request::setResponse * @covers Guzzle\Http\Message\Request::processResponse * @covers Guzzle\Http\Message\Request::getResponseBody */ public function testGetResponse() { $this->assertNull($this->request->getResponse()); $response = new Response(200, array( 'Content-Length' => 3 ), 'abc'); $this->request->setResponse($response); $this->assertEquals($response, $this->request->getResponse()); $request = new Request('GET', 'http://www.google.com/'); $request->setResponse($response, true); $request->setState(RequestInterface::STATE_COMPLETE); $requestResponse = $request->getResponse(); $this->assertSame($response, $requestResponse); // Try again, making sure it's still the same response $this->assertSame($requestResponse, $request->getResponse()); $response = new Response(204); $request = new Request('GET', 'http://www.google.com/'); $request->setResponse($response, true); $request->setState('complete'); $requestResponse = $request->getResponse(); $this->assertSame($response, $requestResponse); $this->assertInstanceOf('Guzzle\\Http\\EntityBody', $response->getBody()); } /** * @covers Guzzle\Http\Message\Request::getResponse * @covers Guzzle\Http\Message\Request::processResponse * @covers Guzzle\Http\Message\Request::getResponseBody */ public function testRequestThrowsExceptionOnBadResponse() { $response = new Response(404, array( 'Content-Length' => 3 ), 'abc'); $request = new Request('GET', 'http://www.google.com/'); $request->setClient($this->client); try { $request->setResponse($response, true); $request->send(); $this->fail('Expected exception not thrown'); } catch (BadResponseException $e) { $this->assertInstanceOf('Guzzle\\Http\\Message\\RequestInterface', $e->getRequest()); $this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $e->getResponse()); $this->assertContains('Client error response', $e->getMessage()); } } /** * @covers Guzzle\Http\Message\Request::getQuery */ public function testManagesQuery() { $this->assertInstanceOf('Guzzle\\Http\\QueryString', $this->request->getQuery()); $this->request->getQuery()->set('test', '123'); $this->assertEquals('?test=123', $this->request->getQuery(true)); } /** * @covers Guzzle\Http\Message\Request::getMethod */ public function testRequestHasMethod() { $this->assertEquals('GET', $this->request->getMethod()); } /** * @covers Guzzle\Http\Message\Request::getScheme * @covers Guzzle\Http\Message\Request::setScheme */ public function testRequestHasScheme() { $this->assertEquals('http', $this->request->getScheme()); $this->assertEquals($this->request, $this->request->setScheme('https')); $this->assertEquals('https', $this->request->getScheme()); } /** * @covers Guzzle\Http\Message\Request::getHost * @covers Guzzle\Http\Message\Request::setHost */ public function testRequestHasHost() { $this->assertEquals('127.0.0.1', $this->request->getHost()); $this->assertEquals('127.0.0.1:8124', (string) $this->request->getHeader('Host')); $this->assertSame($this->request, $this->request->setHost('www2.google.com')); $this->assertEquals('www2.google.com', $this->request->getHost()); $this->assertEquals('www2.google.com:8124', (string) $this->request->getHeader('Host')); $this->assertSame($this->request, $this->request->setHost('www.test.com:8081')); $this->assertEquals('www.test.com', $this->request->getHost()); $this->assertEquals(8081, $this->request->getPort()); } /** * @covers Guzzle\Http\Message\Request::getProtocolVersion * @covers Guzzle\Http\Message\Request::setProtocolVersion */ public function testRequestHasProtocol() { $this->assertEquals('1.1', $this->request->getProtocolVersion()); $this->assertEquals($this->request, $this->request->setProtocolVersion('1.1')); $this->assertEquals('1.1', $this->request->getProtocolVersion()); $this->assertEquals($this->request, $this->request->setProtocolVersion('1.0')); $this->assertEquals('1.0', $this->request->getProtocolVersion()); } /** * @covers Guzzle\Http\Message\Request::getPath * @covers Guzzle\Http\Message\Request::setPath */ public function testRequestHasPath() { $this->assertEquals('/', $this->request->getPath()); $this->assertEquals($this->request, $this->request->setPath('/index.html')); $this->assertEquals('/index.html', $this->request->getPath()); $this->assertEquals($this->request, $this->request->setPath('index.html')); $this->assertEquals('/index.html', $this->request->getPath()); } /** * @covers Guzzle\Http\Message\Request::getPort * @covers Guzzle\Http\Message\Request::setPort */ public function testRequestHasPort() { $this->assertEquals(8124, $this->request->getPort()); $this->assertEquals('127.0.0.1:8124', $this->request->getHeader('Host')); $this->assertEquals($this->request, $this->request->setPort('8080')); $this->assertEquals('8080', $this->request->getPort()); $this->assertEquals('127.0.0.1:8080', $this->request->getHeader('Host')); $this->request->setPort(80); $this->assertEquals('127.0.0.1', $this->request->getHeader('Host')); } /** * @covers Guzzle\Http\Message\Request::getUsername * @covers Guzzle\Http\Message\Request::getPassword * @covers Guzzle\Http\Message\Request::setAuth */ public function testRequestHandlesAuthorization() { // Uninitialized auth $this->assertEquals(null, $this->request->getUsername()); $this->assertEquals(null, $this->request->getPassword()); // Set an auth $this->assertSame($this->request, $this->request->setAuth('michael', '123')); $this->assertEquals('michael', $this->request->getUsername()); $this->assertEquals('123', $this->request->getPassword()); // Remove the auth $this->request->setAuth(false); $this->assertEquals(null, $this->request->getUsername()); $this->assertEquals(null, $this->request->getPassword()); // Make sure that the cURL based auth works too $request = new Request('GET', $this->getServer()->getUrl()); $request->setAuth('michael', 'password', CURLAUTH_DIGEST); $this->assertEquals('michael:password', $request->getCurlOptions()->get(CURLOPT_USERPWD)); $this->assertEquals(CURLAUTH_DIGEST, $request->getCurlOptions()->get(CURLOPT_HTTPAUTH)); } /** * @covers Guzzle\Http\Message\Request::getResourceUri */ public function testGetResourceUri() { $this->assertEquals('/', $this->request->getResourceUri()); $this->request->setPath('/index.html'); $this->assertEquals('/index.html', $this->request->getResourceUri()); $this->request->getQuery()->add('v', '1'); $this->assertEquals('/index.html?v=1', $this->request->getResourceUri()); } /** * @covers Guzzle\Http\Message\Request::getUrl * @covers Guzzle\Http\Message\Request::setUrl */ public function testRequestHasMutableUrl() { $url = 'http://www.test.com:8081/path?q=123#fragment'; $u = Url::factory($url); $this->assertSame($this->request, $this->request->setUrl($url)); $this->assertEquals($url, $this->request->getUrl()); $this->assertSame($this->request, $this->request->setUrl($u)); $this->assertEquals($url, $this->request->getUrl()); } /** * @covers Guzzle\Http\Message\Request::setState * @covers Guzzle\Http\Message\Request::getState */ public function testRequestHasState() { $this->assertEquals(RequestInterface::STATE_NEW, $this->request->getState()); $this->request->setState(RequestInterface::STATE_TRANSFER); $this->assertEquals(RequestInterface::STATE_TRANSFER, $this->request->getState()); } /** * @covers Guzzle\Http\Message\Request::setResponse * @covers Guzzle\Http\Message\Request::getResponse * @covers Guzzle\Http\Message\Request::getState */ public function testSetManualResponse() { $response = new Response(200, array( 'Date' => 'Sat, 16 Oct 2010 17:27:14 GMT', 'Expires' => '-1', 'Cache-Control' => 'private, max-age=0', 'Content-Type' => 'text/html; charset=ISO-8859-1', ), 'response body'); $this->assertSame($this->request, $this->request->setResponse($response), '-> setResponse() must use a fluent interface'); $this->assertEquals('complete', $this->request->getState(), '-> setResponse() must change the state of the request to complete'); $this->assertSame($response, $this->request->getResponse(), '-> setResponse() must set the exact same response that was passed in to it'); } /** * @covers Guzzle\Http\Message\Request::setResponseBody */ public function testRequestCanHaveManuallySetResponseBody() { $file = __DIR__ . '/../../TestData/temp.out'; if (file_exists($file)) { unlink($file); } $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndata"); $request = RequestFactory::getInstance()->create('GET', $this->getServer()->getUrl()); $request->setClient($this->client); $request->setResponseBody(EntityBody::factory(fopen($file, 'w+'))); $request->send(); $this->assertTrue(file_exists($file)); unlink($file); $this->assertEquals('data', $request->getResponse()->getBody(true)); } /** * @covers Guzzle\Http\Message\Request::isResponseBodyRepeatable */ public function testDeterminesIfResponseBodyRepeatable() { // The default stream created for responses is seekable $request = RequestFactory::getInstance()->create('GET', 'http://localhost:' . $this->getServer()->getPort()); $this->assertTrue($request->isResponseBodyRepeatable()); // This should return false because an HTTP stream is not seekable $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndata"); $request->setResponseBody(EntityBody::factory(fopen($this->getServer()->getUrl(), true))); $this->assertFalse($request->isResponseBodyRepeatable()); } /** * @covers Guzzle\Http\Message\Request::canCache */ public function testDeterminesIfCanCacheRequest() { $this->assertTrue(RequestFactory::getInstance()->fromMessage( "GET / HTTP/1.1\r\nHost: www.test.com\r\nCache-Control: no-cache, max-age=120\r\n\r\n" )->canCache()); $this->assertTrue(RequestFactory::getInstance()->fromMessage( "HEAD / HTTP/1.1\r\nHost: www.test.com\r\nCache-Control: no-cache, max-age=120\r\n\r\n" )->canCache()); $this->assertFalse(RequestFactory::getInstance()->fromMessage( "HEAD / HTTP/1.1\r\nHost: www.test.com\r\nCache-Control: no-cache, max-age=120, no-store\r\n\r\n" )->canCache()); $this->assertFalse(RequestFactory::getInstance()->fromMessage( "POST / HTTP/1.1\r\nHost: www.test.com\r\n\r\n" )->canCache()); $this->assertFalse(RequestFactory::getInstance()->fromMessage( "PUT / HTTP/1.1\r\nHost: www.test.com\r\nCache-Control: no-cache, max-age=120\r\n\r\n" )->canCache()); } /** * @covers Guzzle\Http\Message\Request */ public function testHoldsCookies() { $cookie = $this->request->getCookie(); $this->assertInstanceOf('Guzzle\\Http\\Cookie', $this->request->getCookie()); // Ensure that the cookie will not affect the request $this->assertNull($this->request->getCookie('test')); $cookie->set('test', 'abc'); $this->assertNull($this->request->getCookie('test')); // Set a cookie $this->assertSame($this->request, $this->request->addCookie('test', 'abc')); $this->assertEquals('abc', $this->request->getCookie('test')); // Unset the cookies by setting the Cookie header to null $this->request->setHeader('Cookie', null); $this->assertNull($this->request->getCookie('test')); // Set and remove a cookie $this->assertSame($this->request, $this->request->addCookie('test', 'abc')); $this->assertEquals('abc', $this->request->getCookie('test')); $this->assertSame($this->request, $this->request->removeCookie('test')); $this->assertNull($this->request->getCookie('test')); // Remove the cookie header $this->assertSame($this->request, $this->request->addCookie('test', 'abc')); $this->request->removeHeader('Cookie'); $this->assertEquals('', (string)$this->request->getCookie()); // Set the cookie using a cookie object $this->assertSame($this->request, $this->request->setCookie($cookie)); $this->assertEquals($cookie->getAll(), $this->request->getCookie()->getAll()); // Set the cookie using an array $this->assertSame($this->request, $this->request->setCookie(array( 'test' => 'def' ))); $this->assertEquals(array( 'test' => 'def' ), $this->request->getCookie()->getAll()); // Test using an invalid value try { $this->request->setCookie('a'); $this->fail('Did not throw expected exception when passing invalid value'); } catch (\InvalidArgumentException $e) { } } /** * @covers Guzzle\Http\Message\Request::processResponse * @expectedException Guzzle\Http\Exception\RequestException * @expectedExceptionMessage Error completing request */ public function testRequestThrowsExceptionWhenSetToCompleteWithNoResponse() { $this->request->setState(RequestInterface::STATE_COMPLETE); } /** * @covers Guzzle\Http\Message\Request::__clone */ public function testClonedRequestsUseNewInternalState() { $p = new ExponentialBackoffPlugin(); $this->request->getEventDispatcher()->addSubscriber($p); $h = $this->request->getHeader('Host'); $r = clone $this->request; $this->assertEquals(RequestInterface::STATE_NEW, $r->getState()); $this->assertNotSame($r->getQuery(), $this->request->getQuery()); $this->assertNotSame($r->getCurlOptions(), $this->request->getCurlOptions()); $this->assertNotSame($r->getEventDispatcher(), $this->request->getEventDispatcher()); $this->assertEquals($r->getHeaders(), $this->request->getHeaders()); $this->assertNotSame($h, $r->getHeader('Host')); $this->assertNotSame($r->getParams(), $this->request->getParams()); $this->assertNull($r->getParams()->get('queued_response')); $this->assertTrue($this->request->getEventDispatcher()->hasListeners('request.sent')); } /** * @covers Guzzle\Http\Message\Request::changedHeader * @covers Guzzle\Http\Message\Request::setHeader */ public function testCatchesAllHostHeaderChanges() { // Tests setting using headers $this->request->setHeader('Host', 'www.abc.com'); $this->assertEquals('www.abc.com', $this->request->getHost()); $this->assertEquals('www.abc.com:8124', $this->request->getHeader('Host')); $this->assertEquals(8124, $this->request->getPort()); // Tests setting using setHost() $this->request->setHost('abc.com'); $this->assertEquals('abc.com', $this->request->getHost()); $this->assertEquals('abc.com:8124', $this->request->getHeader('Host')); $this->assertEquals(8124, $this->request->getPort()); // Tests setting with a port $this->request->setHost('abc.com:8081'); $this->assertEquals('abc.com', $this->request->getHost()); $this->assertEquals('abc.com:8081', $this->request->getHeader('Host')); $this->assertEquals(8081, $this->request->getPort()); // Tests setting with a port using the Host header $this->request->setHeader('Host', 'solr.com:8983'); $this->assertEquals('solr.com', $this->request->getHost()); $this->assertEquals('solr.com:8983', (string) $this->request->getHeader('Host')); $this->assertEquals(8983, $this->request->getPort()); // Tests setting with an inferred 443 port using the Host header $this->request->setScheme('https'); $this->request->setHeader('Host', 'solr.com'); $this->assertEquals('solr.com', $this->request->getHost()); $this->assertEquals('solr.com:8983', (string) $this->request->getHeader('Host')); $this->assertEquals(8983, $this->request->getPort()); } /** * @covers Guzzle\Http\Message\Request::setUrl */ public function testRecognizesBasicAuthCredentialsInUrls() { $this->request->setUrl('http://michael:test@test.com/'); $this->assertEquals('michael', $this->request->getUsername()); $this->assertEquals('test', $this->request->getPassword()); } /** * This test launches a dummy Guzzle\Http\Server\Server object that listens * for incoming requests. The server allows us to test how cURL sends * requests and receives responses. We can validate the request structure * and whether or not the response was interpreted correctly. * * @covers Guzzle\Http\Message\Request */ public function testRequestCanBeSentUsingCurl() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nExpires: Thu, 01 Dec 1994 16:00:00 GMT\r\nConnection: close\r\n\r\ndata", "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nExpires: Thu, 01 Dec 1994 16:00:00 GMT\r\nConnection: close\r\n\r\ndata", "HTTP/1.1 404 Not Found\r\nContent-Encoding: application/xml\r\nContent-Length: 48\r\n\r\nFile not found" )); $request = RequestFactory::getInstance()->create('GET', $this->getServer()->getUrl()); $request->setClient($this->client); $response = $request->send(); $this->assertEquals('data', $response->getBody(true)); $this->assertEquals(200, (int) $response->getStatusCode()); $this->assertEquals('OK', $response->getReasonPhrase()); $this->assertEquals(4, $response->getContentLength()); $this->assertEquals('Thu, 01 Dec 1994 16:00:00 GMT', $response->getExpires()); // Test that the same handle can be sent twice without setting state to new $response2 = $request->send(); $this->assertNotSame($response, $response2); try { $request = RequestFactory::getInstance()->create('GET', $this->getServer()->getUrl() . 'index.html'); $request->setClient($this->client); $response = $request->send(); $this->fail('Request did not receive a 404 response'); } catch (BadResponseException $e) { } $requests = $this->getServer()->getReceivedRequests(true); $messages = $this->getServer()->getReceivedRequests(false); $port = $this->getServer()->getPort(); $userAgent = Utils::getDefaultUserAgent(); $this->assertEquals('127.0.0.1:' . $port, $requests[0]->getHeader('Host')); $this->assertEquals('127.0.0.1:' . $port, $requests[1]->getHeader('Host')); $this->assertEquals('127.0.0.1:' . $port, $requests[2]->getHeader('Host')); $this->assertEquals('/', $requests[0]->getPath()); $this->assertEquals('/', $requests[1]->getPath()); $this->assertEquals('/index.html', $requests[2]->getPath()); $parts = explode("\r\n", $messages[0]); $this->assertEquals('GET / HTTP/1.1', $parts[0]); $parts = explode("\r\n", $messages[1]); $this->assertEquals('GET / HTTP/1.1', $parts[0]); $parts = explode("\r\n", $messages[2]); $this->assertEquals('GET /index.html HTTP/1.1', $parts[0]); } /** * @covers Guzzle\Http\Message\Request::onRequestError */ public function testThrowsExceptionsWhenUnsuccessfulResponseIsReceivedByDefault() { $this->getServer()->flush(); $this->getServer()->enqueue("HTTP/1.1 404 Not found\r\nContent-Length: 0\r\n\r\n"); try { $request = $this->client->get('/index.html'); $response = $request->send(); $this->fail('Request did not receive a 404 response'); } catch (BadResponseException $e) { $this->assertContains('Client error response', $e->getMessage()); $this->assertContains('[status code] 404', $e->getMessage()); $this->assertContains('[reason phrase] Not found', $e->getMessage()); } } /** * @covers Guzzle\Http\Message\Request::onRequestError */ public function testCanShortCircuitErrorHandling() { $request = $this->request; $response = new Response(404); $request->setResponse($response, true); $out = ''; $that = $this; $request->getEventDispatcher()->addListener('request.error', function($event) use (&$out, $that) { $out .= $event['request'] . "\n" . $event['response'] . "\n"; $event->stopPropagation(); }); $request->send(); $this->assertContains((string) $request, $out); $this->assertContains((string) $request->getResponse(), $out); $this->assertSame($response, $request->getResponse()); } /** * @covers Guzzle\Http\Message\Request::processResponse * @covers Guzzle\Http\Message\Request::onRequestError */ public function testCanOverrideUnsuccessfulResponses() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 404 NOT FOUND\r\n" . "Content-Length: 0\r\n" . "\r\n", "HTTP/1.1 200 OK\r\n" . "Content-Length: 0\r\n" . "\r\n" )); $newResponse = null; $request = $this->request; $request->getEventDispatcher()->addListener('request.error', function($event) use (&$newResponse) { if ($event['response']->getStatusCode() == 404) { $newRequest = clone $event['request']; $newResponse = $newRequest->send(); // Override the original response and bypass additional response processing $event['response'] = $newResponse; // Call $event['request']->setResponse($newResponse); to re-apply events $event->stopPropagation(); } }); $request->send(); $this->assertEquals(200, $request->getResponse()->getStatusCode()); $this->assertSame($newResponse, $request->getResponse()); $this->assertEquals(2, count($this->getServer()->getReceivedRequests())); } /** * @covers Guzzle\Http\Message\Request */ public function testCanRetrieveUrlObject() { $request = new Request('GET', 'http://www.example.com/foo?abc=d'); $this->assertInstanceOf('Guzzle\Http\Url', $request->getUrl(true)); $this->assertEquals('http://www.example.com/foo?abc=d', $request->getUrl()); $this->assertEquals('http://www.example.com/foo?abc=d', (string) $request->getUrl(true)); } /** * Users sometimes want to use a custom stream when receiving a response body. * Because of the various potential for retrying failed requests, the stream * specified by the user should only be written to in the event that a * successful response was received. Otherwise, a new temp stream is created * to store the body of the failed request. * * @covers Guzzle\Http\Message\Request::receiveResponseHeader */ public function testReceivingUnsuccessfulResponseUsesOtherResponseBody() { $request = new Request('GET', $this->getServer()->getUrl()); $body = EntityBody::factory(); $request->setResponseBody($body); $request->receiveResponseHeader('HTTP/1.1 503 Service Unavailable'); $this->assertNotSame($body, $request->getResponse()->getBody()); } } tests/Guzzle/Tests/Http/Message/ResponseTest.php000064400000063441151742734600015763 0ustar00response = new Response(200, new Collection(array( 'Accept-Ranges' => 'bytes', 'Age' => '12', 'Allow' => 'GET, HEAD', 'Cache-Control' => 'no-cache', 'Content-Encoding' => 'gzip', 'Content-Language' => 'da', 'Content-Length' => '348', 'Content-Location' => '/index.htm', 'Content-Disposition' => 'attachment; filename=fname.ext', 'Content-MD5' => 'Q2hlY2sgSW50ZWdyaXR5IQ==', 'Content-Range' => 'bytes 21010-47021/47022', 'Content-Type' => 'text/html; charset=utf-8', 'Date' => 'Tue, 15 Nov 1994 08:12:31 GMT', 'ETag' => '737060cd8c284d8af7ad3082f209582d', 'Expires' => 'Thu, 01 Dec 1994 16:00:00 GMT', 'Last-Modified' => 'Tue, 15 Nov 1994 12:45:26 GMT', 'Location' => 'http://www.w3.org/pub/WWW/People.html', 'Pragma' => 'no-cache', 'Proxy-Authenticate' => 'Basic', 'Retry-After' => '120', 'Server' => 'Apache/1.3.27 (Unix) (Red-Hat/Linux)', 'Set-Cookie' => 'UserID=JohnDoe; Max-Age=3600; Version=1', 'Trailer' => 'Max-Forwards', 'Transfer-Encoding' => 'chunked', 'Vary' => '*', 'Via' => '1.0 fred, 1.1 nowhere.com (Apache/1.1)', 'Warning' => '199 Miscellaneous warning', 'WWW-Authenticate' => 'Basic' )), 'body'); } /** * @covers Guzzle\Http\Message\Response::__construct */ public function testConstructor() { $params = new Collection(); $body = EntityBody::factory(''); $response = new Response(200, $params, $body); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals($body, $response->getBody()); $this->assertEquals('OK', $response->getReasonPhrase()); $this->assertEquals("HTTP/1.1 200 OK\r\n\r\n", $response->getRawHeaders()); // Make sure Content-Length is set automatically $response = new Response(200, $params); $this->assertEquals("HTTP/1.1 200 OK\r\n\r\n", $response->getRawHeaders()); // Pass bodies to the response $response = new Response(200, null, 'data'); $this->assertInstanceOf('Guzzle\\Http\\EntityBody', $response->getBody()); $response = new Response(200, null, EntityBody::factory('data')); $this->assertInstanceOf('Guzzle\\Http\\EntityBody', $response->getBody()); $this->assertEquals('data', $response->getBody(true)); // Make sure the proper exception is thrown try { //$response = new Response(200, null, array('foo' => 'bar')); //$this->fail('Response did not throw exception when passing invalid body'); } catch (HttpException $e) { } // Make sure the proper exception is thrown when sending invalid code try { $response = new Response(2); $this->fail('Response did not throw exception when passing invalid $code'); } catch (BadResponseException $e) { } // Make sure the proper exception is thrown when sending invalid headers try { $response = new Response(200, 'adidas'); $this->fail('Response did not throw exception when passing invalid $headers'); } catch (BadResponseException $e) { } } /** * @covers Guzzle\Http\Message\Response::__toString */ public function test__toString() { $response = new Response(200); $this->assertEquals("HTTP/1.1 200 OK\r\n\r\n", (string) $response); // Add another header $response = new Response(200, array( 'X-Test' => 'Guzzle' )); $this->assertEquals("HTTP/1.1 200 OK\r\nX-Test: Guzzle\r\n\r\n", (string) $response); $response = new Response(200, array( 'Content-Length' => 4 ), 'test'); $this->assertEquals("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ntest", (string) $response); } /** * @covers Guzzle\Http\Message\Response::fromMessage */ public function testFactory() { $response = Response::fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ntest"); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('OK', $response->getReasonPhrase()); $this->assertEquals(4, $response->getContentLength()); $this->assertEquals('test', $response->getBody(true)); // Make sure that automatic Content-Length works $response = Response::fromMessage("HTTP/1.1 200 OK\r\nContent-Length: x\r\n\r\ntest"); $this->assertEquals(4, $response->getContentLength()); $this->assertEquals('test', $response->getBody(true)); } /** * @covers Guzzle\Http\Message\Response::fromMessage */ public function testFactoryRequiresMessage() { $this->assertFalse(Response::fromMessage('')); } /** * @covers Guzzle\Http\Message\Response::getBody */ public function testGetBody() { $body = EntityBody::factory(''); $response = new Response(403, new Collection(), $body); $this->assertEquals($body, $response->getBody()); } /** * @covers Guzzle\Http\Message\Response::getStatusCode */ public function testManagesStatusCode() { $response = new Response(403); $this->assertEquals(403, $response->getStatusCode()); } /** * @covers Guzzle\Http\Message\Response::getMessage */ public function testGetMessage() { $response = new Response(200, new Collection(array( 'Content-Length' => 4 )), 'body'); $this->assertEquals("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nbody", $response->getMessage()); } /** * @covers Guzzle\Http\Message\Response::getRawHeaders */ public function testGetRawHeaders() { $response = new Response(200, new Collection(array( 'Keep-Alive' => 155, 'User-Agent' => 'Guzzle', 'Content-Length' => 4 )), 'body'); $this->assertEquals("HTTP/1.1 200 OK\r\nKeep-Alive: 155\r\nUser-Agent: Guzzle\r\nContent-Length: 4\r\n\r\n", $response->getRawHeaders()); } /** * @covers Guzzle\Http\Message\Response::getRequest */ public function testGetRequest() { $response = new Response(200, new Collection(), 'body'); $this->assertNull($response->getRequest()); $request = new \Guzzle\Http\Message\Request('GET', 'http://www.guzzle-project.com/'); $response->setRequest($request); $this->assertEquals($request, $response->getRequest()); } /** * @covers Guzzle\Http\Message\Response::getReasonPhrase * @covers Guzzle\Http\Message\Response::setStatus */ public function testHandlesStatusAndStatusCodes() { $response = new Response(200, new Collection(), 'body'); $this->assertEquals('OK', $response->getReasonPhrase()); $this->assertSame($response, $response->setStatus(204)); $this->assertEquals('No Content', $response->getReasonPhrase()); $this->assertEquals(204, $response->getStatusCode()); $this->assertSame($response, $response->setStatus(204, 'Testing!')); $this->assertEquals('Testing!', $response->getReasonPhrase()); $this->assertEquals(204, $response->getStatusCode()); try { $response->setStatus(2000); $this->fail('Did not throw expected exception when setting an invalid status'); } catch (BadResponseException $e) { } } /** * @covers Guzzle\Http\Message\Response::isClientError */ public function testIsClientError() { $response = new Response(403); $this->assertTrue($response->isClientError()); $response = new Response(200); $this->assertFalse($response->isClientError()); } /** * @covers Guzzle\Http\Message\Response::isError */ public function testIsError() { $response = new Response(403); $this->assertTrue($response->isError()); $response = new Response(200); $this->assertFalse($response->isError()); $response = new Response(500); $this->assertTrue($response->isError()); } /** * @covers Guzzle\Http\Message\Response::isInformational */ public function testIsInformational() { $response = new Response(100); $this->assertTrue($response->isInformational()); $response = new Response(200); $this->assertFalse($response->isInformational()); } /** * @covers Guzzle\Http\Message\Response::isRedirect */ public function testIsRedirect() { $response = new Response(301); $this->assertTrue($response->isRedirect()); $response = new Response(200); $this->assertFalse($response->isRedirect()); } /** * @covers Guzzle\Http\Message\Response::isServerError */ public function testIsServerError() { $response = new Response(500); $this->assertTrue($response->isServerError()); $response = new Response(400); $this->assertFalse($response->isServerError()); } /** * @covers Guzzle\Http\Message\Response::isSuccessful */ public function testIsSuccessful() { $response = new Response(200); $this->assertTrue($response->isSuccessful()); $response = new Response(403); $this->assertFalse($response->isSuccessful()); } /** * @covers Guzzle\Http\Message\Response::setRequest */ public function testSetRequest() { $response = new Response(200); $this->assertNull($response->getRequest()); $r = new \Guzzle\Http\Message\Request('GET', 'http://www.guzzle-project.com/'); $response->setRequest($r); $this->assertEquals($r, $response->getRequest()); } /** * @covers Guzzle\Http\Message\Response::getAcceptRanges */ public function testGetAcceptRanges() { $this->assertEquals('bytes', $this->response->getAcceptRanges()); } /** * @covers Guzzle\Http\Message\Response::getAge */ public function testGetAge() { $this->assertEquals(12, $this->response->getAge(false)); $this->assertEquals(12, $this->response->getAge(true)); $this->response->removeHeader('Age'); $this->response->removeHeader('Date'); $this->assertNull($this->response->getAge()); $this->response->setHeader('Date', Utils::getHttpDate(strtotime('-1 minute'))); // If the test runs slowly, still pass with a +5 second allowance $this->assertTrue($this->response->getAge() - 60 <= 5); $this->assertNull($this->response->getAge(true)); } /** * @covers Guzzle\Http\Message\Response::getAllow */ public function testGetAllow() { $this->assertEquals('GET, HEAD', $this->response->getAllow()); } /** * @covers Guzzle\Http\Message\Response::getCacheControl */ public function testGetCacheControl() { $this->assertEquals('no-cache', $this->response->getCacheControl()); } /** * @covers Guzzle\Http\Message\Response::getContentEncoding */ public function testGetContentEncoding() { $this->assertEquals('gzip', $this->response->getContentEncoding()); } /** * @covers Guzzle\Http\Message\Response::getContentLanguage */ public function testGetContentLanguage() { $this->assertEquals('da', $this->response->getContentLanguage()); } /** * @covers Guzzle\Http\Message\Response::getContentLength */ public function testGetContentLength() { $this->assertEquals('348', $this->response->getContentLength()); } /** * @covers Guzzle\Http\Message\Response::getContentLocation */ public function testGetContentLocation() { $this->assertEquals('/index.htm', $this->response->getContentLocation()); } /** * @covers Guzzle\Http\Message\Response::getContentDisposition */ public function testGetContentDisposition() { $this->assertEquals('attachment; filename=fname.ext', $this->response->getContentDisposition()); } /** * @covers Guzzle\Http\Message\Response::getContentMd5 */ public function testGetContentMd5() { $this->assertEquals('Q2hlY2sgSW50ZWdyaXR5IQ==', $this->response->getContentMd5()); } /** * @covers Guzzle\Http\Message\Response::getContentRange */ public function testGetContentRange() { $this->assertEquals('bytes 21010-47021/47022', $this->response->getContentRange()); } /** * @covers Guzzle\Http\Message\Response::getContentType */ public function testGetContentType() { $this->assertEquals('text/html; charset=utf-8', $this->response->getContentType()); } /** * @covers Guzzle\Http\Message\Response::getDate */ public function testGetDate() { $this->assertEquals('Tue, 15 Nov 1994 08:12:31 GMT', $this->response->getDate()); } /** * @covers Guzzle\Http\Message\Response::getEtag */ public function testGetEtag() { $this->assertEquals('737060cd8c284d8af7ad3082f209582d', $this->response->getEtag()); } /** * @covers Guzzle\Http\Message\Response::getExpires */ public function testGetExpires() { $this->assertEquals('Thu, 01 Dec 1994 16:00:00 GMT', $this->response->getExpires()); } /** * @covers Guzzle\Http\Message\Response::getLastModified */ public function testGetLastModified() { $this->assertEquals('Tue, 15 Nov 1994 12:45:26 GMT', $this->response->getLastModified()); } /** * @covers Guzzle\Http\Message\Response::getLocation */ public function testGetLocation() { $this->assertEquals('http://www.w3.org/pub/WWW/People.html', $this->response->getLocation()); } /** * @covers Guzzle\Http\Message\Response::getPragma */ public function testGetPragma() { $this->assertEquals('no-cache', $this->response->getPragma()); } /** * @covers Guzzle\Http\Message\Response::getProxyAuthenticate */ public function testGetProxyAuthenticate() { $this->assertEquals('Basic', $this->response->getProxyAuthenticate()); } /** * @covers Guzzle\Http\Message\Response::getRetryAfter */ public function testGetRetryAfter() { $this->assertEquals('120', $this->response->getRetryAfter()); $t = time() + 1000; $d = $t - time(); $this->response->setHeader('Retry-After', date('r', $t)); $this->assertEquals($d, $this->response->getRetryAfter()); $this->response->removeHeader('Retry-After'); $this->assertNull($this->response->getRetryAfter()); } /** * @covers Guzzle\Http\Message\Response::getServer */ public function testGetServer() { $this->assertEquals('Apache/1.3.27 (Unix) (Red-Hat/Linux)', $this->response->getServer()); } /** * @covers Guzzle\Http\Message\Response::getSetCookie */ public function testGetSetCookie() { $this->assertEquals('UserID=JohnDoe; Max-Age=3600; Version=1', $this->response->getSetCookie()); } /** * @covers Guzzle\Http\Message\Response::getSetCookie */ public function testGetSetCookieNormalizesHeaders() { $this->response->addHeaders(array( 'Set-Cooke' => 'boo', 'set-cookie' => 'foo' )); $this->assertEquals(array( 'UserID=JohnDoe; Max-Age=3600; Version=1', 'foo' ), $this->response->getSetCookie()->toArray()); $this->response->addHeaders(array( 'set-cookie' => 'fubu' )); $this->assertEquals(array('UserID=JohnDoe; Max-Age=3600; Version=1', 'foo', 'fubu'), $this->response->getSetCookie()->toArray()); } /** * @covers Guzzle\Http\Message\Response::getTrailer */ public function testGetTrailer() { $this->assertEquals('Max-Forwards', $this->response->getTrailer()); } /** * @covers Guzzle\Http\Message\Response::getTransferEncoding */ public function testGetTransferEncoding() { $this->assertEquals('chunked', $this->response->getTransferEncoding()); } /** * @covers Guzzle\Http\Message\Response::getVary */ public function testGetVary() { $this->assertEquals('*', $this->response->getVary()); } /** * @covers Guzzle\Http\Message\Response::getVia */ public function testReturnsViaHeader() { $this->assertEquals('1.0 fred, 1.1 nowhere.com (Apache/1.1)', $this->response->getVia()); } /** * @covers Guzzle\Http\Message\Response::getWarning */ public function testGetWarning() { $this->assertEquals('199 Miscellaneous warning', $this->response->getWarning()); } /** * @covers Guzzle\Http\Message\Response::getWwwAuthenticate */ public function testReturnsWwwAuthenticateHeader() { $this->assertEquals('Basic', $this->response->getWwwAuthenticate()); } /** * @covers Guzzle\Http\Message\Response::getConnection */ public function testReturnsConnectionHeader() { $this->assertEquals(null, $this->response->getConnection()); $this->response->setHeader('Connection', 'close'); $this->assertEquals('close', $this->response->getConnection()); } /** * @covers Guzzle\Http\Message\Response::getHeader */ public function testReturnsHeaders() { $this->assertEquals('Basic', $this->response->getHeader('WWW-Authenticate', null, true)); $this->assertEquals('chunked', $this->response->getHeader('Transfer-Encoding', null, false)); } /** * @covers Guzzle\Http\Message\Response::setInfo * @covers Guzzle\Http\Message\Response::getInfo */ public function testHasTransferInfo() { $stats = array ( 'url' => 'http://www.google.com/', 'content_type' => 'text/html; charset=ISO-8859-1', 'http_code' => 200, 'header_size' => 606, 'request_size' => 53, 'filetime' => -1, 'ssl_verify_result' => 0, 'redirect_count' => 0, 'total_time' => 0.093284, 'namelookup_time' => 0.001349, 'connect_time' => 0.01635, 'pretransfer_time' => 0.016358, 'size_upload' => 0, 'size_download' => 10330, 'speed_download' => 110737, 'speed_upload' => 0, 'download_content_length' => -1, 'upload_content_length' => 0, 'starttransfer_time' => 0.07066, 'redirect_time' => 0, ); // Uninitialized state $this->assertNull($this->response->getInfo('url')); $this->assertEquals(array(), $this->response->getInfo()); // Set the stats $this->response->setInfo($stats); $this->assertEquals($stats, $this->response->getInfo()); $this->assertEquals(606, $this->response->getInfo('header_size')); $this->assertNull($this->response->getInfo('does_not_exist')); } /** * @return Response */ private function getResponse($code, array $headers = null, EntityBody $body = null) { return new Response($code, $headers, $body); } /** * @covers Guzzle\Http\Message\Response::canCache */ public function testDeterminesIfItCanBeCached() { $this->assertTrue($this->getResponse(200)->canCache()); $this->assertTrue($this->getResponse(410)->canCache()); $this->assertFalse($this->getResponse(404)->canCache()); $this->assertTrue($this->getResponse(200, array( 'Cache-Control' => 'public' ))->canCache()); // This has the no-store directive $this->assertFalse($this->getResponse(200, array( 'Cache-Control' => 'private, no-store' ))->canCache()); // The body cannot be read, so it cannot be cached $tmp = tempnam('/tmp', 'not-readable'); $resource = fopen($tmp, 'w'); $this->assertFalse($this->getResponse(200, array( 'Transfer-Encoding' => 'chunked' ), EntityBody::factory($resource, 10))->canCache()); unlink($tmp); // The body is 0 length, cannot be read, so it can be cached $tmp = tempnam('/tmp', 'not-readable'); $resource = fopen($tmp, 'w'); $this->assertTrue($this->getResponse(200, array(array( 'Content-Length' => 0 )), EntityBody::factory($resource, 0))->canCache()); unlink($tmp); // When an HTTPS request is sent and the Cache-Control directive does // not include a 'public' value, then the response is not to be cached $request = RequestFactory::getInstance()->create('GET', 'https://www.test.com/'); $response = new Response(200); $response->setRequest($request); $this->assertFalse($response->canCache()); // This response can be cache because it is public $response->setHeader('Cache-Control', 'public'); $this->assertTrue($response->canCache()); } /** * @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()); } /** * @covers Guzzle\Http\Message\Response::canValidate */ public function testDeterminesIfItCanValidate() { $response = new Response(200); $this->assertFalse($response->canValidate()); $response->setHeader('ETag', '123'); $this->assertTrue($response->canValidate()); $response->removeHeader('ETag'); $this->assertFalse($response->canValidate()); $response->setHeader('Last-Modified', '123'); $this->assertTrue($response->canValidate()); } /** * @covers Guzzle\Http\Message\Response::getFreshness * @covers Guzzle\Http\Message\Response::isFresh */ public function testCalculatesFreshness() { $response = new Response(200); $this->assertNull($response->isFresh()); $this->assertNull($response->getFreshness()); $response->addCacheControlDirective('max-age', 120); $response->setHeader('Age', 100); $this->assertEquals(20, $response->getFreshness()); $this->assertTrue($response->isFresh()); $response->setHeader('Age', 150); $this->assertEquals(-30, $response->getFreshness()); $this->assertFalse($response->isFresh()); } /** * @covers Guzzle\Http\Message\Response::setProtocol * @covers Guzzle\Http\Message\Response::getProtocol * @covers Guzzle\Http\Message\Response::getProtocolVersion */ public function testHandlesProtocols() { $this->assertSame($this->response, $this->response->setProtocol('HTTP', '1.0')); $this->assertEquals('HTTP', $this->response->getProtocol()); $this->assertEquals('1.0', $this->response->getProtocolVersion()); } /** * @covers Guzzle\Http\Message\Response::isContentType */ public function testComparesContentType() { $response = new Response(200, array( 'Content-Type' => 'text/html; charset=ISO-8859-4' )); $this->assertTrue($response->isContentType('text/html')); $this->assertTrue($response->isContentType('TExT/html')); $this->assertTrue($response->isContentType('charset=ISO-8859-4')); $this->assertFalse($response->isContentType('application/xml')); } /** * @covers Guzzle\Http\Message\Response::isMethodAllowed */ public function testResponseDeterminesIfMethodIsAllowedBaseOnAllowHeader() { $response = new Response(200, array( 'Allow' => 'OPTIONS, POST, deletE,GET' )); $this->assertTrue($response->isMethodAllowed('get')); $this->assertTrue($response->isMethodAllowed('GET')); $this->assertTrue($response->isMethodAllowed('options')); $this->assertTrue($response->isMethodAllowed('post')); $this->assertTrue($response->isMethodAllowed('Delete')); $this->assertFalse($response->isMethodAllowed('put')); $this->assertFalse($response->isMethodAllowed('PUT')); $response = new Response(200); $this->assertFalse($response->isMethodAllowed('get')); } } tests/Guzzle/Tests/Http/Parser/Cookie/CookieParserProvider.php000064400000026033151742734600020503 0ustar00 'allseeing-i.com', 'path' => '/', 'data' => array( 'PHPSESSID' => '6c951590e7a9359bcedde25cda73e43c' ), 'max_age' => NULL, 'expires' => 'Sat, 26-Jul-2008 17:00:42 GMT', 'version' => NULL, 'secure' => NULL, 'discard' => NULL, 'port' => NULL, 'cookies' => array( 'ASIHTTPRequestTestCookie' => 'This is the value' ), 'comment' => null, 'comment_url' => null, 'http_only' => false ) ), array('', false), array('foo', false), // Test setting a blank value for a cookie array(array( 'foo=', 'foo =', 'foo =;', 'foo= ;', 'foo =', 'foo= '), array( 'cookies' => array( 'foo' => '' ), 'data' => array(), 'discard' => null, 'domain' => null, 'expires' => null, 'max_age' => null, 'path' => '/', 'port' => null, 'secure' => null, 'version' => null, 'comment' => null, 'comment_url' => null, 'http_only' => false ) ), // Test setting a value and removing quotes array(array( 'foo=1', 'foo =1', 'foo =1;', 'foo=1 ;', 'foo =1', 'foo= 1', 'foo = 1 ;', 'foo="1"', 'foo="1";', 'foo= "1";'), array( 'cookies' => array( 'foo' => '1' ), 'data' => array(), 'discard' => null, 'domain' => null, 'expires' => null, 'max_age' => null, 'path' => '/', 'port' => null, 'secure' => null, 'version' => null, 'comment' => null, 'comment_url' => null, 'http_only' => false ) ), // Test setting multiple values array(array( 'foo=1; bar=2;', 'foo =1; bar = "2"', 'foo=1; bar=2'), array( 'cookies' => array( 'foo' => '1', 'bar' => '2', ), 'data' => array(), 'discard' => null, 'domain' => null, 'expires' => null, 'max_age' => null, 'path' => '/', 'port' => null, 'secure' => null, 'version' => null, 'comment' => null, 'comment_url' => null, 'http_only' => false ) ), // Tests getting the domain and path from a reference request array(array( 'foo=1; port="80,8081"; httponly', 'foo=1; port="80,8081"; domain=www.test.com; HttpOnly;', 'foo=1; ; domain=www.test.com; path=/path/; port="80,8081"; HttpOnly;'), array( 'cookies' => array( 'foo' => 1 ), 'data' => array(), 'discard' => null, 'domain' => 'www.test.com', 'expires' => null, 'max_age' => null, 'path' => '/path/', 'port' => array('80', '8081'), 'secure' => null, 'version' => null, 'comment' => null, 'comment_url' => null, 'http_only' => true ), 'http://www.test.com/path/' ), // Some of the following tests are based on http://framework.zend.com/svn/framework/standard/trunk/tests/Zend/Http/CookieTest.php array( 'justacookie=foo; domain=example.com', array( 'cookies' => array( 'justacookie' => 'foo' ), 'domain' => 'example.com', 'path' => '', 'data' => array(), 'discard' => null, 'expires' => null, 'max_age' => null, 'path' => '/', 'port' => null, 'secure' => null, 'version' => null, 'comment' => null, 'comment_url' => null, 'http_only' => false ) ), array( 'expires=tomorrow; secure; path=/Space Out/; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com', array( 'cookies' => array( 'expires' => 'tomorrow' ), 'domain' => '.example.com', 'path' => '/Space Out/', 'expires' => 'Tue, 21-Nov-2006 08:33:44 GMT', 'data' => array(), 'discard' => null, 'port' => null, 'secure' => true, 'version' => null, 'max_age' => null, 'comment' => null, 'comment_url' => null, 'http_only' => false ) ), array( 'domain=unittests; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=example.com; path=/some value/', array( 'cookies' => array( 'domain' => 'unittests' ), 'domain' => 'example.com', 'path' => '/some value/', 'expires' => 'Tue, 21-Nov-2006 08:33:44 GMT', 'secure' => false, 'data' => array(), 'discard' => null, 'max_age' => null, 'port' => null, 'version' => null, 'comment' => null, 'comment_url' => null, 'http_only' => false ) ), array( 'path=indexAction; path=/; domain=.foo.com; expires=Tue, 21-Nov-2006 08:33:44 GMT', array( 'cookies' => array( 'path' => 'indexAction' ), 'domain' => '.foo.com', 'path' => '/', 'expires' => 'Tue, 21-Nov-2006 08:33:44 GMT', 'secure' => false, 'data' => array(), 'discard' => null, 'max_age' => null, 'port' => null, 'version' => null, 'comment' => null, 'comment_url' => null, 'http_only' => false ) ), array( 'secure=sha1; secure; SECURE; domain=some.really.deep.domain.com; version=1; Max-Age=86400', array( 'cookies' => array( 'secure' => 'sha1' ), 'domain' => 'some.really.deep.domain.com', 'path' => '/', 'secure' => true, 'data' => array(), 'discard' => null, 'expires' => time() + 86400, 'max_age' => 86400, 'port' => null, 'version' => 1, 'comment' => null, 'comment_url' => null, 'http_only' => false ) ), array( 'PHPSESSID=123456789+abcd%2Cef; secure; discard; domain=.localdomain; path=/foo/baz; expires=Tue, 21-Nov-2006 08:33:44 GMT;', array( 'cookies' => array( 'PHPSESSID' => '123456789 abcd,ef' ), 'domain' => '.localdomain', 'path' => '/foo/baz', 'expires' => 'Tue, 21-Nov-2006 08:33:44 GMT', 'secure' => true, 'data' => array(), 'discard' => true, 'max_age' => null, 'port' => null, 'version' => null, 'comment' => null, 'comment_url' => null, 'http_only' => false ) ), ); } /** * @dataProvider cookieParserDataProvider * @covers Guzzle\Http\Parser\Cookie\CookieParser */ public function testParseCookie($cookie, $parsed, $url = null) { $c = $this->cookieParserClass; $parser = new $c(); $request = null; if ($url) { $url = Url::factory($url); $host = $url->getHost(); $path = $url->getPath(); } else { $host = ''; $path = ''; } foreach ((array) $cookie as $c) { $p = $parser->parseCookie($c, $host, $path); // Remove expires values from the assertion if they are relatively equal if ($p['expires'] != $parsed['expires']) { if (abs($p['expires'] - $parsed['expires']) < 20) { unset($p['expires']); unset($parsed['expires']); } } if (is_array($parsed)) { foreach ($parsed as $key => $value) { $this->assertEquals($parsed[$key], $p[$key], 'Comparing ' . $key . ' ' . var_export($value, true) . ' : ' . var_export($parsed, true) . ' | ' . var_export($p, true)); } foreach ($p as $key => $value) { $this->assertEquals($p[$key], $parsed[$key], 'Comparing ' . $key . ' ' . var_export($value, true) . ' : ' . var_export($parsed, true) . ' | ' . var_export($p, true)); } } else { $this->assertEquals($parsed, $p); } } } } tests/Guzzle/Tests/Http/Parser/Cookie/CookieParserTest.php000064400000000362151742734600017625 0ustar00 'GET', 'protocol' => 'HTTP', 'version' => '1.1', 'request_url' => array( 'scheme' => 'http', 'host' => '', 'port' => '', 'path' => '/', 'query' => '' ), 'headers' => array(), 'body' => '' )), // Path and query string, multiple header values per header and case sensitive storage array("HEAD /path?query=foo HTTP/1.0\r\nHost: example.com\r\nX-Foo: foo\r\nx-foo: Bar\r\nX-Foo: foo\r\nX-Foo: Baz\r\n\r\n", array( 'method' => 'HEAD', 'protocol' => 'HTTP', 'version' => '1.0', 'request_url' => array( 'scheme' => 'http', 'host' => 'example.com', 'port' => '', 'path' => '/path', 'query' => 'query=foo' ), 'headers' => array( 'Host' => 'example.com', 'X-Foo' => array('foo', 'foo', 'Baz'), 'x-foo' => 'Bar' ), 'body' => '' )), // Includes a body array("PUT / HTTP/1.0\r\nhost: example.com:443\r\nContent-Length: 4\r\n\r\ntest", array( 'method' => 'PUT', 'protocol' => 'HTTP', 'version' => '1.0', 'request_url' => array( 'scheme' => 'https', 'host' => 'example.com', 'port' => '443', 'path' => '/', 'query' => '' ), 'headers' => array( 'host' => 'example.com:443', 'Content-Length' => '4' ), 'body' => 'test' )), // Includes Authorization headers array("GET / HTTP/1.1\r\nHost: example.com:8080\r\nAuthorization: Basic {$auth}\r\n\r\n", array( 'method' => 'GET', 'protocol' => 'HTTP', 'version' => '1.1', 'request_url' => array( 'scheme' => 'http', 'host' => 'example.com', 'port' => '8080', 'path' => '/', 'query' => '' ), 'headers' => array( 'Host' => 'example.com:8080', 'Authorization' => "Basic {$auth}" ), 'body' => '' )), // Include authorization header array("GET / HTTP/1.1\r\nHost: example.com:8080\r\nauthorization: Basic {$auth}\r\n\r\n", array( 'method' => 'GET', 'protocol' => 'HTTP', 'version' => '1.1', 'request_url' => array( 'scheme' => 'http', 'host' => 'example.com', 'port' => '8080', 'path' => '/', 'query' => '' ), 'headers' => array( 'Host' => 'example.com:8080', 'authorization' => "Basic {$auth}" ), 'body' => '' )), ); } public function responseProvider() { return array( // Empty request array('', false), array("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", array( 'protocol' => 'HTTP', 'version' => '1.1', 'code' => '200', 'reason_phrase' => 'OK', 'headers' => array( 'Content-Length' => 0 ), 'body' => '' )), array("HTTP/1.0 400 Bad Request\r\nContent-Length: 0\r\n\r\n", array( 'protocol' => 'HTTP', 'version' => '1.0', 'code' => '400', 'reason_phrase' => 'Bad Request', 'headers' => array( 'Content-Length' => 0 ), 'body' => '' )), array("HTTP/1.0 100 Continue\r\n\r\n", array( 'protocol' => 'HTTP', 'version' => '1.0', 'code' => '100', 'reason_phrase' => 'Continue', 'headers' => array(), 'body' => '' )), array("HTTP/1.1 204 No Content\r\nX-Foo: foo\r\nx-foo: Bar\r\nX-Foo: foo\r\n\r\n", array( 'protocol' => 'HTTP', 'version' => '1.1', 'code' => '204', 'reason_phrase' => 'No Content', 'headers' => array( 'X-Foo' => array('foo', 'foo'), 'x-foo' => 'Bar' ), 'body' => '' )), array("HTTP/1.1 200 Ok that is great!\r\nContent-Length: 4\r\n\r\nTest", array( 'protocol' => 'HTTP', 'version' => '1.1', 'code' => '200', 'reason_phrase' => 'Ok that is great!', 'headers' => array( 'Content-Length' => 4 ), 'body' => 'Test' )), ); } public function compareRequestResults($result, $expected) { if (!$result) { $this->assertFalse($expected); return; } $this->assertEquals($result['method'], $expected['method']); $this->assertEquals($result['protocol'], $expected['protocol']); $this->assertEquals($result['version'], $expected['version']); $this->assertEquals($result['request_url'], $expected['request_url']); $this->assertEquals($result['body'], $expected['body']); $this->compareHttpHeaders($result['headers'], $expected['headers']); } public function compareResponseResults($result, $expected) { if (!$result) { $this->assertFalse($expected); return; } $this->assertEquals($result['protocol'], $expected['protocol']); $this->assertEquals($result['version'], $expected['version']); $this->assertEquals($result['code'], $expected['code']); $this->assertEquals($result['reason_phrase'], $expected['reason_phrase']); $this->assertEquals($result['body'], $expected['body']); $this->compareHttpHeaders($result['headers'], $expected['headers']); } protected function normalizeHeaders($headers) { $normalized = array(); foreach ($headers as $key => $value) { $key = strtolower($key); if (!isset($normalized[$key])) { $normalized[$key] = $value; } elseif (!is_array($normalized[$key])) { $normalized[$key] = array($value); } else { $normalized[$key][] = $value; } } foreach ($normalized as $key => &$value) { if (is_array($value)) { sort($value); } } return $normalized; } public function compareHttpHeaders($result, $expected) { // Aggregate all headers case-insensitively $result = $this->normalizeHeaders($result); $expected = $this->normalizeHeaders($expected); $this->assertEquals($result, $expected); } } tests/Guzzle/Tests/Http/Parser/Message/MessageParserTest.php000064400000001763151742734600020161 0ustar00compareRequestResults($parts, $parser->parseRequest($message)); } /** * @covers Guzzle\Http\Parser\Message\MessageParser::parseMessage * @covers Guzzle\Http\Parser\Message\MessageParser::parseResponse * @dataProvider responseProvider */ public function testParsesResponses($message, $parts) { $parser = new MessageParser(); $this->compareResponseResults($parts, $parser->parseResponse($message)); } } tests/Guzzle/Tests/Http/Parser/Message/PeclHttpMessageParserTest.php000064400000001477151742734600021627 0ustar00compareRequestResults($parts, $parser->parseRequest($message)); } /** * @covers Guzzle\Http\Parser\Message\PeclHttpMessageParser::parseResponse * @dataProvider responseProvider */ public function testParsesResponses($message, $parts) { $parser = new PeclHttpMessageParser(); $this->compareResponseResults($parts, $parser->parseResponse($message)); } } tests/Guzzle/Tests/Http/Parser/ParserRegistryTest.php000064400000001771151742734600017020 0ustar00assertSame($c, ParserRegistry::get('foo')); } public function testReturnsNullWhenNotFound() { $this->assertNull(ParserRegistry::get('FOO')); } public function testReturnsLazyLoadedDefault() { // Clear out what might be cached $refObject = new \ReflectionClass('Guzzle\Http\Parser\ParserRegistry'); $refProperty = $refObject->getProperty('instances'); $refProperty->setAccessible(true); $refProperty->setValue(null, array()); $c = ParserRegistry::get('cookie'); $this->assertInstanceOf('Guzzle\Http\Parser\Cookie\CookieParser', $c); $this->assertSame($c, ParserRegistry::get('cookie')); } } tests/Guzzle/Tests/Http/Parser/UriTemplate/UriTemplateTest.php000064400000016703151742734600020522 0ustar00 'value', 'hello' => 'Hello World!', 'empty' => '', 'path' => '/foo/bar', 'x' => '1024', 'y' => '768', 'list' => array('red', 'green', 'blue'), 'keys' => array( "semi" => ';', "dot" => '.', "comma" => ',' ) ); return array_map(function($t) use ($params) { $t[] = $params; return $t; }, array( array('foo', 'foo'), array('{var}', 'value'), array('{hello}', 'Hello%20World%21'), array('{+var}', 'value'), array('{+hello}', 'Hello%20World!'), array('{+path}/here', '/foo/bar/here'), array('here?ref={+path}', 'here?ref=/foo/bar'), array('X{#var}', 'X#value'), array('X{#hello}', 'X#Hello%20World!'), array('map?{x,y}', 'map?1024,768'), array('{x,hello,y}', '1024,Hello%20World%21,768'), array('{+x,hello,y}', '1024,Hello%20World!,768'), array('{+path,x}/here', '/foo/bar,1024/here'), array('{#x,hello,y}', '#1024,Hello%20World!,768'), array('{#path,x}/here', '#/foo/bar,1024/here'), array('X{.var}', 'X.value'), array('X{.x,y}', 'X.1024.768'), array('{/var}', '/value'), array('{/var,x}/here', '/value/1024/here'), array('{;x,y}', ';x=1024;y=768'), array('{;x,y,empty}', ';x=1024;y=768;empty'), array('{?x,y}', '?x=1024&y=768'), array('{?x,y,empty}', '?x=1024&y=768&empty='), array('?fixed=yes{&x}', '?fixed=yes&x=1024'), array('{&x,y,empty}', '&x=1024&y=768&empty='), array('{var:3}', 'val'), array('{var:30}', 'value'), array('{list}', 'red,green,blue'), array('{list*}', 'red,green,blue'), array('{keys}', 'semi,%3B,dot,.,comma,%2C'), array('{keys*}', 'semi=%3B,dot=.,comma=%2C'), array('{+path:6}/here', '/foo/b/here'), array('{+list}', 'red,green,blue'), array('{+list*}', 'red,green,blue'), array('{+keys}', 'semi,;,dot,.,comma,,'), array('{+keys*}', 'semi=;,dot=.,comma=,'), array('{#path:6}/here', '#/foo/b/here'), array('{#list}', '#red,green,blue'), array('{#list*}', '#red,green,blue'), array('{#keys}', '#semi,;,dot,.,comma,,'), array('{#keys*}', '#semi=;,dot=.,comma=,'), array('X{.var:3}', 'X.val'), array('X{.list}', 'X.red,green,blue'), array('X{.list*}', 'X.red.green.blue'), array('X{.keys}', 'X.semi,%3B,dot,.,comma,%2C'), array('X{.keys*}', 'X.semi=%3B.dot=..comma=%2C'), array('{/var:1,var}', '/v/value'), array('{/list}', '/red,green,blue'), array('{/list*}', '/red/green/blue'), array('{/list*,path:4}', '/red/green/blue/%2Ffoo'), array('{/keys}', '/semi,%3B,dot,.,comma,%2C'), array('{/keys*}', '/semi=%3B/dot=./comma=%2C'), array('{;hello:5}', ';hello=Hello'), array('{;list}', ';list=red,green,blue'), array('{;list*}', ';list=red;list=green;list=blue'), array('{;keys}', ';keys=semi,%3B,dot,.,comma,%2C'), array('{;keys*}', ';semi=%3B;dot=.;comma=%2C'), array('{?var:3}', '?var=val'), array('{?list}', '?list=red,green,blue'), array('{?list*}', '?list=red&list=green&list=blue'), array('{?keys}', '?keys=semi,%3B,dot,.,comma,%2C'), array('{?keys*}', '?semi=%3B&dot=.&comma=%2C'), array('{&var:3}', '&var=val'), array('{&list}', '&list=red,green,blue'), array('{&list*}', '&list=red&list=green&list=blue'), array('{&keys}', '&keys=semi,%3B,dot,.,comma,%2C'), array('{&keys*}', '&semi=%3B&dot=.&comma=%2C'), // Test that missing expansions are skipped array('test{&missing*}', 'test'), // Test that multiple expansions can be set array('http://{var}/{var:2}{?keys*}', 'http://value/va?semi=%3B&dot=.&comma=%2C'), // Test that it is backwards compatible with {{ }} syntax array('{{var}}|{{var:3}}', 'value|val'), // Test more complex query string stuff array('http://www.test.com{+path}{?var,keys*}', 'http://www.test.com/foo/bar?var=value&semi=%3B&dot=.&comma=%2C') )); } /** * @dataProvider templateProvider */ public function testExpandsUriTemplates($template, $expansion, $params) { $uri = new UriTemplate($template); $result = $uri->expand($template, $params); $this->assertEquals($expansion, $result); } public function expressionProvider() { return array( array( '{+var*}', array( 'operator' => '+', 'values' => array( array('value' => 'var', 'modifier' => '*') ) ), ), array( '{?keys,var,val}', array( 'operator' => '?', 'values' => array( array('value' => 'keys', 'modifier' => ''), array('value' => 'var', 'modifier' => ''), array('value' => 'val', 'modifier' => '') ) ), ), array( '{+x,hello,y}', array( 'operator' => '+', 'values' => array( array('value' => 'x', 'modifier' => ''), array('value' => 'hello', 'modifier' => ''), array('value' => 'y', 'modifier' => '') ) ) ) ); } /** * @dataProvider expressionProvider */ public function testParsesExpressions($exp, $data) { $template = new UriTemplate($exp); // Access the config object $class = new \ReflectionClass($template); $method = $class->getMethod('parseExpression'); $method->setAccessible(true); $exp = substr($exp, 1, -1); $this->assertEquals($data, $method->invokeArgs($template, array($exp))); } } tests/Guzzle/Tests/Http/Parser/Url/UrlParserProvider.php000064400000001461151742734600017363 0ustar00assertEquals($url, Url::buildUrl($parts)); } public function testCanUseUtf8Query() { $url = Url::factory('http://www.example.com?µ=a'); $this->assertEquals('a', $url->getQuery()->get('µ')); } public function testParsesUtf8UrlQueryStringsWithFragment() { $parser = new UrlParser(); $parser->setUtf8Support(true); $parts = $parser->parseUrl('http://www.example.com?ሴ=a#fragmentishere'); $this->assertEquals('ሴ=a', $parts['query']); $this->assertEquals('fragmentishere', $parts['fragment']); $parts = $parser->parseUrl('http://www.example.com?ሴ=a'); $this->assertEquals('ሴ=a', $parts['query']); $this->assertEquals('', $parts['fragment']); } } tests/Guzzle/Tests/Http/Plugin/BatchQueuePluginTest.php000064400000014525151742734600017243 0ustar00assertArrayHasKey('flush', $events); $this->assertArrayHasKey('client.create_request', $events); $this->assertArrayHasKey('request.before_send', $events); } /** * @covers Guzzle\Http\Plugin\BatchQueuePlugin::count * @covers Guzzle\Http\Plugin\BatchQueuePlugin::onRequestCreate * @covers Guzzle\Http\Plugin\BatchQueuePlugin::addRequest */ public function testAddsRequestToQueue() { $plugin = new BatchQueuePlugin(); $this->assertEquals(0, count($plugin)); $client = new Client('http://test.com/'); $request = $client->get('/'); $event = new Event(array( 'request' => $request )); $plugin->onRequestCreate($event); $this->assertEquals(1, count($plugin)); } /** * @covers Guzzle\Http\Plugin\BatchQueuePlugin::removeRequest * @covers Guzzle\Http\Plugin\BatchQueuePlugin::onRequestBeforeSend */ public function testRemovesRequestsWhenTheyAreSentOutOfContext() { $plugin = new BatchQueuePlugin(); $client = new Client('http://test.com/'); // Create an event to use for our notifications $event = new Event(array( 'request' => $client->get('/') )); // Add a request to the queue $plugin->onRequestCreate($event); $this->assertEquals(1, count($plugin)); // Fake that the request is being sent outside of the queue $plugin->onRequestBeforeSend($event); // Ensure that the request is no longer queued $this->assertEquals(0, count($plugin)); } /** * @covers Guzzle\Http\Plugin\BatchQueuePlugin::flush */ public function testFlushSendsRequestsInQueue() { $this->getServer()->flush(); $plugin = new BatchQueuePlugin(); $client = new Client($this->getServer()->getUrl()); // Create some test requests $requests = array( $client->get('/'), $client->get('/') ); // Add the requests to the batch queue foreach ($requests as $request) { $plugin->onRequestCreate(new Event(array( 'request' => $request ))); $responses[] = new Response(200); } // Queue the test responses on node.js $this->getServer()->enqueue($responses); // Explicitly call flush to send the queued requests $plugin->flush(); $received = $this->getServer()->getReceivedRequests(); $this->assertEquals(count($requests), count($received)); $this->assertEquals(0, count($plugin)); } /** * @covers Guzzle\Http\Plugin\BatchQueuePlugin::__construct * @covers Guzzle\Http\Plugin\BatchQueuePlugin::onRequestCreate * @covers Guzzle\Http\Plugin\BatchQueuePlugin::addRequest */ public function testImplicitlyFlushesRequests() { $this->getServer()->flush(); $plugin = new BatchQueuePlugin(2); $client = new Client($this->getServer()->getUrl()); $this->getServer()->enqueue(array( new Response(200), new Response(200), new Response(200) )); $plugin->onRequestCreate(new Event(array( 'request' => $client->get('/1') ))); $plugin->onRequestCreate(new Event(array( 'request' => $client->get('/2') ))); $this->assertEquals(0, count($plugin)); $received = $this->getServer()->getReceivedRequests(); $this->assertEquals(2, count($received)); $plugin->onRequestCreate(new Event(array( 'request' => $client->get('/3') ))); $this->assertEquals(1, count($plugin)); } /** * @covers Guzzle\Http\Plugin\BatchQueuePlugin::onRequestCreate * @covers Guzzle\Http\Plugin\BatchQueuePlugin::addRequest * @covers Guzzle\Http\Plugin\BatchQueuePlugin::onRequestBeforeSend */ public function testWorksUsingEvents() { // Queue up some test responses $this->getServer()->flush(); $this->getServer()->enqueue(array( new Response(200), new Response(200), new Response(200) )); $plugin = new BatchQueuePlugin(2); $client = new Client($this->getServer()->getUrl()); $client->getEventDispatcher()->addSubscriber($plugin); $client->get('/'); $client->get('/'); // Ensure that the requests were sent implicitly $this->assertEquals(0, count($plugin)); $this->assertEquals(2, count($this->getServer()->getReceivedRequests())); // Add a single request and ensure that it is in queue and not sent $client->get('/'); $this->assertEquals(1, count($plugin)); $this->assertEquals(2, count($this->getServer()->getReceivedRequests())); // Explicitly flush the queued requests $client->dispatch('flush'); $this->assertEquals(0, count($plugin)); $this->assertEquals(3, count($this->getServer()->getReceivedRequests())); } /** * @covers Guzzle\Http\Plugin\BatchQueuePlugin::flush */ public function testWorksWithMockResponses() { $this->getServer()->flush(); $mock = new MockPlugin(array( new Response(200), new Response(201) )); $plugin = new BatchQueuePlugin(); $client = new Client($this->getServer()->getUrl()); $client->getEventDispatcher()->addSubscriber($plugin); $client->getEventDispatcher()->addSubscriber($mock); $request1 = $client->get('/'); $request2 = $client->get('/'); $plugin->flush(); $this->assertEquals(0, count($plugin)); $this->assertEquals(0, count($this->getServer()->getReceivedRequests())); $this->assertEquals(200, $request1->getResponse()->getStatusCode()); $this->assertEquals(201, $request2->getResponse()->getStatusCode()); } } tests/Guzzle/Tests/Http/Plugin/CachePluginTest.php000064400000041707151742734600016222 0ustar00cache = new ArrayCache(); $this->adapter = new DoctrineCacheAdapter($this->cache); } /** * @covers Guzzle\Http\Plugin\CachePlugin::__construct * @covers Guzzle\Http\Plugin\CachePlugin::getCacheAdapter */ public function testConstructorSetsValues() { $plugin = new CachePlugin($this->adapter, true, true, 1200); $this->assertEquals($this->adapter, $plugin->getCacheAdapter()); } /** * @covers Guzzle\Http\Plugin\CachePlugin::onRequestSent * @covers Guzzle\Http\Plugin\CachePlugin::onRequestBeforeSend * @covers Guzzle\Http\Plugin\CachePlugin::saveCache * @covers Guzzle\Http\Plugin\CachePlugin::getCacheKey * @covers Guzzle\Http\Plugin\CachePlugin::canResponseSatisfyRequest */ public function testSavesResponsesInCache() { // Send a 200 OK script to the testing server $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndata", "HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ntest" )); // Create a new Cache plugin $plugin = new CachePlugin($this->adapter, true); $client = new Client($this->getServer()->getUrl()); $client->setCurlMulti(new \Guzzle\Http\Curl\CurlMulti()); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get(); $request->send(); // Calculate the cache key like the cache plugin does $key = $plugin->getCacheKey($request); // Make sure that the cache plugin set the request in cache $this->assertNotNull($this->adapter->fetch($key)); // Clear out the requests stored on the server to make sure we didn't send a new request $this->getServer()->flush(); // Test that the request is set manually // The test server has no more script data, so if it actually sends a // request it will fail the test. $this->assertEquals($key, $plugin->getCacheKey($request)); $request->setState('new'); $request->send(); $this->assertEquals('data', $request->getResponse()->getBody(true)); // Make sure a request wasn't sent $this->assertEquals(0, count($this->getServer()->getReceivedRequests(false))); } /** * @covers Guzzle\Http\Plugin\CachePlugin::onRequestSent * @covers Guzzle\Http\Plugin\CachePlugin::onRequestBeforeSend * @covers Guzzle\Http\Plugin\CachePlugin::saveCache */ public function testSkipsNonReadableResponseBodies() { // Send a 200 OK script to the testing server $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndata"); // Create a new Cache plugin $plugin = new CachePlugin($this->adapter, true); $client = new Client($this->getServer()->getUrl()); $client->getEventDispatcher()->addSubscriber($plugin); // Create a new request using the Cache plugin $request = $client->get(); // Create a temp file that is not readable $tempFile = tempnam('/tmp', 'temp_stream_data'); // Set the non-readable stream as the response body so that it can't be cached $request->setResponseBody(EntityBody::factory( fopen($tempFile, 'w') )); $request->send(); // Calculate the cache key like the cache plugin does $key = $plugin->getCacheKey($request); // Make sure that the cache plugin set the request in cache $this->assertFalse($this->adapter->fetch($key)); } public function cacheKeyDataProvider() { $r = array( array('', 'gz_http_www.test.com/path?q=abc_host=www.test.com&date=123', 'http://www.test.com/path?q=abc', "Host: www.test.com\r\nDate: 123"), array('query = q', 'gz_http_www.test.com/path_host=www.test.com&date=123', 'http://www.test.com/path?q=abc', "Host: www.test.com\r\nDate: 123"), array('query=q; header=Date;', 'gz_http_www.test.com/path_host=www.test.com', 'http://www.test.com/path?q=abc', "Host: www.test.com\r\nDate: 123"), array('query=a, q; header=Date, Host;', 'gz_http_www.test.com/path_', 'http://www.test.com/path?q=abc&a=123', "Host: www.test.com\r\nDate: 123"), ); return $r; } /** * @covers Guzzle\Http\Plugin\CachePlugin::getCacheKey * @dataProvider cacheKeyDataProvider */ public function testCreatesCacheKeysUsingFilters($filter, $key, $url, $headers = null) { // Create a new Cache plugin $plugin = new CachePlugin($this->adapter, true); // Generate the header array $h = null; if ($headers) { $h = array(); foreach (explode("\r\n", $headers) as $header) { list($k, $v) = explode(': ', $header); $h[$k] = $v; } } // Create the request $request = RequestFactory::getInstance()->create('GET', $url, $h); $request->getParams()->set('cache.key_filter', $filter); $request->removeHeader('User-Agent'); $this->assertEquals($key, $plugin->getCacheKey($request, true)); // Make sure that the encoded request is returned when $raw is false $this->assertNotEquals($key, $plugin->getCacheKey($request)); } /** * @covers Guzzle\Http\Plugin\CachePlugin::getCacheKey */ public function testCreatesEncodedKeys() { $plugin = new CachePlugin($this->adapter, true); $request = RequestFactory::getInstance()->fromMessage( "GET / HTTP/1.1\r\nHost: www.test.com\r\nCache-Control: no-cache, no-store, max-age=120" ); $key = $plugin->getCacheKey($request); $this->assertEquals(1, preg_match('/^gz_[a-z0-9]{32}$/', $key)); // Make sure that the same value is returned in a subsequent call $this->assertEquals($key, $plugin->getCacheKey($request)); } /** * @covers Guzzle\Http\Plugin\CachePlugin::onRequestSent * @covers Guzzle\Http\Plugin\CachePlugin::onRequestBeforeSend * @covers Guzzle\Http\Plugin\CachePlugin::saveCache */ public function testRequestsCanOverrideTtlUsingCacheParam() { $plugin = new CachePlugin($this->adapter, true); $client = new Client($this->getServer()->getUrl()); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get('http://www.test.com/'); $request->getParams()->set('cache.override_ttl', 1000); $request->setResponse(Response::fromMessage("HTTP/1.1 200 OK\r\nCache-Control: max-age=100\r\nContent-Length: 4\r\n\r\nData"), true); $request->send(); $request2 = $client->get('http://www.test.com/'); $response = $request2->send(); $token = $response->getTokenizedHeader('X-Guzzle-Cache', ', '); $this->assertEquals(1000, $token['ttl']); $this->assertEquals(true, $token->hasKey('key')); } /** * @covers Guzzle\Http\Plugin\CachePlugin::canResponseSatisfyRequest * @covers Guzzle\Http\Plugin\CachePlugin::onRequestSent * @covers Guzzle\Http\Plugin\CachePlugin::onRequestBeforeSend * @covers Guzzle\Http\Plugin\CachePlugin::saveCache */ public function testRequestsCanAcceptStaleResponses() { $server = $this->getServer(); $client = new Client($this->getServer()->getUrl()); $plugin = new CachePlugin($this->adapter, true); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get('test'); // Cache this response for 1000 seconds if it is cacheable $request->getParams()->set('cache.override_ttl', 1000); $request->setResponse(Response::fromMessage("HTTP/1.1 200 OK\r\nExpires: " . Utils::getHttpDate('-1 second') . "\r\nContent-Length: 4\r\n\r\nData"), true); $request->send(); sleep(1); // Accept responses that are up to 100 seconds expired $request2 = $client->get('test'); $request2->addCacheControlDirective('max-stale', 100); $response = $request2->send(); $token = $response->getTokenizedHeader('X-Guzzle-Cache', ', '); $this->assertEquals(1000, $token['ttl']); // Accepts any stale response $request3 = $client->get('test'); $request3->addCacheControlDirective('max-stale'); $response = $request3->send(); $token = $response->getTokenizedHeader('X-Guzzle-Cache', ', '); $this->assertEquals(1000, $token['ttl']); // Will not accept the stale cached entry $server->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nData"); $request4 = $client->get('test'); $request4->addCacheControlDirective('max-stale', 0); $response = $request4->send(); $this->assertEquals("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nData", $this->removeKeepAlive((string) $response)); } /** * @covers Guzzle\Http\Plugin\CachePlugin::canResponseSatisfyRequest */ public function testChecksIfResponseCanSatisfyRequest() { $plugin = new CachePlugin($this->adapter, true); // Send some responses to the test server for cache validation $server = $this->getServer(); // No restrictions $request = RequestFactory::getInstance()->create('GET', $server->getUrl()); $response = new Response(200, array('Date' => Utils::getHttpDate('now'))); $this->assertTrue($plugin->canResponseSatisfyRequest($request, $response)); // Request max-age is less than response age $request = RequestFactory::getInstance()->create('GET', $server->getUrl()); $request->addCacheControlDirective('max-age', 100); $response = new Response(200, array('Age' => 10)); $this->assertTrue($plugin->canResponseSatisfyRequest($request, $response)); // Request must have something fresher than 200 seconds $response->setHeader('Date', Utils::getHttpDate('-200 days')); $response->removeHeader('Age'); $request->setHeader('Cache-Control', 'max-age=200'); $this->assertFalse($plugin->canResponseSatisfyRequest($request, $response)); // Response says it's too old $request->removeHeader('Cache-Control'); $response->setHeader('Cache-Control', 'max-age=86400'); $this->assertFalse($plugin->canResponseSatisfyRequest($request, $response)); // Response is OK $response->setHeader('Date', Utils::getHttpDate('-1 hour')); $this->assertTrue($plugin->canResponseSatisfyRequest($request, $response)); } /** * Data provider to test cache revalidation * * @return array */ public function cacheRevalidationDataProvider() { return array( // Forces revalidation that passes 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", ), // Forces revalidation that overwrites what is in cache 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" ), // Must get a fresh copy because the request is declining revalidation 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' ), // Skips revalidation because the request is accepting the cached copy 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' ), // Throws an exception during revalidation 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" ), // ETag mismatch 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", ), ); } /** * @covers Guzzle\Http\Plugin\CachePlugin::canResponseSatisfyRequest * @covers Guzzle\Http\Plugin\CachePlugin::revalidate * @dataProvider cacheRevalidationDataProvider */ public function testRevalidatesResponsesAgainstOriginServer($can, $request, $response, $validate = null, $result = null, $param = null) { // Send some responses to the test server for cache validation $server = $this->getServer(); $server->flush(); if ($validate) { $server->enqueue($validate); } $request = RequestFactory::getInstance()->fromMessage("GET / HTTP/1.1\r\nHost: 127.0.0.1:" . $server->getPort() . "\r\n" . $request); $response = Response::fromMessage($response); $request->setClient(new Client()); if ($param) { $request->getParams()->set('cache.revalidate', $param); } $plugin = new CachePlugin($this->adapter, true); $this->assertEquals($can, $plugin->canResponseSatisfyRequest($request, $response), '-> ' . $request . "\n" . $response); if ($result) { // Get rid of dates $this->assertEquals( preg_replace('/(Date:\s)(.*)(\r\n)/', '$1$3', (string) $result), preg_replace('/(Date:\s)(.*)(\r\n)/', '$1$3', (string) $request->getResponse()) ); } if ($validate) { $this->assertEquals(1, count($server->getReceivedRequests())); } } /** * @covers Guzzle\Http\Plugin\CachePlugin */ public function testCachesResponsesAndHijacksRequestsWhenApplicable() { $server = $this->getServer(); $server->flush(); $server->enqueue("HTTP/1.1 200 OK\r\nCache-Control: max-age=1000\r\nContent-Length: 4\r\n\r\nData"); $plugin = new CachePlugin($this->adapter, true); $client = new Client($server->getUrl()); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get(); $request->getCurlOptions()->set(CURLOPT_TIMEOUT, 2); $request2 = $client->get(); $request2->getCurlOptions()->set(CURLOPT_TIMEOUT, 2); $request->send(); $request2->send(); $this->assertEquals(1, count($server->getReceivedRequests())); $this->assertEquals(true, $request2->getResponse()->hasHeader('X-Guzzle-Cache')); } /** * @covers Guzzle\Http\Plugin\CachePlugin::revalidate * @expectedException Guzzle\Http\Exception\BadResponseException */ public function testRemovesMissingEntitesFromCacheWhenRevalidating() { $server = $this->getServer(); $server->enqueue(array( "HTTP/1.1 200 OK\r\nCache-Control: max-age=1000, no-cache\r\nContent-Length: 4\r\n\r\nData", "HTTP/1.1 404 NOT FOUND\r\nContent-Length: 0\r\n\r\n" )); $plugin = new CachePlugin($this->adapter, true); $client = new Client($server->getUrl()); $client->getEventDispatcher()->addSubscriber($plugin); $request1 = $client->get('/'); $request1->send(); $this->assertTrue($this->cache->contains($plugin->getCacheKey($request1))); $client->get('/')->send(); } } tests/Guzzle/Tests/Http/Plugin/CookiePluginTest.php000064400000032201151742734600016415 0ustar00storage = new ArrayCookieJar(); $this->plugin = new CookiePlugin($this->storage); } /** * @covers Guzzle\Http\Plugin\CookiePlugin */ public function testClearsCookiesWhenShuttingDown() { $this->storage->save(array( 'domain' => '.example.com', 'cookie' => array('a', '123') )); $this->assertEquals(1, count($this->storage->getCookies())); unset($this->plugin); $this->assertEquals(0, count($this->storage->getCookies())); } /** * @covers Guzzle\Http\Plugin\CookiePlugin */ public function testClearsTemporaryCookies() { $this->storage->save(array( 'domain' => '.example.com', 'cookie' => array('a', '123') )); $this->assertEquals(1, count($this->storage->getCookies())); $this->plugin->clearTemporaryCookies(); $this->assertEquals(0, count($this->storage->getCookies())); } /** * @covers Guzzle\Http\Plugin\CookiePlugin */ public function testClearsCookies() { $this->assertSame($this->storage, $this->storage->save(array( 'domain' => '.example.com', 'cookie' => array('a', '123') ))); $this->assertSame($this->storage, $this->storage->save(array( 'domain' => 'example.com', 'cookie' => array('b', '123') ))); $this->assertEquals(2, count($this->storage->getCookies())); $this->assertEquals(1, $this->plugin->clearCookies('http://example.com')); $this->assertEquals(1, count($this->storage->getCookies())); } /** * @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\Plugin\CookiePlugin */ public function testAddsCookiesToRequests() { ArrayCookieJarTest::addCookies($this->storage); $this->storage->save(array( 'domain' => '.y.example.com', 'path' => '/acme/', 'cookie' => array('secure', 'sec'), 'expires' => Utils::getHttpDate('+1 day'), 'secure' => true )); // Add a cookie that is only set on a specific port, so it wont be // added to the following requests $this->storage->save(array( 'domain' => '.y.example.com', 'path' => '/acme/', 'cookie' => array('test', 'port'), 'expires' => Utils::getHttpDate('+1 day'), 'secure' => false, 'port' => array(8192) )); $request1 = new Request('GET', 'https://a.y.example.com/acme/'); $request1->setClient(new Client()); $request2 = new Request('GET', 'https://a.y.example.com/acme/'); $request2->setClient(new Client()); $request3 = new Request('GET', 'http://a.y.example.com/acme/'); $request3->setClient(new Client()); $request4 = new Request('GET', 'http://a.y.example.com/acme/'); $request4->setClient(new Client()); $request1->getEventDispatcher()->addSubscriber($this->plugin); $request2->getEventDispatcher()->addSubscriber($this->plugin); $request3->getEventDispatcher()->addSubscriber($this->plugin); $request4->getEventDispatcher()->addSubscriber($this->plugin); // Set a secure cookie $response1 = Response::fromMessage("HTTP/1.1 200 OK\r\nSet-Cookie: a=b; c=d; Max-Age=86400; domain=.example.com; secure;\r\n\r\n"); // Set a regular cookie $response2 = Response::fromMessage("HTTP/1.1 200 OK\r\nSet-Cookie: e=f h; discard; domain=.example.com;\r\n\r\n"); $response3 = Response::fromMessage("HTTP/1.1 200 OK\r\n\r\n"); $request1->setResponse($response1, true); $request2->setResponse($response2, true); $request3->setResponse($response3, true); $request1->send(); $request2->send(); $request3->send(); $this->assertEquals('muppet=cookie_monster;secure=sec', (string) $request1->getCookie()); $this->assertEquals('muppet=cookie_monster;secure=sec;a=b;c=d', (string) $request2->getCookie()); $this->assertEquals('muppet=cookie_monster;e=f h', (string) $request3->getCookie()); // Clear the e=f h temporary cookie $this->plugin->clearTemporaryCookies(); $request4->setResponse($response3, true); $request4->send(); $this->assertEquals('muppet=cookie_monster', (string) $request4->getCookie()); } /** * @covers Guzzle\Http\Plugin\CookiePlugin */ public function testExtractsMultipleCookies() { $this->plugin->clearCookies(); $response = Response::fromMessage( "HTTP/1.1 200 OK\r\n" . "Set-Cookie: IU=deleted; expires=Wed, 03-Mar-2010 02:17:39 GMT; path=/; domain=127.0.0.1\r\n" . "Set-Cookie: PH=deleted; expires=Wed, 03-Mar-2010 02:17:39 GMT; path=/; domain=127.0.0.1\r\n" . "Set-Cookie: fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT; path=/; domain=127.0.0.1\r\n" . "Set-Cookie: FPCK3=AgBNbvoQAGpGEABZLRAAbFsQAF1tEABkDhAAeO0=; expires=Sat, 02-Apr-2019 02:17:40 GMT; path=/; domain=127.0.0.1\r\n" . "Set-Cookie: CH=deleted; expires=Wed, 03-Mar-2010 02:17:39 GMT; path=/; domain=127.0.0.1\r\n" . "Set-Cookie: CH=AgBNbvoQAAEcEAApuhAAMJcQADQvEAAvGxAALe0QAD6uEAATwhAAC1AQAC8t; expires=Sat, 02-Apr-2019 02:17:40 GMT; path=/; domain=127.0.0.1\r\n" . "Set-Cookie: fpt=d=_e2d6jLXesxx4AoiC0W7W3YktnpITDTHoJ6vNxF7TU6JEep6Y5BFk7Z9NgHmhiXoB7jGV4uR_GBQtSDOLjflKBUVZ6UgnGmDztoj4GREK30jm1qDgReyhPv7iWaN8e8ZLpUKXtPioOzQekGha1xR8ZqGR25GT7aYQpcxaaY.2ATjTpbm7HmX8tlBIte6mYMwFpIh_krxtofGPH3R337E_aNF3illhunC5SK6I0IfZvHzBXCoxu9fjH6e0IHzyOBY656YMUIElQiDkSd8werkBIRE6LJi6YU8AWgitEpMLisOIQSkqyGiahcPFt_fsD8DmIX2YAdSeVE0KycIqd0Z9aM7mdJ3xNQ4dmOOfcZ83dDrZ.4hvuKN2jB2FQDKuxEjTVO4DmiCCSyYgcs2wh0Lc3RODVKzqAZNMTYltWMELw9JdUyDFD3EGT3ZCnH8NQ6f_AAWffyj92ZMLYfWJnXHSG.DTKlVHj.IsihVT73QzrfoMFIs&v=1; path=/; domain=127.0.0.1\r\n" . "Set-Cookie: fpps=deleted; expires=Wed, 03-Mar-2010 02:17:39 GMT; path=/; domain=127.0.0.1\r\n" . "set-cookie: fpc_s=d=ng6sEJk.1XnLUt1pfJ2kiUon07QEppAUuwW3nk0tYwcHMQ1CijnSGVZHfgvWSXQxE5eW_1hjvDAA4Nu0CSSn2xk9_.DOkKI_fZLLLUrm0hJ41VMbSUTrklw.u5IlTM5JCeK_PDjSjZNkvHMbNYziu8vwd8fMnbecf9bSo3eDDv1boowyLFk_9mnGYBeSI4U86mnm.mnfOHMARxzL6BVMTAblIAml65cR486SHzPVO6KNYvkqh8zP3m0hVIkRaPhzvDjQkDG28HCbMjq745QR2FcCmI4TNJbk7EtJmsBrlL8wvVyX5DiBmP9W990-&v=2; path=/; domain=127.0.0.1\r\n" . "Content-Length: 0\r\n\r\n" ); $this->getServer()->enqueue(array( (string) $response, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\rn" )); $request = RequestFactory::getInstance()->create('GET', $this->getServer()->getUrl()); $request->getEventDispatcher()->addSubscriber($this->plugin); $request->setClient(new Client()); $request->send(); $this->assertNull($request->getHeader('Cookie')); $request->setState('new'); $request->send(); $this->assertNotNull($request->getHeader('Cookie')); // Doesn't send expired cookies $this->assertEmpty($request->getCookie('IU')); $this->assertEmpty($request->getCookie('PH')); $this->assertNotEmpty($request->getCookie('fpc')); $this->assertNotEmpty($request->getCookie('FPCK3')); $this->assertNotEmpty($request->getCookie('CH')); $this->assertNotEmpty($request->getCookie('fpt')); $this->assertNotEmpty($request->getCookie('fpc_s')); $this->assertNotEmpty($request->getCookie('CH')); $this->assertNotEmpty($request->getCookie('CH')); $this->assertEquals(9, count($this->plugin->extractCookies($response))); } /** * @covers Guzzle\Http\Plugin\CookiePlugin::onRequestSent * @covers Guzzle\Http\Plugin\CookiePlugin::onRequestReceiveStatusLine * @covers Guzzle\Http\Plugin\CookiePlugin::onRequestBeforeSend */ public function testCookiesAreExtractedFromRedirectResponses() { $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($this->plugin); $request = $client->get(); $request->send(); $request = $client->get(); $request->send(); $this->assertEquals('test=583551', $request->getHeader('Cookie')); } /** * @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')); } } tests/Guzzle/Tests/Http/Plugin/CurlAuthPluginTest.php000064400000002325151742734600016737 0ustar00getEventDispatcher()->addSubscriber($plugin); $request = $client->get('/'); $this->assertEquals('michael', $request->getUsername()); $this->assertEquals('test', $request->getPassword()); } public function testAddsDigestAuthentication() { $plugin = new CurlAuthPlugin('julian', 'test', CURLAUTH_DIGEST); $client = new Client('http://www.test.com/'); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get('/'); $this->assertEquals('julian', $request->getUsername()); $this->assertEquals('test', $request->getPassword()); $this->assertEquals('julian:test', $request->getCurlOptions()->get(CURLOPT_USERPWD)); $this->assertEquals(CURLAUTH_DIGEST, $request->getCurlOptions()->get(CURLOPT_HTTPAUTH)); } } tests/Guzzle/Tests/Http/Plugin/ExponentialBackoffTest.php000064400000027230151742734600017575 0ustar00assertEquals(2, $plugin->getMaxRetries()); $this->assertEquals(array(500, 503, 502), $plugin->getFailureCodes()); // You can specify any codes you want... Probably not a good idea though $plugin->setFailureCodes(array(200, 204)); $this->assertEquals(array(200, 204), $plugin->getFailureCodes()); } /** * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::calculateWait */ public function testCalculateWait() { $plugin = new ExponentialBackoffPlugin(2); $this->assertEquals(1, $plugin->calculateWait(0)); $this->assertEquals(2, $plugin->calculateWait(1)); $this->assertEquals(4, $plugin->calculateWait(2)); $this->assertEquals(8, $plugin->calculateWait(3)); $this->assertEquals(16, $plugin->calculateWait(4)); } /** * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin */ public function testRetriesRequests() { // Create a script to return several 500 and 503 response codes $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndata" )); $plugin = new ExponentialBackoffPlugin(2, null, array($this, 'delayClosure')); $client = new Client($this->getServer()->getUrl()); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get(); $request->send(); // Make sure it eventually completed successfully $this->assertEquals(200, $request->getResponse()->getStatusCode()); $this->assertEquals('OK', $request->getResponse()->getReasonPhrase()); $this->assertEquals('data', $request->getResponse()->getBody(true)); // Check that three requests were made to retry this request $this->assertEquals(3, count($this->getServer()->getReceivedRequests(false))); } /** * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin */ public function testRetriesRequestsUsingReasonPhraseMatch() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 400 FooError\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 400 FooError\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndata" )); $plugin = new ExponentialBackoffPlugin(2, array('FooError'), array($this, 'delayClosure')); $client = new Client($this->getServer()->getUrl()); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get(); $request->send(); // Make sure it eventually completed successfully $this->assertEquals(200, $request->getResponse()->getStatusCode()); $this->assertEquals('data', $request->getResponse()->getBody(true)); // Check that three requests were made to retry this request $this->assertEquals(3, count($this->getServer()->getReceivedRequests(false))); } /** * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::onRequestSent * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::onRequestPoll * @covers Guzzle\Http\Message\Request * @expectedException Guzzle\Http\Exception\BadResponseException */ public function testAllowsFailureOnMaxRetries() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n" )); $plugin = new ExponentialBackoffPlugin(2, null, array($this, 'delayClosure')); $client = new Client($this->getServer()->getUrl()); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get(); // This will fail because the plugin isn't retrying the request because // the max number of retries is exceeded (1 > 0) $request->send(); } /** * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::onRequestSent * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::onRequestPoll * @covers Guzzle\Http\Curl\CurlMulti */ public function testRetriesPooledRequestsUsingDelayAndPollingEvent() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndata" )); // Need to sleep for one second to make sure that the polling works // correctly in the observer $plugin = new ExponentialBackoffPlugin(1, null, function($r) { return 1; }); $client = new Client($this->getServer()->getUrl()); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get(); $request->send(); // Make sure it eventually completed successfully $this->assertEquals('data', $request->getResponse()->getBody(true)); // Check that two requests were made to retry this request $this->assertEquals(2, count($this->getServer()->getReceivedRequests(false))); } /** * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::getDefaultFailureCodes */ public function testReturnsDefaultFailureCodes() { $this->assertNotEmpty(ExponentialBackoffPlugin::getDefaultFailureCodes()); } /** * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::__construct * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::getDefaultFailureCodes */ public function testUsesDefaultFailureCodesByDefault() { $p = new ExponentialBackoffPlugin(); $this->assertEquals($p->getFailureCodes(), ExponentialBackoffPlugin::getDefaultFailureCodes()); } /** * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::onRequestSent */ public function testAllowsCallableFailureCodes() { $a = 0; $plugin = new ExponentialBackoffPlugin(1, function($request, $response) use (&$a) { // Look for a Foo header if ($response->hasHeader('Foo')) { $a = 1; return true; } }, array($this, 'delayClosure')); $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\nFoo: Bar\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndata" )); $client = new Client($this->getServer()->getUrl()); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get(); $request->send(); // Make sure it eventually completed successfully $this->assertEquals('data', $request->getResponse()->getBody(true)); // Check that the callback saw the request and header $this->assertEquals(1, $a); // Check that two requests were made to retry this request $this->assertEquals(2, count($this->getServer()->getReceivedRequests(false))); } /** * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::onRequestSent */ public function testExponentiallyBacksOffCurlErrors() { $plugin = $this->getMock('Guzzle\Http\Plugin\ExponentialBackoffPlugin', array('retryRequest')); // Mock the retryRequest method so that it does nothing, but ensure // that it is called exactly once $plugin->expects($this->once()) ->method('retryRequest') ->will($this->returnValue(null)); // Create an exception that is found in the default curl exception list $exception = new CurlException('Curl'); $exception->setError('foo', CURLE_OPERATION_TIMEOUTED); // Create a dummy event to send to the plugin $event = new Event(array( 'request' => new Request('GET', 'http://test.com'), 'response' => null, 'exception' => $exception )); // Ensure the it uses the name we're looking for $event->setName('request.exception'); // Trigger the event $plugin->onRequestSent($event); } /** * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::onRequestSent */ public function testAllowsCustomFailureMethodsToPuntToDefaultMethod() { $count = 0; $plugin = $this->getMockBuilder('Guzzle\Http\Plugin\ExponentialBackoffPlugin') ->setMethods(array('retryRequest')) ->setConstructorArgs(array(2, function() use (&$count) { $count++; }, array($this, 'delayClosure'))) ->getMock(); $plugin->expects($this->once()) ->method('retryRequest') ->will($this->returnValue(null)); $event = new Event(array( 'request' => new Request('GET', 'http://test.com'), 'response' => new Response(500) )); $event->setName('request.exception'); $plugin->onRequestSent($event); $this->assertEquals(1, $count); } /** * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::onRequestPoll */ public function testSeeksToBeginningOfRequestBodyWhenRetrying() { // Create a mock curl multi object $multi = $this->getMockBuilder('Guzzle\Http\Curl\CurlMulti') ->setMethods(array('remove', 'add')) ->getMock(); // Create a request with a body $request = new EntityEnclosingRequest('PUT', 'http://www.example.com'); $request->setBody('abc'); // Set the retry time to be something that will be retried always $request->getParams()->set('plugins.exponential_backoff.retry_time', 2); // Seek to the end of the stream $request->getBody()->seek(3); $this->assertEquals('', $request->getBody()->read(1)); // Create a plugin that does not delay when retrying $plugin = new ExponentialBackoffPlugin(2, null, array($this, 'delayClosure')); // Create an event that is expected for the Poll event $event = new Event(array( 'request' => $request, 'curl_multi' => $multi )); $event->setName(CurlMultiInterface::POLLING_REQUEST); $plugin->onRequestPoll($event); // Ensure that the stream was seeked to 0 $this->assertEquals('a', $request->getBody()->read(1)); } } tests/Guzzle/Tests/Http/Plugin/HistoryPluginTest.php000064400000010021151742734600016641 0ustar00get(); $requests[$i]->setResponse(new Response(200), true); $requests[$i]->send(); $h->add($requests[$i]); } return $requests; } /** * @covers Guzzle\Http\Plugin\HistoryPlugin::getSubscribedEvents */ public function testDescribesSubscribedEvents() { $this->assertInternalType('array', HistoryPlugin::getSubscribedEvents()); } /** * @covers Guzzle\Http\Plugin\HistoryPlugin::getLimit * @covers Guzzle\Http\Plugin\HistoryPlugin::setLimit */ public function testMaintainsLimitValue() { $h = new HistoryPlugin(); $this->assertSame($h, $h->setLimit(10)); $this->assertEquals(10, $h->getLimit()); } /** * @covers Guzzle\Http\Plugin\HistoryPlugin::add * @covers Guzzle\Http\Plugin\HistoryPlugin::count * @covers Guzzle\Http\Plugin\HistoryPlugin::getIterator */ public function testAddsRequests() { $h = new HistoryPlugin(); $requests = $this->addRequests($h, 1); $this->assertEquals(1, count($h)); $i = $h->getIterator(); $this->assertEquals(1, count($i)); $this->assertEquals($requests[0], $i[0]); } /** * @covers Guzzle\Http\Plugin\HistoryPlugin::add */ public function testIgnoresUnsentRequests() { $h = new HistoryPlugin(); $request = RequestFactory::getInstance()->create('GET', 'http://localhost/'); $h->add($request); $this->assertEquals(0, count($h)); } /** * @covers Guzzle\Http\Plugin\HistoryPlugin::add * @depends testAddsRequests */ public function testMaintainsLimit() { $h = new HistoryPlugin(); $h->setLimit(2); $requests = $this->addRequests($h, 3); $this->assertEquals(2, count($h)); $i = 0; foreach ($h as $request) { if ($i > 0) { $this->assertSame($requests[$i], $request); } } } /** * @covers Guzzle\Http\Plugin\HistoryPlugin::getLastRequest */ public function testReturnsLastRequest() { $h = new HistoryPlugin(); $requests = $this->addRequests($h, 5); $this->assertSame(end($requests), $h->getLastRequest()); } /** * @covers Guzzle\Http\Plugin\HistoryPlugin::getLastResponse */ public function testReturnsLastResponse() { $h = new HistoryPlugin(); $requests = $this->addRequests($h, 5); $this->assertSame(end($requests)->getResponse(), $h->getLastResponse()); } /** * @covers Guzzle\Http\Plugin\HistoryPlugin::clear */ public function testClearsHistory() { $h = new HistoryPlugin(); $requests = $this->addRequests($h, 5); $this->assertEquals(5, count($h)); $h->clear(); $this->assertEquals(0, count($h)); } /** * @covers Guzzle\Http\Plugin\HistoryPlugin::onRequestComplete * @depends testAddsRequests */ public function testUpdatesAddRequests() { $h = new HistoryPlugin(); $client = new Client('http://localhost/'); $client->getEventDispatcher()->addSubscriber($h); $request = $client->get(); $request->setResponse(new Response(200), true); $request->send(); $this->assertSame($request, $h->getLastRequest()); } } tests/Guzzle/Tests/Http/Plugin/LogPluginTest.php000064400000025743151742734600015742 0ustar00logAdapter = new ClosureLogAdapter( function($message, $priority, $extras = null) { echo $message . ' - ' . $priority . ' ' . implode(', ', (array) $extras) . "\n"; } ); $this->plugin = new LogPlugin($this->logAdapter); $this->client = new Client($this->getServer()->getUrl()); $this->client->getEventDispatcher()->addSubscriber($this->plugin); } /** * Parse a log message into parts * * @param string $message Message to parse * * @return array */ private function parseMessage($message) { $p = explode(' - ', $message, 4); $parts['host'] = trim($p[0]); $parts['request'] = str_replace('"', '', $p[1]); list($parts['code'], $parts['size']) = explode(' ', $p[2]); list($parts['time'], $parts['up'], $parts['down']) = explode(' ', $p[3]); $parts['extra'] = isset($p[4]) ? $p[4] : null; return $parts; } /** * @covers Guzzle\Http\Plugin\LogPlugin::__construct * @covers Guzzle\Http\Plugin\LogPlugin::getLogAdapter */ public function testPluginHasLogAdapter() { $plugin = new LogPlugin($this->logAdapter); $this->assertEquals($this->logAdapter, $plugin->getLogAdapter()); } public function testLogsRequestAndResponseContext() { $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); $request = $this->client->get(); ob_start(); $request->send(); $message = ob_get_clean(); $parts = $this->parseMessage($message); $this->assertEquals('127.0.0.1', $parts['host']); $this->assertEquals('GET / HTTP/1.1', $parts['request']); $this->assertEquals(200, $parts['code']); $this->assertEquals(0, $parts['size']); $this->assertContains('127.0.0.1 - "GET / HTTP/1.1" - 200 0 - ', $message); $this->assertContains('7 guzzle.request', $message); } public function testLogsRequestAndResponseWireHeaders() { $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndata"); $client = new Client($this->getServer()->getUrl()); $plugin = new LogPlugin($this->logAdapter, LogPlugin::LOG_CONTEXT | LogPlugin::LOG_HEADERS); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get(); ob_start(); $request->send(); $message = ob_get_clean(); // Make sure the context was logged $this->assertContains('127.0.0.1 - "GET / HTTP/1.1" - 200 4 - ', $message); $this->assertContains('7 guzzle.request', $message); // Check that the headers were logged $this->assertContainsIns("GET / HTTP/1.1\r\n", $message); $this->assertContainsIns("User-Agent: " . Utils::getDefaultUserAgent(), $message); $this->assertContainsIns("Accept: */*\r\n", $message); $this->assertContainsIns("Accept-Encoding: deflate, gzip", $message); $this->assertContainsIns("Host: 127.0.0.1:", $message); // Make sure the response headers are present with a line between the request and response $this->assertContainsIns("\n< HTTP/1.1 200 OK\r\n< Content-Length: 4", $message); } public function testLogsRequestAndResponseWireContentAndHeaders() { $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndata"); $client = new Client($this->getServer()->getUrl()); $plugin = new LogPlugin($this->logAdapter, LogPlugin::LOG_CONTEXT | LogPlugin::LOG_HEADERS | LogPlugin::LOG_BODY); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->put('', null, EntityBody::factory('send')); ob_start(); $request->send(); $message = ob_get_clean(); // Make sure the context was logged $this->assertContains('127.0.0.1 - "PUT / HTTP/1.1" - 200 4 - ', $message); // Check that the headers were logged $this->assertContains("PUT / HTTP/1.1\r\n", $message); $this->assertContainsIns("User-Agent: " . Utils::getDefaultUserAgent(), $message); $this->assertContainsIns("Content-Length: 4", $message); // Make sure the response headers are present with a line between the request and response $this->assertContains("\n< HTTP/1.1 200 OK\r\n< Content-Length: 4", $message); // Request payload $this->assertContains("\r\nsend", $message); // Response body $this->assertContains("data", $message); } public function testLogsRequestAndResponseWireContentAndHeadersNonStreamable() { $client = new Client($this->getServer()->getUrl()); $plugin = new LogPlugin($this->logAdapter, LogPlugin::LOG_CONTEXT | LogPlugin::LOG_HEADERS | LogPlugin::LOG_BODY); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->put(); // Send the response from the dummy server as the request body $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nsend"); $stream = fopen($this->getServer()->getUrl(), 'r'); $request->setBody(EntityBody::factory($stream, 4)); $tmpFile = tempnam('/tmp', 'testLogsRequestAndResponseWireContentAndHeadersNonStreamable'); $request->setResponseBody(EntityBody::factory(fopen($tmpFile, 'w'))); $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 8\r\n\r\nresponse"); ob_start(); $request->send(); $message = ob_get_clean(); // Make sure the context was logged $this->assertContains('127.0.0.1 - "PUT / HTTP/1.1" - 200 8 - ', $message); // Check that the headers were logged $this->assertContains("PUT / HTTP/1.1\r\n", $message); $this->assertContainsIns("User-Agent: " . Utils::getDefaultUserAgent(), $message); $this->assertContainsIns("Content-Length: 4", $message); // Request payload $this->assertContains("\r\nsend", $message); // Make sure the response headers are present with a line between the request and response $this->assertContains("\n< HTTP/1.1 200 OK\r\n< Content-Length: 8", $message); // Response body $this->assertContains("\nresponse", $message); unlink($tmpFile); } public function testLogsWhenExceptionsAreThrown() { $client = new Client($this->getServer()->getUrl()); $plugin = new LogPlugin($this->logAdapter, LogPlugin::LOG_VERBOSE); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get(); $this->getServer()->enqueue("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"); ob_start(); try { $request->send(); $this->fail('Exception for 404 was not thrown'); } catch (\Exception $e) {} $message = ob_get_clean(); $this->assertContains('127.0.0.1 - "GET / HTTP/1.1" - 404 0 - ', $message); $this->assertContains("GET / HTTP/1.1\r\n", $message); $this->assertContainsIns("User-Agent: " . Utils::getDefaultUserAgent(), $message); $this->assertContains("\n< HTTP/1.1 404 Not Found\r\n< Content-Length: 0", $message); } /** * Data provider to test log verbosity * * @return array */ public function verbosityProvider() { $u = str_replace('http://', '', substr($this->getServer()->getUrl(), 0, -1)); return array( array(LogPlugin::LOG_CONTEXT, "GET /abc HTTP/1.1\r\nHost: $u\r\n\r\n"), array(LogPlugin::LOG_CONTEXT | LogPlugin::LOG_DEBUG, "GET /abc HTTP/1.1\r\nHost: $u\r\n\r\n"), array(LogPlugin::LOG_CONTEXT | LogPlugin::LOG_DEBUG | LogPlugin::LOG_HEADERS, "GET /abc HTTP/1.1\r\nHost: $u\r\n\r\n"), array(LogPlugin::LOG_CONTEXT | LogPlugin::LOG_DEBUG | LogPlugin::LOG_HEADERS | LogPlugin::LOG_BODY, "GET /abc HTTP/1.1\r\nHost: $u\r\n\r\n"), array(LogPlugin::LOG_VERBOSE, "GET /abc HTTP/1.1\r\nHost: $u\r\n\r\n"), array(LogPlugin::LOG_HEADERS, "GET /abc HTTP/1.1\r\nHost: $u\r\n\r\n"), array(LogPlugin::LOG_CONTEXT, "PUT /abc HTTP/1.1\r\nHost: $u\r\nContent-Length: 4\r\n\r\ndata"), array(LogPlugin::LOG_VERBOSE, "PUT /abc HTTP/1.1\r\nHost: $u\r\nContent-Length: 4\r\n\r\ndata"), ); } /** * @dataProvider verbosityProvider */ public function testLogsTransactionsAtDifferentLevels($level, $request) { $client = new Client(); $request = RequestFactory::getInstance()->fromMessage($request); $request->setClient($client); $plugin = new LogPlugin(new ClosureLogAdapter( function($message, $priority, $extras = null) { echo $message . "\n"; } ), $level); $request->getEventDispatcher()->addSubscriber($plugin); $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nresp"); ob_start(); $request->send(); $gen = ob_get_clean(); $parts = explode("\n", trim($gen), 2); // Check if the context was properly logged if ($level & LogPlugin::LOG_CONTEXT) { $this->assertContains('127.0.0.1 - "' . $request->getMethod() . ' /', $gen); } // Check if the line count is 1 when just logging the context if ($level == LogPlugin::LOG_CONTEXT) { $this->assertEquals(1, count($parts)); return; } // Check if debug information is being logged if ($level & LogPlugin::LOG_DEBUG) { $this->assertContains("\n* Connected to 127.0.0.1 (127.0.0.1) port", $gen); } // Check if the headers are being properly logged if ($level & LogPlugin::LOG_HEADERS) { $this->assertContains("> " . $request->getMethod() . " /abc HTTP/1.1", $gen); // Ensure headers following the request line are logged $this->assertContains("Accept: */*", $gen); // Ensure the response headers are being logged $this->assertContains("< HTTP/1.1 200 OK\r\n< Content-Length: 4", $gen); } // Check if the body of the request and response are being logged if ($level & LogPlugin::LOG_BODY) { if ($request instanceof EntityEnclosingRequest) { $this->assertContains("\r\n\r\n" . $request->getBody(true), $gen); } $this->assertContains("\nresp", $gen); } } } tests/Guzzle/Tests/Http/Plugin/Md5ValidatorPluginTest.php000064400000010227151742734600017503 0ustar00create('GET', 'http://www.test.com/'); $request->getEventDispatcher()->addSubscriber($plugin); $body = 'abc'; $hash = md5($body); $response = new Response(200, array( 'Content-MD5' => $hash, 'Content-Length' => 3 ), 'abc'); $request->dispatch('request.complete', array( 'response' => $response )); // Try again with no Content-MD5 $response->removeHeader('Content-MD5'); $request->dispatch('request.complete', array( 'response' => $response )); } /** * @covers Guzzle\Http\Plugin\Md5ValidatorPlugin * @expectedException UnexpectedValueException */ public function testThrowsExceptionOnInvalidMd5() { $plugin = new Md5ValidatorPlugin(); $request = RequestFactory::getInstance()->create('GET', 'http://www.test.com/'); $request->getEventDispatcher()->addSubscriber($plugin); $request->dispatch('request.complete', array( 'response' => new Response(200, array( 'Content-MD5' => 'foobar', 'Content-Length' => 3 ), 'abc') )); } /** * @covers Guzzle\Http\Plugin\Md5ValidatorPlugin */ public function testSkipsWhenContentLengthIsTooLarge() { $plugin = new Md5ValidatorPlugin(false, 1); $request = RequestFactory::getInstance()->create('GET', 'http://www.test.com/'); $request->getEventDispatcher()->addSubscriber($plugin); $request->dispatch('request.complete', array( 'response' => new Response(200, array( 'Content-MD5' => 'foobar', 'Content-Length' => 3 ), 'abc') )); } /** * @covers Guzzle\Http\Plugin\Md5ValidatorPlugin */ public function testProperlyValidatesWhenUsingContentEncoding() { $plugin = new Md5ValidatorPlugin(true); $request = RequestFactory::getInstance()->create('GET', 'http://www.test.com/'); $request->getEventDispatcher()->addSubscriber($plugin); // Content-MD5 is the MD5 hash of the canonical content after all // content-encoding has been applied. Because cURL will automaticall // decompress entity bodies, we need to re-compress it to calculate. $body = EntityBody::factory('abc'); $body->compress(); $hash = $body->getContentMd5(); $body->uncompress(); $response = new Response(200, array( 'Content-MD5' => $hash, 'Content-Encoding' => 'gzip' ), 'abc'); $request->dispatch('request.complete', array( 'response' => $response )); $this->assertEquals('abc', $response->getBody(true)); // Try again with an unknown encoding $response = new Response(200, array( 'Content-MD5' => $hash, 'Content-Encoding' => 'foobar' ), 'abc'); $request->dispatch('request.complete', array( 'response' => $response )); // Try again with compress $body->compress('bzip2.compress'); $response = new Response(200, array( 'Content-MD5' => $body->getContentMd5(), 'Content-Encoding' => 'compress' ), 'abc'); $request->dispatch('request.complete', array( 'response' => $response )); // Try again with encoding and disabled content-encoding checks $request->getEventDispatcher()->removeSubscriber($plugin); $plugin = new Md5ValidatorPlugin(false); $request->getEventDispatcher()->addSubscriber($plugin); $request->dispatch('request.complete', array( 'response' => $response )); } } tests/Guzzle/Tests/Http/Plugin/MockPluginTest.php000064400000014413151742734600016102 0ustar00assertInternalType('array', MockPlugin::getSubscribedEvents()); } /** * @covers Guzzle\Http\Plugin\MockPlugin::getAllEvents */ public function testDescribesEvents() { $this->assertInternalType('array', MockPlugin::getAllEvents()); } /** * @covers Guzzle\Http\Plugin\MockPlugin::__construct * @covers Guzzle\Http\Plugin\MockPlugin::isTemporary */ public function testCanBeTemporary() { $plugin = new MockPlugin(); $this->assertFalse($plugin->isTemporary()); $plugin = new MockPlugin(null, true); $this->assertTrue($plugin->isTemporary()); } /** * @covers Guzzle\Http\Plugin\MockPlugin::count */ public function testIsCountable() { $plugin = new MockPlugin(); $plugin->addResponse(Response::fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")); $this->assertEquals(1, count($plugin)); } /** * @covers Guzzle\Http\Plugin\MockPlugin::clearQueue * @depends testIsCountable */ public function testCanClearQueue() { $plugin = new MockPlugin(); $plugin->addResponse(Response::fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")); $plugin->clearQueue(); $this->assertEquals(0, count($plugin)); } /** * @covers Guzzle\Http\Plugin\MockPlugin::getQueue */ public function testCanInspectQueue() { $plugin = new MockPlugin(); $this->assertInternalType('array', $plugin->getQueue()); $plugin->addResponse(Response::fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")); $queue = $plugin->getQueue(); $this->assertInternalType('array', $queue); $this->assertEquals(1, count($queue)); } /** * @covers Guzzle\Http\Plugin\MockPlugin::getMockFile */ public function testRetrievesResponsesFromFiles() { $response = MockPlugin::getMockFile(__DIR__ . '/../../TestData/mock_response'); $this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $response); $this->assertEquals(200, $response->getStatusCode()); } /** * @covers Guzzle\Http\Plugin\MockPlugin::getMockFile * @expectedException InvalidArgumentException */ public function testThrowsExcpetionWhenResponseFileIsNotFound() { MockPlugin::getMockFile('missing/filename'); } /** * @covers Guzzle\Http\Plugin\MockPlugin::addResponse * @expectedException InvalidArgumentException */ public function testInvalidResponsesThrowAnException() { $p = new MockPlugin(); $p->addResponse($this); } /** * @covers Guzzle\Http\Plugin\MockPlugin::addResponse */ public function testAddsResponseObjectsToQueue() { $p = new MockPlugin(); $response = Response::fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); $p->addResponse($response); $this->assertEquals(array($response), $p->getQueue()); } /** * @covers Guzzle\Http\Plugin\MockPlugin::addResponse */ public function testAddsResponseFilesToQueue() { $p = new MockPlugin(); $p->addResponse(__DIR__ . '/../../TestData/mock_response'); $this->assertEquals(1, count($p)); } /** * @covers Guzzle\Http\Plugin\MockPlugin::onRequestCreate * @covers Guzzle\Http\Plugin\MockPlugin::addResponse * @covers Guzzle\Http\Plugin\MockPlugin::dequeue * @depends testAddsResponseFilesToQueue */ public function testAddsMockResponseToRequestFromClient() { $p = new MockPlugin(); $response = MockPlugin::getMockFile(__DIR__ . '/../../TestData/mock_response'); $p->addResponse($response); $client = new Client('http://localhost:123/'); $client->getEventDispatcher()->addSubscriber($p, 9999); $request = $client->get(); $request->send(); $this->assertSame($response, $request->getResponse()); $this->assertEquals(0, count($p)); } /** * @covers Guzzle\Http\Plugin\MockPlugin::onRequestCreate * @depends testAddsResponseFilesToQueue */ public function testUpdateIgnoresWhenEmpty() { $p = new MockPlugin(); $p->onRequestCreate(new Event()); } /** * @covers Guzzle\Http\Plugin\MockPlugin::onRequestCreate * @covers Guzzle\Http\Plugin\MockPlugin::dequeue * @depends testAddsMockResponseToRequestFromClient */ public function testDetachesTemporaryWhenEmpty() { $p = new MockPlugin(null, true); $p->addResponse(MockPlugin::getMockFile(__DIR__ . '/../../TestData/mock_response')); $client = new Client('http://localhost:123/'); $client->getEventDispatcher()->addSubscriber($p, 9999); $request = $client->get(); $request->send(); $this->assertFalse($this->hasSubscriber($client, $p)); } /** * @covers Guzzle\Http\Plugin\MockPlugin::__construct */ public function testLoadsResponsesFromConstructor() { $p = new MockPlugin(array(new Response(200))); $this->assertEquals(1, $p->count()); } /** * @covers Guzzle\Http\Plugin\MockPlugin::getReceivedRequests * @covers Guzzle\Http\Plugin\MockPlugin::flush */ public function testStoresMockedRequests() { $p = new MockPlugin(array(new Response(200), new Response(200))); $client = new Client('http://localhost:123/'); $client->getEventDispatcher()->addSubscriber($p, 9999); $request1 = $client->get(); $request1->send(); $this->assertEquals(array($request1), $p->getReceivedRequests()); $request2 = $client->get(); $request2->send(); $this->assertEquals(array($request1, $request2), $p->getReceivedRequests()); $p->flush(); $this->assertEquals(array(), $p->getReceivedRequests()); } } tests/Guzzle/Tests/Http/Plugin/OauthPluginTest.php000064400000014177151742734600016300 0ustar00 'foo', 'consumer_secret' => 'bar', 'token' => 'count', 'token_secret' => 'dracula' ); protected function getRequest() { return RequestFactory::getInstance()->create('POST', 'http://www.test.com/path?a=b&c=d', null, array( 'e' => 'f' )); } /** * @covers Guzzle\Http\Plugin\OauthPlugin::getSubscribedEvents */ public function testSubscribesToEvents() { $events = OauthPlugin::getSubscribedEvents(); $this->assertArrayHasKey('request.before_send', $events); } /** * @covers Guzzle\Http\Plugin\OauthPlugin::__construct */ public function testAcceptsConfigurationData() { $p = new OauthPlugin($this->config); // Access the config object $class = new \ReflectionClass($p); $property = $class->getProperty('config'); $property->setAccessible(true); $config = $property->getValue($p); $this->assertEquals('foo', $config['consumer_key']); $this->assertEquals('bar', $config['consumer_secret']); $this->assertEquals('count', $config['token']); $this->assertEquals('dracula', $config['token_secret']); $this->assertEquals('1.0', $config['version']); $this->assertEquals('HMAC-SHA1', $config['signature_method']); } /** * @covers Guzzle\Http\Plugin\OauthPlugin::getStringToSign */ public function testCreatesStringToSignFromPostRequest() { $p = new OauthPlugin($this->config); $request = $this->getRequest(); $this->assertEquals( // Method and URL 'POST&http%3A%2F%2Fwww.test.com%2Fpath' . // Sorted parameters from query string and body '&a%3Db%26c%3Dd%26e%3Df%26oauth_consumer_key%3Dfoo' . '%26oauth_nonce%3D22c3b010c30c17043c3d2dd3a7aa3ae6c5549b32%26' . 'oauth_signature_method%3DHMAC-SHA1' . '%26oauth_timestamp%3D' . self::TIMESTAMP . '%26oauth_token%3Dcount%26oauth_version%3D1.0', $p->getStringToSign($request, self::TIMESTAMP) ); } /** * @covers Guzzle\Http\Plugin\OauthPlugin::getStringToSign */ public function testCreatesStringToSignFromPostRequestWithCustomContentType() { $p = new OauthPlugin($this->config); $request = $this->getRequest(); $request->setHeader('Content-Type', 'Foo'); $this->assertEquals( // Method and URL 'POST&http%3A%2F%2Fwww.test.com%2Fpath' . // Sorted parameters from query string and body '&a%3Db%26c%3Dd%26oauth_consumer_key%3Dfoo' . '%26oauth_nonce%3D22c3b010c30c17043c3d2dd3a7aa3ae6c5549b32%26' . 'oauth_signature_method%3DHMAC-SHA1' . '%26oauth_timestamp%3D' . self::TIMESTAMP . '%26oauth_token%3Dcount%26oauth_version%3D1.0', $p->getStringToSign($request, self::TIMESTAMP) ); } /** * @covers Guzzle\Http\Plugin\OauthPlugin::getStringToSign * @depends testCreatesStringToSignFromPostRequest */ public function testConvertsBooleansToStrings() { $p = new OauthPlugin($this->config); $request = $this->getRequest(); $request->getQuery()->set('a', true); $request->getQuery()->set('c', false); $this->assertContains('&a%3Dtrue%26c%3Dfalse', $p->getStringToSign($request, self::TIMESTAMP)); } /** * @covers Guzzle\Http\Plugin\OauthPlugin::getSignature * @depends testCreatesStringToSignFromPostRequest */ public function testSignsStrings() { $p = new OauthPlugin(array_merge($this->config, array( 'signature_callback' => function($string, $key) { return "_{$string}|{$key}_"; } ))); $request = $this->getRequest(); $sig = $p->getSignature($request, self::TIMESTAMP); $this->assertEquals( '_POST&http%3A%2F%2Fwww.test.com%2Fpath&a%3Db%26c%3Dd%26e%3Df%26oauth_consumer_key%3Dfoo' . '%26oauth_nonce%3D22c3b010c30c17043c3d2dd3a7aa3ae6c5549b32%26oauth_signature_method%3DHMAC-SHA1' . '%26oauth_timestamp%3D' . self::TIMESTAMP . '%26oauth_token%3Dcount%26oauth_version%3D1.0|' . 'bar&dracula_', base64_decode($sig) ); } /** * @covers Guzzle\Http\Plugin\OauthPlugin::onRequestBeforeSend * @covers Guzzle\Http\Plugin\OauthPlugin::__construct */ public function testSignsOauthRequests() { $p = new OauthPlugin($this->config); $event = new Event(array( 'request' => $this->getRequest(), 'timestamp' => self::TIMESTAMP )); $p->onRequestBeforeSend($event); $this->assertTrue($event['request']->hasHeader('Authorization')); $this->assertEquals('OAuth oauth_consumer_key="foo", ' . 'oauth_nonce="22c3b010c30c17043c3d2dd3a7aa3ae6c5549b32", ' . 'oauth_signature="BqUAsVHc1cYJ3FA9%2BtLMkJnizJk%3D", ' . 'oauth_signature_method="HMAC-SHA1", ' . 'oauth_timestamp="' . self::TIMESTAMP . '", ' . 'oauth_token="count", ' . 'oauth_version="1.0"', (string) $event['request']->getHeader('Authorization') ); } /** * @covers Guzzle\Http\Plugin\OauthPlugin::generateNonce */ public function testGeneratesUniqueNonce() { $p = new OauthPlugin($this->config); $method = new \ReflectionMethod('Guzzle\Http\Plugin\OauthPlugin', 'generateNonce'); $method->setAccessible(true); $request = RequestFactory::getInstance()->create('GET', 'http://www.example.com'); $result = $method->invoke($p, $request, 1335936584); $this->assertEquals('29f72fa5fc2893972060b28a0df8623c41cbb5d2', $result); } } tests/Guzzle/Tests/Http/QueryStringTest.php000064400000017424151742734600015075 0ustar00q = new QueryString(); } /** * @covers \Guzzle\Http\QueryString::getFieldSeparator */ public function testGetFieldSeparator() { $this->assertEquals('&', $this->q->getFieldSeparator()); } /** * @covers \Guzzle\Http\QueryString::getPrefix */ public function testGetPrefix() { $this->assertEquals('?', $this->q->getPrefix()); } /** * @covers \Guzzle\Http\QueryString::getValueSeparator */ public function testGetValueSeparator() { $this->assertEquals('=', $this->q->getValueSeparator()); } /** * @covers \Guzzle\Http\QueryString::isEncodingFields * @covers \Guzzle\Http\QueryString::setEncodeFields */ public function testIsEncodingFields() { $this->assertTrue($this->q->isEncodingFields()); $this->assertEquals($this->q, $this->q->setEncodeFields(false)); $this->assertFalse($this->q->isEncodingFields()); } /** * @covers \Guzzle\Http\QueryString::isEncodingValues * @covers \Guzzle\Http\QueryString::setEncodeValues */ public function testIsEncodingValues() { $this->assertTrue($this->q->isEncodingValues()); $this->assertEquals($this->q, $this->q->setEncodeValues(false)); $this->assertFalse($this->q->isEncodingValues()); } /** * @covers \Guzzle\Http\QueryString::setFieldSeparator * @covers \Guzzle\Http\QueryString::setFieldSeparator */ public function testSetFieldSeparator() { $this->assertEquals($this->q, $this->q->setFieldSeparator('/')); $this->assertEquals('/', $this->q->getFieldSeparator()); } /** * @covers \Guzzle\Http\QueryString::setPrefix * @covers \Guzzle\Http\QueryString::getPrefix */ public function testSetPrefix() { $this->assertEquals($this->q, $this->q->setPrefix('')); $this->assertEquals('', $this->q->getPrefix()); } /** * @covers \Guzzle\Http\QueryString::setValueSeparator * @covers \Guzzle\Http\QueryString::getValueSeparator */ public function testSetValueSeparator() { $this->assertEquals($this->q, $this->q->setValueSeparator('/')); $this->assertEquals('/', $this->q->getValueSeparator()); } /** * @covers \Guzzle\Http\QueryString::urlEncode * @covers \Guzzle\Http\QueryString::encodeData * @covers \Guzzle\Http\QueryString::replace */ public function testUrlEncode() { $params = array( 'test' => 'value', 'test 2' => 'this is a test?', 'test3' => array('v1', 'v2', 'v3'), 'ሴ' => 'bar' ); $encoded = array( 'test' => 'value', 'test%202' => rawurlencode('this is a test?'), 'test3%5B0%5D' => 'v1', 'test3%5B1%5D' => 'v2', 'test3%5B2%5D' => 'v3', '%E1%88%B4' => 'bar' ); $this->q->replace($params); $this->assertEquals($encoded, $this->q->urlEncode()); // Disable field encoding $testData = array( 'test 2' => 'this is a test' ); $this->q->replace($testData); $this->q->setEncodeFields(false); $this->assertEquals(array( 'test 2' => rawurlencode('this is a test') ), $this->q->urlEncode()); // Disable encoding of both fields and values $this->q->setEncodeValues(false); $this->assertEquals($testData, $this->q->urlEncode()); } /** * @covers \Guzzle\Http\QueryString * @covers \Guzzle\Http\QueryString::__toString * @covers \Guzzle\Http\QueryString::setEncodeFields * @covers \Guzzle\Http\QueryString::replace * @covers \Guzzle\Http\QueryString::setAggregateFunction * @covers \Guzzle\Http\QueryString::encodeData */ public function testToString() { // Check with no parameters $this->assertEquals('', $this->q->__toString()); $params = array( 'test' => 'value', 'test 2' => 'this is a test?', 'test3' => array( 'v1', 'v2', 'v3' ) ); $this->q->replace($params); $this->assertEquals('?test=value&test%202=this%20is%20a%20test%3F&test3%5B0%5D=v1&test3%5B1%5D=v2&test3%5B2%5D=v3', $this->q->__toString()); $this->q->setEncodeFields(false); $this->q->setEncodeValues(false); $this->assertEquals('?test=value&test 2=this is a test?&test3[0]=v1&test3[1]=v2&test3[2]=v3', $this->q->__toString()); // Use an alternative aggregator $this->q->setAggregateFunction(array($this->q, 'aggregateUsingComma')); $this->assertEquals('?test=value&test 2=this is a test?&test3=v1,v2,v3', $this->q->__toString()); } /** * @covers \Guzzle\Http\QueryString::__toString * @covers \Guzzle\Http\QueryString::aggregateUsingDuplicates */ public function testAllowsMultipleValuesPerKey() { $q = new QueryString(); $q->add('facet', 'size'); $q->add('facet', 'width'); $q->add('facet.field', 'foo'); // Use the duplicate aggregator $q->setAggregateFunction(array($this->q, 'aggregateUsingDuplicates')); $this->assertEquals('?facet=size&facet=width&facet.field=foo', $q->__toString()); } /** * @covers \Guzzle\Http\QueryString::__toString * @covers \Guzzle\Http\QueryString::encodeData * @covers \Guzzle\Http\QueryString::aggregateUsingPhp */ public function testAllowsNestedQueryData() { $this->q->replace(array( 'test' => 'value', 't' => array( 'v1' => 'a', 'v2' => 'b', 'v3' => array( 'v4' => 'c', 'v5' => 'd', ) ) )); $this->q->setEncodeFields(false); $this->q->setEncodeValues(false); $this->assertEquals('?test=value&t[v1]=a&t[v2]=b&t[v3][v4]=c&t[v3][v5]=d', $this->q->__toString()); } public function parseQueryProvider() { return array( // Ensure that multiple query string values are allowed per value array('q=a&q=b', array( 'q' => array('a', 'b') )), // Ensure that PHP array style query string values are parsed array('q[]=a&q[]=b', array( 'q' => array('a', 'b') )), // Ensure that decimals are allowed in query strings array('q.a=a&q.b=b', array( 'q.a' => 'a', 'q.b' => 'b' )), // Ensure that query string values are percent decoded array('q%20a=a%20b', array( 'q a' => 'a b' )), // Ensure that values can be set without have a value array('q', array( 'q' => null )), ); } /** * @covers Guzzle\Http\QueryString::fromString * @dataProvider parseQueryProvider */ public function testParsesQueryStrings($query, $data) { $query = QueryString::fromString($query); $this->assertEquals($data, $query->getAll()); } /** * @covers Guzzle\Http\QueryString::fromString */ public function testProperlyDealsWithDuplicateQueryStringValues() { $query = QueryString::fromString('foo=a&foo=b&?µ=c'); $this->assertEquals(array('a', 'b'), $query->get('foo')); $this->assertEquals('c', $query->get('?µ')); } } tests/Guzzle/Tests/Http/Server.php000064400000014777151742734600013217 0ustar00port = $port ?: self::DEFAULT_PORT; $this->client = new Client($this->getUrl()); } /** * Destructor to safely shutdown the node.js server if it is still running */ public function __destruct() { // Disabled for now if (false && $this->running) { try { $this->stop(); } catch (\Exception $e) {} } } /** * Flush the received requests from the server * * @return bool Returns TRUE on success or FALSE on failure * @throws RuntimeException */ public function flush() { if (!$this->isRunning()) { return false; } return $this->client->delete('guzzle-server/requests') ->send()->getStatusCode() == 200; } /** * Queue an array of responses or a single response on the server. * * Any currently queued responses will be overwritten. Subsequent requests * on the server will return queued responses in FIFO order. * * @param array|Response $responses A single or array of Responses to queue * * @return bool Returns TRUE on success or FALSE on failure * @throws BadResponseException */ public function enqueue($responses) { $data = array(); foreach ((array) $responses as $response) { // Create the response object from a string if (is_string($response)) { $response = Response::fromMessage($response); } elseif (!($response instanceof Response)) { throw new BadResponseException( 'Responses must be strings or implement Response' ); } $data[] = array( 'statusCode' => $response->getStatusCode(), 'reasonPhrase' => $response->getReasonPhrase(), 'headers' => $response->getHeaders()->getAll(), 'body' => $response->getBody(true) ); } $request = $this->client->put('guzzle-server/responses', null, json_encode($data)); $request->removeHeader('Expect'); $response = $request->send(); return $response->getStatusCode() == 200; } /** * Check if the server is running * * @return bool */ public function isRunning() { if ($this->running) { return true; } else { $fp = @fsockopen('127.0.0.1', $this->port, $errno, $errstr, 1); if (!$fp) { return false; } else { fclose($fp); return true; } } } /** * Get the URL to the server * * @return string */ public function getUrl() { return 'http://127.0.0.1:' . $this->getPort() . '/'; } /** * Get the port that the server is listening on * * @return int */ public function getPort() { return $this->port; } /** * Get all of the received requests * * @param bool $hydrate Set to TRUE to turn the messages into * actual {@see RequestInterface} objects. If $hydrate is FALSE, * requests will be returned as strings. * * @return array * @throws RuntimeException */ public function getReceivedRequests($hydrate = false) { $data = array(); if ($this->isRunning()) { $response = $this->client->get('guzzle-server/requests')->send(); $data = array_filter(explode(self::REQUEST_DELIMITER, $response->getBody(true))); if ($hydrate) { $data = array_map(function($message) { return RequestFactory::getInstance()->fromMessage($message); }, $data); } } return $data; } /** * Start running the node.js server in the background */ public function start() { if (!$this->isRunning()) { exec('node ' . __DIR__ . \DIRECTORY_SEPARATOR . 'server.js ' . $this->port . ' >> /tmp/server.log 2>&1 &'); // Wait at most 5 seconds for the server the setup before proceeding $start = time(); while (!$this->isRunning() && time() - $start < 5); if (!$this->isRunning()) { throw new RuntimeException( 'Unable to contact server.js. Have you installed node.js ' . 'v0.5.0+? The node.js executable, node, must also be in ' . 'your path.' ); } } $this->running = true; } /** * Stop running the node.js server * * @return bool Returns TRUE on success or FALSE on failure * @throws RuntimeException */ public function stop() { if (!$this->isRunning()) { return false; } $this->running = false; return $this->client->delete('guzzle-server')->send() ->getStatusCode() == 200; } } tests/Guzzle/Tests/Http/UrlTest.php000064400000021066151742734600013340 0ustar00assertEquals(80, Url::factory('http://www.test.com/')->getPort()); $this->assertEquals(443, Url::factory('https://www.test.com/')->getPort()); $this->assertEquals(null, Url::factory('ftp://www.test.com/')->getPort()); $this->assertEquals(8192, Url::factory('http://www.test.com:8192/')->getPort()); } /** * @covers Guzzle\Http\Url::__clone */ public function testCloneCreatesNewInternalObjects() { $u1 = Url::factory('http://www.test.com/'); $u2 = clone $u1; $this->assertNotSame($u1->getQuery(), $u2->getQuery()); } /** * @covers Guzzle\Http\Url::__construct * @covers Guzzle\Http\Url::factory * @covers Guzzle\Http\Url::__toString * @covers Guzzle\Http\Url::isAbsolute */ public function testValidatesUrlPartsInFactory() { $url = Url::factory('/index.php'); $this->assertEquals('/index.php', (string) $url); $this->assertFalse($url->isAbsolute()); $url = 'http://michael:test@test.com:80/path/123?q=abc#test'; $u = Url::factory($url); $this->assertEquals('http://michael:test@test.com/path/123?q=abc#test', (string) $u); $this->assertTrue($u->isAbsolute()); } /** * @covers Guzzle\Http\Url */ public function testUrlStoresParts() { $url = Url::factory('http://test:pass@www.test.com:8081/path/path2/?a=1&b=2#fragment'); $this->assertEquals('http', $url->getScheme()); $this->assertEquals('test', $url->getUsername()); $this->assertEquals('pass', $url->getPassword()); $this->assertEquals('www.test.com', $url->getHost()); $this->assertEquals(8081, $url->getPort()); $this->assertEquals('/path/path2/', $url->getPath()); $this->assertEquals('fragment', $url->getFragment()); $this->assertEquals('?a=1&b=2', (string) $url->getQuery()); $this->assertEquals(array( 'fragment' => 'fragment', 'host' => 'www.test.com', 'pass' => 'pass', 'path' => '/path/path2/', 'port' => 8081, 'query' => '?a=1&b=2', 'query_prefix' => '?', 'scheme' => 'http', 'user' => 'test' ), $url->getParts()); } /** * @covers Guzzle\Http\Url::setPath * @covers Guzzle\Http\Url::getPath * @covers Guzzle\Http\Url::getPathSegments * @covers Guzzle\Http\Url::buildUrl */ public function testHandlesPathsCorrectly() { $url = Url::factory('http://www.test.com'); $this->assertEquals('/', $url->getPath()); $url->setPath('test'); $this->assertEquals('/test', $url->getPath()); $url->setPath('/test/123/abc'); $this->assertEquals(array('test', '123', 'abc'), $url->getPathSegments()); $parts = parse_url('http://www.test.com/test'); $parts['path'] = ''; $this->assertEquals('http://www.test.com/', Url::buildUrl($parts)); $parts['path'] = 'test'; $this->assertEquals('http://www.test.com/test', Url::buildUrl($parts)); } /** * @covers Guzzle\Http\Url::buildUrl */ public function testAddsQueryStringIfPresent() { $this->assertEquals('/?foo=bar', Url::buildUrl(array( 'query' => 'foo=bar' ))); } /** * @covers Guzzle\Http\Url::addPath */ public function testAddsToPath() { // Does nothing here $this->assertEquals('http://e.com/base?a=1', (string) Url::factory('http://e.com/base?a=1')->addPath(false)); $this->assertEquals('http://e.com/base?a=1', (string) Url::factory('http://e.com/base?a=1')->addPath('')); $this->assertEquals('http://e.com/base?a=1', (string) Url::factory('http://e.com/base?a=1')->addPath('/')); $this->assertEquals('http://e.com/base/relative?a=1', (string) Url::factory('http://e.com/base?a=1')->addPath('relative')); $this->assertEquals('http://e.com/base/relative?a=1', (string) Url::factory('http://e.com/base?a=1')->addPath('/relative')); } /** * URL combination data provider * * @return array */ public function urlCombineDataProvider() { return array( array('http://www.example.com/', 'http://www.example.com/', 'http://www.example.com/'), array('http://www.example.com/path', '/absolute', 'http://www.example.com/absolute'), array('http://www.example.com/path', '/absolute?q=2', 'http://www.example.com/absolute?q=2'), array('http://www.example.com/path', 'more', 'http://www.example.com/path/more'), array('http://www.example.com/path', 'more?q=1', 'http://www.example.com/path/more?q=1'), array('http://www.example.com/', '?q=1', 'http://www.example.com/?q=1'), array('http://www.example.com/path', 'http://test.com', 'http://test.com/path'), array('http://www.example.com:8080/path', 'http://test.com', 'http://test.com/path'), array('http://www.example.com:8080/path', '?q=2#abc', 'http://www.example.com:8080/path?q=2#abc'), array('http://u:a@www.example.com/path', 'test', 'http://u:a@www.example.com/path/test'), array('http://www.example.com/path', 'http://u:a@www.example.com/', 'http://u:a@www.example.com/path'), array('/path?q=2', 'http://www.test.com/', 'http://www.test.com/path?q=2'), ); } /** * @covers Guzzle\Http\Url::combine * @dataProvider urlCombineDataProvider */ public function testCombinesUrls($a, $b, $c) { $this->assertEquals($c, (string) Url::factory($a)->combine($b)); } /** * @covers Guzzle\Http\Url */ public function testHasGettersAndSetters() { $url = Url::factory('http://www.test.com/'); $this->assertEquals('example.com', $url->setHost('example.com')->getHost()); $this->assertEquals('8080', $url->setPort(8080)->getPort()); $this->assertEquals('/foo/bar', $url->setPath(array('foo', 'bar'))->getPath()); $this->assertEquals('a', $url->setPassword('a')->getPassword()); $this->assertEquals('b', $url->setUsername('b')->getUsername()); $this->assertEquals('abc', $url->setFragment('abc')->getFragment()); $this->assertEquals('https', $url->setScheme('https')->getScheme()); $this->assertEquals('?a=123', (string) $url->setQuery('a=123')->getQuery()); $this->assertEquals('https://b:a@example.com:8080/foo/bar?a=123#abc', (string)$url); $this->assertEquals('?b=boo', (string) $url->setQuery(new QueryString(array( 'b' => 'boo' )))->getQuery()); $this->assertEquals('https://b:a@example.com:8080/foo/bar?b=boo#abc', (string)$url); } /** * @covers Guzzle\Http\Url::setQuery */ public function testSetQueryAcceptsArray() { $url = Url::factory('http://www.test.com'); $url->setQuery(array('a' => 'b')); $this->assertEquals('http://www.test.com/?a=b', (string) $url); } function urlProvider() { return array( array('/foo/..', '/'), array('//foo//..', '/'), array('/foo/../..', '/'), array('/foo/../.', '/'), array('/./foo/..', '/'), array('/./foo', '/foo'), array('/./foo/', '/foo/'), array('/./foo/bar/baz/pho/../..', '/foo/bar'), array('*', '*') ); } /** * @covers Guzzle\Http\Url::normalizePath * @dataProvider urlProvider */ public function testNormalizesPaths($path, $result) { $url = Url::factory('http://www.example.com/'); $url->setPath($path)->normalizePath(); $this->assertEquals($result, $url->getPath()); } /** * @covers Guzzle\Http\Url::setHost */ public function testSettingHostWithPortModifiesPort() { $url = Url::factory('http://www.example.com'); $url->setHost('foo:8983'); $this->assertEquals('foo', $url->getHost()); $this->assertEquals(8983, $url->getPort()); } /** * @covers Guzzle\Http\Url::buildUrl */ public function testUrlOnlyContainsHashWhenHashIsNotEmpty() { $url = Url::factory('http://www.example.com/'); $url->setFragment(''); $this->assertEquals('http://www.example.com/', (string) $url); } } tests/Guzzle/Tests/Http/UtilsTest.php000064400000002374151742734600013677 0ustar00assertEquals(gmdate($fmt), Utils::getHttpDate('now')); $this->assertEquals(gmdate($fmt), Utils::getHttpDate(strtotime('now'))); $this->assertEquals(gmdate($fmt, strtotime('+1 day')), Utils::getHttpDate('+1 day')); } /** * @covers Guzzle\Http\Utils::getDefaultUserAgent */ public function testGetDefaultUserAgent() { // Clear out the user agent cache $refObject = new \ReflectionClass('Guzzle\Http\Utils'); $refProperty = $refObject->getProperty('userAgent'); $refProperty->setAccessible(true); $refProperty->setValue(null, null); $version = curl_version(); $agent = sprintf('Guzzle/%s curl/%s PHP/%s openssl/%s', Version::VERSION, PHP_VERSION, $version['version'], $version['ssl_version']); $this->assertEquals($agent, Utils::getDefaultUserAgent()); // Get it from cache this time $this->assertEquals($agent, Utils::getDefaultUserAgent()); } } tests/Guzzle/Tests/Http/server.js000064400000012671151742734600013073 0ustar00/** * Guzzle node.js test server to return queued responses to HTTP requests and * expose a RESTful API for enqueueing responses and retrieving the requests * that have been received. * * - Delete all requests that have been received: * DELETE /guzzle-server/requests * Host: 127.0.0.1:8124 * * - Enqueue responses * PUT /guzzle-server/responses * Host: 127.0.0.1:8124 * * [{ "statusCode": 200, "reasonPhrase": "OK", "headers": {}, "body": "" }] * * - Get the recevied requests * GET /guzzle-server/requests * Host: 127.0.0.1:8124 * * - Shutdown the server * DELETE /guzzle-server * Host: 127.0.0.1:8124 * * @package Guzzle PHP * @license See the LICENSE file that was distributed with this source code. */ var http = require("http"); /** * Guzzle node.js server * @class */ var GuzzleServer = function(port) { this.port = port; this.log = log; this.responses = []; this.requests = []; var that = this; /** * Handle a Guzzle Server control request * @param (String) request HTTP request as a string * @param (ServerRequest) req Received server request * @param (ServerResponse) res Outgoing server response */ var controlRequest = function(request, req, res) { if (req.method == "DELETE") { if (req.url == "/guzzle-server/requests") { // Clear the received requests that.requests = []; res.writeHead(200, "OK", { "Content-Length": 0 }); res.end(); if (this.log) { console.log("Flushing requests"); } } else if (req.url == "/guzzle-server") { // Shutdown the server res.writeHead(200, "OK", { "Content-Length": 0, "Connection": "close" }); res.end(); if (this.log) { console.log("Shutting down"); } that.server.close(); } } else if (req.method == "GET") { if (req.url === "/guzzle-server/requests") { // Get received requests var data = that.requests.join("\n----[request]\n"); res.writeHead(200, "OK", { "Content-Length": data.length }); res.end(data); if (that.log) { console.log("Sending receiving requests"); } } } else if (req.method == "PUT") { if (req.url == "/guzzle-server/responses") { if (that.log) { console.log("Adding responses..."); } // Received respones to queue var data = request.split("\r\n\r\n")[1]; if (!data) { if (that.log) { console.log("No response data was provided"); } res.writeHead(500, "NO RESPONSES IN REQUEST", { "Content-Length": 0 }); } else { that.responses = eval("(" + data + ")"); if (that.log) { console.log(that.responses); } res.writeHead(200, "OK", { "Content-Length": 0 }); } res.end(); } } }; /** * Received a complete request * @param (String) request HTTP request as a string * @param (ServerRequest) req Received server request * @param (ServerResponse) res Outgoing server response */ var receivedRequest = function(request, req, res) { if (req.url.indexOf("/guzzle-server") === 0) { controlRequest(request, req, res); } else { var response = that.responses.shift(); res.writeHead(response.statusCode, response.reasonPhrase, response.headers); res.end(response.body); that.requests.push(request); } }; /** * Start the node.js Guzzle server */ this.start = function() { that.server = http.createServer(function(req, res) { // If this is not a control request and no responses are in queue, return 500 response if (req.url.indexOf("/guzzle-server") == -1 && !that.responses.length) { res.writeHead(500); res.end("No responses in queue"); return; } // Begin building the request message as a string var request = req.method + " " + req.url + " HTTP/" + req.httpVersion + "\r\n"; // Add the request headers for (var i in req.headers) { request += i + ": " + req.headers[i] + "\r\n"; } request += "\r\n"; // Receive each chunk of the request body req.addListener("data", function(chunk) { request += chunk; }); // Called when the request completes req.addListener("end", function() { receivedRequest(request, req, res); }); }); that.server.listen(port, "127.0.0.1"); if (this.log) { console.log("Server running at http://127.0.0.1:8124/"); } }; }; // Get the port from the arguments port = process.argv.length >= 3 ? process.argv[2] : 8124; log = process.argv.length >= 4 ? process.argv[3] : false; // Start the server server = new GuzzleServer(port, log); server.start(); tests/Guzzle/Tests/Mock/ExceptionMock.php000064400000000223151742734600014450 0ustar00multiHandle; } } tests/Guzzle/Tests/Mock/MockObserver.php000064400000002667151742734600014317 0ustar00events as $event) { if ($event->getName() == $eventName) { return true; } } return false; } public function getLastEvent() { return end($this->events); } public function count() { return count($this->events); } public function getGrouped() { $events = array(); foreach ($this->events as $event) { if (!isset($events[$event->getName()])) { $events[$event->getName()] = array(); } $events[$event->getName()][] = $event; } return $events; } public function getData($event, $key, $occurence = 0) { $grouped = $this->getGrouped(); if (isset($grouped[$event])) { return $grouped[$event][$occurence][$key]; } return null; } /** * {@inheritdoc} */ public function update(Event $event) { $this->events[] = $event; } } tests/Guzzle/Tests/Mock/MockSubject.php000064400000000150151742734600014110 0ustar00getMockBuilder('Guzzle\Service\AbstractFactory') ->setMethods(array('getCacheTtlKey', 'throwException', 'getClassName')) ->getMockForAbstractClass(); } public function testCachesArtifacts() { $jsonFile = __DIR__ . '/../TestData/test_service.json'; $adapter = new DoctrineCacheAdapter(new ArrayCache()); $factory = $this->getFactory(); $factory->expects($this->once()) ->method('getClassName') ->will($this->returnValue('Guzzle\Service\Builder\JsonServiceBuilderFactory')); // Create a service and add it to the cache $service = $factory->build($jsonFile, array( 'cache.adapter' => $adapter )); // Ensure the cache key was set $this->assertTrue($adapter->contains('guzzle' . crc32($jsonFile))); // Grab the service from the cache $this->assertEquals($service, $factory->build($jsonFile, array( 'cache.adapter' => $adapter ))); } /** * @expectedException Guzzle\Service\Exception\ServiceBuilderException */ public function testThrowsExceptionsWhenNoFactoryResolves() { $factory = $this->getFactory(); $factory->expects($this->any()) ->method('getClassName') ->will($this->returnValue(false)); // Exceptions are mocked and disabled, so nothing happens here $service = $factory->build('foo'); // Throw an exception when it's supposed to $factory->expects($this->any()) ->method('throwException') ->will($this->throwException(new ServiceBuilderException())); $service = $factory->build('foo'); } } tests/Guzzle/Tests/Service/Builder/ArrayServiceBuilderFactoryTest.php000064400000005277151742734600022111 0ustar00 array( 'abstract' => array( 'params' => array( 'access_key' => 'xyz', 'secret' => 'abc', ), ), 'foo' => array( 'extends' => 'abstract', 'params' => array( 'baz' => 'bar', ), ), 'mock' => array( 'extends' => 'abstract', 'params' => array( 'username' => 'foo', 'password' => 'baz', 'subdomain' => 'bar', ) ) ) ); $builder = $arrayFactory->build($data); // Ensure that services were parsed $this->assertTrue(isset($builder['mock'])); $this->assertTrue(isset($builder['abstract'])); $this->assertTrue(isset($builder['foo'])); $this->assertFalse(isset($builder['jimmy'])); } /** * @expectedException Guzzle\Service\Exception\ServiceNotFoundException * @expectedExceptionMessage foo is trying to extend a non-existent service: abstract */ public function testThrowsExceptionWhenExtendingNonExistentService() { $arrayFactory = new ArrayServiceBuilderFactory(); $data = array( 'services' => array( 'foo' => array( 'extends' => 'abstract' ) ) ); $builder = $arrayFactory->build($data); } public function testAllowsGlobalParameterOverrides() { $arrayFactory = new ArrayServiceBuilderFactory(); $data = array( 'services' => array( 'foo' => array( 'params' => array( 'foo' => 'baz', 'bar' => 'boo' ) ) ) ); $builder = $arrayFactory->build($data, array( 'bar' => 'jar', 'far' => 'car' )); $compiled = json_decode($builder->serialize(), true); $this->assertEquals(array( 'foo' => 'baz', 'bar' => 'jar', 'far' => 'car' ), $compiled['foo']['params']); } } tests/Guzzle/Tests/Service/Builder/JsonServiceBuilderFactoryTest.php000064400000001644151742734600021736 0ustar00build($file); // Build it again, get a similar result using the same JsonLoader $this->assertEquals($builder, $j->build($file)); // Ensure that services were parsed $this->assertTrue(isset($builder['mock'])); $this->assertTrue(isset($builder['abstract'])); $this->assertTrue(isset($builder['foo'])); $this->assertFalse(isset($builder['jimmy'])); } } tests/Guzzle/Tests/Service/Builder/ServiceBuilderAbstractFactoryTest.php000064400000003311151742734600022561 0ustar00xmlFile = __DIR__ . '/../../TestData/services/new_style.xml'; $this->jsonFile = __DIR__ . '/../../TestData/services/json1.json'; } public function testFactoryDelegatesToConcreteFactories() { $factory = new ServiceBuilderAbstractFactory(); $this->assertInstanceOf('Guzzle\Service\Builder\ServiceBuilder', $factory->build($this->xmlFile)); $this->assertInstanceOf('Guzzle\Service\Builder\ServiceBuilder', $factory->build($this->jsonFile)); $xml = new \SimpleXMLElement(file_get_contents($this->xmlFile)); $xml->includes = null; $this->assertInstanceOf('Guzzle\Service\Builder\ServiceBuilder', $factory->build($xml)); } /** * @expectedException Guzzle\Service\Exception\ServiceBuilderException * @expectedExceptionMessage Unable to build service builder */ public function testFactoryEnsuresItCanHandleTheTypeOfFileOrArray() { $factory = new ServiceBuilderAbstractFactory(); $factory->build('jarJarBinks'); } /** * @expectedException Guzzle\Service\Exception\ServiceBuilderException * @expectedExceptionMessage Must pass a file name, array, or SimpleXMLElement */ public function testThrowsExceptionWhenUnknownTypeIsPassed() { $factory = new ServiceBuilderAbstractFactory(); $factory->build(new \stdClass()); } } tests/Guzzle/Tests/Service/Builder/ServiceBuilderTest.php000064400000026527151742734600017563 0ustar00 array( 'class' => 'Guzzle\\Tests\\Service\\Mock\\MockClient', 'params' => array( 'username' => 'michael', 'password' => 'testing123', 'subdomain' => 'michael', ), ), 'billy.mock' => array( 'class' => 'Guzzle\\Tests\\Service\\Mock\\MockClient', 'params' => array( 'username' => 'billy', 'password' => 'passw0rd', 'subdomain' => 'billy', ), ), 'billy.testing' => array( 'extends' => 'billy.mock', 'params' => array( 'subdomain' => 'test.billy', ), ), 'missing_params' => array( 'extends' => 'billy.mock' ) ); /** * @covers Guzzle\Service\Builder\ServiceBuilder::serialize * @covers Guzzle\Service\Builder\ServiceBuilder::unserialize */ public function testAllowsSerialization() { $builder = ServiceBuilder::factory($this->arrayData); $cached = unserialize(serialize($builder)); $this->assertEquals($cached, $builder); } /** * @covers Guzzle\Service\Builder\ServiceBuilder::factory */ public function testDelegatesFactoryMethodToAbstractFactory() { $builder = ServiceBuilder::factory($this->arrayData); $c = $builder->get('michael.mock'); $this->assertInstanceOf('Guzzle\\Tests\\Service\\Mock\\MockClient', $c); } /** * @covers Guzzle\Service\Builder\ServiceBuilder::get * @expectedException Guzzle\Service\Exception\ServiceNotFoundException * @expectedExceptionMessage No service is registered as foobar */ public function testThrowsExceptionWhenGettingInvalidClient() { ServiceBuilder::factory($this->arrayData)->get('foobar'); } /** * @covers Guzzle\Service\Builder\ServiceBuilder::get */ public function testStoresClientCopy() { $builder = ServiceBuilder::factory($this->arrayData); $client = $builder->get('michael.mock'); $this->assertInstanceOf('Guzzle\\Tests\\Service\\Mock\\MockClient', $client); $this->assertEquals('http://127.0.0.1:8124/v1/michael', $client->getBaseUrl()); $this->assertEquals($client, $builder->get('michael.mock')); // Get another client but throw this one away $client2 = $builder->get('billy.mock', true); $this->assertInstanceOf('Guzzle\\Tests\\Service\\Mock\\MockClient', $client2); $this->assertEquals('http://127.0.0.1:8124/v1/billy', $client2->getBaseUrl()); // Make sure the original client is still there and set $this->assertTrue($client === $builder->get('michael.mock')); // Create a new billy.mock client that is stored $client3 = $builder->get('billy.mock'); // Make sure that the stored billy.mock client is equal to the other stored client $this->assertTrue($client3 === $builder->get('billy.mock')); // Make sure that this client is not equal to the previous throwaway client $this->assertFalse($client2 === $builder->get('billy.mock')); } /** * @covers Guzzle\Service\Builder\ServiceBuilder */ public function testBuildersPassOptionsThroughToClients() { $s = new ServiceBuilder(array( 'michael.mock' => array( 'class' => 'Guzzle\\Tests\\Service\\Mock\\MockClient', 'params' => array( 'base_url' => 'http://www.test.com/', 'subdomain' => 'michael', 'password' => 'test', 'username' => 'michael', 'curl.curlopt_proxyport' => 8080 ) ) )); $c = $s->get('michael.mock'); $this->assertEquals(8080, $c->getConfig('curl.curlopt_proxyport')); } /** * @covers Guzzle\Service\Builder\ServiceBuilder */ public function testUsesTheDefaultBuilderWhenNoBuilderIsSpecified() { $s = new ServiceBuilder(array( 'michael.mock' => array( 'class' => 'Guzzle\\Tests\\Service\\Mock\\MockClient', 'params' => array( 'base_url' => 'http://www.test.com/', 'subdomain' => 'michael', 'password' => 'test', 'username' => 'michael', 'curl.curlopt_proxyport' => 8080 ) ) )); $c = $s->get('michael.mock'); $this->assertInstanceOf('Guzzle\\Tests\\Service\\Mock\\MockClient', $c); } /** * @covers Guzzle\Service\Builder\ServiceBuilder::set * @covers Guzzle\Service\Builder\ServiceBuilder::offsetSet * @covers Guzzle\Service\Builder\ServiceBuilder::offsetGet * @covers Guzzle\Service\Builder\ServiceBuilder::offsetUnset * @covers Guzzle\Service\Builder\ServiceBuilder::offsetExists */ public function testUsedAsArray() { $b = ServiceBuilder::factory($this->arrayData); $this->assertTrue($b->offsetExists('michael.mock')); $this->assertFalse($b->offsetExists('not_there')); $this->assertInstanceOf('Guzzle\\Service\\Client', $b['michael.mock']); unset($b['michael.mock']); $this->assertFalse($b->offsetExists('michael.mock')); $b['michael.mock'] = new Client('http://www.test.com/'); $this->assertInstanceOf('Guzzle\\Service\\Client', $b['michael.mock']); } /** * @covers Guzzle\Service\Builder\ServiceBuilder::factory */ public function testFactoryCanCreateFromJson() { $tmp = sys_get_temp_dir() . '/test.js'; file_put_contents($tmp, json_encode($this->arrayData)); $b = ServiceBuilder::factory($tmp); unlink($tmp); $s = $b->get('billy.testing'); $this->assertEquals('test.billy', $s->getConfig('subdomain')); $this->assertEquals('billy', $s->getConfig('username')); } /** * @covers Guzzle\Service\Builder\ServiceBuilder::factory */ public function testFactoryCanCreateFromArray() { $b = ServiceBuilder::factory($this->arrayData); $s = $b->get('billy.testing'); $this->assertEquals('test.billy', $s->getConfig('subdomain')); $this->assertEquals('billy', $s->getConfig('username')); } /** * @covers Guzzle\Service\Builder\ServiceBuilder::factory * @expectedException Guzzle\Service\Exception\ServiceBuilderException * @expectedExceptionMessage Unable to build service builder */ public function testFactoryValidatesFileExtension() { $tmp = sys_get_temp_dir() . '/test.abc'; file_put_contents($tmp, 'data'); try { ServiceBuilder::factory($tmp); } catch (\RuntimeException $e) { unlink($tmp); throw $e; } } /** * @covers Guzzle\Service\Builder\ServiceBuilder::factory * @expectedException Guzzle\Service\Exception\ServiceBuilderException * @expectedExceptionMessage Must pass a file name, array, or SimpleXMLElement */ public function testFactoryValidatesObjectTypes() { ServiceBuilder::factory(new \stdClass()); } /** * @covers Guzzle\Service\Builder\ServiceBuilder::factory */ public function testFactoryDoesNotRequireParams() { $b = ServiceBuilder::factory($this->arrayData); $s = $b->get('missing_params'); $this->assertEquals('billy', $s->getConfig('username')); } /** * @covers Guzzle\Service\Builder\ServiceBuilder */ public function testBuilderAllowsReferencesBetweenClients() { $builder = ServiceBuilder::factory(array( 'a' => array( 'class' => 'Guzzle\\Tests\\Service\\Mock\\MockClient', 'params' => array( 'other_client' => '{{ b }}', 'username' => 'x', 'password' => 'y', 'subdomain' => 'z' ) ), 'b' => array( 'class' => 'Guzzle\\Tests\\Service\\Mock\\MockClient', 'params' => array( 'username' => '1', 'password' => '2', 'subdomain' => '3' ) ) )); $client = $builder['a']; $this->assertEquals('x', $client->getConfig('username')); $this->assertSame($builder['b'], $client->getConfig('other_client')); $this->assertEquals('1', $builder['b']->getConfig('username')); } /** * @covers Guzzle\Service\Builder\ServiceBuilder::getAllEvents * @covers Guzzle\Service\Builder\ServiceBuilder::get */ public function testEmitsEventsWhenClientsAreCreated() { // Ensure that the client signals that it emits an event $this->assertEquals(array('service_builder.create_client'), ServiceBuilder::getAllEvents()); // Create a test service builder $builder = ServiceBuilder::factory(array( 'a' => array( 'class' => 'Guzzle\\Tests\\Service\\Mock\\MockClient', 'params' => array( 'username' => 'test', 'password' => '123', 'subdomain' => 'z' ) ) )); $emits = 0; $emitted = null; // Add an event listener to pick up client creation events $builder->getEventDispatcher()->addListener('service_builder.create_client', function($event) use (&$emits, &$emitted) { $emits++; $emitted = $event['client']; }); // Get the 'a' client by name $client = $builder->get('a'); // Ensure that the event was emitted once, and that the client was present $this->assertEquals(1, $emits); $this->assertInstanceOf('Guzzle\Tests\Service\Mock\MockClient', $client); } /** * @covers Guzzle\Service\Builder\ServiceBuilder::factory */ public function testCanAddGlobalParametersToServicesOnLoad() { $builder = ServiceBuilder::factory($this->arrayData, array( 'username' => 'fred', 'new_value' => 'test' )); $data = json_decode($builder->serialize(), true); foreach ($data as $service) { $this->assertEquals('fred', $service['params']['username']); $this->assertEquals('test', $service['params']['new_value']); } } public function testDescriptionIsCacheable() { $jsonFile = __DIR__ . '/../../TestData/test_service.json'; $adapter = new DoctrineCacheAdapter(new ArrayCache()); $builder = ServiceBuilder::factory($jsonFile, array( 'cache.adapter' => $adapter )); // Ensure the cache key was set $this->assertTrue($adapter->contains('guzzle' . crc32($jsonFile))); // Grab the service from the cache $this->assertEquals($builder, ServiceBuilder::factory($jsonFile, array( 'cache.adapter' => $adapter ))); } } tests/Guzzle/Tests/Service/Builder/XmlServiceBuilderFactoryTest.php000064400000003054151742734600021562 0ustar00build($file); // Ensure that services were parsed $this->assertTrue(isset($builder['mock'])); $this->assertTrue(isset($builder['abstract'])); $this->assertTrue(isset($builder['foo'])); $this->assertFalse(isset($builder['jimmy'])); } public function testBuildsServiceBuildersUsingSimpleXmlElement() { $xmlFactory = new XmlServiceBuilderFactory(); $file = __DIR__ . '/../../TestData/services/new_style.xml'; $xml = new \SimpleXMLElement(file_get_contents($file)); $xml->includes = null; $this->assertInstanceOf('Guzzle\Service\Builder\ServiceBuilder', $xmlFactory->build($xml)); } /** * @expectedException Guzzle\Service\Exception\ServiceBuilderException */ public function testCannotExtendWhenUsingSimpleXMLElement() { $xmlFactory = new XmlServiceBuilderFactory(); $file = __DIR__ . '/../../TestData/services/new_style.xml'; $xml = new \SimpleXMLElement(file_get_contents($file)); $xmlFactory->build($xml); } } tests/Guzzle/Tests/Service/ClientTest.php000064400000033671151742734600014502 0ustar00serviceTest = new ServiceDescription(array( 'test_command' => new ApiCommand(array( 'doc' => 'documentationForCommand', 'method' => 'DELETE', 'class' => 'Guzzle\\Tests\\Service\\Mock\\Command\\MockCommand', 'args' => array( 'bucket' => array( 'required' => true ), 'key' => array( 'required' => true ) ) )) )); $this->service = ServiceDescription::factory(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'TestData' . DIRECTORY_SEPARATOR . 'test_service.xml'); } /** * @covers Guzzle\Service\Client::factory */ public function testFactoryCreatesClient() { $client = Client::factory(array( 'base_url' => 'http://www.test.com/', 'test' => '123' )); $this->assertEquals('http://www.test.com/', $client->getBaseUrl()); $this->assertEquals('123', $client->getConfig('test')); } /** * @covers Guzzle\Service\Client::getAllEvents */ public function testDescribesEvents() { $this->assertInternalType('array', Client::getAllEvents()); } /** * @covers Guzzle\Service\Client::execute */ public function testExecutesCommands() { $this->getServer()->flush(); $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); $client = new Client($this->getServer()->getUrl()); $cmd = new MockCommand(); $client->execute($cmd); $this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $cmd->getResponse()); $this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $cmd->getResult()); $this->assertEquals(1, count($this->getServer()->getReceivedRequests(false))); } /** * @covers Guzzle\Service\Client::execute */ public function testExecutesCommandsWithArray() { $client = new Client('http://www.test.com/'); $client->getEventDispatcher()->addSubscriber(new MockPlugin(array( new Response(200), new Response(200) ))); // Create a command set and a command $set = array(new MockCommand(), new MockCommand()); $client->execute($set); // Make sure it sent $this->assertTrue($set[0]->isExecuted()); $this->assertTrue($set[1]->isExecuted()); } /** * @covers Guzzle\Service\Client::execute * @expectedException Guzzle\Service\Exception\CommandSetException */ public function testThrowsExceptionWhenExecutingMixedClientCommandSets() { $client = new Client('http://www.test.com/'); $otherClient = new Client('http://www.test-123.com/'); // Create a command set and a command $set = new CommandSet(); $cmd = new MockCommand(); $set->addCommand($cmd); // Associate the other client with the command $cmd->setClient($otherClient); // Send the set with the wrong client, causing an exception $client->execute($set); } /** * @covers Guzzle\Service\Client::execute * @expectedException Guzzle\Common\Exception\InvalidArgumentException */ public function testThrowsExceptionWhenExecutingInvalidCommandSets() { $client = new Client('http://www.test.com/'); $client->execute(new \stdClass()); } /** * @covers Guzzle\Service\Client::execute */ public function testExecutesCommandSets() { $client = new Client('http://www.test.com/'); $client->getEventDispatcher()->addSubscriber(new MockPlugin(array( new Response(200) ))); // Create a command set and a command $set = new CommandSet(); $cmd = new MockCommand(); $set->addCommand($cmd); $this->assertSame($set, $client->execute($set)); // Make sure it sent $this->assertTrue($cmd->isExecuted()); $this->assertTrue($cmd->isPrepared()); $this->assertEquals(200, $cmd->getResponse()->getStatusCode()); } /** * @covers Guzzle\Service\Client::getCommand * @expectedException InvalidArgumentException */ public function testThrowsExceptionWhenMissingCommand() { $client = new Client(); $mock = $this->getMock('Guzzle\\Service\\Command\\Factory\\FactoryInterface'); $mock->expects($this->any()) ->method('factory') ->with($this->equalTo('test')) ->will($this->returnValue(null)); $client->setCommandFactory($mock); $client->getCommand('test'); } /** * @covers Guzzle\Service\Client::getCommand */ public function testCreatesCommandsUsingCommandFactory() { $mockCommand = new MockCommand(); $client = new Mock\MockClient(); $mock = $this->getMock('Guzzle\\Service\\Command\\Factory\\FactoryInterface'); $mock->expects($this->any()) ->method('factory') ->with($this->equalTo('foo')) ->will($this->returnValue($mockCommand)); $client->setCommandFactory($mock); $command = $client->getCommand('foo', array( 'acl' => '123' )); $this->assertSame($mockCommand, $command); $this->assertSame($client, $command->getClient()); } /** * @covers Guzzle\Service\Client::getDescription * @covers Guzzle\Service\Client::setDescription */ public function testOwnsServiceDescription() { $client = new Mock\MockClient(); $this->assertNull($client->getDescription()); $description = $this->getMock('Guzzle\\Service\\Description\\ServiceDescription'); $this->assertSame($client, $client->setDescription($description)); $this->assertSame($description, $client->getDescription()); } /** * @covers Guzzle\Service\Client::setDescription */ public function testSettingServiceDescriptionUpdatesFactories() { $client = new Mock\MockClient(); $factory = $this->getMockBuilder('Guzzle\\Service\\Command\\Factory\\MapFactory') ->disableOriginalConstructor() ->getMock(); $client->setCommandFactory($factory); $description = $this->getMock('Guzzle\\Service\\Description\\ServiceDescription'); $client->setDescription($description); $cf = $this->readAttribute($client, 'commandFactory'); $this->assertNotSame($factory, $cf); $this->assertInstanceOf('Guzzle\\Service\\Command\\Factory\\CompositeFactory', $cf); $array = $cf->getIterator()->getArrayCopy(); $this->assertSame($array[0], $factory); $this->assertInstanceOf('Guzzle\\Service\\Command\\Factory\\ServiceDescriptionFactory', $array[1]); $this->assertSame($description, $array[1]->getServiceDescription()); $description2 = $this->getMock('Guzzle\\Service\\Description\\ServiceDescription'); $client->setDescription($description2); $cf = $this->readAttribute($client, 'commandFactory'); $array = $cf->getIterator()->getArrayCopy(); $this->assertSame($array[0], $factory); $this->assertInstanceOf('Guzzle\\Service\\Command\\Factory\\ServiceDescriptionFactory', $array[1]); $this->assertSame($description2, $array[1]->getServiceDescription()); } /** * @covers Guzzle\Service\Client::__call * @expectedException BadMethodCallException */ public function testMagicCallBehaviorIsDisabledByDefault() { $client = new Client(); $client->foo(); } /** * @covers Guzzle\Service\Client::__call * @covers Guzzle\Service\Client::setMagicCallBehavior * @expectedException InvalidArgumentException * @expectedExceptionMessage Command was not found matching foo */ public function testMagicCallBehaviorEnsuresCommandExists() { $client = new Mock\MockClient(); $client->setDescription($this->service); $client->setMagicCallBehavior(Client::MAGIC_CALL_RETURN); $client->foo(); } /** * @covers Guzzle\Service\Client::__call */ public function testMagicCallBehaviorReturnReturnsCommands() { $client = new Mock\MockClient(); $client->setMagicCallBehavior(Client::MAGIC_CALL_RETURN); $client->setDescription($this->service); $this->assertInstanceOf('Guzzle\Tests\Service\Mock\Command\MockCommand', $client->mockCommand()); } /** * @covers Guzzle\Service\Client::__call */ public function testMagicCallBehaviorExecuteExecutesCommands() { $client = new Mock\MockClient(); $client->setMagicCallBehavior(Client::MAGIC_CALL_EXECUTE); $client->setDescription($this->service); $client->getEventDispatcher()->addSubscriber(new MockPlugin(array(new Response(200)))); $this->assertInstanceOf('Guzzle\Http\Message\Response', $client->mockCommand()); } /** * @covers Guzzle\Service\Client::getCommandFactory * @covers Guzzle\Service\Client::setCommandFactory */ public function testOwnsCommandFactory() { $client = new Mock\MockClient(); $method = new \ReflectionMethod($client, 'getCommandFactory'); $method->setAccessible(TRUE); $cf1 = $method->invoke($client); $cf = $this->readAttribute($client, 'commandFactory'); $this->assertInstanceOf('Guzzle\\Service\\Command\\Factory\\CompositeFactory', $cf); $this->assertSame($method->invoke($client), $cf1); $mock = $this->getMock('Guzzle\\Service\\Command\\Factory\\CompositeFactory'); $client->setCommandFactory($mock); $this->assertSame($mock, $this->readAttribute($client, 'commandFactory')); } /** * @covers Guzzle\Service\Client::getResourceIteratorFactory * @covers Guzzle\Service\Client::setResourceIteratorFactory */ public function testOwnsResourceIteratorFactory() { $client = new Mock\MockClient(); $method = new \ReflectionMethod($client, 'getResourceIteratorFactory'); $method->setAccessible(TRUE); $rf1 = $method->invoke($client); $rf = $this->readAttribute($client, 'resourceIteratorFactory'); $this->assertInstanceOf('Guzzle\\Service\\Resource\\ResourceIteratorClassFactory', $rf); $this->assertSame($rf1, $rf); $rf = new ResourceIteratorClassFactory('Guzzle\Tests\Service\Mock'); $client->setResourceIteratorFactory($rf); $this->assertNotSame($rf1, $rf); } /** * @covers Guzzle\Service\Client::getCommand * @depends testMagicCallBehaviorExecuteExecutesCommands */ public function testEnablesMagicMethodCallsOnCommandsIfEnabledOnClient() { $client = new Mock\MockClient(); $command = $client->getCommand('other_command'); $this->assertNull($command->get('command.magic_method_call')); $client->setMagicCallBehavior(Client::MAGIC_CALL_EXECUTE); $command = $client->getCommand('other_command'); $this->assertTrue($command->get('command.magic_method_call')); } /** * @covers Guzzle\Service\Client::execute */ public function testClientResetsRequestsBeforeExecutingCommands() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nHi", "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\nI" )); $client = new Mock\MockClient($this->getServer()->getUrl()); $command = $client->getCommand('mock_command'); $client->execute($command); $client->execute($command); $this->assertEquals('I', $command->getResponse()->getBody(true)); } /** * @covers Guzzle\Service\Client::getIterator */ public function testClientCreatesIterators() { $client = new Mock\MockClient(); $iterator = $client->getIterator('mock_command', array( 'foo' => 'bar' ), array( 'limit' => 10 )); $this->assertInstanceOf('Guzzle\Tests\Service\Mock\Model\MockCommandIterator', $iterator); $this->assertEquals(10, $this->readAttribute($iterator, 'limit')); $command = $this->readAttribute($iterator, 'originalCommand'); $this->assertEquals('bar', $command->get('foo')); } /** * @covers Guzzle\Service\Client::getIterator */ public function testClientCreatesIteratorsWithNoOptions() { $client = new Mock\MockClient(); $iterator = $client->getIterator('mock_command'); $this->assertInstanceOf('Guzzle\Tests\Service\Mock\Model\MockCommandIterator', $iterator); } /** * @covers Guzzle\Service\Client::getIterator */ public function testClientCreatesIteratorsWithCommands() { $client = new Mock\MockClient(); $command = new MockCommand(); $iterator = $client->getIterator($command); $this->assertInstanceOf('Guzzle\Tests\Service\Mock\Model\MockCommandIterator', $iterator); $iteratorCommand = $this->readAttribute($iterator, 'originalCommand'); $this->assertSame($command, $iteratorCommand); } } tests/Guzzle/Tests/Service/Command/AbstractCommandTest.php000064400000001070151742734600017670 0ustar00setDescription($service); return $client; } } tests/Guzzle/Tests/Service/Command/ClosureCommandTest.php000064400000003356151742734600017552 0ustar00 function($command, $api) { $command->set('testing', '123'); $request = RequestFactory::getInstance()->create('GET', 'http://www.test.com/'); return $request; } )); $client = $this->getServiceBuilder()->get('mock'); $c->setClient($client)->prepare(); $this->assertEquals('123', $c->get('testing')); $this->assertEquals('http://www.test.com/', $c->getRequest()->getUrl()); } /** * @covers Guzzle\Service\Command\ClosureCommand * @expectedException UnexpectedValueException * @expectedExceptionMessage Closure command did not return a RequestInterface object */ public function testMustReturnRequest() { $c = new ClosureCommand(array( 'closure' => function($command, $api) { return false; } )); $client = $this->getServiceBuilder()->get('mock'); $c->setClient($client)->prepare(); } } tests/Guzzle/Tests/Service/Command/CommandSetTest.php000064400000013434151742734600016667 0ustar00assertTrue($commandSet->hasCommand($cmd)); } /** * @covers Guzzle\Service\Command\CommandSet::hasCommand * @covers Guzzle\Service\Command\CommandSet::addCommand * @covers Guzzle\Service\Command\CommandSet::removeCommand * @covers Guzzle\Service\Command\CommandSet::getCommands * @covers Guzzle\Service\Command\CommandSet::count * @covers Guzzle\Service\Command\CommandSet::getIterator */ public function testAllowsCommandManipulationAndIntrospection() { $commandSet = new CommandSet(); // Check when no commands are set $this->assertEquals(array(), $commandSet->getCommands()); // Create some mock commands $command1 = new MockCommand(); $command2 = new MockCommand(); // Check the fluent interface $this->assertEquals($commandSet, $commandSet->addCommand($command1)); $commandSet->addCommand($command2); // Check that the commands are registered and findable $this->assertTrue($commandSet->hasCommand($command1)); $this->assertTrue($commandSet->hasCommand($command2)); $this->assertTrue($commandSet->hasCommand('Guzzle\\Tests\\Service\\Mock\\Command\\MockCommand')); // Test that the Countable interface is working $this->assertEquals(2, count($commandSet)); // Test that the IteratorAggregate interface is working $this->assertInstanceOf('ArrayIterator', $commandSet->getIterator()); $this->assertEquals(2, count($commandSet->getIterator())); $this->assertEquals(array($command1, $command2), $commandSet->getCommands()); // Remove the command by object $commandSet->removeCommand($command1); $this->assertFalse($commandSet->hasCommand($command1)); $this->assertTrue($commandSet->hasCommand($command2)); // Remove the command by class $commandSet->removeCommand('Guzzle\\Tests\\Service\\Mock\\Command\\MockCommand'); $this->assertFalse($commandSet->hasCommand($command2)); } /** * @covers Guzzle\Service\Command\CommandSet::execute * @covers Guzzle\Service\Exception\CommandSetException */ public function testThrowsExceptionWhenAnyCommandHasNoClient() { $cmd = new MockCommand; $commandSet = new CommandSet(array($cmd)); try { $commandSet->execute(); $this->fail('CommandSetException not thrown when a command did not have a client'); } catch (\Guzzle\Service\Exception\CommandSetException $e) { $this->assertEquals(array($cmd), $e->getCommands()); } } /** * @covers Guzzle\Service\Command\CommandSet::execute * @covers Guzzle\Service\Command\CommandSet::update */ public function testExecutesCommands() { $client = $this->getClient(); $observer = $this->getWildcardObserver($client); // Create a Mock response $response = new Response(200, array( 'Content-Type' => 'application/xml' ), '123'); $client->getEventDispatcher()->addSubscriber(new MockPlugin(array( $response, $response, $response ))); $command1 = new MockCommand(); $command1->setClient($client); $command2 = new MockCommand(); $command2->setClient($client); $command3 = new MockCommand(); $command3->setClient($client); $commandSet = new CommandSet(array($command1, $command2, $command3)); $commandSet->execute(); $this->assertTrue($command1->isExecuted()); $this->assertTrue($command1->isPrepared()); $this->assertTrue($command2->isExecuted()); $this->assertTrue($command2->isPrepared()); $this->assertTrue($command3->isExecuted()); $this->assertTrue($command3->isPrepared()); $this->assertEquals($response, $command1->getResponse()); $this->assertEquals($response, $command2->getResponse()); $grouped = $observer->getGrouped(); $this->assertEquals(3, count($grouped['command.before_send'])); $this->assertEquals(3, count($grouped['command.after_send'])); // make sure the command set was detached as a listener on the request $listeners = $command1->getRequest()->getEventDispatcher()->getListeners('request.complete'); $this->assertFalse(in_array($commandSet, $listeners)); // make sure that the command reference was removed $this->assertFalse($command1->getRequest()->getParams()->hasKey('command')); // Make sure that the command.after_send events are staggered, meaning they happened as requests completed $lastEvent = ''; foreach ($observer->events as $e) { if ($lastEvent == 'command.after_send' && $e == 'command.after_send') { $this->fail('Not completing commands as they complete: ' . var_export($observer->events, true)); } $lastEvent = $e; } } } tests/Guzzle/Tests/Service/Command/CommandTest.php000064400000031176151742734600016216 0ustar00assertEquals('123', $command->get('test')); $this->assertFalse($command->isPrepared()); $this->assertFalse($command->isExecuted()); } /** * @covers Guzzle\Service\Command\AbstractCommand::getName */ public function testDeterminesShortName() { $api = new ApiCommand(array( 'name' => 'foobar' )); $command = new MockCommand(array(), $api); $this->assertEquals('foobar', $command->getName()); $command = new MockCommand(); $this->assertEquals('mock_command', $command->getName()); $command = new Sub(); $this->assertEquals('sub.sub', $command->getName()); } /** * @covers Guzzle\Service\Command\AbstractCommand::getRequest * @expectedException RuntimeException */ public function testGetRequestThrowsExceptionBeforePreparation() { $command = new MockCommand(); $command->getRequest(); } /** * @covers Guzzle\Service\Command\AbstractCommand::getResponse * @expectedException RuntimeException */ public function testGetResponseThrowsExceptionBeforePreparation() { $command = new MockCommand(); $command->getResponse(); } /** * @covers Guzzle\Service\Command\AbstractCommand::getResult * @expectedException RuntimeException */ public function testGetResultThrowsExceptionBeforePreparation() { $command = new MockCommand(); $command->getResult(); } /** * @covers Guzzle\Service\Command\AbstractCommand::setClient * @covers Guzzle\Service\Command\AbstractCommand::getClient * @covers Guzzle\Service\Command\AbstractCommand::prepare * @covers Guzzle\Service\Command\AbstractCommand::isPrepared */ public function testSetClient() { $command = new MockCommand(); $client = $this->getClient(); $command->setClient($client); $this->assertEquals($client, $command->getClient()); unset($client); unset($command); $command = new MockCommand(); $client = $this->getClient(); $command->setClient($client)->prepare(); $this->assertEquals($client, $command->getClient()); $this->assertTrue($command->isPrepared()); } /** * @covers Guzzle\Service\Command\AbstractCommand::execute * @covers Guzzle\Service\Command\AbstractCommand::setClient * @covers Guzzle\Service\Command\AbstractCommand::getRequest * @covers Guzzle\Service\Command\AbstractCommand::getResponse * @covers Guzzle\Service\Command\AbstractCommand::getResult * @covers Guzzle\Service\Command\AbstractCommand::prepare * @covers Guzzle\Service\Command\AbstractCommand::process * @covers Guzzle\Service\Command\AbstractCommand::prepare * @covers Guzzle\Service\Client::execute */ public function testExecute() { $client = $this->getClient(); $response = new \Guzzle\Http\Message\Response(200, array( 'Content-Type' => 'application/xml' ), '123'); // Set a mock response $client->getEventDispatcher()->addSubscriber(new MockPlugin(array( $response ))); $command = new MockCommand(); $this->assertSame($command, $command->setClient($client)); // Returns the result of the command $this->assertInstanceOf('SimpleXMLElement', $command->execute()); $this->assertTrue($command->isPrepared()); $this->assertTrue($command->isExecuted()); $this->assertSame($response, $command->getResponse()); $this->assertInstanceOf('Guzzle\\Http\\Message\\Request', $command->getRequest()); // Make sure that the result was automatically set to a SimpleXMLElement $this->assertInstanceOf('SimpleXMLElement', $command->getResult()); $this->assertEquals('123', (string)$command->getResult()->data); } /** * @covers Guzzle\Service\Command\AbstractCommand::process */ public function testConvertsJsonResponsesToArray() { $client = $this->getClient(); $client->getEventDispatcher()->addSubscriber(new MockPlugin(array( new \Guzzle\Http\Message\Response(200, array( 'Content-Type' => 'application/json' ), '{ "key": "Hi!" }' ) ))); $command = new MockCommand(); $command->setClient($client); $command->execute(); $this->assertEquals(array( 'key' => 'Hi!' ), $command->getResult()); } /** * @covers Guzzle\Service\Command\AbstractCommand::process * @expectedException Guzzle\Service\Exception\JsonException */ public function testConvertsInvalidJsonResponsesToArray() { $client = $this->getClient(); $client->getEventDispatcher()->addSubscriber(new MockPlugin(array( new \Guzzle\Http\Message\Response(200, array( 'Content-Type' => 'application/json' ), '{ "key": "Hi!" }invalid' ) ))); $command = new MockCommand(); $command->setClient($client); $command->execute(); } /** * @covers Guzzle\Service\Command\AbstractCommand::process */ public function testProcessResponseIsNotXml() { $client = $this->getClient(); $client->getEventDispatcher()->addSubscriber(new MockPlugin(array( new Response(200, array( 'Content-Type' => 'application/octect-stream' ), 'abc,def,ghi') ))); $command = new MockCommand(); $client->execute($command); // Make sure that the result was not converted to XML $this->assertFalse($command->getResult() instanceof \SimpleXMLElement); } /** * @covers Guzzle\Service\Command\AbstractCommand::execute * @expectedException RuntimeException */ public function testExecuteThrowsExceptionWhenNoClientIsSet() { $command = new MockCommand(); $command->execute(); } /** * @covers Guzzle\Service\Command\AbstractCommand::prepare * @expectedException RuntimeException */ public function testPrepareThrowsExceptionWhenNoClientIsSet() { $command = new MockCommand(); $command->prepare(); } /** * @covers Guzzle\Service\Command\AbstractCommand::prepare * @covers Guzzle\Service\Command\AbstractCommand::getRequestHeaders */ public function testCommandsAllowsCustomRequestHeaders() { $command = new MockCommand(); $command->getRequestHeaders()->set('test', '123'); $this->assertInstanceOf('Guzzle\Common\Collection', $command->getRequestHeaders()); $this->assertEquals('123', $command->getRequestHeaders()->get('test')); $command->setClient($this->getClient())->prepare(); $this->assertEquals('123', (string) $command->getRequest()->getHeader('test')); } /** * @covers Guzzle\Service\Command\AbstractCommand::__construct */ public function testCommandsAllowsCustomRequestHeadersAsArray() { $command = new MockCommand(array( 'headers' => array( 'Foo' => 'Bar' ) )); $this->assertInstanceOf('Guzzle\Common\Collection', $command->getRequestHeaders()); $this->assertEquals('Bar', $command->getRequestHeaders()->get('Foo')); } private function getApiCommand() { return new ApiCommand(array( 'name' => 'foobar', 'method' => 'POST', 'class' => 'Guzzle\\Tests\\Service\\Mock\\Command\\MockCommand', 'params' => array( 'test' => array( 'default' => '123', 'type' => 'string' ) ))); } /** * @covers Guzzle\Service\Command\AbstractCommand */ public function testCommandsUsesApiCommand() { $api = $this->getApiCommand(); $command = new MockCommand(array(), $api); $this->assertSame($api, $command->getApiCommand()); $command->setClient($this->getClient())->prepare(); $this->assertEquals('123', $command->get('test')); $this->assertSame($api, $command->getApiCommand($api)); } /** * @covers Guzzle\Service\Command\AbstractCommand::__call * @expectedException Guzzle\Common\Exception\BadMethodCallException * @expectedExceptionMessage Magic method calls are disabled for this command. Consider enabling magic method calls by setting the command.magic_method_call parameter to true. */ public function testMissingMethodCallsThrowExceptionsWhenMagicIsDisabled() { $command = new MockCommand(array()); $command->setFooBarBaz('123'); } /** * @covers Guzzle\Service\Command\AbstractCommand::__call * @expectedException Guzzle\Common\Exception\BadMethodCallException * @expectedExceptionMessage Missing method setFoo */ public function testMissingMethodCallsThrowExceptionsWhenParameterIsInvalid() { $command = new MockCommand(array( 'command.magic_method_call' => true ), $this->getApiCommand()); $command->setFoo('bar_baz'); } /** * @covers Guzzle\Service\Command\AbstractCommand::__call */ public function testMissingMethodCallsAllowedWhenEnabled() { $command = new MockCommand(array( 'command.magic_method_call' => true ), $this->getApiCommand()); $command->setTest('foo'); $this->assertEquals('foo', $command->get('test')); } /** * @covers Guzzle\Service\Command\AbstractCommand::__clone */ public function testCloneMakesNewRequest() { $client = $this->getClient(); $command = new MockCommand(array( 'command.magic_method_call' => true ), $this->getApiCommand()); $command->setClient($client); $command->prepare(); $this->assertTrue($command->isPrepared()); $command2 = clone $command; $this->assertFalse($command2->isPrepared()); } /** * @covers Guzzle\Service\Command\AbstractCommand::setOnComplete * @covers Guzzle\Service\Command\AbstractCommand::__construct * @covers Guzzle\Service\Command\AbstractCommand::getResult */ public function testHasOnCompleteMethod() { $that = $this; $called = 0; $testFunction = function($command) use (&$called, $that) { $called++; $that->assertInstanceOf('Guzzle\Service\Command\CommandInterface', $command); }; $client = $this->getClient(); $command = new MockCommand(array( 'command.on_complete' => $testFunction ), $this->getApiCommand()); $command->setClient($client); $command->prepare()->setResponse(new Response(200)); $command->execute(); $this->assertEquals(1, $called); } /** * @covers Guzzle\Service\Command\AbstractCommand::setOnComplete * @expectedException Guzzle\Common\Exception\InvalidArgumentException */ public function testOnCompleteMustBeCallable() { $client = $this->getClient(); $command = new MockCommand(); $command->setOnComplete('foo'); } /** * @covers Guzzle\Service\Command\AbstractCommand::setInspector * @covers Guzzle\Service\Command\AbstractCommand::getInspector */ public function testInspectorCanBeInjected() { $instance = Inspector::getInstance(); $command = new MockCommand(); $refObject = new \ReflectionObject($command); $method = $refObject->getMethod('getInspector'); $method->setAccessible(true); $this->assertSame($instance, $method->invoke($command)); $newInspector = new Inspector(); $command->setInspector($newInspector); $this->assertSame($newInspector, $method->invoke($command)); } } tests/Guzzle/Tests/Service/Command/DynamicCommandTest.php000064400000016456151742734600017527 0ustar00service = new ServiceDescription( array( 'test_command' => new ApiCommand(array( 'doc' => 'documentationForCommand', 'method' => 'HEAD', 'uri' => '{/key}', 'params' => array( 'bucket' => array( 'required' => true, 'append' => '.' ), 'key' => array( 'prepend' => 'hi_' ), 'acl' => array( 'location' => 'query' ), 'meta' => array( 'location' => 'header:X-Amz-Meta', 'append' => ':meta' ) ) )), 'body' => new ApiCommand(array( 'doc' => 'doc', 'method' => 'PUT', 'params' => array( 'b' => array( 'required' => true, 'prepend' => 'begin_body::', 'append' => '::end_body', 'location' => 'body' ), 'q' => array( 'location' => 'query:test' ), 'h' => array( 'location' => 'header:X-Custom' ), 'i' => array( 'static' => 'test', 'location' => 'query' ), // Data locations means the argument is just a placeholder for data // that can be referenced by other arguments 'data' => array( 'location' => 'data' ) ) )), 'concrete' => new ApiCommand(array( 'class' => 'Guzzle\\Tests\\Service\\Mock\\Command\\MockCommand', 'params' => array() )) ) ); $this->factory = new ServiceDescriptionFactory($this->service); } /** * @covers Guzzle\Service\Command\DynamicCommand */ public function testBuildsUsingPathParametersAndAppendSlashPrepend() { $client = new Client('http://www.example.com/'); $client->setDescription($this->service); $command = $this->factory->factory('test_command', array( 'bucket' => 'test', 'key' => 'key' )); $request = $command->setClient($client)->prepare(); // Ensure that the path values were injected into the path and base_url $this->assertEquals('/hi_key', $request->getPath()); $this->assertEquals('www.example.com', $request->getHost()); // Check the complete request $this->assertEquals( "HEAD /hi_key HTTP/1.1\r\n" . "Host: www.example.com\r\n" . "User-Agent: " . Utils::getDefaultUserAgent() . "\r\n" . "\r\n", (string) $request); } /** * @covers Guzzle\Service\Command\DynamicCommand * @expectedException Guzzle\Service\Exception\ValidationException */ public function testValidatesArgs() { $client = new Client('http://www.fragilerock.com/'); $client->setDescription($this->service); $command = $this->factory->factory('test_command', array()); $client->execute($command); } /** * @covers Guzzle\Service\Command\DynamicCommand */ public function testUsesDifferentLocations() { $client = new Client('http://www.tazmania.com/'); $command = $this->factory->factory('body', array( 'b' => 'my-data', 'q' => 'abc', 'h' => 'haha' )); $request = $command->setClient($client)->prepare(); $this->assertEquals( "PUT /?test=abc&i=test HTTP/1.1\r\n" . "Host: www.tazmania.com\r\n" . "User-Agent: " . Utils::getDefaultUserAgent() . "\r\n" . "Expect: 100-Continue\r\n" . "Content-Length: 29\r\n" . "X-Custom: haha\r\n" . "\r\n" . "begin_body::my-data::end_body", (string) $request); unset($command); unset($request); $command = $this->factory->factory('body', array( 'b' => 'my-data', 'q' => 'abc', 'h' => 'haha', 'i' => 'does not change the value because it\'s static' )); $request = $command->setClient($client)->prepare(); $this->assertEquals( "PUT /?test=abc&i=test HTTP/1.1\r\n" . "Host: www.tazmania.com\r\n" . "User-Agent: " . Utils::getDefaultUserAgent() . "\r\n" . "Expect: 100-Continue\r\n" . "Content-Length: 29\r\n" . "X-Custom: haha\r\n" . "\r\n" . "begin_body::my-data::end_body", (string) $request); } /** * @covers Guzzle\Service\Command\DynamicCommand::build */ public function testBuildsConcreteCommands() { $c = $this->factory->factory('concrete'); $this->assertEquals('Guzzle\\Tests\\Service\\Mock\\Command\\MockCommand', get_class($c)); } /** * @covers Guzzle\Service\Command\DynamicCommand::build */ public function testUsesAbsolutePaths() { $service = new ServiceDescription( array( 'test_path' => new ApiCommand(array( 'method' => 'GET', 'uri' => '/test', )) ) ); $client = new Client('http://www.test.com/'); $client->setDescription($service); $command = $client->getCommand('test_path'); $request = $command->prepare(); $this->assertEquals('/test', $request->getPath()); } /** * @covers Guzzle\Service\Command\DynamicCommand::build */ public function testUsesRelativePaths() { $service = new ServiceDescription( array( 'test_path' => new ApiCommand(array( 'method' => 'GET', 'uri' => 'test/abc', )) ) ); $client = new Client('http://www.test.com/api/v2'); $client->setDescription($service); $command = $client->getCommand('test_path'); $request = $command->prepare(); $this->assertEquals('/api/v2/test/abc', $request->getPath()); } } tests/Guzzle/Tests/Service/Command/Factory/AliasFactoryTest.php000064400000004537151742734600020631 0ustar00client = new Client(); $map = new MapFactory(array( 'test' => 'Guzzle\Tests\Service\Mock\Command\MockCommand', 'test1' => 'Guzzle\Tests\Service\Mock\Command\OtherCommand' )); $this->factory = new AliasFactory($this->client, array( 'foo' => 'test', 'bar' => 'sub', 'sub' => 'test1', 'krull' => 'test3', 'krull_2' => 'krull', 'sub_2' => 'bar', 'bad_link' => 'jarjar' )); $map2 = new MapFactory(array( 'test3' => 'Guzzle\Tests\Service\Mock\Command\Sub\Sub' )); $this->client->setCommandFactory(new CompositeFactory(array($map, $this->factory, $map2))); } public function aliasProvider() { return array( array('foo', 'Guzzle\Tests\Service\Mock\Command\MockCommand', false), array('bar', 'Guzzle\Tests\Service\Mock\Command\OtherCommand', false), array('sub', 'Guzzle\Tests\Service\Mock\Command\OtherCommand', false), array('sub_2', 'Guzzle\Tests\Service\Mock\Command\OtherCommand', false), array('krull', 'Guzzle\Tests\Service\Mock\Command\Sub\Sub', false), array('krull_2', 'Guzzle\Tests\Service\Mock\Command\Sub\Sub', false), array('missing', null, true), array('bad_link', null, true) ); } /** * @covers Guzzle\Service\Command\Factory\AliasFactory * @dataProvider aliasProvider */ public function testAliasesCommands($key, $result, $exception) { try { $command = $this->client->getCommand($key); if (is_null($result)) { $this->assertNull($command); } else { $this->assertInstanceof($result, $command); } } catch (\Exception $e) { if (!$exception) { $this->fail('Got exception when it was not expected'); } } } } tests/Guzzle/Tests/Service/Command/Factory/CompositeFactoryTest.php000064400000013055151742734600021535 0ustar00getMockBuilder($class) ->disableOriginalConstructor() ->getMock(); } /** * @covers Guzzle\Service\Command\Factory\CompositeFactory::getIterator * @covers Guzzle\Service\Command\Factory\CompositeFactory::count */ public function testIsIterable() { $factory = new CompositeFactory(array($this->getFactory(), $this->getFactory())); $this->assertEquals(2, count($factory)); $this->assertEquals(2, count(iterator_to_array($factory->getIterator()))); } /** * @covers Guzzle\Service\Command\Factory\CompositeFactory::find * @covers Guzzle\Service\Command\Factory\CompositeFactory::has */ public function testFindsFactories() { $f1 = $this->getFactory(); $f2 = $this->getFactory('Guzzle\\Service\\Command\\Factory\\CompositeFactory'); $factory = new CompositeFactory(array($f1, $f2)); $this->assertNull($factory->find('foo')); $this->assertNull($factory->find($this->getFactory())); $this->assertSame($f1, $factory->find('Guzzle\\Service\\Command\\Factory\\MapFactory')); $this->assertSame($f2, $factory->find('Guzzle\\Service\\Command\\Factory\\CompositeFactory')); $this->assertSame($f1, $factory->find($f1)); $this->assertSame($f2, $factory->find($f2)); $this->assertFalse($factory->has('foo')); $this->assertTrue($factory->has('Guzzle\\Service\\Command\\Factory\\MapFactory')); $this->assertTrue($factory->has('Guzzle\\Service\\Command\\Factory\\CompositeFactory')); } /** * @covers Guzzle\Service\Command\Factory\CompositeFactory::factory * @covers Guzzle\Service\Command\Factory\CompositeFactory::__construct */ public function testCreatesCommands() { $factory = new CompositeFactory(); $this->assertNull($factory->factory('foo')); $f1 = $this->getFactory(); $mockCommand1 = $this->getMockForAbstractClass('Guzzle\\Service\\Command\\AbstractCommand'); $f1->expects($this->once()) ->method('factory') ->with($this->equalTo('foo')) ->will($this->returnValue($mockCommand1)); $factory = new CompositeFactory(array($f1)); $this->assertSame($mockCommand1, $factory->factory('foo')); } /** * @covers Guzzle\Service\Command\Factory\CompositeFactory::remove */ public function testAllowsRemovalOfFactories() { $f1 = $this->getFactory(); $f2 = $this->getFactory(); $f3 = $this->getFactory('Guzzle\\Service\\Command\\Factory\\CompositeFactory'); $factories = array($f1, $f2, $f3); $factory = new CompositeFactory($factories); $factory->remove('foo'); $this->assertEquals($factories, $factory->getIterator()->getArrayCopy()); $factory->remove($f1); $this->assertEquals(array($f2, $f3), $factory->getIterator()->getArrayCopy()); $factory->remove('Guzzle\\Service\\Command\\Factory\\MapFactory'); $this->assertEquals(array($f3), $factory->getIterator()->getArrayCopy()); $factory->remove('Guzzle\\Service\\Command\\Factory\\CompositeFactory'); $this->assertEquals(array(), $factory->getIterator()->getArrayCopy()); $factory->remove('foo'); $this->assertEquals(array(), $factory->getIterator()->getArrayCopy()); } /** * @covers Guzzle\Service\Command\Factory\CompositeFactory::add */ public function testAddsFactoriesBeforeAndAtEnd() { $f1 = $this->getFactory(); $f2 = $this->getFactory(); $f3 = $this->getFactory('Guzzle\\Service\\Command\\Factory\\CompositeFactory'); $f4 = $this->getFactory(); $factory = new CompositeFactory(); $factory->add($f1); $this->assertEquals(array($f1), $factory->getIterator()->getArrayCopy()); $factory->add($f2); $this->assertEquals(array($f1, $f2), $factory->getIterator()->getArrayCopy()); $factory->add($f3, $f2); $this->assertEquals(array($f1, $f3, $f2), $factory->getIterator()->getArrayCopy()); $factory->add($f4, 'Guzzle\\Service\\Command\\Factory\\CompositeFactory'); $this->assertEquals(array($f1, $f4, $f3, $f2), $factory->getIterator()->getArrayCopy()); } /** * @covers Guzzle\Service\Command\Factory\CompositeFactory::getDefaultChain */ public function testProvidesDefaultChainForClients() { $client = $this->getMock('Guzzle\\Service\\Client'); $chain = CompositeFactory::getDefaultChain($client); $a = $chain->getIterator()->getArrayCopy(); $this->assertEquals(1, count($a)); $this->assertInstanceOf('Guzzle\\Service\\Command\\Factory\\ConcreteClassFactory', $a[0]); $description = $this->getMock('Guzzle\\Service\\Description\\ServiceDescription'); $client->expects($this->once()) ->method('getDescription') ->will($this->returnValue($description)); $chain = CompositeFactory::getDefaultChain($client); $a = $chain->getIterator()->getArrayCopy(); $this->assertEquals(2, count($a)); $this->assertInstanceOf('Guzzle\\Service\\Command\\Factory\\ServiceDescriptionFactory', $a[0]); $this->assertInstanceOf('Guzzle\\Service\\Command\\Factory\\ConcreteClassFactory', $a[1]); } } tests/Guzzle/Tests/Service/Command/Factory/ConcreteClassFactoryTest.php000064400000003321151742734600022316 0ustar00 $prefix )); } $factory = new ConcreteClassFactory($client); if (is_null($result)) { $this->assertNull($factory->factory($key)); } else { $this->assertInstanceof($result, $factory->factory($key)); } } } tests/Guzzle/Tests/Service/Command/Factory/MapFactoryTest.php000064400000001741151742734600020307 0ustar00 'Guzzle\Tests\Service\Mock\Command\MockCommand', 'test1' => 'Guzzle\Tests\Service\Mock\Command\OtherCommand' )); if (is_null($result)) { $this->assertNull($factory->factory($key)); } else { $this->assertInstanceof($result, $factory->factory($key)); } } } tests/Guzzle/Tests/Service/Command/Factory/ServiceDescriptionFactoryTest.php000064400000002570151742734600023377 0ustar00 array( 'jarjar' => array( 'class' => 'Guzzle\Tests\Service\Mock\Command\MockCommand' ), 'binks' => array( 'class' => 'Guzzle\Tests\Service\Mock\Command\OtherCommand' ) ) )); $factory = new ServiceDescriptionFactory($d); $this->assertSame($d, $factory->getServiceDescription()); if (is_null($result)) { $this->assertNull($factory->factory($key)); } else { $this->assertInstanceof($result, $factory->factory($key)); } } } tests/Guzzle/Tests/Service/Description/ApiCommandTest.php000064400000005725151742734600017556 0ustar00 'test', 'doc' => 'doc', 'method' => 'POST', 'uri' => '/api/v1', 'params' => array( 'key' => array( 'required' => 'true', 'type' => 'string', 'max_length' => 10 ), 'key_2' => array( 'required' => 'true', 'type' => 'integer', 'default' => 10 ) ) )); $this->assertEquals('test', $c->getName()); $this->assertEquals('doc', $c->getDoc()); $this->assertEquals('POST', $c->getMethod()); $this->assertEquals('/api/v1', $c->getUri()); $this->assertEquals('Guzzle\\Service\\Command\\DynamicCommand', $c->getConcreteClass()); $this->assertEquals(array( 'key' => new ApiParam(array( 'required' => 'true', 'type' => 'string', 'max_length' => 10 )), 'key_2' => new ApiParam(array( 'required' => 'true', 'type' => 'integer', 'default' => 10 )) ), $c->getParams()); $this->assertEquals(new ApiParam(array( 'required' => 'true', 'type' => 'integer', 'default' => 10 )), $c->getParam('key_2')); $this->assertNull($c->getParam('afefwef')); } /** * @covers Guzzle\Service\Description\ApiCommand::__construct */ public function testAllowsConcreteCommands() { $c = new ApiCommand(array( 'name' => 'test', 'class' => 'Guzzle\\Service\\Command\ClosureCommand', 'params' => array( 'p' => new ApiParam(array( 'name' => 'foo' )) ) )); $this->assertEquals('Guzzle\\Service\\Command\ClosureCommand', $c->getConcreteClass()); } /** * @covers Guzzle\Service\Description\ApiCommand::toArray */ public function testConvertsToArray() { $data = array( 'name' => 'test', 'class' => 'Guzzle\\Service\\Command\ClosureCommand', 'doc' => 'test', 'method' => 'PUT', 'uri' => '/', 'params' => array( 'p' => new ApiParam(array( 'name' => 'foo' )) ) ); $c = new ApiCommand($data); $this->assertEquals($data, $c->toArray()); } } tests/Guzzle/Tests/Service/Description/ApiParamTest.php000064400000006307151742734600017235 0ustar00 'foo', 'type' => 'bar', 'required' => true, 'default' => '123', 'doc' => '456', 'min_length' => 2, 'max_length' => 5, 'location' => 'body', 'static' => 'static!', 'prepend' => 'before.', 'append' => '.after', 'filters' => 'trim,json_encode' ); public function testCreatesParamFromArray() { $p = new ApiParam($this->data); $this->assertEquals('foo', $p->getName()); $this->assertEquals('bar', $p->getType()); $this->assertEquals(true, $p->getRequired()); $this->assertEquals('123', $p->getDefault()); $this->assertEquals('456', $p->getDoc()); $this->assertEquals(2, $p->getMinLength()); $this->assertEquals(5, $p->getMaxLength()); $this->assertEquals('body', $p->getLocation()); $this->assertEquals('static!', $p->getStatic()); $this->assertEquals('before.', $p->getPrepend()); $this->assertEquals('.after', $p->getAppend()); $this->assertEquals(array('trim', 'json_encode'), $p->getFilters()); } public function testCanConvertToArray() { $p = new ApiParam($this->data); $this->assertEquals($this->data, $p->toArray()); } public function testFromArrayConvertsBooleans() { $d = $this->data; $d['required'] = 'false'; $p = new ApiParam($d); $this->assertEquals(false, $p->getRequired()); $d['required'] = 'true'; $p = new ApiParam($d); $this->assertEquals(true, $p->getRequired()); } public function testUsesStatic() { $d = $this->data; $d['static'] = 'foo'; $p = new ApiParam($d); $this->assertEquals('foo', $p->getValue('bar')); } public function testUsesDefault() { $d = $this->data; $d['default'] = 'foo'; $d['static'] = null; $p = new ApiParam($d); $this->assertEquals('foo', $p->getValue(null)); } public function testConvertsBooleanValues() { $d = $this->data; $d['static'] = 'true'; $p = new ApiParam($d); $this->assertEquals(true, $p->getValue(null)); $d['static'] = 'false'; $p = new ApiParam($d); $this->assertEquals(false, $p->getValue(null)); } public function testReturnsYourValue() { $d = $this->data; $d['static'] = null; $p = new ApiParam($d); $this->assertEquals('foo', $p->getValue('foo')); } public function testFiltersValues() { $d = $this->data; $d['static'] = null; $d['filters'] = 'strtoupper'; $p = new ApiParam($d); $this->assertEquals('FOO', $p->filter('foo')); } public function testUsesArrayByDefaultForFilters() { $d = $this->data; $d['filters'] = null; $p = new ApiParam($d); $this->assertEquals(array(), $p->getFilters()); } } tests/Guzzle/Tests/Service/Description/ArrayDescriptionBuilderTest.php000064400000005602151742734600022331 0ustar00 array( 'abstract' => array( 'method' => 'GET', 'params' => array( 'test' => array( 'type' => 'string', 'required' => true ) ) ), 'abstract2' => array( 'uri' => '/test', 'extends' => 'abstract' ), 'concrete' => array( 'extends' => 'abstract2' ) ) )); $c = $d->getCommand('concrete'); $this->assertEquals('/test', $c->getUri()); $this->assertEquals('GET', $c->getMethod()); $params = $c->getParams(); $param = $params['test']; $this->assertEquals('string', $param->getType()); $this->assertTrue($param->getRequired()); } /** * @covers Guzzle\Service\Description\ServiceDescription::factory * @covers Guzzle\Service\Description\ArrayDescriptionBuilder::build * @expectedException RuntimeException */ public function testThrowsExceptionWhenExtendingMissingCommand() { ServiceDescription::factory(array( 'commands' => array( 'concrete' => array( 'extends' => 'missing' ) ) )); } /** * @covers Guzzle\Service\Description\ArrayDescriptionBuilder::build */ public function testRegistersCustomTypes() { ServiceDescription::factory(array( 'types' => array( 'foo' => array( 'class' => 'Guzzle\\Common\\Validation\\Regex', 'pattern' => '/[0-9]+/' ) ) )); $valid = Inspector::getInstance()->validateConstraint('foo', 'abc'); $this->assertEquals('abc does not match the regular expression', $valid); } /** * @covers Guzzle\Service\Description\ArrayDescriptionBuilder::build * @expectedException RuntimeException * @expectedExceptionMessage Custom types require a class attribute */ public function testCustomTypesRequireClassAttribute() { ServiceDescription::factory(array( 'types' => array( 'slug' => array() ) )); } } tests/Guzzle/Tests/Service/Description/JsonDescriptionBuilderTest.php000064400000002036151742734600022162 0ustar00build('/foo.does.not.exist'); } public function testBuildsServiceDescriptions() { $j = new JsonDescriptionBuilder(); $description = $j->build(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'TestData' . DIRECTORY_SEPARATOR . 'test_service.json'); $this->assertTrue($description->hasCommand('test')); $test = $description->getCommand('test'); $this->assertEquals('/path', $test->getUri()); $test = $description->getCommand('concrete'); $this->assertEquals('/abstract', $test->getUri()); } } tests/Guzzle/Tests/Service/Description/ServiceDescriptionAbstractFactoryTest.php000064400000002522151742734600024356 0ustar00xmlFile = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'TestData' . DIRECTORY_SEPARATOR . 'test_service.xml'; $this->jsonFile = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'TestData' . DIRECTORY_SEPARATOR . 'test_service.json'; } public function testFactoryDelegatesToConcreteFactories() { $factory = new ServiceDescriptionAbstractFactory(); $this->assertInstanceOf('Guzzle\Service\Description\ServiceDescription', $factory->build($this->xmlFile)); $this->assertInstanceOf('Guzzle\Service\Description\ServiceDescription', $factory->build($this->jsonFile)); } /** * @expectedException Guzzle\Service\Exception\DescriptionBuilderException */ public function testFactoryEnsuresItCanHandleTheTypeOfFileOrArray() { $factory = new ServiceDescriptionAbstractFactory(); $factory->build('jarJarBinks'); } } tests/Guzzle/Tests/Service/Description/ServiceDescriptionTest.php000064400000005150151742734600021342 0ustar00serviceData = array( 'test_command' => new ApiCommand(array( 'doc' => 'documentationForCommand', 'method' => 'DELETE', 'class' => 'Guzzle\\Tests\\Service\\Mock\\Command\\MockCommand', 'params' => array( 'bucket' => array( 'required' => true ), 'key' => array( 'required' => true ) ) )) ); } /** * @covers Guzzle\Service\Description\ServiceDescription::factory * @covers Guzzle\Service\Description\ArrayDescriptionBuilder::build */ public function testFactoryDelegatesToConcreteFactories() { $xmlFile = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'TestData' . DIRECTORY_SEPARATOR . 'test_service.xml'; $jsonFile = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'TestData' . DIRECTORY_SEPARATOR . 'test_service.json'; $this->assertInstanceOf('Guzzle\Service\Description\ServiceDescription', ServiceDescription::factory($xmlFile)); $this->assertInstanceOf('Guzzle\Service\Description\ServiceDescription', ServiceDescription::factory($jsonFile)); } /** * @covers Guzzle\Service\Description\ServiceDescription * @covers Guzzle\Service\Description\ServiceDescription::__construct * @covers Guzzle\Service\Description\ServiceDescription::getCommands * @covers Guzzle\Service\Description\ServiceDescription::getCommand */ public function testConstructor() { $service = new ServiceDescription($this->serviceData); $this->assertEquals(1, count($service->getCommands())); $this->assertFalse($service->hasCommand('foobar')); $this->assertTrue($service->hasCommand('test_command')); } /** * @covers Guzzle\Service\Description\ServiceDescription::serialize * @covers Guzzle\Service\Description\ServiceDescription::unserialize */ public function testIsSerializable() { $service = new ServiceDescription($this->serviceData); $data = serialize($service); $d2 = unserialize($data); $this->assertEquals($service, $d2); } } tests/Guzzle/Tests/Service/Description/XmlDescriptionBuilderTest.php000064400000005365151742734600022021 0ustar00build('file_not_found'); } /** * @covers Guzzle\Service\Description\XmlDescriptionBuilder * @covers Guzzle\Service\Description\ServiceDescription */ public function testBuildsServiceUsingFile() { $x = new XmlDescriptionBuilder(); $service = $x->build(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'TestData' . DIRECTORY_SEPARATOR . 'test_service.xml'); $this->assertTrue($service->hasCommand('search')); $this->assertTrue($service->hasCommand('test')); $this->assertTrue($service->hasCommand('trends.location')); $this->assertTrue($service->hasCommand('geo.id')); $this->assertInstanceOf('Guzzle\\Service\\Description\\ApiCommand', $service->getCommand('search')); $this->assertInternalType('array', $service->getCommands()); $this->assertEquals(7, count($service->getCommands())); $this->assertNull($service->getCommand('missing')); $command = $service->getCommand('test'); $this->assertInstanceOf('Guzzle\\Service\\Description\\ApiCommand', $command); $this->assertEquals('test', $command->getName()); $this->assertInternalType('array', $command->getParams()); $this->assertEquals(array( 'name' => 'bucket', 'required' => true, 'doc' => 'Bucket location' ), array_filter($command->getParam('bucket')->toArray())); $this->assertEquals('DELETE', $command->getMethod()); $this->assertEquals('{ bucket }/{ key }{ format }', $command->getUri()); $this->assertEquals('Documentation', $command->getDoc()); $this->assertArrayHasKey('custom_filter', Inspector::getInstance()->getRegisteredConstraints()); } /** * @covers Guzzle\Service\Description\XmlDescriptionBuilder */ public function testCanExtendOtherFiles() { $x = new XmlDescriptionBuilder(); $service = $x->build(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'TestData' . DIRECTORY_SEPARATOR . 'test_service.xml'); $command = $service->getCommand('concrete'); $this->assertEquals('/test', $command->getUri()); } } tests/Guzzle/Tests/Service/InflectorTest.php000064400000005000151742734600015172 0ustar00assertEquals('camel_case', Inflector::snake('camelCase')); $this->assertEquals('camel_case', Inflector::snake('CamelCase')); $this->assertEquals('camel_case_words', Inflector::snake('CamelCaseWords')); $this->assertEquals('camel_case_words', Inflector::snake('CamelCase_words')); $this->assertEquals('test', Inflector::snake('test')); $this->assertEquals('test', Inflector::snake('test')); $this->assertEquals('expect100_continue', Inflector::snake('Expect100Continue')); } public function testCamel() { $this->assertEquals('CamelCase', Inflector::camel('camel_case')); $this->assertEquals('CamelCaseWords', Inflector::camel('camel_case_words')); $this->assertEquals('Test', Inflector::camel('test')); $this->assertEquals('Expect100Continue', ucfirst(Inflector::camel('expect100_continue'))); // Get from cache $this->assertEquals('Test', Inflector::camel('test', false)); } public function testProtectsAgainstCacheOverflow() { $perCachePurge = Inflector::MAX_ENTRIES_PER_CACHE * 0.1; $cached = Inflector::getCache(); $currentSnake = count($cached['snake']); $currentCamel = count($cached['camel']); unset($cached); // Fill each cache with garbage, then make sure it flushes out cached // entries and maintains a cache cap while (++$currentSnake < Inflector::MAX_ENTRIES_PER_CACHE + 1) { Inflector::snake(uniqid()); } while (++$currentCamel < Inflector::MAX_ENTRIES_PER_CACHE + 1) { Inflector::camel(uniqid()); } $cached = Inflector::getCache(); $this->assertEquals(Inflector::MAX_ENTRIES_PER_CACHE, count($cached['snake'])); $this->assertEquals(Inflector::MAX_ENTRIES_PER_CACHE, count($cached['camel'])); unset($cached); // Add another element to each cache to remove 10% of the cache Inflector::snake(uniqid()); Inflector::camel(uniqid()); $cached = Inflector::getCache(); $this->assertEquals(Inflector::MAX_ENTRIES_PER_CACHE - $perCachePurge + 1, count($cached['snake'])); $this->assertEquals(Inflector::MAX_ENTRIES_PER_CACHE - $perCachePurge + 1, count($cached['camel'])); } } tests/Guzzle/Tests/Service/InspectorTest.php000064400000027334151742734600015231 0ustar00assertNotEmpty($inspector->getRegisteredConstraints()); } /** * @covers Guzzle\Service\Inspector::prepareConfig */ public function testPreparesConfig() { $c = Inspector::prepareConfig(array( 'a' => '123', 'base_url' => 'http://www.test.com/' ), array( 'a' => 'xyz', 'b' => 'lol' ), array('a')); $this->assertInstanceOf('Guzzle\Common\Collection', $c); $this->assertEquals(array( 'a' => '123', 'b' => 'lol', 'base_url' => 'http://www.test.com/' ), $c->getAll()); try { $c = Inspector::prepareConfig(null, null, array('a')); $this->fail('Exception not throw when missing config'); } catch (ValidationException $e) { } } /** * @covers Guzzle\Service\Inspector */ public function testAddsDefaultAndInjectsConfigs() { $col = new Collection(array( 'username' => 'user', 'string' => 'test', 'float' => 1.23 )); $inspector = Inspector::getInstance(); $inspector->validateConfig($inspector->getApiParamsForClass(__CLASS__), $col); $this->assertEquals(false, $col->get('bool_2')); $this->assertEquals('user_test_', $col->get('dynamic')); $this->assertEquals(1.23, $col->get('float')); } /** * @covers Guzzle\Service\Inspector::validateConfig * @covers Guzzle\Service\Inspector::getApiParamsForClass * @expectedException Guzzle\Service\Exception\ValidationException */ public function testValidatesTypeHints() { $inspector = Inspector::getInstance(); $inspector->validateConfig($inspector->getApiParamsForClass(__CLASS__), new Collection(array( 'test' => 'uh oh', 'username' => 'test' ))); } /** * @covers Guzzle\Service\Inspector::validateConfig */ public function testConvertsBooleanDefaults() { $c = new Collection(array( 'test' => $this, 'username' => 'test' )); $inspector = Inspector::getInstance(); $inspector->validateConfig($inspector->getApiParamsForClass(__CLASS__), $c); $this->assertTrue($c->get('bool_1')); $this->assertFalse($c->get('bool_2')); } /** * @covers Guzzle\Service\Inspector */ public function testInspectsClassArgs() { $doc = <<setAccessible(true); $params = $method->invoke($inspector, $doc); $this->assertEquals(array( 'required' => true, 'doc' => 'API username', 'type' => 'string' ), array_filter($params['username']->toArray())); $this->assertEquals(array( 'required' => true, 'default' => 'v1', 'doc' => 'API version', 'type' => "choice:'v1','v2',v3" ), array_filter($params['api_version']->toArray())); $this->assertEquals(array( 'required' => true, 'default' => 'https', 'doc' => 'HTTP protocol (http or https)', 'type' => 'string' ), array_filter($params['protocol']->toArray())); $this->assertEquals(array( 'required' => true, 'default' => '{ protocol }://{ subdomain }.unfuddle.com/api/{ api_version }/', 'doc' => 'Unfuddle API base URL', 'type' => 'string' ), array_filter($params['base_url']->toArray())); $this->assertEquals(array( 'type' => "type:object" ), array_filter($params['class']->toArray())); $config = new Collection(array( 'username' => 'test', 'password' => 'pass', 'subdomain' => 'sub', 'api_version' => 'v2' )); Inspector::getInstance()->validateConfig($params, $config); // make sure the configs were injected $this->assertEquals('https://sub.unfuddle.com/api/v2/', $config->get('base_url')); try { Inspector::getInstance()->validateConfig($params, new Collection(array( 'base_url' => '', 'username' => '', 'password' => '', 'class' => '123', 'api_version' => 'v10' ))); $this->fail('Expected exception not thrown when params are invalid'); } catch (ValidationException $e) { $concat = $e->getMessage(); $this->assertContains("Validation errors: Requires that the username argument be supplied. (API username)", $concat); $this->assertContains("Requires that the password argument be supplied. (API password)", $concat); $this->assertContains("Requires that the subdomain argument be supplied. (Unfuddle project subdomain)", $concat); $this->assertContains("api_version: Value must be one of: v1, v2, v3", $concat); $this->assertContains("class: Value must be of type object", $concat); } } /** * @covers Guzzle\Service\Inspector::registerConstraint * @covers Guzzle\Service\Inspector::getConstraint * @covers Guzzle\Service\Inspector::getRegisteredConstraints */ public function testRegistersCustomConstraints() { $constraintClass = 'Guzzle\\Common\\Validation\\Ip'; Inspector::getInstance()->registerConstraint('mock', $constraintClass); Inspector::getInstance()->registerConstraint('mock_2', $constraintClass, array( 'version' => '4' )); $this->assertArrayHasKey('mock', Inspector::getInstance()->getRegisteredConstraints()); $this->assertArrayHasKey('mock_2', Inspector::getInstance()->getRegisteredConstraints()); $this->assertInstanceOf($constraintClass, Inspector::getInstance()->getConstraint('mock')); $this->assertInstanceOf($constraintClass, Inspector::getInstance()->getConstraint('mock_2')); $validating = new Collection(array( 'data' => '192.168.16.121', 'test' => '10.1.1.0' )); $this->assertTrue(Inspector::getInstance()->validateConfig(array( 'data' => new ApiParam(array( 'type' => 'mock', 'name' => 'data' )), 'test' => new ApiParam(array( 'type' => 'mock_2', 'name' => 'test' )) ), $validating, false)); } /** * @covers Guzzle\Service\Inspector * @expectedException InvalidArgumentException */ public function testChecksFilterValidity() { Inspector::getInstance()->validateConfig(array( 'data' => new ApiParam(array( 'type' => 'invalid' )) ), new Collection(array( 'data' => 'false' ))); } /** * @covers Guzzle\Service\Inspector */ public function testValidatesArgs() { $config = new Collection(array( 'data' => 123, 'min' => 'a', 'max' => 'aaa' )); $result = Inspector::getInstance()->validateConfig(array( 'data' => new ApiParam(array( 'type' => 'string' )), 'min' => new ApiParam(array( 'type' => 'string', 'min_length' => 2 )), 'max' => new ApiParam(array( 'type' => 'string', 'max_length' => 2 )) ), $config, false); $concat = implode("\n", $result); $this->assertContains("Value must be of type string", $concat); $this->assertContains("Requires that the min argument be >= 2 characters", $concat); $this->assertContains("Requires that the max argument be <= 2 characters", $concat); } /** * @covers Guzzle\Service\Inspector::parseDocBlock */ public function testVerifiesGuzzleAnnotations() { $inspector = new Inspector(); $method = new \ReflectionMethod($inspector, 'parseDocBlock'); $method->setAccessible(true); $result = $method->invoke($inspector, 'testing'); $this->assertEquals(array(), $result); } /** * @covers Guzzle\Service\Inspector::validateConfig * @covers Guzzle\Service\Inspector::getApiParamsForClass */ public function testRunsValuesThroughFilters() { $data = new Collection(array( 'username' => 'TEST', 'test_function' => 'foo' )); $inspector = Inspector::getInstance(); $inspector->validateConfig($inspector->getApiParamsForClass(__CLASS__), $data); $this->assertEquals('test', $data->get('username')); $this->assertEquals('FOO', $data->get('test_function')); } /** * @covers Guzzle\Service\Inspector::setTypeValidation * @covers Guzzle\Service\Inspector::validateConfig */ public function testTypeValidationCanBeDisabled() { $i = Inspector::getInstance(); $i->setTypeValidation(false); // Ensure that the type is not validated $i->validateConfig(array( 'data' => new ApiParam(array( 'type' => 'string' )) ), new Collection(array( 'data' => new \stdClass() )), true); $i->setTypeValidation(true); // Ensure that nothing is validated $i->validateConfig(array( 'data' => new ApiParam(array( 'type' => 'string' )) ), new Collection(array( 'data' => new \stdClass() )), true, false); } /** * @covers Guzzle\Service\Inspector::validateConfig */ public function testSkipsFurtherValidationIfNotSet() { $i = Inspector::getInstance(); // Ensure that the type is not validated $this->assertEquals(true, $i->validateConfig(array( 'data' => new ApiParam(array( 'type' => 'string' )) ), new Collection(), true)); } } tests/Guzzle/Tests/Service/JsonLoaderTest.php000064400000002473151742734600015320 0ustar00parseJsonFile('fooooooo!'); } /** * @expectedException Guzzle\Service\Exception\JsonException * @expectedExceptionMessage Error loading JSON data from */ public function testJsonMustBeValue() { $loader = new JsonLoader(); $loader->parseJsonFile(__FILE__); } public function testFactoryCanCreateFromJson() { $file = dirname(__DIR__) . '/TestData/services/json1.json'; $loader = new JsonLoader(); $data = $loader->parseJsonFile($file); $this->assertArrayHasKey('includes', $data); $this->assertArrayHasKey('services', $data); $this->assertInternalType('array', $data['services']['foo']); $this->assertInternalType('array', $data['services']['abstract']); $this->assertInternalType('array', $data['services']['mock']); $this->assertEquals('bar', $data['services']['foo']['params']['baz']); } } tests/Guzzle/Tests/Service/Mock/Command/IterableCommand.php000064400000001164151742734600017711 0ustar00request = $this->client->createRequest('GET'); // Add the next token and page size query string values $this->request->getQuery()->set('next_token', $this->get('next_token')); if ($this->get('page_size')) { $this->request->getQuery()->set('page_size', $this->get('page_size')); } } } tests/Guzzle/Tests/Service/Mock/Command/MockCommand.php000064400000000625151742734600017054 0ustar00request = $this->client->createRequest(); } } tests/Guzzle/Tests/Service/Mock/Command/OtherCommand.php000064400000000645151742734600017246 0ustar00request = $this->client->getRequest('HEAD'); } } tests/Guzzle/Tests/Service/Mock/Command/Sub/Sub.php000064400000000255151742734600016145 0ustar00 '{{scheme}}://127.0.0.1:8124/{{api_version}}/{{subdomain}}', 'scheme' => 'http', 'api_version' => 'v1' ), array('username', 'password', 'subdomain')); return new self($config->get('base_url'), $config); } } tests/Guzzle/Tests/Service/Mock/MockInflector.php000064400000000575151742734600016051 0ustar00nextToken) { $this->command->set('next_token', $this->nextToken); } $this->command->set('page_size', (int) $this->calculatePageSize()); $this->command->execute(); $data = json_decode($this->command->getResponse()->getBody(true), true); $this->nextToken = $data['next_token']; return $data['resources']; } } tests/Guzzle/Tests/Service/Resource/ResourceIteratorApplyBatchedTest.php000064400000004334151742734600022627 0ustar00assertInternalType('array', ResourceIteratorApplyBatched::getAllEvents()); } /** * @covers Guzzle\Service\Resource\ResourceIteratorApplyBatched */ public function testSendsRequestsForNextSetOfResources() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"g\", \"resources\": [\"d\", \"e\", \"f\"] }", "HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"j\", \"resources\": [\"g\", \"h\", \"i\"] }", "HTTP/1.1 200 OK\r\nContent-Length: 41\r\n\r\n{ \"next_token\": \"\", \"resources\": [\"j\"] }", )); $ri = new MockCommandIterator($this->getServiceBuilder()->get('mock')->getCommand('iterable_command'), array( 'page_size' => 3, 'limit' => 7 )); $received = array(); $apply = new ResourceIteratorApplyBatched($ri, function(ResourceIterator $i, array $batch) use (&$received) { $received[] = $batch; }); $apply->apply(3); $requests = $this->getServer()->getReceivedRequests(true); $this->assertEquals(3, count($requests)); $this->assertEquals(3, $requests[0]->getQuery()->get('page_size')); $this->assertEquals(3, $requests[1]->getQuery()->get('page_size')); $this->assertEquals(1, $requests[2]->getQuery()->get('page_size')); $this->assertEquals(array('d', 'e', 'f'), array_values($received[0])); $this->assertEquals(array('g', 'h', 'i'), array_values($received[1])); $this->assertEquals(array('j'), array_values($received[2])); $this->assertEquals(3, $apply->getBatchCount()); $this->assertEquals(7, $apply->getIteratedCount()); } } tests/Guzzle/Tests/Service/Resource/ResourceIteratorClassFactoryTest.php000064400000002011151742734600022652 0ustar00build('foo'); } public function testBuildsResourceIterators() { $factory = new ResourceIteratorClassFactory('Guzzle\Tests\Service\Mock\Model'); $command = new MockCommand(); $iterator = $factory->build($command, array( 'client.namespace' => 'Guzzle\Tests\Service\Mock' )); $this->assertInstanceOf('Guzzle\Tests\Service\Mock\Model\MockCommandIterator', $iterator); } } tests/Guzzle/Tests/Service/Resource/ResourceIteratorTest.php000064400000017136151742734600020352 0ustar00assertInternalType('array', ResourceIterator::getAllEvents()); } /** * @covers Guzzle\Service\Resource\ResourceIterator */ public function testConstructorConfiguresDefaults() { $ri = $this->getMockForAbstractClass('Guzzle\\Service\\Resource\\ResourceIterator', array( $this->getServiceBuilder()->get('mock')->getCommand('iterable_command'), array( 'limit' => 10, 'page_size' => 3 ) ), 'MockIterator'); $this->assertEquals(false, $ri->getNextToken()); $this->assertEquals(false, $ri->current()); } /** * @covers Guzzle\Service\Resource\ResourceIterator */ public function testSendsRequestsForNextSetOfResources() { // Queue up an array of responses for iterating $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"g\", \"resources\": [\"d\", \"e\", \"f\"] }", "HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"j\", \"resources\": [\"g\", \"h\", \"i\"] }", "HTTP/1.1 200 OK\r\nContent-Length: 41\r\n\r\n{ \"next_token\": \"\", \"resources\": [\"j\"] }" )); // Create a new resource iterator using the IteraableCommand mock $ri = new MockCommandIterator($this->getServiceBuilder()->get('mock')->getCommand('iterable_command'), array( 'page_size' => 3 )); // Ensure that no requests have been sent yet $this->assertEquals(0, count($this->getServer()->getReceivedRequests(false))); //$this->assertEquals(array('d', 'e', 'f', 'g', 'h', 'i', 'j'), $ri->toArray()); $ri->toArray(); $requests = $this->getServer()->getReceivedRequests(true); $this->assertEquals(3, count($requests)); $this->assertEquals(3, $requests[0]->getQuery()->get('page_size')); $this->assertEquals(3, $requests[1]->getQuery()->get('page_size')); $this->assertEquals(3, $requests[2]->getQuery()->get('page_size')); // Reset and resend $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"g\", \"resources\": [\"d\", \"e\", \"f\"] }", "HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"j\", \"resources\": [\"g\", \"h\", \"i\"] }", "HTTP/1.1 200 OK\r\nContent-Length: 41\r\n\r\n{ \"next_token\": \"\", \"resources\": [\"j\"] }", )); $d = array(); reset($ri); foreach ($ri as $data) { $d[] = $data; } $this->assertEquals(array('d', 'e', 'f', 'g', 'h', 'i', 'j'), $d); } /** * @covers Guzzle\Service\Resource\ResourceIterator */ public function testCalculatesPageSize() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"g\", \"resources\": [\"d\", \"e\", \"f\"] }", "HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"j\", \"resources\": [\"g\", \"h\", \"i\"] }", "HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"j\", \"resources\": [\"j\", \"k\"] }" )); $ri = new MockCommandIterator($this->getServiceBuilder()->get('mock')->getCommand('iterable_command'), array( 'page_size' => 3, 'limit' => 7 )); $this->assertEquals(array('d', 'e', 'f', 'g', 'h', 'i', 'j'), $ri->toArray()); $requests = $this->getServer()->getReceivedRequests(true); $this->assertEquals(3, count($requests)); $this->assertEquals(3, $requests[0]->getQuery()->get('page_size')); $this->assertEquals(3, $requests[1]->getQuery()->get('page_size')); $this->assertEquals(1, $requests[2]->getQuery()->get('page_size')); } /** * @covers Guzzle\Service\Resource\ResourceIterator */ public function testUseAsArray() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"g\", \"resources\": [\"d\", \"e\", \"f\"] }", "HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"\", \"resources\": [\"g\", \"h\", \"i\"] }" )); $ri = new MockCommandIterator($this->getServiceBuilder()->get('mock')->getCommand('iterable_command')); // Ensure that the key is never < 0 $this->assertEquals(0, $ri->key()); $this->assertEquals(0, count($ri)); // Ensure that the iterator can be used as KVP array $data = array(); foreach ($ri as $key => $value) { $data[$key] = $value; } // Ensure that the iterate is countable $this->assertEquals(6, count($ri)); $this->assertEquals(array('d', 'e', 'f', 'g', 'h', 'i'), $data); } /** * @covers Guzzle\Service\Resource\ResourceIterator */ public function testBailsWhenSendReturnsNoResults() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\n\r\n{ \"next_token\": \"g\", \"resources\": [\"d\", \"e\", \"f\"] }", "HTTP/1.1 200 OK\r\n\r\n{ \"next_token\": \"\", \"resources\": [] }" )); $ri = new MockCommandIterator($this->getServiceBuilder()->get('mock')->getCommand('iterable_command')); // Ensure that the iterator can be used as KVP array $data = $ri->toArray(); // Ensure that the iterate is countable $this->assertEquals(3, count($ri)); $this->assertEquals(array('d', 'e', 'f'), $data); } /** * @covers Guzzle\Service\Resource\ResourceIterator::set * @covers Guzzle\Service\Resource\ResourceIterator::get */ public function testHoldsDataOptions() { $ri = new MockCommandIterator($this->getServiceBuilder()->get('mock')->getCommand('iterable_command')); $this->assertNull($ri->get('foo')); $this->assertSame($ri, $ri->set('foo', 'bar')); $this->assertEquals('bar', $ri->get('foo')); } /** * @covers Guzzle\Service\Resource\ResourceIterator::setLimit * @covers Guzzle\Service\Resource\ResourceIterator::setPageSize */ public function testSettingLimitOrPageSizeClearsData() { $this->getServer()->flush(); $this->getServer()->enqueue(array( "HTTP/1.1 200 OK\r\n\r\n{ \"next_token\": \"\", \"resources\": [\"d\", \"e\", \"f\"] }", "HTTP/1.1 200 OK\r\n\r\n{ \"next_token\": \"\", \"resources\": [\"d\", \"e\", \"f\"] }", "HTTP/1.1 200 OK\r\n\r\n{ \"next_token\": \"\", \"resources\": [\"d\", \"e\", \"f\"] }" )); $ri = new MockCommandIterator($this->getServiceBuilder()->get('mock')->getCommand('iterable_command')); $ri->toArray(); $this->assertNotEmpty($this->readAttribute($ri, 'resources')); $ri->setLimit(10); $this->assertEmpty($this->readAttribute($ri, 'resources')); $ri->toArray(); $this->assertNotEmpty($this->readAttribute($ri, 'resources')); $ri->setPageSize(10); $this->assertEmpty($this->readAttribute($ri, 'resources')); } } tests/Guzzle/Tests/TestData/FileBody.txt000075500000000000151742734600014241 0ustar00tests/Guzzle/Tests/TestData/mock_response000064400000000043151742734600014601 0ustar00HTTP/1.1 200 OK Content-Length: 0 tests/Guzzle/Tests/TestData/services/json1.json000064400000000647151742734600015571 0ustar00{ "includes": [ "json2.json" ], "services": { "abstract": { "access_key": "xyz", "secret": "abc" }, "mock": { "class": "Guzzle.Tests.Service.Mock.MockClient", "extends": "abstract", "params": { "username": "foo", "password": "baz", "subdomain": "bar" } } } } tests/Guzzle/Tests/TestData/services/json2.json000064400000000332151742734600015561 0ustar00{ "services": { "foo": { "class": "Guzzle.Tests.Service.Mock.MockClient", "extends": "abstract", "params": { "baz": "bar" } } } } tests/Guzzle/Tests/TestData/services/new_style.xml000064400000001246151742734600016373 0ustar00 Guzzle.Service.Builder.ServiceBuilder tests/Guzzle/Tests/TestData/services/old_style.xml000064400000000336151742734600016357 0ustar00 tests/Guzzle/Tests/TestData/services/services.json000064400000000471151742734600016355 0ustar00{ "abstract": { "access_key": "xyz", "secret": "abc" }, "mock": { "class": "Guzzle.Tests.Service.Mock.MockClient", "extends": "abstract", "params": { "username": "foo", "password": "baz", "subdomain": "bar" } } } tests/Guzzle/Tests/TestData/services/services.xml000064400000003343151742734600016205 0ustar00 tests/Guzzle/Tests/TestData/test_service.json000064400000000321151742734600015400 0ustar00{ "includes": [ "test_service2.json" ], "types": {}, "commands": { "test": { "uri": "/path" }, "concrete": { "extends": "abstract" } } } tests/Guzzle/Tests/TestData/test_service.xml000064400000004641151742734600015240 0ustar00 Documentation tests/Guzzle/Tests/TestData/test_service2.json000064400000000133151742734600015463 0ustar00{ "commands": { "abstract": { "uri": "/abstract" } } } tests/Guzzle/Tests/TestData/test_service2.xml000064400000000226151742734600015315 0ustar00 tests/bootstrap.php000064400000002532151742734600010447 0ustar00