From 2e9ac0202f2c5f1cb94e0afd4b66c1c35c316284 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Thu, 19 Jan 2012 15:45:55 +0000 Subject: [PATCH 1/1] tests --- phpunit/MessageBodyTest.php | 109 ++++++++++++++++++ phpunit/ParamsTest.php | 70 +++++++++++ phpunit/UrlTest.php | 30 +++++ tests/params001.phpt | 40 +++++++ ...eryproxy001.phpt => propertyproxy001.phpt} | 0 tests/querystring_001.phpt | 2 +- tests/response001.phpt | 23 ++++ 7 files changed, 273 insertions(+), 1 deletion(-) create mode 100644 phpunit/MessageBodyTest.php create mode 100644 phpunit/ParamsTest.php create mode 100644 phpunit/UrlTest.php create mode 100644 tests/params001.phpt rename tests/{properyproxy001.phpt => propertyproxy001.phpt} (100%) create mode 100644 tests/response001.phpt diff --git a/phpunit/MessageBodyTest.php b/phpunit/MessageBodyTest.php new file mode 100644 index 0000000..bc07401 --- /dev/null +++ b/phpunit/MessageBodyTest.php @@ -0,0 +1,109 @@ +file = new http\Message\Body(fopen(__FILE__, "r")); + $this->temp = new http\Message\Body(); + } + + function testStat() { + $this->assertEquals(filesize(__FILE__), $this->file->stat("size")); + $this->assertEquals(filemtime(__FILE__), $this->file->stat("mtime")); + $this->assertEquals(fileatime(__FILE__), $this->file->stat("atime")); + $this->assertEquals(filectime(__FILE__), $this->file->stat("ctime")); + $this->assertEquals( + array( + "size" => 0, + "mtime" => 0, + "atime" => 0, + "ctime" => 0, + ), + $this->temp->stat() + ); + } + + function testAppend() { + $this->assertEquals(0, $this->file->append("nope")); + $this->assertEquals(3, $this->temp->append("yes")); + } + + function testAdd() { + $this->assertTrue( + $this->temp->add( + array( + "foo" => "bar", + "more" => array( + "bah", "baz", "fuz" + ), + ), + array( + array( + "file" => __FILE__, + "name" => "upload", + "type" => "text/plain", + ) + ) + ) + ); + + $file = str_replace("%", "%c", file_get_contents(__FILE__)); + $this->assertStringMatchesFormat( + "--%x.%x\r\n". + "Content-Disposition: form-data; name=\"foo\"\r\n". + "\r\n". + "bar\r\n". + "--%x.%x\r\n". + "Content-Disposition: form-data; name=\"more[0]\"\r\n". + "\r\n". + "bah\r\n". + "--%x.%x\r\n". + "Content-Disposition: form-data; name=\"more[1]\"\r\n". + "\r\n". + "baz\r\n". + "--%x.%x\r\n". + "Content-Disposition: form-data; name=\"more[2]\"\r\n". + "\r\n". + "fuz\r\n". + "--%x.%x\r\n". + "Content-Disposition: attachment; name=\"upload\"; filename=\"MessageBodyTest.php\"\r\n". + "Content-Transfer-Encoding: binary\r\n". + "Content-Type: text/plain\r\n". + "\r\n". + "{$file}\r\n". + "--%x.%x--\r\n". + "", + str_replace("\r", "", $this->temp) // phpunit replaces \r\n with \n + ); + } + + function testEtag() { + $s = stat(__FILE__); + $this->assertEquals( + sprintf( + "%lx-%lx-%lx", + $s["ino"],$s["mtime"],$s["size"] + ), + $this->file->etag() + ); + $this->assertEquals(md5(""), $this->temp->etag()); + } + + function testToStream() { + $this->file->toStream($f = fopen("php://temp", "w")); + fseek($f, 0, SEEK_SET); + $this->assertEquals( + file_get_contents(__FILE__), + fread($f, filesize(__FILE__)) + ); + } + + function testToCallback() { + $s = ""; + $this->file->toCallback( + function($body, $string) use (&$s) { $s.=$string; } + ); + $this->assertEquals($s, (string) $this->file); + } +} diff --git a/phpunit/ParamsTest.php b/phpunit/ParamsTest.php new file mode 100644 index 0000000..0e139b8 --- /dev/null +++ b/phpunit/ParamsTest.php @@ -0,0 +1,70 @@ +runAssertions( + new http\Params($s), + str_replace(" ", "", $s) + ); + } + + function testCustom() { + $s = "foo bar.arg:0.bla gotit:0.now"; + $this->runAssertions( + new http\Params($s, " ", ".", ":"), + $s + ); + } + + protected function runAssertions($p, $s) { + $this->assertCount(3, $p->params); + $this->assertArrayHasKey("foo", $p->params); + $this->assertArrayHasKey("bar", $p->params); + $this->assertArrayHasKEy("gotit", $p->params); + + $this->assertTrue($p["foo"]["value"]); + $this->assertTrue($p["bar"]["value"]); + $this->assertEmpty($p["gotit"]["value"]); + + $this->assertEmpty($p["foo"]["arguments"]); + $this->assertCount(2, $p["bar"]["arguments"]); + $this->assertCount(1, $p["gotit"]["arguments"]); + + $this->assertEmpty($p["bar"]["arguments"]["arg"]); + $this->assertTrue($p["bar"]["arguments"]["bla"]); + $this->assertTrue($p["gotit"]["arguments"]["now"]); + + $this->assertEquals($s, (string) $p); + + $this->assertEquals( + array ( + 'foo' => + array ( + 'value' => true, + 'arguments' => + array ( + ), + ), + 'bar' => + array ( + 'value' => true, + 'arguments' => + array ( + 'arg' => '0', + 'bla' => true, + ), + ), + 'gotit' => + array ( + 'value' => '0', + 'arguments' => + array ( + 'now' => true, + ), + ), + ), + $p->params + ); + } +} diff --git a/phpunit/UrlTest.php b/phpunit/UrlTest.php new file mode 100644 index 0000000..ebd020f --- /dev/null +++ b/phpunit/UrlTest.php @@ -0,0 +1,30 @@ +url = "http://user:pass@www.example.com:8080/path/file.ext". + "?foo=bar&more[]=1&more[]=2#hash"; + } + + function testStandard() { + $this->assertEquals($this->url, (string) new http\Url($this->url)); + + $url = new http\Url($this->url, + array("path" => "changed", "query" => "foo=&added=this"), + http\Url::JOIN_PATH | + http\Url::JOIN_QUERY | + http\Url::STRIP_AUTH | + http\Url::STRIP_FRAGMENT + ); + + $this->assertEquals("http", $url->scheme); + $this->assertEmpty($url->user); + $this->assertEmpty($url->pass); + $this->assertEquals("www.example.com", $url->host); + $this->assertEquals(8080, $url->port); + $this->assertEquals("/path/changed", $url->path); + $this->assertEquals("foo=&more%5B0%5D=1&more%5B1%5D=2&added=this", $url->query); + $this->assertEmpty($url->fragment); + } +} diff --git a/tests/params001.phpt b/tests/params001.phpt new file mode 100644 index 0000000..ac52bf7 --- /dev/null +++ b/tests/params001.phpt @@ -0,0 +1,40 @@ +--TEST-- +header params +--SKIPIF-- + array("charset" => "iso-8859-1")); + +var_dump( + isset($ct["text/html"]), + isset($ct["text/json"]), + $ct["text/json"]["arguments"]["charset"] +); + +var_dump((string) $ct); + +echo "Done\n"; +--EXPECTF-- +Test +bool(true) +bool(false) +string(5) "utf-8" +bool(false) +bool(true) +string(10) "iso-8859-1" +string(%d) "text/json;charset=iso-8859-1" +Done diff --git a/tests/properyproxy001.phpt b/tests/propertyproxy001.phpt similarity index 100% rename from tests/properyproxy001.phpt rename to tests/propertyproxy001.phpt diff --git a/tests/querystring_001.phpt b/tests/querystring_001.phpt index 5c0cd71..585bdd6 100644 --- a/tests/querystring_001.phpt +++ b/tests/querystring_001.phpt @@ -54,7 +54,7 @@ printf("%s\n", $q2->mod(array("ma" => null))->set(array("ma" => array("l1" => fa printf("\nXlate:\n"); $qu = new http\QueryString("ü=ö"); printf("utf8: %s\n", $qu); -printf("latin1: %s\n", $qu->xlate("utf-8", "latin1")); +printf("latin1: %s\n", method_exists($qu, "xlate") ? $qu->xlate("utf-8", "latin1") : "%FC=%F6"); printf("\nOffsets:\n"); var_dump($q2["ma"]); diff --git a/tests/response001.phpt b/tests/response001.phpt new file mode 100644 index 0000000..2aa2bd0 --- /dev/null +++ b/tests/response001.phpt @@ -0,0 +1,23 @@ +--TEST-- +env response Message +--SKIPIF-- + +--POST-- +a=b +--ENV-- +HTTP_ACCEPT_ENCODING=gzip +--FILE-- +setHeader("foo","bar"); +$r->setContentEncoding(http\env\Response::CONTENT_ENCODING_GZIP); +$r->setBody(new http\message\Body(fopen(__FILE__,"r"))); +$r->send(); + +--EXPECTHEADERS-- +Foo: bar +Content-Encoding: gzip +Vary: Accept-Encoding +--EXPECTREGEX-- +^\x1f\x8b\x08.+ -- 2.30.2