HttpClientTest.php
69 lines
| 1 | <?php |
| 2 | /** |
| 3 | * HttpClientTest.php |
| 4 | * |
| 5 | * @package Tests |
| 6 | * @author Michael Pratt <yo@michael-pratt.com> |
| 7 | * @link http://www.michael-pratt.com/ |
| 8 | * |
| 9 | * For the full copyright and license information, please view the LICENSE |
| 10 | * file that was distributed with this source code. |
| 11 | */ |
| 12 | namespace Embera\Http; |
| 13 | |
| 14 | use PHPUnit\Framework\TestCase; |
| 15 | |
| 16 | class HttpClientTest extends TestCase |
| 17 | { |
| 18 | protected function fetchData($useCurl = true) |
| 19 | { |
| 20 | $ua = 'PHP/Embera Test - ' . date('Y-m-d') . ' / Mozilla/5.0 Compatible'; |
| 21 | $http = new HttpClient([ |
| 22 | 'use_curl' => (bool) $useCurl, |
| 23 | 'user_agent' => $ua, |
| 24 | ]); |
| 25 | |
| 26 | if (!$useCurl && !ini_get('allow_url_fopen')) { |
| 27 | $this->markTestIncomplete('Could not test file_get_contents wrapper, allow_url_fopen is closed'); |
| 28 | return ; |
| 29 | } |
| 30 | |
| 31 | $response = $http->fetch('https://httpbin.org/user-agent'); |
| 32 | $response = json_decode($response, true); |
| 33 | $this->assertEquals($ua, $response['user-agent']); |
| 34 | } |
| 35 | |
| 36 | /** @large */ |
| 37 | public function testCanFetchUrlDataWithCurl() |
| 38 | { |
| 39 | $this->fetchData(); |
| 40 | } |
| 41 | |
| 42 | /** @large */ |
| 43 | public function testCanFetchUrlDataWithfopen() |
| 44 | { |
| 45 | $this->fetchData(false); |
| 46 | } |
| 47 | |
| 48 | public function testCanDetectInvalidUrl() |
| 49 | { |
| 50 | $this->expectException('\InvalidArgumentException'); |
| 51 | |
| 52 | $http = new HttpClient(); |
| 53 | $http->fetch('this is an invalid url'); |
| 54 | } |
| 55 | |
| 56 | public function testCurlCanDetectInvalidStatusCode() |
| 57 | { |
| 58 | $this->expectException('\Exception'); |
| 59 | |
| 60 | $ua = 'PHP/Embera Test - ' . date('Y-m-d'); |
| 61 | $http = new HttpClient([ |
| 62 | 'use_curl' => true, |
| 63 | 'user_agent' => $ua, |
| 64 | ]); |
| 65 | |
| 66 | $http->fetch('https://httpbin.org/status/404'); |
| 67 | } |
| 68 | } |
| 69 |