Skip to content
This repository was archived by the owner on Aug 9, 2019. It is now read-only.

Commit 6394153

Browse files
committed
Added test suite
1 parent 2a6de91 commit 6394153

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed

phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
>
9+
<php>
10+
<ini name="error_reporting" value="-1" />
11+
</php>
12+
13+
<testsuites>
14+
<testsuite name="FlameCore Webtools Test Suite">
15+
<directory>./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<filter>
20+
<whitelist>
21+
<directory>./lib/</directory>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

tests/HttpClientTest.php

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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.1
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-Foo' => 'bar']);
85+
86+
$result = $http->get('http://httpbin.org/headers', ['X-Bar' => 'baz']);
87+
88+
$info = $this->examineResult($result);
89+
90+
// ->getHeaders()
91+
$this->assertInternalType('array', $http->getHeaders());
92+
$this->assertArrayHasKey('X-Foo', $http->getHeaders());
93+
94+
// Headers set by ->setHeaders()
95+
$this->assertObjectHasAttribute('X-Foo', $info->headers);
96+
$this->assertEquals('bar', $info->headers->{'X-Foo'});
97+
98+
// Headers set by extra
99+
$this->assertObjectHasAttribute('X-Bar', $info->headers);
100+
$this->assertEquals('baz', $info->headers->{'X-Bar'});
101+
}
102+
103+
public function testSetUserAgent()
104+
{
105+
$uastring = 'TestUA/1.0 (fake; FlameCore Webtools/1.0)';
106+
$http = new HttpClient();
107+
$http->setUserAgent($uastring);
108+
109+
$result = $http->get('http://httpbin.org/user-agent');
110+
111+
$info = $this->examineResult($result);
112+
113+
$this->assertEquals($uastring, $info->{'user-agent'});
114+
}
115+
116+
public function testSetEncoding()
117+
{
118+
$http = new HttpClient();
119+
$http->setEncoding(HttpClient::ENCODING_GZIP);
120+
121+
$result = $http->get('http://httpbin.org/gzip');
122+
123+
$info = $this->examineResult($result);
124+
125+
$this->assertObjectHasAttribute('Accept-Encoding', $info->headers);
126+
$this->assertEquals('gzip', $info->headers->{'Accept-Encoding'});
127+
}
128+
129+
public function testAcceptCookies()
130+
{
131+
$http = new HttpClient();
132+
$http->acceptCookies();
133+
$result = $http->get('http://httpbin.org/cookies/set?foo=bar');
134+
135+
$info = $this->examineResult($result);
136+
137+
$this->assertEquals(['foo' => 'bar'], (array) $info->cookies);
138+
}
139+
140+
public function testError()
141+
{
142+
$http = new HttpClient();
143+
$result = $http->get('http://httpbin.org/status/404');
144+
145+
$this->assertFalse($result->success);
146+
$this->assertEquals(404, $result->http_code);
147+
}
148+
149+
public function testFail()
150+
{
151+
$http = new HttpClient();
152+
$result = $http->get('http://nowhere.test.flamecore.org');
153+
154+
$this->assertFalse($result->success);
155+
}
156+
157+
/**
158+
* @param \stdClass $result
159+
* @return \stdClass
160+
*/
161+
private function examineResult(\stdClass $result)
162+
{
163+
$this->assertTrue($result->success);
164+
165+
return $result->success ? json_decode($result->data) : new \stdClass();
166+
}
167+
}

0 commit comments

Comments
 (0)