- added missing http_request() function stub
[m6w6/ext-http] / tests / ut_HttpUtil.phpt
1 --TEST--
2 PHPUnit HttpUtil
3 --SKIPIF--
4 <?php
5 include 'skip.inc';
6 checkcls('HttpUtil');
7 skipif(!@include 'PHPUnit2/Framework/TestCase.php', 'need PHPUnit2');
8 ?>
9 --FILE--
10 <?php
11 echo "-TEST\n";
12
13 error_reporting(E_ALL);
14 ini_set('html_errors', 0);
15
16 require_once 'PHPUnit2/Framework/TestSuite.php';
17 require_once 'PHPUnit2/Framework/TestCase.php';
18 require_once 'PHPUnit2/TextUI/ResultPrinter.php';
19
20 class HttpUtilTest extends PHPUnit2_Framework_TestCase
21 {
22 function test_date()
23 {
24 $this->assertEquals('Thu, 01 Jan 1970 00:00:01 GMT', HttpUtil::date(1));
25 }
26
27 function test_buildUrl()
28 {
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)));
33 }
34
35 function test_negotiateLanguage()
36 {
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);
40 }
41
42 function test_negotiateCharset()
43 {
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);
47 }
48
49 function test_negotiateContentType()
50 {
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);
54 }
55
56 function test_matchModified()
57 {
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']);
62
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']);
67 }
68
69 function test_matchEtag()
70 {
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']);
75
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']);
80
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']);
85
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']);
90 }
91
92 function test_matchRequestHeader()
93 {
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));
98 }
99
100 function test_zlib()
101 {
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__))));
105 } else {
106 $this->assertFalse($support);
107 }
108 }
109 }
110
111 $s = new PHPUnit2_Framework_TestSuite('HttpUtilTest');
112 $p = new PHPUnit2_TextUI_ResultPrinter();
113 $p->printResult($s->run(), 0);
114
115 echo "Done\n";
116 ?>
117 --EXPECTF--
118 %sTEST
119
120
121 Time: 0
122
123 OK (9 tests)
124 Done