- PHPUnit HttpUtil
[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
15 require_once 'PHPUnit2/Framework/TestSuite.php';
16 require_once 'PHPUnit2/Framework/TestCase.php';
17 require_once 'PHPUnit2/TextUI/ResultPrinter.php';
18
19 class HttpUtilTest extends PHPUnit2_Framework_TestCase
20 {
21 function test_date()
22 {
23 $this->assertEquals('Thu, 01 Jan 1970 00:00:01 GMT', HttpUtil::date(1));
24 }
25
26 function test_buildUri()
27 {
28 $_SERVER['SERVER_NAME'] = 'www.example.com';
29 $this->assertEquals('http://www.example.com/test.php?foo=bar', HttpUtil::buildUri('/test.php?foo=bar', null, null, 80));
30 $this->assertEquals('https://www.example.com/', HttpUtil::buildUri('/', 'https'));
31 $this->assertEquals('ftp://ftp.example.com/pub', HttpUtil::buildUri('/pub', null, 'ftp.example.com', 21));
32 }
33
34 function test_negotiateLanguage()
35 {
36 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en, de;q=0.5, it-IT; q = 0.5 ';
37 $this->assertEquals('de', HttpUtil::negotiateLanguage(array('de','it'), $r));
38 $this->assertEquals(array('de'=>0.5,'it'=>0.45), $r);
39 }
40
41 function test_negotiateCharset()
42 {
43 $_SERVER['HTTP_ACCEPT_CHARSET'] = ' iso-8859-1, Unicode ;q=0 , utf-8 ';
44 $this->assertEquals('iso-8859-1', HttpUtil::negotiateCharset(array('utf-8','iso-8859-1'), $r));
45 $this->assertEquals(array('iso-8859-1'=>1000.0,'utf-8'=>999.0), $r);
46 }
47
48 function test_negotiateContentType()
49 {
50 $_SERVER['HTTP_ACCEPT'] = ' text/xml+xhtml, text/html;q = .9, *';
51 $this->assertEquals('text/xml+xhtml', HttpUtil::negotiateContentType(array('text/xml+xhtml', 'text/html'), $r));
52 $this->assertEquals(array('text/xml+xhtml'=>1000.0,'text/html'=>0.9), $r);
53 }
54
55 function test_matchModified()
56 {
57 $_SERVER['HTTP_IF_MODIFIED_SINCE'] = 'Fri, 02 Jan 1970 00:00:01 GMT';
58 $this->assertTrue(HttpUtil::matchModified(1));
59 $this->assertFalse(HttpUtil::matchModified(2*24*60*60+1));
60 unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
61
62 $_SERVER['HTTP_IF_UNMODIFIED_SINCE'] = 'Fri, 02 Jan 1970 00:00:01 GMT';
63 $this->assertTrue(HttpUtil::matchModified(1, true));
64 $this->assertFalse(HttpUtil::matchModified(2*24*60*60+1, true));
65 unset($_SERVER['HTTP_IF_UNMODIFIED_SINCE']);
66 }
67
68 function test_matchEtag()
69 {
70 $_SERVER['HTTP_IF_NONE_MATCH'] = '"abc"';
71 $this->assertTrue(HttpUtil::matchEtag('abc'));
72 $this->assertFalse(HttpUtil::matchEtag('ABC'));
73 unset($_SERVER['HTTP_IF_NONE_MATCH']);
74
75 $_SERVER['HTTP_IF_MATCH'] = '"abc"';
76 $this->assertTrue(HttpUtil::matchEtag('abc', true));
77 $this->assertFalse(HttpUtil::matchEtag('ABC', true));
78 unset($_SERVER['HTTP_IF_MATCH']);
79
80 $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
81 $this->assertTrue(HttpUtil::matchEtag('abc'));
82 $this->assertTrue(HttpUtil::matchEtag('ABC'));
83 unset($_SERVER['HTTP_IF_NONE_MATCH']);
84
85 $_SERVER['HTTP_IF_MATCH'] = '*';
86 $this->assertTrue(HttpUtil::matchEtag('abc', true));
87 $this->assertTrue(HttpUtil::matchEtag('ABC', true));
88 unset($_SERVER['HTTP_IF_MATCH']);
89 }
90
91 function test_matchRequestHeader()
92 {
93 $_SERVER['HTTP_FOO'] = 'FoObAr';
94 $this->assertTrue(HttpUtil::matchRequestHeader('foo', 'foobar', false));
95 $this->assertTrue(HttpUtil::matchRequestHeader('foo', 'FoObAr', true));
96 $this->assertFalse(HttpUtil::matchRequestHeader('foo', 'foobar', true));
97 }
98
99 function test_zlib()
100 {
101 if ($support = http_support(HTTP_SUPPORT_ENCODINGS)) {
102 $this->assertEquals(file_get_contents(__FILE__), http_gzdecode(http_gzencode(file_get_contents(__FILE__))));
103 $this->assertEquals(file_get_contents(__FILE__), http_inflate(http_deflate(file_get_contents(__FILE__))));
104 $this->assertEquals(file_get_contents(__FILE__), http_uncompress(http_compress(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