X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=phpunit%2FHeaderTest.php;h=184f21a58065223e3617f9414e379e155d51beff;hb=eba42bcdd8a59dacc713f04e68d6293ab52861c6;hp=41d3af82c4ae6ea981b3b24a4b2bf7577133d388;hpb=61c5a47e7530b142df6b67332e60a339998c2492;p=m6w6%2Fext-http diff --git a/phpunit/HeaderTest.php b/phpunit/HeaderTest.php index 41d3af8..184f21a 100644 --- a/phpunit/HeaderTest.php +++ b/phpunit/HeaderTest.php @@ -2,16 +2,24 @@ class HeaderTest extends PHPUnit_Framework_TestCase { function setUp() { - $this->h = new http\Header("foo", "bar"); + } function testString() { - $this->assertEquals("Foo: bar", (string) $this->h); + $h = new http\Header("foo", "bar"); + $this->assertEquals("Foo: bar", (string) $h); } function testSerialize() { - $this->assertEquals("Foo: bar", (string) unserialize(serialize($this->h))); + $h = new http\Header("foo", "bar"); + $this->assertEquals("Foo: bar", (string) unserialize(serialize($h))); } + function testNumeric() { + $h = new http\Header(123, 456); + $this->assertEquals("123: 456", (string) $h); + $this->assertEquals("123: 456", (string) unserialize(serialize($h))); + } + function testMatch() { $ae = new http\Header("Accept-encoding", "gzip, deflate"); $this->assertTrue($ae->match("gzip", http\Header::MATCH_WORD)); @@ -24,4 +32,52 @@ class HeaderTest extends PHPUnit_Framework_TestCase { $this->assertFalse($ae->match("zip", http\Header::MATCH_WORD)); $this->assertFalse($ae->match("gzip", http\Header::MATCH_FULL)); } + + function testNegotiate() { + $a = new http\Header("Accept", "text/html, text/plain;q=0.5, */*;q=0"); + $this->assertEquals("text/html", $a->negotiate(array("text/plain","text/html"))); + $this->assertEquals("text/html", $a->negotiate(array("text/plain","text/html"), $rs)); + $this->assertEquals(array("text/html"=>0.99, "text/plain"=>0.5), $rs); + $this->assertEquals("text/plain", $a->negotiate(array("foo/bar", "text/plain"), $rs)); + $this->assertEquals(array("text/plain"=>0.5), $rs); + $this->assertEquals("foo/bar", $a->negotiate(array("foo/bar"), $rs)); + $this->assertEquals(array(), $rs); + } + + function testParse() { + $header = "Foo: bar\nBar: foo\n"; + $this->assertEquals(array("Foo"=>"bar","Bar"=>"foo"), http\Header::parse($header)); + $headers = http\Header::parse($header, "http\\Header"); + $this->assertCount(2, $headers); + $this->assertContainsOnlyInstancesOf("http\\Header", $headers); + } + + function testParseError() { + $header = "wass\nup"; + $this->setExpectedException("PHPUnit_Framework_Error_Warning", "Could not parse headers"); + $this->assertFalse(http\Header::parse($header)); + } + + function testParams() { + $header = new http\Header("Cache-control", "public, must-revalidate, max-age=0"); + $this->assertEquals( + array( + "public" => array("value" => true, "arguments" => array()), + "must-revalidate" => array("value" => true, "arguments" => array()), + "max-age" => array("value" => 0, "arguments" => array()), + ), + $header->getParams()->params + ); + } + + function testParamsWithArgs() { + $header = new http\Header("Custom", '"foo" is "bar". "bar" is "bis" where "bis" is "where".'); + $this->assertEquals( + array( + "foo" => array("value" => "bar", "arguments" => array()), + "bar" => array("value" => "bis", "arguments" => array("bis" => "where")) + ), + $header->getParams(".", "where", "is")->params + ); + } }