184f21a58065223e3617f9414e379e155d51beff
[m6w6/ext-http] / phpunit / HeaderTest.php
1 <?php
2
3 class HeaderTest extends PHPUnit_Framework_TestCase {
4 function setUp() {
5
6 }
7 function testString() {
8 $h = new http\Header("foo", "bar");
9 $this->assertEquals("Foo: bar", (string) $h);
10 }
11
12 function testSerialize() {
13 $h = new http\Header("foo", "bar");
14 $this->assertEquals("Foo: bar", (string) unserialize(serialize($h)));
15 }
16
17 function testNumeric() {
18 $h = new http\Header(123, 456);
19 $this->assertEquals("123: 456", (string) $h);
20 $this->assertEquals("123: 456", (string) unserialize(serialize($h)));
21 }
22
23 function testMatch() {
24 $ae = new http\Header("Accept-encoding", "gzip, deflate");
25 $this->assertTrue($ae->match("gzip", http\Header::MATCH_WORD));
26 $this->assertTrue($ae->match("gzip", http\Header::MATCH_WORD|http\Header::MATCH_CASE));
27 $this->assertFalse($ae->match("gzip", http\Header::MATCH_STRICT));
28 $this->assertTrue($ae->match("deflate", http\Header::MATCH_WORD));
29 $this->assertTrue($ae->match("deflate", http\Header::MATCH_WORD|http\Header::MATCH_CASE));
30 $this->assertFalse($ae->match("deflate", http\Header::MATCH_STRICT));
31
32 $this->assertFalse($ae->match("zip", http\Header::MATCH_WORD));
33 $this->assertFalse($ae->match("gzip", http\Header::MATCH_FULL));
34 }
35
36 function testNegotiate() {
37 $a = new http\Header("Accept", "text/html, text/plain;q=0.5, */*;q=0");
38 $this->assertEquals("text/html", $a->negotiate(array("text/plain","text/html")));
39 $this->assertEquals("text/html", $a->negotiate(array("text/plain","text/html"), $rs));
40 $this->assertEquals(array("text/html"=>0.99, "text/plain"=>0.5), $rs);
41 $this->assertEquals("text/plain", $a->negotiate(array("foo/bar", "text/plain"), $rs));
42 $this->assertEquals(array("text/plain"=>0.5), $rs);
43 $this->assertEquals("foo/bar", $a->negotiate(array("foo/bar"), $rs));
44 $this->assertEquals(array(), $rs);
45 }
46
47 function testParse() {
48 $header = "Foo: bar\nBar: foo\n";
49 $this->assertEquals(array("Foo"=>"bar","Bar"=>"foo"), http\Header::parse($header));
50 $headers = http\Header::parse($header, "http\\Header");
51 $this->assertCount(2, $headers);
52 $this->assertContainsOnlyInstancesOf("http\\Header", $headers);
53 }
54
55 function testParseError() {
56 $header = "wass\nup";
57 $this->setExpectedException("PHPUnit_Framework_Error_Warning", "Could not parse headers");
58 $this->assertFalse(http\Header::parse($header));
59 }
60
61 function testParams() {
62 $header = new http\Header("Cache-control", "public, must-revalidate, max-age=0");
63 $this->assertEquals(
64 array(
65 "public" => array("value" => true, "arguments" => array()),
66 "must-revalidate" => array("value" => true, "arguments" => array()),
67 "max-age" => array("value" => 0, "arguments" => array()),
68 ),
69 $header->getParams()->params
70 );
71 }
72
73 function testParamsWithArgs() {
74 $header = new http\Header("Custom", '"foo" is "bar". "bar" is "bis" where "bis" is "where".');
75 $this->assertEquals(
76 array(
77 "foo" => array("value" => "bar", "arguments" => array()),
78 "bar" => array("value" => "bis", "arguments" => array("bis" => "where"))
79 ),
80 $header->getParams(".", "where", "is")->params
81 );
82 }
83 }