├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── UPGRADE.md ├── composer.json ├── doc ├── installation.md └── usage.md ├── phpunit.xml.dist ├── src └── Widop │ └── HttpAdapter │ ├── AbstractHttpAdapter.php │ ├── BuzzHttpAdapter.php │ ├── CurlHttpAdapter.php │ ├── GuzzleHttpAdapter.php │ ├── HttpAdapterException.php │ ├── HttpAdapterInterface.php │ ├── HttpResponse.php │ ├── StreamHttpAdapter.php │ └── ZendHttpAdapter.php └── tests ├── Widop └── Tests │ └── HttpAdapter │ ├── AbstractHttpAdapterTest.php │ ├── BuzzHttpAdapterTest.php │ ├── CurlHttpAdapterTest.php │ ├── Fixtures │ └── file.txt │ ├── GuzzleHttpAdapterTest.php │ ├── HttpResponseTest.php │ ├── StreamHttpAdapterTest.php │ └── ZendHttpAdapterTest.php └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | # Composer 2 | /bin 3 | /composer.lock 4 | /vendor 5 | 6 | # PHPUnit 7 | /phpunit.xml 8 | /report 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3.3 5 | - 5.3 6 | - 5.4 7 | - 5.5 8 | 9 | before_script: 10 | - composer self-update 11 | - composer install --prefer-source 12 | 13 | script: bin/phpunit 14 | 15 | branches: 16 | only: master 17 | 18 | notifications: 19 | email: 20 | - timothee@widop.com 21 | - eric@widop.com 22 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # CHANGELOG 2 | 3 | ### 1.1.0 (2014-07-20) 4 | 5 | * 5285f30 - [Response] Add http status code support 6 | * 708bb4d - [Response] Add headers support 7 | * 4bc6ee5 - Return a response object instead of the body string 8 | * 04cfb94 - Add POST files support 9 | 10 | ### 1.0.2 (2013-10-30) 11 | 12 | * 29218fb - Add redirect support 13 | * 3a25fc3 - Add Zend http adapter support 14 | 15 | ### 1.0.1 (2013-09-23) 16 | 17 | * b3afdbc - Simplify stream http adapter 18 | * 4570f87 - Fix Curl & Stream adpaters with post array data 19 | 20 | ### 1.0.0 (2013-09-22) 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Wid'op 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 4 | documentation files (the "Software"), to deal in the Software without restriction, including without limitation the 5 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit 6 | persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the 9 | Software. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 12 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 13 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 14 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # README 2 | 3 | [![Build Status](https://secure.travis-ci.org/widop/http-adapter.png)](http://travis-ci.org/widop/http-adapter) 4 | 5 | The library allows to issue HTTP requests with PHP 5.3+. Currently, the supported adapters are: 6 | 7 | - [cURL](http://curl.haxx.se/) 8 | - [Stream](http://php.net/manual/en/book.stream.php) 9 | - [Buzz](https://github.com/kriswallsmith/Buzz) 10 | - [Guzzle](http://guzzlephp.org/) 11 | - [Zend](http://framework.zend.com/manual/2.0/en/modules/zend.http.client.html) 12 | 13 | Documentation 14 | ------------- 15 | 16 | 1. [Installation](http://github.com/widop/http-adapter/blob/master/doc/installation.md) 17 | 2. [Usage](http://github.com/widop/http-adapter/blob/master/doc/usage.md) 18 | 19 | ## Testing 20 | 21 | The library is fully unit tested by [PHPUnit](http://www.phpunit.de/) with a code coverage close to **100%**. To 22 | execute the test suite, check the travis [configuration](https://github.com/widop/http-adapter/blob/master/.travis.yml). 23 | 24 | ## Contribute 25 | 26 | We love contributors! The library is open source, if you'd like to contribute, feel free to propose a PR! 27 | 28 | ## License 29 | 30 | The Wid'op Http Adapter is under the MIT license. For the full copyright and license information, please read the 31 | [LICENSE](https://github.com/widop/http-adapter/blob/master/LICENSE) file that was distributed with this 32 | source code. 33 | -------------------------------------------------------------------------------- /UPGRADE.md: -------------------------------------------------------------------------------- 1 | # UPGRADE 2 | 3 | ## 1.0 to 1.1 4 | 5 | * The `Widop\HttpAdapter\HttpAdapterInterface::getContent` and the 6 | `Widop\HttpAdapter\HttpAdapterInterface::postContent` returns a `Widop\HttpAdapter\HttpResponse` instead of the 7 | response body string. 8 | * The third argument of the `Widop\HttpAdapter\HttpAdapterInterface::postContent` (`$content`) is now typehinted as 9 | `array` in order to be consistent with other parameters. 10 | * All protected properties/methods have been updated to private except for explicit entry points. 11 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "widop/http-adapter", 3 | "description": "Issue HTTP request.", 4 | "keywords": [ "request", "http" ], 5 | "type": "library", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Widop", 10 | "email": "contact@widop.com" 11 | }, 12 | { 13 | "name": "Eric GELOEN", 14 | "email": "geloen.eric@gmail.com" 15 | } 16 | ], 17 | "require": { 18 | "php": ">=5.3.3" 19 | }, 20 | "require-dev": { 21 | "phpunit/phpunit": "3.7.*", 22 | "ext-curl": "*", 23 | "kriswallsmith/buzz": "*", 24 | "guzzle/guzzle": "*", 25 | "zendframework/zend-http": "*" 26 | }, 27 | "suggest": { 28 | "ext-curl": "Allows you to use cURL adapter", 29 | "kriswallsmith/buzz": "Allows you to use Buzz adapter", 30 | "guzzle/guzzle": "Allows you to use Guzzle adapter", 31 | "zendframework/zend-http": "Allows you to use Zend adapter" 32 | }, 33 | "autoload": { 34 | "psr-0": { "Widop\\HttpAdapter": "src/" } 35 | }, 36 | "config": { 37 | "bin-dir": "bin" 38 | }, 39 | "extra": { 40 | "branch-alias": { 41 | "dev-master": "1.1-dev" 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /doc/installation.md: -------------------------------------------------------------------------------- 1 | # Installation 2 | 3 | To install the Wid'op http adapter library, you will need [Composer](http://getcomposer.org). It's a PHP 5.3+ 4 | dependency manager which allows you to declare the dependent libraries your project needs and it will install & 5 | autoload them for you. 6 | 7 | ## Set up Composer 8 | 9 | Composer comes with a simple phar file. To easily access it from anywhere on your system, you can execute: 10 | 11 | ``` 12 | $ curl -s https://getcomposer.org/installer | php 13 | $ sudo mv composer.phar /usr/local/bin/composer 14 | ``` 15 | 16 | ## Define dependencies 17 | 18 | Create a ``composer.json`` file at the root directory of your project and simply require the 19 | ``widop/http-adapter`` package: 20 | 21 | ``` 22 | { 23 | "require": { 24 | "widop/http-adapter": "*" 25 | } 26 | } 27 | ``` 28 | 29 | ## Install dependencies 30 | 31 | Now, you have define your dependencies, you can install them: 32 | 33 | ``` 34 | $ composer install 35 | ``` 36 | 37 | Composer will automatically download your dependencies & create an autoload file in the ``vendor`` directory. 38 | 39 | ## Autoload 40 | 41 | So easy, you just have to require the generated autoload file and you are already ready to play: 42 | 43 | ``` php 44 | getContent($url); 83 | ``` 84 | 85 | If you want to pass custom headers, you can use the second argument: 86 | 87 | ``` php 88 | $response = $httpAdapter->getContent($url, $headers); 89 | ``` 90 | 91 | ## Make a POST request 92 | 93 | Each adapter allows you to make a POST request: 94 | 95 | ``` php 96 | $response = $httpAdapter->postContent($url); 97 | ``` 98 | 99 | If you want to pass custom headers, you can use the second argument: 100 | 101 | ``` php 102 | $response = $httpAdapter->postContent($url, $headers); 103 | ``` 104 | 105 | If you want to pass POST datas, you can use the third argument: 106 | 107 | ``` php 108 | $response = $httpAdapter->postContent($url, $headers, $data); 109 | ``` 110 | 111 | If you want to pass POST files, you can use the fourth argument: 112 | 113 | ``` php 114 | $response = $httpAdapter->postContent($url, $headers, $data, $files); 115 | ``` 116 | 117 | ## Inspect the response 118 | 119 | All http adapter return a `Widop\HttpAdapter\HttpResponse` which wraps the base URL, the status code, the headers, the 120 | body and the effective URL. 121 | 122 | ``` php 123 | $url = $response->getUrl(); 124 | $statusCode = $response->getStatusCode(); 125 | $headers = $response->getHeaders(); 126 | $header = $reponse->getHeader('Date'); 127 | $body = $response->getBody(); 128 | $effectiveUrl = $response->getEffectiveUrl(); 129 | ``` 130 | 131 | Be aware that the effective URL is supported by most of the adapters except Buzz and Zend ones... If you know a 132 | workaround, a PR is welcome! 133 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ./tests 7 | 8 | 9 | 10 | 11 | ./src 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/Widop/HttpAdapter/AbstractHttpAdapter.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\HttpAdapter; 13 | 14 | /** 15 | * Abstract http adapter. 16 | * 17 | * @author GeLo 18 | */ 19 | abstract class AbstractHttpAdapter implements HttpAdapterInterface 20 | { 21 | /** @var integer */ 22 | private $maxRedirects; 23 | 24 | /** 25 | * Creates an Http adapter. 26 | * 27 | * @param integer $maxRedirects The maximum redirects. 28 | */ 29 | public function __construct($maxRedirects = 5) 30 | { 31 | $this->setMaxRedirects($maxRedirects); 32 | } 33 | 34 | /** 35 | * Gets the maximum redirects. 36 | * 37 | * @return integer The maximum redirects. 38 | */ 39 | public function getMaxRedirects() 40 | { 41 | return $this->maxRedirects; 42 | } 43 | 44 | /** 45 | * Sets the maximum redirects. 46 | * 47 | * @param integer $maxRedirects The maximum redirects. 48 | */ 49 | public function setMaxRedirects($maxRedirects) 50 | { 51 | $this->maxRedirects = $maxRedirects; 52 | } 53 | 54 | /** 55 | * Fixes the URL to match the http format. 56 | * 57 | * @param string $url The url. 58 | * 59 | * @return string The fixed url. 60 | */ 61 | protected function fixUrl($url) 62 | { 63 | if ((strpos($url, 'http://') !== 0) && (strpos($url, 'https://') !== 0)) { 64 | return sprintf('http://%s', $url); 65 | } 66 | 67 | return $url; 68 | } 69 | 70 | /** 71 | * Fixes the headers to match the http format. 72 | * 73 | * @param array $headers The headers. 74 | * 75 | * @return array The fixes headers. 76 | */ 77 | protected function fixHeaders(array $headers) 78 | { 79 | $fixedHeaders = array(); 80 | 81 | foreach ($headers as $key => $value) { 82 | if (is_int($key)) { 83 | $fixedHeaders[] = $value; 84 | } else { 85 | $fixedHeaders[] = sprintf('%s:%s', $key, $value); 86 | } 87 | } 88 | 89 | return $fixedHeaders; 90 | } 91 | 92 | /** 93 | * Fixes the content to match the http format. 94 | * 95 | * @param array|string $content The content. 96 | * 97 | * @return string The content. 98 | */ 99 | protected function fixContent($content) 100 | { 101 | return is_array($content) ? http_build_query($content) : $content; 102 | } 103 | 104 | /** 105 | * Creates an Http response. 106 | * 107 | * @param integer|null $statusCode The response status code. 108 | * @param string $url The response URL. 109 | * @param string|array $headers The response headers. 110 | * @param string $body The response body. 111 | * @param string|null $effectiveUrl The response effective URL. 112 | * 113 | * @return \Widop\HttpAdapter\HttpResponse The response. 114 | */ 115 | protected function createResponse($statusCode, $url, $headers, $body, $effectiveUrl = null) 116 | { 117 | return new HttpResponse($statusCode, $url, $this->createHeaders($headers), $body, $effectiveUrl); 118 | } 119 | 120 | /** 121 | * Creates the headers. 122 | * 123 | * @param string|array $headers The headers. 124 | * 125 | * @return array The created headers. 126 | */ 127 | private function createHeaders($headers) 128 | { 129 | if (is_string($headers)) { 130 | return $this->createHeaders(explode("\r\n", $headers)); 131 | } 132 | 133 | $fixedHeaders = array(); 134 | 135 | foreach ($headers as $key => $header) { 136 | if (is_int($key)) { 137 | if (($pos = strpos($header, ':')) === false) { 138 | continue; 139 | } 140 | 141 | $fixedHeaders[substr($header, 0, $pos)] = substr($header, $pos + 1); 142 | } else { 143 | $fixedHeaders[$key] = $this->createHeader($header); 144 | } 145 | } 146 | 147 | return $fixedHeaders; 148 | } 149 | 150 | /** 151 | * Creates an header. 152 | * 153 | * @param string|array $header The header. 154 | * 155 | * @return string The created header. 156 | */ 157 | private function createHeader($header) 158 | { 159 | if (is_array($header)) { 160 | return implode(';', $header); 161 | } 162 | 163 | return $header; 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /src/Widop/HttpAdapter/BuzzHttpAdapter.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\HttpAdapter; 13 | 14 | use Buzz\Browser; 15 | use Buzz\Message\RequestInterface; 16 | 17 | /** 18 | * Buzz Http adapter. 19 | * 20 | * @author GeLo 21 | */ 22 | class BuzzHttpAdapter extends AbstractHttpAdapter 23 | { 24 | /** @var \Buzz\Browser */ 25 | private $browser; 26 | 27 | /** 28 | * Constructor. 29 | * 30 | * @param \Buzz\Browser $browser The buzz browser. 31 | * @param integer $maxRedirects The maximum redirects. 32 | */ 33 | public function __construct(Browser $browser = null, $maxRedirects = 5) 34 | { 35 | parent::__construct($maxRedirects); 36 | 37 | if ($browser === null) { 38 | $browser = new Browser(); 39 | } 40 | 41 | $this->browser = $browser; 42 | } 43 | 44 | /** 45 | * {@inheritdoc} 46 | */ 47 | public function getContent($url, array $headers = array()) 48 | { 49 | return $this->sendRequest($url, RequestInterface::METHOD_GET, $headers); 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | public function postContent($url, array $headers = array(), array $content = array(), array $files = array()) 56 | { 57 | if (!empty($files)) { 58 | $content = array_merge($content, array_map(function($file) { return '@'.$file; }, $files)); 59 | } 60 | 61 | return $this->sendRequest($url, RequestInterface::METHOD_POST, $headers, $content); 62 | } 63 | 64 | /** 65 | * {@inheritdoc} 66 | */ 67 | public function head($url, array $headers = array()) 68 | { 69 | return $this->sendRequest($url, RequestInterface::METHOD_HEAD, $headers); 70 | } 71 | 72 | /** 73 | * {@inheritdoc} 74 | */ 75 | public function put($url, array $headers = array(), array $content = array(), array $files = array()) 76 | { 77 | if (!empty($files)) { 78 | $content = array_merge($content, array_map(function($file) { return '@'.$file; }, $files)); 79 | } 80 | 81 | return $this->sendRequest($url, RequestInterface::METHOD_PUT, $headers, $content); 82 | } 83 | 84 | /** 85 | * {@inheritdoc} 86 | */ 87 | public function getName() 88 | { 89 | return 'buzz'; 90 | } 91 | 92 | /** 93 | * Sends a request. 94 | * 95 | * @param string $url The url. 96 | * @param string $method The http method. 97 | * @param array $headers The header. 98 | * @param array $content The content. 99 | * 100 | * @throws \Widop\HttpAdapter\HttpAdapterException If an error occured. 101 | * 102 | * @return \Widop\HttpAdapter\HttpResponse The response. 103 | */ 104 | private function sendRequest($url, $method, array $headers = array(), array $content = array()) 105 | { 106 | $this->browser->getClient()->setMaxRedirects($this->getMaxRedirects()); 107 | 108 | try { 109 | $response = $this->browser->call($url, $method, $headers, $content); 110 | } catch (\Exception $e) { 111 | throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage()); 112 | } 113 | 114 | return $this->createResponse( 115 | $response->getStatusCode(), 116 | $url, 117 | $response->getHeaders(), 118 | $response->getContent() 119 | ); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/Widop/HttpAdapter/CurlHttpAdapter.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\HttpAdapter; 13 | 14 | /** 15 | * Curl Http adapter. 16 | * 17 | * @author GeLo 18 | */ 19 | class CurlHttpAdapter extends AbstractHttpAdapter 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getContent($url, array $headers = array()) 25 | { 26 | return $this->execute($url, $headers); 27 | } 28 | 29 | /** 30 | * {@inheritdoc} 31 | */ 32 | public function postContent($url, array $headers = array(), array $content = array(), array $files = array()) 33 | { 34 | return $this->execute($url, $headers, $this->getPostRequestClosure(true, $content, $files)); 35 | } 36 | 37 | /** 38 | * {@inheritdoc} 39 | */ 40 | public function head($url, array $headers = array()) 41 | { 42 | return $this->execute($url, $headers, function ($curl) { 43 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD'); 44 | curl_setopt($curl, CURLOPT_NOBODY, true); 45 | }); 46 | } 47 | 48 | /** 49 | * {@inheritdoc} 50 | */ 51 | public function put($url, array $headers = array(), array $content = array(), array $files = array()) 52 | { 53 | return $this->execute($url, $headers, $this->getPostRequestClosure(false, $content, $files)); 54 | } 55 | 56 | /** 57 | * {@inheritdoc} 58 | */ 59 | public function getName() 60 | { 61 | return 'curl'; 62 | } 63 | 64 | /** 65 | * Gets the POST/PUT request closure. 66 | * 67 | * @param boolean $isPostMethod TRUE in case of a POST method, else FALSE. 68 | * @param array $content The POST/PUT content (optional). 69 | * @param array $files The POST/PUT files (optional). 70 | * 71 | * @return \Closure The POST/PUT request closure. 72 | */ 73 | private function getPostRequestClosure($isPostMethod, array $content = array(), array $files = array()) 74 | { 75 | $fixedContent = $this->fixContent($content); 76 | 77 | return function ($curl) use ($content, $files, $fixedContent, $isPostMethod) { 78 | if (!empty($files)) { 79 | if (version_compare(PHP_VERSION, '5.5.0') >= 0) { 80 | curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true); 81 | $post = array_merge($content, array_map(function($file) { return new \CURLFile($file); }, $files)); 82 | } else { 83 | foreach ($content as &$value) { 84 | if (is_string($value) && strpos($value, '@') === 0) { 85 | $value = sprintf("\0%s", $value); 86 | } 87 | } 88 | 89 | $post = array_merge($content, array_map(function($file) { return '@'.$file; }, $files)); 90 | } 91 | } else { 92 | $post = $fixedContent; 93 | } 94 | 95 | if ($isPostMethod) { 96 | curl_setopt($curl, CURLOPT_POST, true); 97 | } else { 98 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); 99 | } 100 | 101 | curl_setopt($curl, CURLOPT_POSTFIELDS, $post); 102 | }; 103 | } 104 | 105 | /** 106 | * Fetches a response from an URL. 107 | * 108 | * @param string $url The url to fetch. 109 | * @param array $headers The http headers. 110 | * @param callable $callable A callable function executed before fetching the url. 111 | * 112 | * @return \Widop\HttpAdapter\HttpResponse The response. 113 | */ 114 | private function execute($url, array $headers = array(), $callable = null) 115 | { 116 | $curl = curl_init(); 117 | 118 | curl_setopt($curl, CURLOPT_URL, $this->fixUrl($url)); 119 | curl_setopt($curl, CURLOPT_HEADER, true); 120 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 121 | 122 | if ($this->getMaxRedirects() > 0) { 123 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 124 | curl_setopt($curl, CURLOPT_MAXREDIRS, $this->getMaxRedirects()); 125 | } 126 | 127 | if (!empty($headers)) { 128 | curl_setopt($curl, CURLOPT_HTTPHEADER, $this->fixHeaders($headers)); 129 | } 130 | 131 | if ($callable !== null) { 132 | call_user_func($callable, $curl); 133 | } 134 | 135 | if (($response = curl_exec($curl)) === false) { 136 | $error = curl_error($curl); 137 | curl_close($curl); 138 | 139 | throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $error); 140 | } 141 | 142 | $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 143 | $headersSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); 144 | $effectiveUrl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL); 145 | 146 | curl_close($curl); 147 | 148 | $headers = substr($response, 0, $headersSize); 149 | $body = substr($response, $headersSize); 150 | 151 | return $this->createResponse($statusCode, $url, $headers, $body, $effectiveUrl); 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /src/Widop/HttpAdapter/GuzzleHttpAdapter.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\HttpAdapter; 13 | 14 | use Guzzle\Http\Client; 15 | use Guzzle\Http\ClientInterface; 16 | use Guzzle\Http\Message\RequestInterface; 17 | 18 | /** 19 | * Guzzle Http adapter. 20 | * 21 | * @author Gunnar Lium 22 | */ 23 | class GuzzleHttpAdapter extends AbstractHttpAdapter 24 | { 25 | /** @var \Guzzle\Http\ClientInterface */ 26 | private $client; 27 | 28 | /** 29 | * Creates a guzzle adapter. 30 | * 31 | * @param \Guzzle\Http\ClientInterface $client The guzzle client. 32 | * @param integer $maxRedirects The maximum redirects. 33 | */ 34 | public function __construct(ClientInterface $client = null, $maxRedirects = 5) 35 | { 36 | parent::__construct($maxRedirects); 37 | 38 | if ($client === null) { 39 | $client = new Client(); 40 | } 41 | 42 | $this->client = $client; 43 | } 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public function getContent($url, array $headers = array()) 49 | { 50 | return $this->sendRequest($this->client->get($url, $headers)); 51 | } 52 | 53 | /** 54 | * {@inheritdoc} 55 | */ 56 | public function postContent($url, array $headers = array(), array $content = array(), array $files = array()) 57 | { 58 | $request = $this->client->post($url, $headers, $content); 59 | 60 | foreach ($files as $key => $file) { 61 | $request->addPostFile($key, $file); 62 | } 63 | 64 | return $this->sendRequest($request); 65 | } 66 | 67 | /** 68 | * {@inheritdoc} 69 | */ 70 | public function head($url, array $headers = array()) 71 | { 72 | return $this->sendRequest($this->client->head($url, $headers)); 73 | } 74 | 75 | /** 76 | * {@inheritdoc} 77 | */ 78 | public function put($url, array $headers = array(), array $content = array(), array $files = array()) 79 | { 80 | $request = $this->client->put($url, $headers, $content); 81 | 82 | foreach ($files as $key => $file) { 83 | $request->addPostFile($key, $file); 84 | } 85 | 86 | return $this->sendRequest($request); 87 | } 88 | 89 | /** 90 | * {@inheritdoc} 91 | */ 92 | public function getName() 93 | { 94 | return 'guzzle'; 95 | } 96 | 97 | /** 98 | * Sends a request. 99 | * 100 | * @param \Guzzle\Http\Message\RequestInterface $request The request. 101 | * 102 | * @throws \Widop\HttpAdapter\HttpAdapterException If an error occured. 103 | * 104 | * @return \Widop\HttpAdapter\HttpResponse The response. 105 | */ 106 | private function sendRequest(RequestInterface $request) 107 | { 108 | $request->getParams()->set('redirect.max', $this->getMaxRedirects()); 109 | 110 | try { 111 | $response = $request->send(); 112 | } catch (\Exception $e) { 113 | throw HttpAdapterException::cannotFetchUrl($request->getUrl(), $this->getName(), $e->getMessage()); 114 | } 115 | 116 | return $this->createResponse( 117 | $response->getStatusCode(), 118 | $request->getUrl(), 119 | $response->getHeaders()->toArray(), 120 | $response->getBody(true), 121 | $response->getEffectiveUrl() 122 | ); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/Widop/HttpAdapter/HttpAdapterException.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\HttpAdapter; 13 | 14 | /** 15 | * Http adapter exception. 16 | * 17 | * @author Geoffrey Brier 18 | */ 19 | class HttpAdapterException extends \Exception 20 | { 21 | /** 22 | * Gets the "CANNOT FETCH URL" exception. 23 | * 24 | * @param string $url The URL. 25 | * @param string $adapter The adapter name. 26 | * @param string $info Additional informations about the error. 27 | * 28 | * @return \Widop\HttpAdapterBundle\Exception\HttpAdapterException An exception. 29 | */ 30 | public static function cannotFetchUrl($url, $adapter, $info) 31 | { 32 | return new self(sprintf( 33 | 'An error occured when fetching the URL "%s" with the adapter "%s" ("%s").', 34 | $url, 35 | $adapter, 36 | $info 37 | )); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Widop/HttpAdapter/HttpAdapterInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\HttpAdapter; 13 | 14 | /** 15 | * Http adapter interface. 16 | * 17 | * @author GeLo 18 | */ 19 | interface HttpAdapterInterface 20 | { 21 | /** 22 | * Gets the content fetched from the given URL. 23 | * 24 | * @param string $url The url to request. 25 | * @param array $headers The http headers (optional). 26 | * 27 | * @throws \Widop\HttpAdapter\HttpAdapterException If an error occured. 28 | * 29 | * @return \Widop\HttpAdapter\HttpResponse The http response. 30 | */ 31 | function getContent($url, array $headers = array()); 32 | 33 | /** 34 | * Gets the content fetched from the given url & POST datas. 35 | * 36 | * @param string $url The url to request. 37 | * @param array $headers The http headers (optional). 38 | * @param array $content The post content (optional). 39 | * @param array $files The post files (optional). 40 | * 41 | * @throws \Widop\HttpAdapter\HttpAdapterException If an error occured. 42 | * 43 | * @return \Widop\HttpAdapter\HttpResponse The http response. 44 | */ 45 | function postContent($url, array $headers = array(), array $content = array(), array $files = array()); 46 | 47 | /** 48 | * Performs a HEAD request. 49 | * 50 | * @param string $url The url to request. 51 | * @param array $headers The http headers (optional). 52 | * 53 | * @throws \Widop\HttpAdapter\HttpAdapterException If an error occured. 54 | * 55 | * @return \Widop\HttpAdapter\HttpResponse The http response. 56 | */ 57 | function head($url, array $headers = array()); 58 | 59 | /** 60 | * Performs a PUT request. 61 | * 62 | * @param string $url The url to request. 63 | * @param array $headers The http headers (optional). 64 | * @param array $content The post content (optional). 65 | * @param array $files The post files (optional). 66 | * 67 | * @throws \Widop\HttpAdapter\HttpAdapterException If an error occured. 68 | * 69 | * @return \Widop\HttpAdapter\HttpResponse The http response. 70 | */ 71 | function put($url, array $headers = array(), array $content = array(), array $files = array()); 72 | 73 | /** 74 | * Gets the name of the http adapter. 75 | * 76 | * @return string The http adapter name. 77 | */ 78 | function getName(); 79 | } 80 | -------------------------------------------------------------------------------- /src/Widop/HttpAdapter/HttpResponse.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\HttpAdapter; 13 | 14 | /** 15 | * Http response. 16 | * 17 | * @author GeLo 18 | * @author Dudu 19 | */ 20 | class HttpResponse 21 | { 22 | /** @var null|integer */ 23 | private $statusCode; 24 | 25 | /** @var string */ 26 | private $url; 27 | 28 | /** @var array */ 29 | private $headers; 30 | 31 | /** @var string */ 32 | private $body; 33 | 34 | /** @var null|string */ 35 | private $effectiveUrl; 36 | 37 | /** 38 | * Creates an http response. 39 | * 40 | * @param null|integer $statusCode The response status code. 41 | * @param string $url The response url. 42 | * @param array $headers The response headers. 43 | * @param string $body The response body. 44 | * @param null|string $effectiveUrl The response effective url. 45 | */ 46 | public function __construct($statusCode, $url, array $headers, $body, $effectiveUrl = null) 47 | { 48 | $this->statusCode = $statusCode; 49 | $this->url = $url; 50 | $this->body = $body; 51 | $this->effectiveUrl = $effectiveUrl; 52 | 53 | $this->headers = array(); 54 | foreach ($headers as $key => $value) { 55 | $this->headers[strtolower($key)] = $value; 56 | } 57 | } 58 | 59 | /** 60 | * Gets the status code. 61 | * 62 | * @return null|integer The status code. 63 | */ 64 | public function getStatusCode() 65 | { 66 | return $this->statusCode; 67 | } 68 | 69 | /** 70 | * Gets the url. 71 | * 72 | * @return string The url. 73 | */ 74 | public function getUrl() 75 | { 76 | return $this->url; 77 | } 78 | 79 | /** 80 | * Gets the headers. 81 | * 82 | * @return array The headers. 83 | */ 84 | public function getHeaders() 85 | { 86 | return $this->headers; 87 | } 88 | 89 | /** 90 | * Gets a header. 91 | * 92 | * @param string $name The header name. 93 | * 94 | * @return mixed The header. 95 | */ 96 | public function getHeader($name) 97 | { 98 | $name = strtolower($name); 99 | 100 | return isset($this->headers[$name]) ? $this->headers[$name] : null; 101 | } 102 | 103 | /** 104 | * Gets the body. 105 | * 106 | * @return string The body. 107 | */ 108 | public function getBody() 109 | { 110 | return $this->body; 111 | } 112 | 113 | /** 114 | * Gets the effective url. 115 | * 116 | * @return null|string The effective url. 117 | */ 118 | public function getEffectiveUrl() 119 | { 120 | return $this->effectiveUrl; 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/Widop/HttpAdapter/StreamHttpAdapter.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\HttpAdapter; 13 | 14 | use Widop\HttpAdapter\HttpAdapterException; 15 | 16 | /** 17 | * Stream Http adapter. 18 | * 19 | * @author Geoffrey Brier 20 | * @author GeLo 21 | */ 22 | class StreamHttpAdapter extends AbstractHttpAdapter 23 | { 24 | /** 25 | * {@inheritdoc} 26 | */ 27 | public function getContent($url, array $headers = array()) 28 | { 29 | return $this->execute($url, $this->createStreamContext('GET', $headers)); 30 | } 31 | 32 | /** 33 | * {@inheritdoc} 34 | */ 35 | public function postContent($url, array $headers = array(), array $content = array(), array $files = array()) 36 | { 37 | return $this->execute($url, $this->createStreamContext('POST', $headers, $content, $files)); 38 | } 39 | 40 | /** 41 | * {@inheritdoc} 42 | */ 43 | public function head($url, array $headers = array()) 44 | { 45 | return $this->execute($url, $this->createStreamContext('HEAD', $headers)); 46 | } 47 | 48 | /** 49 | * {@inheritdoc} 50 | */ 51 | public function put($url, array $headers = array(), array $content = array(), array $files = array()) 52 | { 53 | return $this->execute($url, $this->createStreamContext('PUT', $headers, $content, $files)); 54 | } 55 | 56 | /** 57 | * {@inheritdoc} 58 | */ 59 | public function getName() 60 | { 61 | return 'stream'; 62 | } 63 | 64 | /** 65 | * Calls an URL given a context. 66 | * 67 | * @param string $url An url. 68 | * @param resource $context A resource (created from stream_context_create). 69 | * 70 | * @throws \Widop\HttpAdapterBundle\Exception\HttpAdapterException If an error occured. 71 | * 72 | * @return @return \Widop\HttpAdapter\HttpResponse The response. 73 | */ 74 | private function execute($url, $context) 75 | { 76 | if (($stream = @fopen($this->fixUrl($url), 'rb', false, $context)) === false) { 77 | throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), print_r(error_get_last(), true)); 78 | } 79 | 80 | $metadata = stream_get_meta_data($stream); 81 | 82 | if (preg_match_all('#Location:([^,]+)#', implode(',', $metadata['wrapper_data']), $matches)) { 83 | $effectiveUrl = trim($matches[1][count($matches[1]) - 1]); 84 | } else { 85 | $effectiveUrl = $url; 86 | } 87 | 88 | $content = stream_get_contents($stream); 89 | fclose($stream); 90 | 91 | return $this->createResponse( 92 | isset($metadata['wrapper_data'][0]) ? $this->parseStatusCode($metadata['wrapper_data'][0]) : null, 93 | $url, 94 | $metadata['wrapper_data'], 95 | $content, 96 | $effectiveUrl 97 | ); 98 | } 99 | 100 | /** 101 | * Creates the stream context. 102 | * 103 | * @param string $method The HTTP method. 104 | * @param array $headers The headers. 105 | * @param array $content The content. 106 | * @param array $files The files. 107 | * 108 | * @throws \Widop\HttpAdapter\HttpAdapterException If there are files (not supported). 109 | * 110 | * @return resource A stream context resource. 111 | */ 112 | private function createStreamContext($method, array $headers, array $content = array(), array $files = array()) 113 | { 114 | if (!empty($files)) { 115 | throw new HttpAdapterException(sprintf('The "%s" does not support files.', __CLASS__)); 116 | } 117 | 118 | $contextOptions = array('http' => array('method' => $method)); 119 | 120 | if (!empty($headers)) { 121 | $contextOptions['http']['header'] = ''; 122 | 123 | foreach ($this->fixHeaders($headers) as $header) { 124 | $contextOptions['http']['header'] .= sprintf("%s\r\n", $header); 125 | } 126 | } 127 | 128 | if ($this->getMaxRedirects() > 0) { 129 | $contextOptions['http']['follow_location'] = 1; 130 | $contextOptions['http']['max_redirects'] = $this->getMaxRedirects(); 131 | } 132 | 133 | if ($method === 'POST' || $method === 'PUT') { 134 | $contextOptions['http']['content'] = $this->fixContent($content); 135 | } 136 | 137 | return stream_context_create($contextOptions); 138 | } 139 | 140 | /** 141 | * Parses the status code from the status line. 142 | * 143 | * @param string $status The status line. 144 | * 145 | * @return null|integer The status code. 146 | */ 147 | private function parseStatusCode($status) 148 | { 149 | $parts = explode(' ', $status, 2); 150 | 151 | if (isset($parts[1])) { 152 | return (integer) $parts[1]; 153 | } 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/Widop/HttpAdapter/ZendHttpAdapter.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\HttpAdapter; 13 | 14 | use Widop\HttpAdapter\HttpAdapterException; 15 | use Zend\Http\Client; 16 | use Zend\Http\Request; 17 | 18 | /** 19 | * Zend http adapter. 20 | * 21 | * @author GeLo 22 | */ 23 | class ZendHttpAdapter extends AbstractHttpAdapter 24 | { 25 | /** @var \Zend\Http\Client */ 26 | private $client; 27 | 28 | /** 29 | * Creates a Zend http adapter. 30 | * 31 | * @param \Zend\Http\Client $client The Zend client. 32 | * @param integer $maxRedirects The max redirects. 33 | */ 34 | public function __construct(Client $client = null, $maxRedirects = 5) 35 | { 36 | parent::__construct($maxRedirects); 37 | 38 | if ($client === null) { 39 | $client = new Client(); 40 | } 41 | 42 | $this->client = $client; 43 | } 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public function getContent($url, array $headers = array()) 49 | { 50 | return $this->sendRequest($url, Request::METHOD_GET, $headers); 51 | } 52 | 53 | /** 54 | * {@inheritdoc} 55 | */ 56 | public function postContent($url, array $headers = array(), array $content = array(), array $files = array()) 57 | { 58 | return $this->sendRequest($url, Request::METHOD_POST, $headers, $content, $files); 59 | } 60 | 61 | /** 62 | * {@inheritdoc} 63 | */ 64 | public function head($url, array $headers = array()) 65 | { 66 | return $this->sendRequest($url, Request::METHOD_HEAD, $headers); 67 | } 68 | 69 | /** 70 | * {@inheritdoc} 71 | */ 72 | public function put($url, array $headers = array(), array $content = array(), array $files = array()) 73 | { 74 | return $this->sendRequest($url, Request::METHOD_PUT, $headers, $content, $files); 75 | } 76 | 77 | /** 78 | * {@inheritdoc} 79 | */ 80 | public function getName() 81 | { 82 | return 'zend'; 83 | } 84 | 85 | /** 86 | * Sends a request. 87 | * 88 | * @param string $url The url. 89 | * @param string $method The http method. 90 | * @param array $headers The headers. 91 | * @param array $content The content. 92 | * @param array $files The files. 93 | * 94 | * @throws \Widop\HttpAdapter\HttpAdapterException If an error occured. 95 | * 96 | * @return \Widop\HttpAdapter\HttpResponse The response. 97 | */ 98 | private function sendRequest( 99 | $url, 100 | $method, 101 | array $headers = array(), 102 | array $content = array(), 103 | array $files = array() 104 | ) { 105 | $this->client 106 | ->resetParameters() 107 | ->setOptions(array('maxredirects' => $this->getMaxRedirects())) 108 | ->setMethod($method) 109 | ->setUri($url) 110 | ->setHeaders($headers) 111 | ->setParameterPost($content); 112 | 113 | foreach ($files as $key => $file) { 114 | $this->client->setFileUpload($file, $key); 115 | } 116 | 117 | try { 118 | $response = $this->client->send(); 119 | } catch (\Exception $e) { 120 | throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage()); 121 | } 122 | 123 | return $this->createResponse( 124 | $response->getStatusCode(), 125 | $url, 126 | $response->getHeaders()->toArray(), 127 | $method === Request::METHOD_HEAD ? '' : $response->getBody() 128 | ); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /tests/Widop/Tests/HttpAdapter/AbstractHttpAdapterTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\Tests\HttpAdapter; 13 | 14 | /** 15 | * Abstract http adapter test. 16 | * 17 | * @author GeLo 18 | */ 19 | abstract class AbstractHttpAdapterTest extends \PHPUnit_Framework_TestCase 20 | { 21 | /** @var \Widop\HttpAdapter\HttpAdapterInterface */ 22 | protected $httpAdapter; 23 | 24 | /** @var string */ 25 | protected $url; 26 | 27 | /** @var array */ 28 | protected $headers; 29 | 30 | /** @var array */ 31 | protected $content; 32 | 33 | /** @var array */ 34 | protected $files; 35 | 36 | /** 37 | * {@inheritdoc} 38 | */ 39 | protected function setUp() 40 | { 41 | $this->statusCode = 200; 42 | $this->url = 'http://www.widop.com'; 43 | $this->headers = array('Accept-Charset' => 'utf-8', 'Accept-Language: en-US,en;q=0.8'); 44 | $this->content = array('param' => 'value'); 45 | $this->files = array('file' => realpath(__DIR__.'/Fixtures/file.txt')); 46 | } 47 | 48 | /** 49 | * {@inheritdoc} 50 | */ 51 | protected function tearDown() 52 | { 53 | unset($this->files); 54 | unset($this->content); 55 | unset($this->headers); 56 | unset($this->url); 57 | unset($this->httpAdapter); 58 | } 59 | 60 | /** 61 | * Asserts an http response. 62 | * 63 | * @param mixed $response The http response. 64 | */ 65 | protected function assertResponse($response) 66 | { 67 | $this->assertInstanceOf('Widop\HttpAdapter\HttpResponse', $response); 68 | $this->assertSame($this->statusCode, $response->getStatusCode()); 69 | $this->assertSame($this->url, $response->getUrl()); 70 | $this->assertNotEmpty($response->getHeaders()); 71 | $this->assertNotEmpty($response->getBody()); 72 | } 73 | 74 | abstract public function testName(); 75 | 76 | public function testGetContentWithoutHeaders() 77 | { 78 | $this->assertResponse($this->httpAdapter->getContent($this->url)); 79 | } 80 | 81 | public function testGetContentWithHeaders() 82 | { 83 | $this->assertResponse($this->httpAdapter->getContent($this->url,$this->headers)); 84 | } 85 | 86 | /** 87 | * @expectedException \Widop\HttpAdapter\HttpAdapterException 88 | */ 89 | public function testGetContentWithInvalidUrl() 90 | { 91 | $this->httpAdapter->getContent('foo'); 92 | } 93 | 94 | public function testPostContentWithoutHeaders() 95 | { 96 | $this->assertResponse($this->httpAdapter->postContent($this->url)); 97 | } 98 | 99 | public function testPostContentWithHeaders() 100 | { 101 | $this->assertResponse($this->httpAdapter->postContent($this->url, $this->headers)); 102 | } 103 | 104 | public function testPostContentWithHeadersAndContent() 105 | { 106 | $this->assertResponse($this->httpAdapter->postContent($this->url, $this->headers, $this->content)); 107 | } 108 | 109 | public function testPostContentWithHeadersAndContentAndFiles() 110 | { 111 | $this->assertResponse($this->httpAdapter->postContent( 112 | $this->url, 113 | $this->headers, 114 | $this->content, 115 | $this->files 116 | )); 117 | } 118 | 119 | /** 120 | * @expectedException \Widop\HttpAdapter\HttpAdapterException 121 | */ 122 | public function testPostContentWithInvalidUrl() 123 | { 124 | $this->httpAdapter->postContent('foo'); 125 | } 126 | 127 | public function testHead() 128 | { 129 | $response = $this->httpAdapter->head($this->url, $this->headers); 130 | 131 | $this->assertInstanceOf('Widop\HttpAdapter\HttpResponse', $response); 132 | $this->assertSame($this->statusCode, $response->getStatusCode()); 133 | $this->assertSame($this->url, $response->getUrl()); 134 | $this->assertNotEmpty($response->getHeaders()); 135 | } 136 | 137 | public function testPutContentWithHeadersAndContent() 138 | { 139 | $this->assertResponse($this->httpAdapter->put( 140 | $this->url, 141 | $this->headers, 142 | $this->content 143 | )); 144 | } 145 | 146 | public function testPutContentWithHeadersAndContentAndFiles() 147 | { 148 | $this->assertResponse($this->httpAdapter->put( 149 | $this->url, 150 | $this->headers, 151 | $this->content, 152 | $this->files 153 | )); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /tests/Widop/Tests/HttpAdapter/BuzzHttpAdapterTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\Tests\HttpAdapter; 13 | 14 | use Widop\HttpAdapter\BuzzHttpAdapter; 15 | 16 | /** 17 | * Buzz http adapter test. 18 | * 19 | * @author GeLo 20 | */ 21 | class BuzzHttpAdapterTest extends AbstractHttpAdapterTest 22 | { 23 | /** 24 | * {@inheritdoc} 25 | */ 26 | protected function setUp() 27 | { 28 | parent::setUp(); 29 | 30 | $this->httpAdapter = new BuzzHttpAdapter(); 31 | } 32 | 33 | public function testPutContentWithHeadersAndContent() 34 | { 35 | $this->headers['Content-Length'] = 0; 36 | 37 | parent::testPutContentWithHeadersAndContent(); 38 | } 39 | 40 | public function testPutContentWithHeadersAndContentAndFiles() 41 | { 42 | $this->headers['Content-Length'] = 0; 43 | 44 | parent::testPutContentWithHeadersAndContentAndFiles(); 45 | } 46 | 47 | public function testName() 48 | { 49 | $this->assertSame('buzz', $this->httpAdapter->getName()); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/Widop/Tests/HttpAdapter/CurlHttpAdapterTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\Tests\HttpAdapter; 13 | 14 | use Widop\HttpAdapter\CurlHttpAdapter; 15 | 16 | /** 17 | * Curl http adapter test. 18 | * 19 | * @author GeLo 20 | */ 21 | class CurlHttpAdapterTest extends AbstractHttpAdapterTest 22 | { 23 | /** 24 | * {@inheritdoc} 25 | */ 26 | protected function setUp() 27 | { 28 | parent::setUp(); 29 | 30 | $this->httpAdapter = new CurlHttpAdapter(); 31 | } 32 | 33 | public function testName() 34 | { 35 | $this->assertSame('curl', $this->httpAdapter->getName()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/Widop/Tests/HttpAdapter/Fixtures/file.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/widop/http-adapter/9f21040064d290cc70db4affe3191828f419f5af/tests/Widop/Tests/HttpAdapter/Fixtures/file.txt -------------------------------------------------------------------------------- /tests/Widop/Tests/HttpAdapter/GuzzleHttpAdapterTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\Tests\HttpAdapter; 13 | 14 | use Widop\HttpAdapter\GuzzleHttpAdapter; 15 | 16 | /** 17 | * Guzzle http adapter test. 18 | * 19 | * @author Gunnar Lium 20 | */ 21 | class GuzzleHttpAdapterTest extends AbstractHttpAdapterTest 22 | { 23 | /** 24 | * {@inheritdoc} 25 | */ 26 | protected function setUp() 27 | { 28 | parent::setUp(); 29 | 30 | $this->httpAdapter = new GuzzleHttpAdapter(); 31 | } 32 | 33 | public function testName() 34 | { 35 | $this->assertSame('guzzle', $this->httpAdapter->getName()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/Widop/Tests/HttpAdapter/HttpResponseTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\Tests\HttpAdapter; 13 | 14 | use Widop\HttpAdapter\HttpResponse; 15 | 16 | /** 17 | * Http response test. 18 | * 19 | * @author GeLo 20 | */ 21 | class HttpResponseTest extends \PHPUnit_Framework_TestCase 22 | { 23 | /** @var \Widop\HttpAdapter\HttpResponse */ 24 | protected $httpResponse; 25 | 26 | /** @var integer */ 27 | protected $statusCode; 28 | 29 | /** @var string */ 30 | protected $url; 31 | 32 | /** @var array */ 33 | protected $headers; 34 | 35 | /** @var string */ 36 | protected $body; 37 | 38 | /** @var string */ 39 | protected $effectiveUrl; 40 | 41 | /** 42 | * {@inheritdoc} 43 | */ 44 | protected function setUp() 45 | { 46 | $this->statusCode = 200; 47 | $this->url = 'url'; 48 | $this->headers = array('foo' => 'bar'); 49 | $this->body = 'body'; 50 | $this->effectiveUrl = 'effective_url'; 51 | 52 | $this->httpResponse = new HttpResponse($this->statusCode, $this->url, $this->headers, $this->body, $this->effectiveUrl); 53 | } 54 | 55 | /** 56 | * {@inheritdoc} 57 | */ 58 | protected function tearDown() 59 | { 60 | unset($this->statusCode); 61 | unset($this->effectiveUrl); 62 | unset($this->body); 63 | unset($this->headers); 64 | unset($this->url); 65 | unset($this->httpResponse); 66 | } 67 | 68 | public function testStatusCode() 69 | { 70 | $this->assertSame($this->statusCode, $this->httpResponse->getStatusCode()); 71 | } 72 | 73 | public function testUrl() 74 | { 75 | $this->assertSame($this->url, $this->httpResponse->getUrl()); 76 | } 77 | 78 | public function testHeaders() 79 | { 80 | $this->assertSame($this->headers, $this->httpResponse->getHeaders()); 81 | } 82 | 83 | public function testHeaderWithExistingHeader() 84 | { 85 | $this->assertSame($this->headers['foo'], $this->httpResponse->getHeader('foo')); 86 | } 87 | 88 | public function testHeaderWithCaseSensititvity() 89 | { 90 | $this->assertSame($this->headers['foo'], $this->httpResponse->getHeader('FoO')); 91 | } 92 | 93 | public function testHeaderWithNoExistingHeader() 94 | { 95 | $this->assertNull($this->httpResponse->getHeader('bar')); 96 | } 97 | 98 | public function testBody() 99 | { 100 | $this->assertSame($this->body, $this->httpResponse->getBody()); 101 | } 102 | 103 | public function testEffectiveUrl() 104 | { 105 | $this->assertSame($this->effectiveUrl, $this->httpResponse->getEffectiveUrl()); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /tests/Widop/Tests/HttpAdapter/StreamHttpAdapterTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\Tests\HttpAdapter; 13 | 14 | use Widop\HttpAdapter\StreamHttpAdapter; 15 | 16 | /** 17 | * Stream http adapter test. 18 | * 19 | * @author Geoffrey Brier 20 | */ 21 | class StreamHttpAdapterTest extends AbstractHttpAdapterTest 22 | { 23 | /** 24 | * {@inheritdoc} 25 | */ 26 | protected function setUp() 27 | { 28 | parent::setUp(); 29 | 30 | $this->httpAdapter = new StreamHttpAdapter(); 31 | } 32 | 33 | public function testName() 34 | { 35 | $this->assertSame('stream', $this->httpAdapter->getName()); 36 | } 37 | 38 | /** 39 | * @expectedException \Widop\HttpAdapter\HttpAdapterException 40 | * @expectedExceptionMessage The "Widop\HttpAdapter\StreamHttpAdapter" does not support files. 41 | */ 42 | public function testPostContentWithHeadersAndContentAndFiles() 43 | { 44 | parent::testPostContentWithHeadersAndContentAndFiles(); 45 | } 46 | 47 | public function testPutContentWithHeadersAndContent() 48 | { 49 | $this->headers['Content-Length'] = 0; 50 | 51 | parent::testPutContentWithHeadersAndContent(); 52 | } 53 | 54 | /** 55 | * @expectedException \Widop\HttpAdapter\HttpAdapterException 56 | * @expectedExceptionMessage The "Widop\HttpAdapter\StreamHttpAdapter" does not support files. 57 | */ 58 | public function testPutContentWithHeadersAndContentAndFiles() 59 | { 60 | parent::testPutContentWithHeadersAndContentAndFiles(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /tests/Widop/Tests/HttpAdapter/ZendHttpAdapterTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Widop\Tests\HttpAdapter; 13 | 14 | use Widop\HttpAdapter\ZendHttpAdapter; 15 | 16 | /** 17 | * Zend http adapter test. 18 | * 19 | * @author GeLo 20 | */ 21 | class ZendHttpAdapterTest extends AbstractHttpAdapterTest 22 | { 23 | /** 24 | * {@inheritdoc} 25 | */ 26 | protected function setUp() 27 | { 28 | parent::setUp(); 29 | 30 | $this->httpAdapter = new ZendHttpAdapter(); 31 | } 32 | 33 | public function testName() 34 | { 35 | $this->assertSame('zend', $this->httpAdapter->getName()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please read the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | // Autoloads bundle. 13 | require __DIR__.'/../vendor/autoload.php'; 14 | --------------------------------------------------------------------------------