|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Webtools Library |
| 4 | + * Copyright (C) 2014 IceFlame.net |
| 5 | + * |
| 6 | + * Permission to use, copy, modify, and/or distribute this software for |
| 7 | + * any purpose with or without fee is hereby granted, provided that the |
| 8 | + * above copyright notice and this permission notice appear in all copies. |
| 9 | + * |
| 10 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE |
| 13 | + * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY |
| 14 | + * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER |
| 15 | + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
| 16 | + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | + * |
| 18 | + * @package FlameCore\Webtools |
| 19 | + * @version 1.2 |
| 20 | + * @link http://www.flamecore.org |
| 21 | + * @license ISC License <http://opensource.org/licenses/ISC> |
| 22 | + */ |
| 23 | + |
| 24 | +namespace FlameCore\Webtools\Tests; |
| 25 | + |
| 26 | +use FlameCore\Webtools\HttpClient; |
| 27 | + |
| 28 | +/** |
| 29 | + * Test class for HttpClient |
| 30 | + */ |
| 31 | +class HttpClientTest extends \PHPUnit_Framework_TestCase |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @var \FlameCore\Webtools\HttpClient |
| 35 | + */ |
| 36 | + protected $http; |
| 37 | + |
| 38 | + public function setUp() |
| 39 | + { |
| 40 | + if (!fsockopen('httpbin.org', 80, $errno, $errstr, 30)) { |
| 41 | + $this->markTestSkipped('HTTP test server seems to be offline.'); |
| 42 | + } |
| 43 | + |
| 44 | + $this->http = new HttpClient(); |
| 45 | + } |
| 46 | + |
| 47 | + public function testPost() |
| 48 | + { |
| 49 | + $result = $this->http->post('http://httpbin.org/post', 'foo=bar'); |
| 50 | + |
| 51 | + $info = $this->examineResult($result); |
| 52 | + |
| 53 | + $this->assertEquals(['foo' => 'bar'], (array) $info->form); |
| 54 | + } |
| 55 | + |
| 56 | + public function testGet() |
| 57 | + { |
| 58 | + $result = $this->http->get('http://httpbin.org/get?foo=bar'); |
| 59 | + |
| 60 | + $info = $this->examineResult($result); |
| 61 | + |
| 62 | + $this->assertEquals(['foo' => 'bar'], (array) $info->args); |
| 63 | + $this->assertEquals('Mozilla/5.0 (compatible; FlameCore Webtools/1.2)', $info->headers->{'User-Agent'}); |
| 64 | + } |
| 65 | + |
| 66 | + public function testSetHeader() |
| 67 | + { |
| 68 | + $http = new HttpClient(); |
| 69 | + $http->setHeader('X-Foo', 'bar'); |
| 70 | + |
| 71 | + $result = $http->get('http://httpbin.org/headers'); |
| 72 | + |
| 73 | + $info = $this->examineResult($result); |
| 74 | + |
| 75 | + $this->assertInternalType('array', $http->getHeaders()); |
| 76 | + $this->assertArrayHasKey('X-Foo', $http->getHeaders()); |
| 77 | + $this->assertObjectHasAttribute('X-Foo', $info->headers); |
| 78 | + $this->assertEquals('bar', $info->headers->{'X-Foo'}); |
| 79 | + } |
| 80 | + |
| 81 | + public function testSetHeaders() |
| 82 | + { |
| 83 | + $http = new HttpClient(); |
| 84 | + $http->setHeaders(['X-Bar' => 'baz']); |
| 85 | + |
| 86 | + $result = $http->get('http://httpbin.org/headers'); |
| 87 | + |
| 88 | + $info = $this->examineResult($result); |
| 89 | + |
| 90 | + $this->assertInternalType('array', $http->getHeaders()); |
| 91 | + $this->assertArrayHasKey('X-Bar', $http->getHeaders()); |
| 92 | + $this->assertObjectHasAttribute('X-Bar', $info->headers); |
| 93 | + $this->assertEquals('baz', $info->headers->{'X-Bar'}); |
| 94 | + } |
| 95 | + |
| 96 | + public function testSetUserAgent() |
| 97 | + { |
| 98 | + $uastring = 'TestUA/1.0 (fake; FlameCore Webtools/1.2)'; |
| 99 | + $http = new HttpClient(); |
| 100 | + $http->setUserAgent($uastring); |
| 101 | + |
| 102 | + $result = $http->get('http://httpbin.org/user-agent'); |
| 103 | + |
| 104 | + $info = $this->examineResult($result); |
| 105 | + |
| 106 | + $this->assertEquals($uastring, $info->{'user-agent'}); |
| 107 | + } |
| 108 | + |
| 109 | + public function testSetEncoding() |
| 110 | + { |
| 111 | + $http = new HttpClient(); |
| 112 | + $http->setEncoding(HttpClient::ENCODING_GZIP); |
| 113 | + |
| 114 | + $result = $http->get('http://httpbin.org/gzip'); |
| 115 | + |
| 116 | + $info = $this->examineResult($result); |
| 117 | + |
| 118 | + $this->assertObjectHasAttribute('Accept-Encoding', $info->headers); |
| 119 | + $this->assertEquals('gzip', $info->headers->{'Accept-Encoding'}); |
| 120 | + } |
| 121 | + |
| 122 | + public function testError() |
| 123 | + { |
| 124 | + $http = new HttpClient(); |
| 125 | + $result = $http->get('http://httpbin.org/status/404'); |
| 126 | + |
| 127 | + $this->assertFalse($result->success); |
| 128 | + $this->assertEquals(404, $result->http_code); |
| 129 | + } |
| 130 | + |
| 131 | + public function testFail() |
| 132 | + { |
| 133 | + $http = new HttpClient(); |
| 134 | + $result = $http->get('http://nowhere.test.flamecore.org'); |
| 135 | + |
| 136 | + $this->assertFalse($result->success); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @param \stdClass $result |
| 141 | + * @return \stdClass |
| 142 | + */ |
| 143 | + private function examineResult(\stdClass $result) |
| 144 | + { |
| 145 | + $this->assertTrue($result->success); |
| 146 | + |
| 147 | + return $result->success ? json_decode($result->data) : new \stdClass(); |
| 148 | + } |
| 149 | +} |
0 commit comments