7 skipif(!@include 'PHPUnit2/Framework/TestCase.php', 'need PHPUnit2');
13 error_reporting(E_ALL);
14 ini_set('html_errors', 0);
16 require_once 'PHPUnit2/Framework/TestSuite.php';
17 require_once 'PHPUnit2/Framework/TestCase.php';
18 require_once 'PHPUnit2/TextUI/ResultPrinter.php';
20 class HttpUtilTest extends PHPUnit2_Framework_TestCase
24 $this->assertEquals('Thu, 01 Jan 1970 00:00:01 GMT', HttpUtil::date(1));
27 function test_buildUrl()
29 $_SERVER['SERVER_NAME'] = 'www.example.com';
30 $this->assertEquals('http://www.example.com/test.php?foo=bar', HttpUtil::buildUrl('/test.php?foo=bar', array('port' => 80)));
31 $this->assertEquals('https://www.example.com/', HttpUtil::buildUrl('/', array('scheme' => 'https')));
32 $this->assertEquals('ftp://ftp.example.com/pub', HttpUtil::buildUrl('/pub', array('host' => 'ftp.example.com', 'port' => 21)));
35 function test_negotiateLanguage()
37 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en, de;q=0.5, it-IT; q = 0.5 ';
38 $this->assertEquals('de', HttpUtil::negotiateLanguage(array('de','it'), $r));
39 $this->assertEquals(array('de'=>0.5,'it'=>0.45), $r);
42 function test_negotiateCharset()
44 $_SERVER['HTTP_ACCEPT_CHARSET'] = ' iso-8859-1, Unicode ;q=0 , utf-8 ';
45 $this->assertEquals('iso-8859-1', HttpUtil::negotiateCharset(array('utf-8','iso-8859-1'), $r));
46 $this->assertEquals(array('iso-8859-1'=>1000.0,'utf-8'=>999.0), $r);
49 function test_negotiateContentType()
51 $_SERVER['HTTP_ACCEPT'] = ' text/xml+xhtml, text/html;q = .9, *';
52 $this->assertEquals('text/xml+xhtml', HttpUtil::negotiateContentType(array('text/xml+xhtml', 'text/html'), $r));
53 $this->assertEquals(array('text/xml+xhtml'=>1000.0,'text/html'=>0.9), $r);
56 function test_matchModified()
58 $_SERVER['HTTP_IF_MODIFIED_SINCE'] = 'Fri, 02 Jan 1970 00:00:01 GMT';
59 $this->assertTrue(HttpUtil::matchModified(1));
60 $this->assertFalse(HttpUtil::matchModified(2*24*60*60+1));
61 unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
63 $_SERVER['HTTP_IF_UNMODIFIED_SINCE'] = 'Fri, 02 Jan 1970 00:00:01 GMT';
64 $this->assertTrue(HttpUtil::matchModified(1, true));
65 $this->assertFalse(HttpUtil::matchModified(2*24*60*60+1, true));
66 unset($_SERVER['HTTP_IF_UNMODIFIED_SINCE']);
69 function test_matchEtag()
71 $_SERVER['HTTP_IF_NONE_MATCH'] = '"abc"';
72 $this->assertTrue(HttpUtil::matchEtag('abc'));
73 $this->assertFalse(HttpUtil::matchEtag('ABC'));
74 unset($_SERVER['HTTP_IF_NONE_MATCH']);
76 $_SERVER['HTTP_IF_MATCH'] = '"abc"';
77 $this->assertTrue(HttpUtil::matchEtag('abc', true));
78 $this->assertFalse(HttpUtil::matchEtag('ABC', true));
79 unset($_SERVER['HTTP_IF_MATCH']);
81 $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
82 $this->assertTrue(HttpUtil::matchEtag('abc'));
83 $this->assertTrue(HttpUtil::matchEtag('ABC'));
84 unset($_SERVER['HTTP_IF_NONE_MATCH']);
86 $_SERVER['HTTP_IF_MATCH'] = '*';
87 $this->assertTrue(HttpUtil::matchEtag('abc', true));
88 $this->assertTrue(HttpUtil::matchEtag('ABC', true));
89 unset($_SERVER['HTTP_IF_MATCH']);
92 function test_matchRequestHeader()
94 $_SERVER['HTTP_FOO'] = 'FoObAr';
95 $this->assertTrue(HttpUtil::matchRequestHeader('foo', 'foobar', false));
96 $this->assertTrue(HttpUtil::matchRequestHeader('foo', 'FoObAr', true));
97 $this->assertFalse(HttpUtil::matchRequestHeader('foo', 'foobar', true));
102 if ($support = http_support(HTTP_SUPPORT_ENCODINGS)) {
103 $this->assertEquals(file_get_contents(__FILE__), http_inflate(http_deflate(file_get_contents(__FILE__), HTTP_DEFLATE_TYPE_GZIP)));
104 $this->assertEquals(file_get_contents(__FILE__), http_inflate(http_deflate(file_get_contents(__FILE__))));
106 $this->assertFalse($support);
111 $s = new PHPUnit2_Framework_TestSuite('HttpUtilTest');
112 $p = new PHPUnit2_TextUI_ResultPrinter();
113 $p->printResult($s->run(), 0);