From: Michael Wallner Date: Mon, 12 Aug 2013 19:49:55 +0000 (+0000) Subject: test reafactoring; release beta5 X-Git-Tag: RELEASE_2_1_0_RC3~10^2^2~24 X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=commitdiff_plain;h=40688e36ecfc7b22a1221079a8d40edd122ee550 test reafactoring; release beta5 --- diff --git a/package.xml b/package.xml index 158a29c..a0b1a68 100644 --- a/package.xml +++ b/package.xml @@ -28,9 +28,9 @@ support. Parallel requests are available for PHP 5 and greater. mike@php.net yes - 2012-12-31 + 2013-08-12 - 2.0.0dev + 2.0.0beta5 2.0.0 @@ -140,6 +140,9 @@ Extended HTTP support. Again. Keep in mind that it's got the major version 2, be + + + @@ -157,6 +160,20 @@ Extended HTTP support. Again. Keep in mind that it's got the major version 2, be + + + + + + + + + + + + + + @@ -165,6 +182,15 @@ Extended HTTP support. Again. Keep in mind that it's got the major version 2, be + + + + + + + + + @@ -173,38 +199,38 @@ Extended HTTP support. Again. Keep in mind that it's got the major version 2, be + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - diff --git a/php_http.h b/php_http.h index bb9c2d0..68f4293 100644 --- a/php_http.h +++ b/php_http.h @@ -13,7 +13,7 @@ #ifndef PHP_EXT_HTTP_H #define PHP_EXT_HTTP_H -#define PHP_HTTP_EXT_VERSION "2.0.0dev" +#define PHP_HTTP_EXT_VERSION "2.0.0beta5" zend_module_entry http_module_entry; #define phpext_http_ptr &http_module_entry diff --git a/phpunit/ClientTest.php b/phpunit/ClientTest.php deleted file mode 100644 index 21493f3..0000000 --- a/phpunit/ClientTest.php +++ /dev/null @@ -1,182 +0,0 @@ -getProgressInfo($r)) $c->pi .= "-"; - } -} -class ProgressObserver2 implements SplObserver { - function update(SplSubject $c, $r = null) { - if ($c->getProgressInfo($r)) $c->pi .= "."; - } -} -class CallbackObserver implements SplObserver { - public $callback; - function __construct($callback) { - $this->callback = $callback; - } - function update(SplSubject $c, $r = null) { - call_user_func($this->callback, $c, $r); - } -} - -class RequestTest extends PHPUnit_Framework_TestCase -{ - /** - * @var http\Client - */ - protected $r; - - function setUp() { - $this->r = new http\Client; - $this->r->pi = ""; - $this->r->setOptions( - array( - "connecttimeout" => 30, - "timeout" => 300, - ) - ); - $this->r->setOptions( - array( - "timeout" => 600 - ) - ); - $this->assertEquals( - array( - "connecttimeout" => 30, - "timeout" => 600, - ), - $this->r->getOptions() - ); - } - - function testObserver() { - $test = $this; - $this->r->attach($o1 = new ProgressObserver1); - $this->r->attach($o2 = new ProgressObserver2); - $this->r->attach( - $o3 = new CallbackObserver( - function ($c, $r) use ($test) { - $p = (array) $c->getProgressInfo($r); - $test->assertArrayHasKey("started", $p); - $test->assertArrayHasKey("finished", $p); - $test->assertArrayHasKey("dlnow", $p); - $test->assertArrayHasKey("ulnow", $p); - $test->assertArrayHasKey("dltotal", $p); - $test->assertArrayHasKey("ultotal", $p); - $test->assertArrayHasKey("info", $p); - } - ) - ); - $this->r->enqueue(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/"))->send(); - $this->assertRegexp("/(\.-)+/", $this->r->pi); - $this->assertCount(3, $this->r->getObservers()); - $this->r->detach($o1); - $this->assertCount(2, $this->r->getObservers()); - $this->r->detach($o2); - $this->assertCount(1, $this->r->getObservers()); - $this->r->detach($o3); - $this->assertCount(0, $this->r->getObservers()); - } - - function testSsl() { - $this->r->setSslOptions(array("verify_peer" => true)); - $this->r->addSslOptions(array("verify_host" => 2)); - $this->assertEquals( - array( - "verify_peer" => true, - "verify_host" => 2, - ), - $this->r->getSslOptions() - ); - $this->r->enqueue($req = new http\Client\Request("GET", "https://twitter.com/")); - $this->r->send(); - $ti = $this->r->getTransferInfo($req); - $this->assertArrayHasKey("ssl_engines", $ti); - $this->assertGreaterThan(0, count($ti["ssl_engines"])); - } - - function testHistory() { - $body = new http\Message\Body; - $body->append("foobar"); - - $request = new http\Client\Request; - $request->setBody($body); - $request->setRequestMethod("POST"); - $request->setRequestUrl("http://dev.iworks.at/ext-http/.print_request.php"); - - $this->r->recordHistory = true; - $this->r->enqueue($request)->send(); - - $this->assertStringMatchesFormat(<<r->getHistory()->toString(true)) - ); - - - $this->r->requeue($request)->send(); - - $this->assertStringMatchesFormat(<<r->getHistory()->toString(true)) - ); - } -} - diff --git a/phpunit/HeaderTest.php b/phpunit/HeaderTest.php deleted file mode 100644 index 184f21a..0000000 --- a/phpunit/HeaderTest.php +++ /dev/null @@ -1,83 +0,0 @@ -assertEquals("Foo: bar", (string) $h); - } - - function testSerialize() { - $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)); - $this->assertTrue($ae->match("gzip", http\Header::MATCH_WORD|http\Header::MATCH_CASE)); - $this->assertFalse($ae->match("gzip", http\Header::MATCH_STRICT)); - $this->assertTrue($ae->match("deflate", http\Header::MATCH_WORD)); - $this->assertTrue($ae->match("deflate", http\Header::MATCH_WORD|http\Header::MATCH_CASE)); - $this->assertFalse($ae->match("deflate", http\Header::MATCH_STRICT)); - - $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 - ); - } -} diff --git a/phpunit/MessageTest.php b/phpunit/MessageTest.php deleted file mode 100644 index b572f57..0000000 --- a/phpunit/MessageTest.php +++ /dev/null @@ -1,283 +0,0 @@ -str = $str; - } - function __toString() { - return (string) $this->str; - } -} - -class message extends http\Message { - function __call($m, $args) { - if (preg_match("/^test(get|set)(.*)\$/i", $m, $match)) { - list(, $getset, $prop) = $match; - $prop = strtolower($prop[0]).substr($prop,1); - switch(strtolower($getset)) { - case "get": - return $this->$prop; - case "set": - $this->$prop = current($args); - break; - } - } - } -} - -class MessageTest extends PHPUnit_Framework_TestCase -{ - function testProperties() { - $test = new message; - $this->assertEquals(0, $test->testGetType()); - $this->assertEquals(null, $test->testGetBody()); - $this->assertEquals("", $test->testGetRequestMethod()); - $this->assertEquals("", $test->testGetRequestUrl()); - $this->assertEquals("", $test->testGetResponseStatus()); - $this->assertEquals(0, $test->testGetResponseCode()); - $this->assertEquals("1.1", $test->testGetHttpVersion()); - $this->assertEquals(array(), $test->testGetHeaders()); - $this->assertEquals(null, $test->testGetParentMessage()); - $test->testSetType(http\Message::TYPE_REQUEST); - $this->assertEquals(http\Message::TYPE_REQUEST, $test->testGetType()); - $this->assertEquals(http\Message::TYPE_REQUEST, $test->getType()); - $body = new http\Message\Body; - $test->testSetBody($body); - $this->assertEquals($body, $test->testGetBody()); - $this->assertEquals($body, $test->getBody()); - $file = fopen(__FILE__,"r"); - $test->testSetBody($file); - $this->assertEquals($file, $test->testGetBody()->getResource()); - $this->assertEquals($file, $test->getBody()->getResource()); - $test->testSetBody("data"); - $this->assertEquals("data", (string) $test->testGetBody()); - $this->assertEquals("data", (string) $test->getBody()); - $test->testSetRequestMethod("HEAD"); - $this->assertEquals("HEAD", $test->testGetRequestMethod()); - $this->assertEquals("HEAD", $test->getRequestMethod()); - $test->testSetRequestUrl("/"); - $this->assertEquals("/", $test->testGetRequestUrl()); - $this->assertEquals("/", $test->getRequestUrl()); - $this->assertEquals("HEAD / HTTP/1.1", $test->getInfo()); - $test->testSetType(http\Message::TYPE_RESPONSE); - $test->setResponseStatus("Created"); - $this->assertEquals("Created", $test->testGetResponseStatus()); - $this->assertEquals("Created", $test->getResponseStatus()); - $test->setResponseCode(201); - $this->assertEquals(201, $test->testGetResponseCode()); - $this->assertEquals(201, $test->getResponseCode()); - $test->testSetResponseStatus("Ok"); - $this->assertEquals("Ok", $test->testGetResponseStatus()); - $this->assertEquals("Ok", $test->getResponseStatus()); - $test->testSetResponseCode(200); - $this->assertEquals(200, $test->testGetResponseCode()); - $this->assertEquals(200, $test->getResponseCode()); - $test->testSetHttpVersion("1.0"); - $this->assertEquals("1.0", $test->testGetHttpVersion()); - $this->assertEquals("1.0", $test->getHttpVersion()); - $this->assertEquals("HTTP/1.0 200 OK", $test->getInfo()); - $test->setHttpVersion("1.1"); - $this->assertEquals("1.1", $test->testGetHttpVersion()); - $this->assertEquals("1.1", $test->getHttpVersion()); - $this->assertEquals("HTTP/1.1 200 OK", $test->getInfo()); - $test->setInfo("HTTP/1.1 201 Created"); - $this->assertEquals("Created", $test->testGetResponseStatus()); - $this->assertEquals("Created", $test->getResponseStatus()); - $this->assertEquals(201, $test->testGetResponseCode()); - $this->assertEquals(201, $test->getResponseCode()); - $this->assertEquals("1.1", $test->testGetHttpVersion()); - $this->assertEquals("1.1", $test->getHttpVersion()); - $test->testSetHeaders(array("Foo" => "bar")); - $this->assertEquals(array("Foo" => "bar"), $test->testGetHeaders()); - $this->assertEquals(array("Foo" => "bar"), $test->getHeaders()); - $this->assertEquals("bar", $test->getHeader("foo")); - $this->assertEquals(null, $test->getHeader("bar")); - $parent = new message; - $test->testSetParentMessage($parent); - $this->assertEquals($parent, $test->testGetParentMessage()); - $this->assertEquals($parent, $test->getParentMessage()); - } - - function testAddBody() { - $m = new http\Message; - $body = new http\Message\Body; - $body->append("foo"); - $m->addBody($body); - $body = new http\Message\Body; - $body->append("bar"); - $m->addBody($body); - $this->assertEquals("foobar", (string) $m->getBody()); - } - - function testAddHeaders() { - $m = new http\Message; - $m->addHeaders(array("foo"=>"bar","bar"=>"foo")); - $this->assertEquals(array("Foo"=>"bar","Bar"=>"foo"), $m->getHeaders()); - $m->addHeaders(array("key"=>"val","more"=>"Stuff")); - $this->assertEquals(array("Foo"=>"bar","Bar"=>"foo","Key"=>"val","More"=>"Stuff"), $m->getHeaders()); - } - - function testArrayHeaders() { - $m = new http\Message("GET / HTTP/1.1"); - $m->addHeader("Accept", "text/html"); - $m->addHeader("Accept", "text/xml;q=0"); - $m->addHeader("Accept", "text/plain;q=0.5"); - $this->assertEquals( - "GET / HTTP/1.1\r\n". - "Accept: text/html, text/xml;q=0, text/plain;q=0.5\r\n", - $m->toString() - ); - } - - function testHeaderValueTypes() { - $m = new http\Message("HTTP/1.1 200 Ok"); - $m->addHeader("Bool", true); - $m->addHeader("Int", 123); - $m->addHeader("Float", 1.23); - $m->addHeader("Array", array(1,2,3)); - $m->addHeader("Object", new strval("test")); - $m->addHeader("Set-Cookie", - array( - array( - "cookies" => array("foo" => "bar"), - "expires" => date_create("2012-12-31 22:59:59 GMT")->format( - DateTime::COOKIE - ), - "path" => "/somewhere" - ) - ) - ); - $m->addHeader("Set-Cookie", "val=0"); - - $this->assertEquals( - "HTTP/1.1 200 Ok\r\n". - "Bool: true\r\n". - "Int: 123\r\n". - "Float: 1.23\r\n". - "Array: 1, 2, 3\r\n". - "Object: test\r\n". - "Set-Cookie: foo=bar; path=/somewhere; expires=Mon, 31 Dec 2012 22:59:59 GMT; \r\n". - "Set-Cookie: val=0\r\n", - $m->toString() - ); - } - - function testDetach() { - $m = new http\Message( - "HTTP/1.1 200 Ok\r\n". - "HTTP/1.1 201 Created\n". - "HTTP/1.1 302 Found\r\n" - ); - - $this->assertCount(3, $m); - $d = $m->detach(); - $this->assertCount(3, $m); - $this->assertCount(1, $d); - - $this->assertEquals("HTTP/1.1 302 Found\r\n\r\n", $d->toString(true)); - } - - function testPrepend() { - for ($i=0; $i<9; ++$i) { - $a[] = new http\Message("HTTP/1.1 ".($i+200)); - } - - foreach ($a as $m) { - if (isset($p)) $m->prepend($p); - $p = $m; - } - - $this->assertEquals( - "HTTP/1.1 200\r\n\r\n". - "HTTP/1.1 201\r\n\r\n". - "HTTP/1.1 202\r\n\r\n". - "HTTP/1.1 203\r\n\r\n". - "HTTP/1.1 204\r\n\r\n". - "HTTP/1.1 205\r\n\r\n". - "HTTP/1.1 206\r\n\r\n". - "HTTP/1.1 207\r\n\r\n". - "HTTP/1.1 208\r\n\r\n", - $m->toString(true) - ); - } - - function testEmptyUrlWarning() { - $m = new http\Message; - $this->setExpectedException("PHPUnit_Framework_Error_Notice"); - $m->setRequestUrl("/foo"); - $m->setType(http\Message::TYPE_REQUEST); - $this->setExpectedException("PHPUnit_Framework_Error_Warning"); - $m->setRequestUrl(""); - } - - function testEmptyParentMessage() { - $m = new http\Message; - try { - $m->getParentMessage(); - $this->assertFalse("this code should not be reached"); - } catch (http\Exception $e) { - $this->assertEquals("HttpMessage does not have a parent message", $e->getMessage()); - } - } - - function testPrependError() { - $m = new http\Message("HTTP/1.1 200\r\nHTTP/1.1 201"); - try { - $m->prepend($m->getParentMessage()); - $this->assertFalse("this code should not be reached"); - } catch (http\Exception $e) { - $this->assertEquals("Cannot prepend a message located within the same message chain", $e->getMessage()); - } - } - - function testToCallback() { - $m = new http\Message("HTTP/1.1 200 Ok"); - $m->addHeader("Content-Type", "text/plain"); - $m->getBody()->append("this\nis\nthe\ntext"); - - $d = new http\Encoding\Stream\Deflate; - $s = ""; - $m->toCallback(function ($m, $data) use ($d, &$s) { - $s.=$d->update($data); - }); - $s.=$d->finish(); - $this->assertEquals($m->toString(), http\Encoding\Stream\Inflate::decode($s)); - } - - function testToStream() { - $m = new http\Message("HTTP/1.1 200 Ok"); - $m->addHeader("Content-Type", "text/plain"); - $m->getBody()->append("this\nis\nthe\ntext"); - - $f = tmpfile(); - $m->toStream($f); - rewind($f); - $this->assertEquals((string) $m, stream_get_contents($f)); - fclose($f); - } - - function testBoundary() { - $p = new http\Message; - $p->addHeader("Content-Type", "text/plain"); - $p->getBody()->append("data"); - - $m = new http\Message("HTTP/1.1 200"); - $m->getBody()->addPart($p); - $this->assertStringMatchesFormat( - "HTTP/1.1 200\r\n". - "Content-Length: %d\r\n". - "Content-Type: multipart/form-data; boundary=\"%x.%x\"\r\n". - "\r\n". - "--%x.%x\r\n". - "Content-Type: text/plain\r\n". - "Content-Length: 4\r\n". - "\r\n". - "data\r\n". - "--%x.%x--\r\n". - "", - str_replace("\r", "", $m->toString()) // phpunit replaces \r\n with \n - ); - } -} - diff --git a/phpunit/ObjectTest.php b/phpunit/ObjectTest.php deleted file mode 100644 index 968f3f8..0000000 --- a/phpunit/ObjectTest.php +++ /dev/null @@ -1,71 +0,0 @@ -assertEquals(http\Object::EH_NORMAL, http\Object::getDefaultErrorHandling()); - http\Object::setDefaultErrorHandling(http\Object::EH_SUPPRESS); - $this->assertEquals(http\Object::EH_SUPPRESS, http\Object::getDefaultErrorHandling()); - } - - function testErrorHandling() { - $eh = new eh; - $this->assertEquals(eh::EH_NORMAL, $eh->getErrorHandling()); - $eh->setErrorHandling(eh::EH_SUPPRESS); - $this->assertEquals(eh::EH_SUPPRESS, $eh->getErrorHandling()); - } - - function testSuppress() { - http\Object::setDefaultErrorHandling(http\Object::EH_SUPPRESS); - $o = new eh; - $o->triggerError(E_USER_WARNING, http\Exception::E_UNKNOWN, "suppress"); - } - - function testException() { - http\Object::setDefaultErrorHandling(http\Object::EH_THROW); - $this->setExpectedException("http\\Exception"); - $o = new eh; - $o->triggerError(E_USER_WARNING, http\Exception::E_UNKNOWN, "exception"); - } - - function testNormalError() { - http\Object::setDefaultErrorHandling(http\Object::EH_NORMAL); - $this->setExpectedException("PHPUnit_Framework_Error_Warning"); - $o = new eh; - $o->triggerError(E_USER_WARNING, http\Exception::E_UNKNOWN, "warning"); - } - - function testSuppress2() { - $eh = new eh; - $eh->setErrorHandling(http\Object::EH_SUPPRESS); - $eh->triggerError(E_USER_WARNING, http\Exception::E_UNKNOWN, "suppress"); - } - - function testException2() { - $eh = new eh; - $eh->setErrorHandling(http\Object::EH_THROW); - $this->setExpectedException("http\\Exception"); - $eh->triggerError(E_USER_WARNING, http\Exception::E_UNKNOWN, "exception"); - } - - function testNormalError2() { - $eh = new eh; - $eh->setErrorHandling(http\Object::EH_NORMAL); - $this->setExpectedException("PHPUnit_Framework_Error_Warning"); - $eh->triggerError(E_USER_WARNING, http\Exception::E_UNKNOWN, "warning"); - } - - function testUnknownDefaultErrorHandling() { - $this->setExpectedException("PHPUnit_Framework_Error_Warning"); - http\Object::setDefaultErrorHandling(12345); - } - - function testUnknownErrorHandling() { - $eh = new eh; - $this->setExpectedException("PHPUnit_Framework_Error_Warning"); - $eh->setErrorHandling(12345); - } -} - diff --git a/tests/client011.phpt b/tests/client011.phpt new file mode 100644 index 0000000..188b178 --- /dev/null +++ b/tests/client011.phpt @@ -0,0 +1,90 @@ +--TEST-- +client history +--SKIPIF-- + +--FILE-- +append("foobar"); + +$request = new http\Client\Request; +$request->setBody($body); +$request->setRequestMethod("POST"); +$request->setRequestUrl("http://dev.iworks.at/ext-http/.print_request.php"); + +$client = new http\Client; +$client->recordHistory = true; + +$client->enqueue($request)->send(); +echo $client->getHistory()->toString(true); + +$client->requeue($request)->send(); +echo $client->getHistory()->toString(true); + +?> +Done +--EXPECTF-- +Test +POST /ext-http/.print_request.php HTTP/1.1 +User-Agent: %s +Host: dev.iworks.at +Accept: */* +Content-Length: 6 +Content-Type: application/x-www-form-urlencoded +X-Original-Content-Length: 6 + +foobar +HTTP/1.1 200 OK +Date: %s +Server: %s +Vary: %s +Content-Length: 19 +Content-Type: text/html +X-Original-Content-Length: 19 + +string(6) "foobar" + +POST /ext-http/.print_request.php HTTP/1.1 +User-Agent: %s +Host: dev.iworks.at +Accept: */* +Content-Length: 6 +Content-Type: application/x-www-form-urlencoded +X-Original-Content-Length: 6 + +foobar +HTTP/1.1 200 OK +Date: %s +Server: %s +Vary: %s +Content-Length: 19 +Content-Type: text/html +X-Original-Content-Length: 19 + +string(6) "foobar" + +POST /ext-http/.print_request.php HTTP/1.1 +User-Agent: %s +Host: dev.iworks.at +Accept: */* +Content-Length: 6 +Content-Type: application/x-www-form-urlencoded +X-Original-Content-Length: 6 + +foobar +HTTP/1.1 200 OK +Date: %s +Server: %s +Vary: %s +Content-Length: 19 +Content-Type: text/html +X-Original-Content-Length: 19 + +string(6) "foobar" + +Done diff --git a/tests/client012.phpt b/tests/client012.phpt new file mode 100644 index 0000000..f9ae690 --- /dev/null +++ b/tests/client012.phpt @@ -0,0 +1,36 @@ +--TEST-- +client ssl +--SKIPIF-- + +--FILE-- +setSslOptions(array("verify_peer" => true)); +$client->addSslOptions(array("verify_host" => 2)); +var_dump( + array( + "verify_peer" => true, + "verify_host" => 2, + ) === $client->getSslOptions() +); + +$client->enqueue($req = new http\Client\Request("GET", "https://twitter.com/")); +$client->send(); + +$ti = $client->getTransferInfo($req); +var_dump(array_key_exists("ssl_engines", $ti)); +var_dump(0 < count($ti["ssl_engines"])); +?> +Done +--EXPECTF-- +Test +bool(true) +bool(true) +bool(true) +Done diff --git a/tests/client013.phpt b/tests/client013.phpt new file mode 100644 index 0000000..0de0547 --- /dev/null +++ b/tests/client013.phpt @@ -0,0 +1,66 @@ +--TEST-- +client observers +--SKIPIF-- + +--FILE-- +getProgressInfo($r)) $c->pi .= "-"; + } +} +class ProgressObserver2 implements SplObserver { + function update(SplSubject $c, $r = null) { + if ($c->getProgressInfo($r)) $c->pi .= "."; + } +} +class CallbackObserver implements SplObserver { + public $callback; + function __construct($callback) { + $this->callback = $callback; + } + function update(SplSubject $c, $r = null) { + call_user_func($this->callback, $c, $r); + } +} + +$client = new Client; +$client->attach($o1 = new ProgressObserver1); +$client->attach($o2 = new ProgressObserver2); +$client->attach( + $o3 = new CallbackObserver( + function ($c, $r) { + $p = (array) $c->getProgressInfo($r); + var_dump(array_key_exists("started", $p)); + var_dump(array_key_exists("finished", $p)); + var_dump(array_key_exists("dlnow", $p)); + var_dump(array_key_exists("ulnow", $p)); + var_dump(array_key_exists("dltotal", $p)); + var_dump(array_key_exists("ultotal", $p)); + var_dump(array_key_exists("info", $p)); + } + ) +); + +$client->enqueue(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/"))->send(); +var_dump(1 === preg_match("/(\.-)+/", $client->pi)); +var_dump(3 === count($client->getObservers())); +$client->detach($o1); +var_dump(2 === count($client->getObservers())); +$client->detach($o2); +var_dump(1 === count($client->getObservers())); +$client->detach($o3); +var_dump(0 === count($client->getObservers())); + +?> +Done +--EXPECTREGEX-- +Test\n(bool\(true\)\n)+Done diff --git a/tests/clientrequest004.phpt b/tests/clientrequest004.phpt index abff8d9..f00ee2e 100644 --- a/tests/clientrequest004.phpt +++ b/tests/clientrequest004.phpt @@ -12,6 +12,9 @@ echo "Test\n"; $r = new http\Client\Request("GET", "http://localhost"); var_dump($r === $r->setOptions($o = array("redirect"=>5, "timeout"=>5))); var_dump($o === $r->getOptions()); +var_dump($r === $r->setOptions(array("timeout"=>50))); +$o["timeout"] = 50; +var_dump($o === $r->getOptions()); var_dump($r === $r->setSslOptions($o = array("verify_peer"=>false))); var_dump($o === $r->getSslOptions()); @@ -23,4 +26,6 @@ bool(true) bool(true) bool(true) bool(true) +bool(true) +bool(true) Done diff --git a/tests/envrequestheader001.phpt b/tests/envrequestheader001.phpt index 2a6a940..b259ea5 100644 --- a/tests/envrequestheader001.phpt +++ b/tests/envrequestheader001.phpt @@ -1,5 +1,5 @@ --TEST-- -request header +env request header --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.+ diff --git a/tests/envresponse002.phpt b/tests/envresponse002.phpt new file mode 100644 index 0000000..e11f7bb --- /dev/null +++ b/tests/envresponse002.phpt @@ -0,0 +1,28 @@ +--TEST-- +env response cache negative +--SKIPIF-- + +--GET-- +a=b +--ENV-- +HTTP_IF_MODIFIED_SINCE=Fri, 13 Feb 2009 23:31:30 GMT +HTTP_IF_NONE_MATCH=0000-00-0000 +--FILE-- +setBody(new http\Message\Body(fopen(__FILE__,"rb"))); +$r->setEtag("abc"); +$r->setLastModified(1234567891); +$r->send(); +?> +--EXPECTHEADERS-- +ETag: "abc" +Last-Modified: Fri, 13 Feb 2009 23:31:31 GMT +--EXPECT-- +setBody(new http\Message\Body(fopen(__FILE__,"rb"))); +$r->setEtag("abc"); +$r->setLastModified(1234567891); +$r->send(); +?> diff --git a/tests/envresponse003.phpt b/tests/envresponse003.phpt new file mode 100644 index 0000000..3bbf760 --- /dev/null +++ b/tests/envresponse003.phpt @@ -0,0 +1,24 @@ +--TEST-- +env response ranges +--SKIPIF-- + +--ENV-- +HTTP_RANGE=bytes=2-4 +--GET-- +a=b +--FILE-- +setContentType("text/plain"); +$r->setContentDisposition( + array("attachment" => array(array("filename" => basename(__FILE__)))) +); +$r->setBody(new http\Message\Body(fopen(__FILE__, "rb"))); +$r->send(); + +?> +--EXPECTHEADERS-- +Content-Type: text/plain +--EXPECTF-- +php diff --git a/tests/envresponse004.phpt b/tests/envresponse004.phpt new file mode 100644 index 0000000..f84c860 --- /dev/null +++ b/tests/envresponse004.phpt @@ -0,0 +1,28 @@ +--TEST-- +env reponse callback +--SKIPIF-- + +--GET-- +dummy=1 +--FILE-- +setCacheControl("public,must-revalidate,max-age=0"); +$r->setThrottleRate(1, 0.1); +ob_start($r); + +echo "foo"; +echo "bar"; + +ob_end_flush(); +$r->send(); +--EXPECTHEADERS-- +Accept-Ranges: bytes +Cache-Control: public,must-revalidate,max-age=0 +ETag: "9ef61f95" +--EXPECTF-- +foobar diff --git a/tests/envresponse005.phpt b/tests/envresponse005.phpt new file mode 100644 index 0000000..6d55168 --- /dev/null +++ b/tests/envresponse005.phpt @@ -0,0 +1,23 @@ +--TEST-- +env response cache positive +--SKIPIF-- + +--GET-- +a=b +--ENV-- +HTTP_IF_MODIFIED_SINCE=Fri, 13 Feb 2009 23:31:32 GMT +--FILE-- +setBody(new http\Message\Body(fopen(__FILE__,"rb"))); +$r->setEtag("abc"); +$r->setLastModified(1234567891); +$r->isCachedByEtag("If-None-Match") and die("Huh? etag? really?\n"); +$r->isCachedByLastModified("If-Modified-Since") or die("yep, I should be cached"); +$r->send(); +?> +--EXPECTHEADERS-- +HTTP/1.1 304 Not Modified +ETag: "abc" +Last-Modified: Fri, 13 Feb 2009 23:31:31 GMT +--EXPECT-- diff --git a/tests/envresponse006.phpt b/tests/envresponse006.phpt new file mode 100644 index 0000000..76141c1 --- /dev/null +++ b/tests/envresponse006.phpt @@ -0,0 +1,31 @@ +--TEST-- +env response stream +--SKIPIF-- + +--FILE-- +addHeader("foo", array("bar","baz")); +$r->getBody()->append("foo"); + +$r->send($f); + +rewind($f); +var_dump(stream_get_contents($f)); +?> +Done +--EXPECT-- +Test +string(77) "HTTP/1.1 200 OK +Accept-Ranges: bytes +Foo: bar, baz +ETag: "8c736521" + +foo" +Done diff --git a/tests/envresponse007.phpt b/tests/envresponse007.phpt new file mode 100644 index 0000000..5495fe2 --- /dev/null +++ b/tests/envresponse007.phpt @@ -0,0 +1,39 @@ +--TEST-- +env response env request +--SKIPIF-- + +--GET-- +dummy=1 +--FILE-- +setHeader("Range", "bytes=2-4"); + +$res = new http\Env\Response; +$res->setEnvRequest($req); +$res->setContentType("text/plain"); +$res->getBody()->append("012345679"); +$res->send($tmp); + +rewind($tmp); +var_dump(stream_get_contents($tmp)); + +?> +Done +--EXPECTF-- +Test +string(%d) "HTTP/1.1 206 Partial Content%c +Accept-Ranges: bytes%c +X-Powered-By: %s%c +Content-Type: text/plain%c +Content-Range: bytes 2-4/9%c +%c +234" +Done diff --git a/tests/envresponse008.phpt b/tests/envresponse008.phpt new file mode 100644 index 0000000..379ab57 --- /dev/null +++ b/tests/envresponse008.phpt @@ -0,0 +1,30 @@ +--TEST-- +env response stream message +--SKIPIF-- + +--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($f); + +rewind($f); +var_dump(stream_get_contents($f)); + +--EXPECTREGEX-- +string\(\d+\) "HTTP\/1\.1 200 OK +Accept-Ranges: bytes +Foo: bar +Content-Encoding: gzip +Vary: Accept-Encoding +ETag: "\w+-\w+-\w+" +Last-Modified: \w+, \d+ \w+ \d{4} \d\d:\d\d:\d\d GMT + +\x1f\x8b\x08.+ diff --git a/tests/envresponse009.phpt b/tests/envresponse009.phpt new file mode 100644 index 0000000..2a337e4 --- /dev/null +++ b/tests/envresponse009.phpt @@ -0,0 +1,39 @@ +--TEST-- +env response stream cache negative +--SKIPIF-- + +--GET-- +a=b +--ENV-- +HTTP_IF_MODIFIED_SINCE=Fri, 13 Feb 2009 23:31:30 GMT +HTTP_IF_NONE_MATCH=0000-00-0000 +--FILE-- +setBody(new http\Message\Body(fopen(__FILE__,"rb"))); +$r->setEtag("abc"); +$r->setLastModified(1234567891); +$r->send($f); +rewind($f); +var_dump(stream_get_contents($f)); +?> +--EXPECTF-- +string(%d) "HTTP/1.1 200 OK%c +Accept-Ranges: bytes%c +X-Powered-By: %s%c +ETag: "abc"%c +Last-Modified: %s%c +%c +setBody(new http\Message\Body(fopen(__FILE__,"rb"))); +$r->setEtag("abc"); +$r->setLastModified(1234567891); +$r->send($f); +rewind($f); +var_dump(stream_get_contents($f)); +?> +" + diff --git a/tests/envresponse010.phpt b/tests/envresponse010.phpt new file mode 100644 index 0000000..d365497 --- /dev/null +++ b/tests/envresponse010.phpt @@ -0,0 +1,29 @@ +--TEST-- +env response stream ranges +--SKIPIF-- + +--ENV-- +HTTP_RANGE=bytes=2-4 +--GET-- +a=b +--FILE-- +setContentType("text/plain"); +$r->setContentDisposition( + array("attachment" => array(array("filename" => basename(__FILE__)))) +); +$r->setBody(new http\Message\Body(fopen(__FILE__, "rb"))); +$r->send($f); +rewind($f); +var_dump(stream_get_contents($f)); +?> +--EXPECTF-- +string(%i) "HTTP/1.1 206 Partial Content%c +Accept-Ranges: bytes%c +X-Powered-By: PHP/%s%c +Content-Type: text/plain%c +Content-Range: bytes 2-4/311%c +%c +php" diff --git a/tests/envresponse011.phpt b/tests/envresponse011.phpt new file mode 100644 index 0000000..45e2f6e --- /dev/null +++ b/tests/envresponse011.phpt @@ -0,0 +1,24 @@ +--TEST-- +env response cache positive with env message +--SKIPIF-- + +--GET-- +dummy=1 +--FILE-- +setHeader("If-Modified-Since", "Fri, 13 Feb 2009 23:31:32 GMT"); +$r = new http\Env\Response; +$r->setEnvRequest($e); +$r->setBody(new http\Message\Body(fopen(__FILE__,"rb"))); +$r->setEtag("abc"); +$r->setLastModified(1234567891); +$r->isCachedByEtag("If-None-Match") and die("Huh? etag? really?\n"); +$r->isCachedByLastModified("If-Modified-Since") or die("yep, I should be cached"); +$r->send(); +?> +--EXPECTHEADERS-- +HTTP/1.1 304 Not Modified +ETag: "abc" +Last-Modified: Fri, 13 Feb 2009 23:31:31 GMT +--EXPECT-- diff --git a/tests/envresponse012.phpt b/tests/envresponse012.phpt new file mode 100644 index 0000000..58e0a3b --- /dev/null +++ b/tests/envresponse012.phpt @@ -0,0 +1,29 @@ +--TEST-- +env response content disposition +--SKIPIF-- + +--GET-- +dummy=1 +--FILE-- +setContentDisposition(array("attachment"=>array("filename"=>basename(__FILE__)))); +$r->setBody(new http\Message\Body(fopen(__FILE__,"r"))); +$r->send(); + +?> +--EXPECTHEADERS-- +Content-Disposition: attachment;filename=envresponse012.php +--EXPECT-- +setContentDisposition(array("attachment"=>array("filename"=>basename(__FILE__)))); +$r->setBody(new http\Message\Body(fopen(__FILE__,"r"))); +$r->send(); + +?> + diff --git a/tests/envresponse013.phpt b/tests/envresponse013.phpt new file mode 100644 index 0000000..d21f604 --- /dev/null +++ b/tests/envresponse013.phpt @@ -0,0 +1,29 @@ +--TEST-- +env response deflate +--SKIPIF-- + +--GET-- +dummy=1 +--FILE-- +setHeader("Accept-Encoding", "deflate"); + +$res = new http\Env\Response; +$res->setCacheControl("public, max-age=3600"); +$res->setContentEncoding(http\Env\Response::CONTENT_ENCODING_GZIP); +$res->getBody()->append("foobar"); + +$res->setEnvRequest($req); +$res->send(); +?> +--EXPECTHEADERS-- +Content-Encoding: deflate +Vary: Accept-Encoding +Cache-Control: public, max-age=3600 +--EXPECTREGEX-- +^\x78\x9c.+ + diff --git a/tests/envresponse014.phpt b/tests/envresponse014.phpt new file mode 100644 index 0000000..ef85e2c --- /dev/null +++ b/tests/envresponse014.phpt @@ -0,0 +1,24 @@ +--TEST-- +env response invalid ranges +--SKIPIF-- + +--FILE-- +setHeader("Range", "bytes=321-123,123-0"); +$res = new http\Env\Response; +$res->getBody()->append("foobar"); +$res->setEnvRequest($req); +$res->send($f); +rewind($f); +var_dump(stream_get_contents($f)); +--EXPECTF-- +string(96) "HTTP/1.1 416 Requested Range Not Satisfiable +Accept-Ranges: bytes +Content-Range: bytes */6 + +" + diff --git a/tests/header001.phpt b/tests/header001.phpt new file mode 100644 index 0000000..5e3d938 --- /dev/null +++ b/tests/header001.phpt @@ -0,0 +1,20 @@ +--TEST-- +header string +--SKIPIF-- + +--FILE-- + +Done +--EXPECT-- +Test +bool(true) +Done diff --git a/tests/header002.phpt b/tests/header002.phpt new file mode 100644 index 0000000..36b25c7 --- /dev/null +++ b/tests/header002.phpt @@ -0,0 +1,20 @@ +--TEST-- +header numeric +--SKIPIF-- + +--FILE-- + +Done +--EXPECT-- +Test +bool(true) +Done diff --git a/tests/header003.phpt b/tests/header003.phpt new file mode 100644 index 0000000..d02e7e7 --- /dev/null +++ b/tests/header003.phpt @@ -0,0 +1,23 @@ +--TEST-- +header serialize +--SKIPIF-- + +--FILE-- + +Done +--EXPECT-- +Test +bool(true) +bool(true) +Done diff --git a/tests/header004.phpt b/tests/header004.phpt new file mode 100644 index 0000000..1517f1f --- /dev/null +++ b/tests/header004.phpt @@ -0,0 +1,34 @@ +--TEST-- +header match +--SKIPIF-- + +--FILE-- +match("gzip", http\Header::MATCH_WORD)); +var_dump($ae->match("gzip", http\Header::MATCH_WORD|http\Header::MATCH_CASE)); +var_dump($ae->match("gzip", http\Header::MATCH_STRICT)); +var_dump($ae->match("deflate", http\Header::MATCH_WORD)); +var_dump($ae->match("deflate", http\Header::MATCH_WORD|http\Header::MATCH_CASE)); +var_dump($ae->match("deflate", http\Header::MATCH_STRICT)); +var_dump($ae->match("zip", http\Header::MATCH_WORD)); +var_dump($ae->match("gzip", http\Header::MATCH_FULL)); + +?> +Done +--EXPECT-- +Test +bool(true) +bool(true) +bool(false) +bool(true) +bool(true) +bool(false) +bool(false) +bool(false) +Done diff --git a/tests/header005.phpt b/tests/header005.phpt new file mode 100644 index 0000000..07c27cb --- /dev/null +++ b/tests/header005.phpt @@ -0,0 +1,32 @@ +--TEST-- +header negotiation +--SKIPIF-- + +--FILE-- +negotiate(array("text/plain","text/html"))); +var_dump("text/html" === $a->negotiate(array("text/plain","text/html"), $rs)); +var_dump(array("text/html"=>0.99, "text/plain"=>0.5) === $rs); +var_dump("text/plain" === $a->negotiate(array("foo/bar", "text/plain"), $rs)); +var_dump(array("text/plain"=>0.5) === $rs); +var_dump("foo/bar" === $a->negotiate(array("foo/bar"), $rs)); +var_dump(array() === $rs); + +?> +Done +--EXPECT-- +Test +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +Done diff --git a/tests/header006.phpt b/tests/header006.phpt new file mode 100644 index 0000000..92a6215 --- /dev/null +++ b/tests/header006.phpt @@ -0,0 +1,27 @@ +--TEST-- +header parsing +--SKIPIF-- + +--FILE-- +"bar","Bar"=>"foo") === http\Header::parse($header)); + +$headers = http\Header::parse($header, "http\\Header"); +var_dump(2 === count($headers)); +var_dump(2 === array_reduce($headers, function($count, $header) { + return $count + ($header instanceof http\Header); +}, 0)); +?> +Done +--EXPECT-- +Test +bool(true) +bool(true) +bool(true) +Done diff --git a/tests/header007.phpt b/tests/header007.phpt new file mode 100644 index 0000000..7f6f9eb --- /dev/null +++ b/tests/header007.phpt @@ -0,0 +1,24 @@ +--TEST-- +header parse error +--SKIPIF-- + +--FILE-- + +Done +--EXPECTF-- +Test + +Warning: http\Header::parse(): Could not parse headers in %s on line %d + +Warning: http\Header::parse(): Could not parse headers in %s on line %d +Done diff --git a/tests/header008.phpt b/tests/header008.phpt new file mode 100644 index 0000000..089e2ea --- /dev/null +++ b/tests/header008.phpt @@ -0,0 +1,26 @@ +--TEST-- +header params +--SKIPIF-- + +--FILE-- + array("value" => true, "arguments" => array()), + "must-revalidate" => array("value" => true, "arguments" => array()), + "max-age" => array("value" => "0", "arguments" => array()), + ) === $header->getParams()->params +); + +?> +Done +--EXPECT-- +Test +bool(true) +Done diff --git a/tests/header009.phpt b/tests/header009.phpt new file mode 100644 index 0000000..d3ca1de --- /dev/null +++ b/tests/header009.phpt @@ -0,0 +1,25 @@ +--TEST-- +header params w/ args +--SKIPIF-- + +--FILE-- + array("value" => "bar", "arguments" => array()), + "bar" => array("value" => "bis", "arguments" => array("bis" => "where")) + ) === $header->getParams(".", "where", "is")->params +); + +?> +Done +--EXPECT-- +Test +bool(true) +Done diff --git a/tests/message002.phpt b/tests/message002.phpt index 810c1e8..f515af5 100644 --- a/tests/message002.phpt +++ b/tests/message002.phpt @@ -1,5 +1,5 @@ --TEST-- -env request Message +env request message --SKIPIF-- --POST_RAW-- diff --git a/tests/message007.phpt b/tests/message007.phpt new file mode 100644 index 0000000..478f411 --- /dev/null +++ b/tests/message007.phpt @@ -0,0 +1,27 @@ +--TEST-- +message to stream +--SKIPIF-- + +--FILE-- +addHeader("Content-Type", "text/plain"); +$m->getBody()->append("this\nis\nthe\ntext"); + +$f = tmpfile(); +$m->toStream($f); +rewind($f); +var_dump((string) $m === stream_get_contents($f)); +fclose($f); + +?> +Done +--EXPECT-- +Test +bool(true) +Done diff --git a/tests/message008.phpt b/tests/message008.phpt new file mode 100644 index 0000000..3f5154d --- /dev/null +++ b/tests/message008.phpt @@ -0,0 +1,29 @@ +--TEST-- +message to callback +--SKIPIF-- + +--FILE-- +addHeader("Content-Type", "text/plain"); +$m->getBody()->append("this\nis\nthe\ntext"); + +$d = new http\Encoding\Stream\Deflate; +$s = ""; +$m->toCallback(function ($m, $data) use ($d, &$s) { + $s.=$d->update($data); +}); +$s.=$d->finish(); +var_dump($m->toString() === http\Encoding\Stream\Inflate::decode($s)); + +?> +Done +--EXPECT-- +Test +bool(true) +Done diff --git a/tests/message009.phpt b/tests/message009.phpt new file mode 100644 index 0000000..34a923f --- /dev/null +++ b/tests/message009.phpt @@ -0,0 +1,149 @@ +--TEST-- +message properties +--SKIPIF-- + +--FILE-- +$prop; + case "set": + $this->$prop = current($args); + break; + } + } + } +} + +$test = new message; +var_dump(0 === $test->testGetType()); +var_dump(null === $test->testGetBody()); +var_dump(null === $test->testGetRequestMethod()); +var_dump(null === $test->testGetRequestUrl()); +var_dump(null === $test->testGetResponseStatus()); +var_dump(null === $test->testGetResponseCode()); +var_dump("1.1" === $test->testGetHttpVersion()); +var_dump(array() === $test->testGetHeaders()); +var_dump(null === $test->testGetParentMessage()); +$test->testSetType(http\Message::TYPE_REQUEST); +var_dump(http\Message::TYPE_REQUEST === $test->testGetType()); +var_dump(http\Message::TYPE_REQUEST === $test->getType()); +$body = new http\Message\Body; +$test->testSetBody($body); +var_dump($body === $test->testGetBody()); +var_dump($body === $test->getBody()); +$file = fopen(__FILE__,"r"); +$test->testSetBody($file); +var_dump($file === $test->testGetBody()->getResource()); +var_dump($file === $test->getBody()->getResource()); +$test->testSetBody("data"); +var_dump("data" === (string) $test->testGetBody()); +var_dump("data" === (string) $test->getBody()); +$test->testSetRequestMethod("HEAD"); +var_dump("HEAD" === $test->testGetRequestMethod()); +var_dump("HEAD" === $test->getRequestMethod()); +$test->testSetRequestUrl("/"); +var_dump("/" === $test->testGetRequestUrl()); +var_dump("/" === $test->getRequestUrl()); +var_dump("HEAD / HTTP/1.1" === $test->getInfo()); +$test->testSetType(http\Message::TYPE_RESPONSE); +$test->setResponseStatus("Created"); +var_dump("Created" === $test->testGetResponseStatus()); +var_dump("Created" === $test->getResponseStatus()); +$test->setResponseCode(201); +var_dump(201 === $test->testGetResponseCode()); +var_dump(201 === $test->getResponseCode()); +$test->testSetResponseStatus("Ok"); +var_dump("Ok" === $test->testGetResponseStatus()); +var_dump("Ok" === $test->getResponseStatus()); +$test->testSetResponseCode(200); +var_dump(200 === $test->testGetResponseCode()); +var_dump(200 === $test->getResponseCode()); +$test->testSetHttpVersion("1.0"); +var_dump("1.0" === $test->testGetHttpVersion()); +var_dump("1.0" === $test->getHttpVersion()); +var_dump("HTTP/1.0 200 OK" === $test->getInfo()); +$test->setHttpVersion("1.1"); +var_dump("1.1" === $test->testGetHttpVersion()); +var_dump("1.1" === $test->getHttpVersion()); +var_dump("HTTP/1.1 200 OK" === $test->getInfo()); +$test->setInfo("HTTP/1.1 201 Created"); +var_dump("Created" === $test->testGetResponseStatus()); +var_dump("Created" === $test->getResponseStatus()); +var_dump(201 === $test->testGetResponseCode()); +var_dump(201 === $test->getResponseCode()); +var_dump("1.1" === $test->testGetHttpVersion()); +var_dump("1.1" === $test->getHttpVersion()); +$test->testSetHeaders(array("Foo" => "bar")); +var_dump(array("Foo" => "bar") === $test->testGetHeaders()); +var_dump(array("Foo" => "bar") === $test->getHeaders()); +var_dump("bar" === $test->getHeader("foo")); +var_dump(false === $test->getHeader("bar")); +$parent = new message; +$test->testSetParentMessage($parent); +var_dump($parent === $test->testGetParentMessage()); +var_dump($parent === $test->getParentMessage()); + +?> +Done +--EXPECT-- +Test +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +Done diff --git a/tests/message010.phpt b/tests/message010.phpt new file mode 100644 index 0000000..5a51510 --- /dev/null +++ b/tests/message010.phpt @@ -0,0 +1,26 @@ +--TEST-- +message body +--SKIPIF-- + +--FILE-- +append("foo"); +$m->addBody($body); +$body = new http\Message\Body; +$body->append("bar"); +$m->addBody($body); +var_dump("foobar" === (string) $m->getBody()); + +?> +Done +--EXPECT-- +Test +bool(true) +Done diff --git a/tests/message011.phpt b/tests/message011.phpt new file mode 100644 index 0000000..53aa992 --- /dev/null +++ b/tests/message011.phpt @@ -0,0 +1,77 @@ +--TEST-- +message headers +--SKIPIF-- + +--INI-- +date.timezone=UTC +--FILE-- +str = $str; + } + function __toString() { + return (string) $this->str; + } +} + +$m = new http\Message; +$m->addHeaders(array("foo"=>"bar","bar"=>"foo")); +var_dump(array("Foo"=>"bar", "Bar"=>"foo") === $m->getHeaders()); +$m->addHeaders(array("key"=>"val","more"=>"Stuff")); +var_dump(array("Foo"=>"bar", "Bar"=>"foo","Key"=>"val","More"=>"Stuff") === $m->getHeaders()); +$m = new http\Message("GET / HTTP/1.1"); +$m->addHeader("Accept", "text/html"); +$m->addHeader("Accept", "text/xml;q=0"); +$m->addHeader("Accept", "text/plain;q=0.5"); +var_dump( + "GET / HTTP/1.1\r\n". + "Accept: text/html, text/xml;q=0, text/plain;q=0.5\r\n" === + $m->toString() +); +$m = new http\Message("HTTP/1.1 200 Ok"); +$m->addHeader("Bool", true); +$m->addHeader("Int", 123); +$m->addHeader("Float", 1.23); +$m->addHeader("Array", array(1,2,3)); +$m->addHeader("Object", new strval("test")); +$m->addHeader("Set-Cookie", + array( + array( + "cookies" => array("foo" => "bar"), + "expires" => date_create("2012-12-31 22:59:59 GMT")->format( + DateTime::COOKIE + ), + "path" => "/somewhere" + ) + ) +); +$m->addHeader("Set-Cookie", "val=0"); + +var_dump( + "HTTP/1.1 200 Ok\r\n". + "Bool: true\r\n". + "Int: 123\r\n". + "Float: 1.23\r\n". + "Array: 1, 2, 3\r\n". + "Object: test\r\n". + "Set-Cookie: foo=bar; path=/somewhere; expires=Mon, 31 Dec 2012 22:59:59 GMT; \r\n". + "Set-Cookie: val=0\r\n" === + $m->toString() +); + +?> +Done +--EXPECT-- +Test +bool(true) +bool(true) +bool(true) +bool(true) +Done diff --git a/tests/message012.phpt b/tests/message012.phpt new file mode 100644 index 0000000..5f46c48 --- /dev/null +++ b/tests/message012.phpt @@ -0,0 +1,34 @@ +--TEST-- +message part +--SKIPIF-- + +--FILE-- +addHeader("Content-Type", "text/plain"); +$p->getBody()->append("data"); + +$m = new http\Message("HTTP/1.1 200"); +$m->getBody()->addPart($p); +echo $m; + +?> +Done +--EXPECTF-- +Test +HTTP/1.1 200 +Content-Length: %d +Content-Type: multipart/form-data; boundary="%x.%x" + +--%x.%x +Content-Type: text/plain +Content-Length: 4 + +data +--%x.%x-- +Done diff --git a/tests/message013.phpt b/tests/message013.phpt new file mode 100644 index 0000000..8a192a5 --- /dev/null +++ b/tests/message013.phpt @@ -0,0 +1,33 @@ +--TEST-- +message detach +--SKIPIF-- + +--FILE-- +detach(); +var_dump(3 === count($m)); +var_dump(1 === count($d)); + +var_dump("HTTP/1.1 302 Found\r\n\r\n" === $d->toString(true)); + +?> +Done +--EXPECTF-- +Test +bool(true) +bool(true) +bool(true) +bool(true) +Done diff --git a/tests/message014.phpt b/tests/message014.phpt new file mode 100644 index 0000000..485c47f --- /dev/null +++ b/tests/message014.phpt @@ -0,0 +1,39 @@ +--TEST-- +message prepend +--SKIPIF-- + +--FILE-- +prepend($p); + $p = $m; +} + +var_dump( + "HTTP/1.1 200\r\n\r\n". + "HTTP/1.1 201\r\n\r\n". + "HTTP/1.1 202\r\n\r\n". + "HTTP/1.1 203\r\n\r\n". + "HTTP/1.1 204\r\n\r\n". + "HTTP/1.1 205\r\n\r\n". + "HTTP/1.1 206\r\n\r\n". + "HTTP/1.1 207\r\n\r\n". + "HTTP/1.1 208\r\n\r\n" === + $m->toString(true) +); + +?> +Done +--EXPECTF-- +Test +bool(true) +Done diff --git a/tests/message015.phpt b/tests/message015.phpt new file mode 100644 index 0000000..f11efc8 --- /dev/null +++ b/tests/message015.phpt @@ -0,0 +1,43 @@ +--TEST-- +message errors +--SKIPIF-- + +--FILE-- +setRequestUrl("/foo"); +$m->setType(http\Message::TYPE_REQUEST); +$m->setRequestUrl(""); + +$m = new http\Message; +try { + $m->getParentMessage(); + die("unreached"); +} catch (http\Exception $e) { + var_dump($e->getMessage()); +} + +$m = new http\Message("HTTP/1.1 200\r\nHTTP/1.1 201"); +try { + $m->prepend($m->getParentMessage()); + die("unreached"); +} catch (http\Exception $e) { + var_dump($e->getMessage()); +} + +?> +Done +--EXPECTF-- +Test + +Notice: http\Message::setRequestUrl(): HttpMessage is not of type REQUEST in %s on line %d + +Warning: http\Message::setRequestUrl(): Cannot set HttpMessage::requestUrl to an empty string in %s on line %d +string(42) "HttpMessage does not have a parent message" +string(62) "Cannot prepend a message located within the same message chain" +Done diff --git a/tests/object001.phpt b/tests/object001.phpt new file mode 100644 index 0000000..fe3d012 --- /dev/null +++ b/tests/object001.phpt @@ -0,0 +1,22 @@ +--TEST-- +object default error handling +--SKIPIF-- + +--FILE-- + +Done +--EXPECT-- +Test +bool(true) +bool(true) +Done diff --git a/tests/object002.phpt b/tests/object002.phpt new file mode 100644 index 0000000..b3e3743 --- /dev/null +++ b/tests/object002.phpt @@ -0,0 +1,25 @@ +--TEST-- +object instance error handling +--SKIPIF-- + +--FILE-- +getErrorHandling()); +$eh->setErrorHandling(eh::EH_SUPPRESS); +var_dump(eh::EH_SUPPRESS === $eh->getErrorHandling()); + +?> +Done +--EXPECT-- +Test +bool(true) +bool(true) +Done diff --git a/tests/object003.phpt b/tests/object003.phpt new file mode 100644 index 0000000..842bb0d --- /dev/null +++ b/tests/object003.phpt @@ -0,0 +1,22 @@ +--TEST-- +object error suppression +--SKIPIF-- + +--FILE-- +triggerError(E_USER_WARNING, http\Exception::E_UNKNOWN, "suppress"); + +?> +Done +--EXPECT-- +Test +Done diff --git a/tests/object004.phpt b/tests/object004.phpt new file mode 100644 index 0000000..93a062d --- /dev/null +++ b/tests/object004.phpt @@ -0,0 +1,27 @@ +--TEST-- +object error exception +--SKIPIF-- + +--FILE-- +triggerError(E_USER_WARNING, http\Exception::E_UNKNOWN, "exception"); + +?> +Done +--EXPECTF-- +Test + +Fatal error: Uncaught exception 'http\Exception' with message 'exception' in %s:%d +Stack trace: +#0 %s(%d): http\Object->triggerError(512, 0, 'exception') +#1 {main} + thrown in %s on line %d diff --git a/tests/object005.phpt b/tests/object005.phpt new file mode 100644 index 0000000..0330d32 --- /dev/null +++ b/tests/object005.phpt @@ -0,0 +1,24 @@ +--TEST-- +object normal error +--SKIPIF-- + +--FILE-- +triggerError(E_USER_WARNING, http\Exception::E_UNKNOWN, "warning"); + +?> +Done +--EXPECTF-- +Test + +Warning: http\Object::triggerError(): warning in %s on line %d +Done diff --git a/tests/object006.phpt b/tests/object006.phpt new file mode 100644 index 0000000..a11529d --- /dev/null +++ b/tests/object006.phpt @@ -0,0 +1,22 @@ +--TEST-- +object error suppression2 +--SKIPIF-- + +--FILE-- +setErrorHandling(http\Object::EH_SUPPRESS); +$eh->triggerError(E_USER_WARNING, http\Exception::E_UNKNOWN, "suppress"); + +?> +Done +--EXPECTF-- +Test +Done diff --git a/tests/object007.phpt b/tests/object007.phpt new file mode 100644 index 0000000..475b4b8 --- /dev/null +++ b/tests/object007.phpt @@ -0,0 +1,27 @@ +--TEST-- +object error exception2 +--SKIPIF-- + +--FILE-- +setErrorHandling(http\Object::EH_THROW); +$eh->triggerError(E_USER_WARNING, http\Exception::E_UNKNOWN, "exception"); + +?> +Done +--EXPECTF-- +Test + +Fatal error: Uncaught exception 'http\Exception' with message 'exception' in %s:%d +Stack trace: +#0 /home/mike/src/pecl_http-DEV_2.svn/tests/object007.php(9): http\Object->triggerError(512, 0, 'exception') +#1 {main} + thrown in %s on line %d diff --git a/tests/object008.phpt b/tests/object008.phpt new file mode 100644 index 0000000..6dff7d5 --- /dev/null +++ b/tests/object008.phpt @@ -0,0 +1,24 @@ +--TEST-- +object normal error2 +--SKIPIF-- + +--FILE-- +setErrorHandling(http\Object::EH_NORMAL); +$eh->triggerError(E_USER_WARNING, http\Exception::E_UNKNOWN, "warning"); + +?> +Done +--EXPECTF-- +Test + +Warning: http\Object::triggerError(): warning in %s on line %d +Done diff --git a/tests/object009.phpt b/tests/object009.phpt new file mode 100644 index 0000000..4d6e574 --- /dev/null +++ b/tests/object009.phpt @@ -0,0 +1,27 @@ +--TEST-- +object unknown error handling +--SKIPIF-- + +--FILE-- +setErrorHandling(12345); + +?> +Done +--EXPECTF-- +Test + +Warning: http\Object::setDefaultErrorHandling(): unknown error handling code (12345) in %s on line %d + +Warning: http\Object::setErrorHandling(): unknown error handling code (12345) in %s on line %d +Done diff --git a/tests/response001.phpt b/tests/response001.phpt deleted file mode 100644 index 2aa2bd0..0000000 --- a/tests/response001.phpt +++ /dev/null @@ -1,23 +0,0 @@ ---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.+ diff --git a/tests/response002.phpt b/tests/response002.phpt deleted file mode 100644 index 9a2d3da..0000000 --- a/tests/response002.phpt +++ /dev/null @@ -1,28 +0,0 @@ ---TEST-- -http response cache negative ---SKIPIF-- - ---GET-- -a=b ---ENV-- -HTTP_IF_MODIFIED_SINCE=Fri, 13 Feb 2009 23:31:30 GMT -HTTP_IF_NONE_MATCH=0000-00-0000 ---FILE-- -setBody(new http\Message\Body(fopen(__FILE__,"rb"))); -$r->setEtag("abc"); -$r->setLastModified(1234567891); -$r->send(); -?> ---EXPECTHEADERS-- -ETag: "abc" -Last-Modified: Fri, 13 Feb 2009 23:31:31 GMT ---EXPECT-- -setBody(new http\Message\Body(fopen(__FILE__,"rb"))); -$r->setEtag("abc"); -$r->setLastModified(1234567891); -$r->send(); -?> diff --git a/tests/response003.phpt b/tests/response003.phpt deleted file mode 100644 index d2f91c1..0000000 --- a/tests/response003.phpt +++ /dev/null @@ -1,24 +0,0 @@ ---TEST-- -response ranges ---SKIPIF-- - ---ENV-- -HTTP_RANGE=bytes=2-4 ---GET-- -a=b ---FILE-- -setContentType("text/plain"); -$r->setContentDisposition( - array("attachment" => array(array("filename" => basename(__FILE__)))) -); -$r->setBody(new http\Message\Body(fopen(__FILE__, "rb"))); -$r->send(); - -?> ---EXPECTHEADERS-- -Content-Type: text/plain ---EXPECTF-- -php diff --git a/tests/response004.phpt b/tests/response004.phpt deleted file mode 100644 index bda3be0..0000000 --- a/tests/response004.phpt +++ /dev/null @@ -1,28 +0,0 @@ ---TEST-- -reponse callback ---SKIPIF-- - ---GET-- -dummy=1 ---FILE-- -setCacheControl("public,must-revalidate,max-age=0"); -$r->setThrottleRate(1, 0.1); -ob_start($r); - -echo "foo"; -echo "bar"; - -ob_end_flush(); -$r->send(); ---EXPECTHEADERS-- -Accept-Ranges: bytes -Cache-Control: public,must-revalidate,max-age=0 -ETag: "9ef61f95" ---EXPECTF-- -foobar diff --git a/tests/response005.phpt b/tests/response005.phpt deleted file mode 100644 index f4f747d..0000000 --- a/tests/response005.phpt +++ /dev/null @@ -1,23 +0,0 @@ ---TEST-- -http response cache positive ---SKIPIF-- - ---GET-- -a=b ---ENV-- -HTTP_IF_MODIFIED_SINCE=Fri, 13 Feb 2009 23:31:32 GMT ---FILE-- -setBody(new http\Message\Body(fopen(__FILE__,"rb"))); -$r->setEtag("abc"); -$r->setLastModified(1234567891); -$r->isCachedByEtag("If-None-Match") and die("Huh? etag? really?\n"); -$r->isCachedByLastModified("If-Modified-Since") or die("yep, I should be cached"); -$r->send(); -?> ---EXPECTHEADERS-- -HTTP/1.1 304 Not Modified -ETag: "abc" -Last-Modified: Fri, 13 Feb 2009 23:31:31 GMT ---EXPECT-- diff --git a/tests/response006.phpt b/tests/response006.phpt deleted file mode 100644 index c555be6..0000000 --- a/tests/response006.phpt +++ /dev/null @@ -1,31 +0,0 @@ ---TEST-- -http response stream ---SKIPIF-- - ---FILE-- -addHeader("foo", array("bar","baz")); -$r->getBody()->append("foo"); - -$r->send($f); - -rewind($f); -var_dump(stream_get_contents($f)); -?> -Done ---EXPECT-- -Test -string(77) "HTTP/1.1 200 OK -Accept-Ranges: bytes -Foo: bar, baz -ETag: "8c736521" - -foo" -Done diff --git a/tests/response007.phpt b/tests/response007.phpt deleted file mode 100644 index 64fbd65..0000000 --- a/tests/response007.phpt +++ /dev/null @@ -1,39 +0,0 @@ ---TEST-- -response env request ---SKIPIF-- - ---GET-- -dummy=1 ---FILE-- -setHeader("Range", "bytes=2-4"); - -$res = new http\Env\Response; -$res->setEnvRequest($req); -$res->setContentType("text/plain"); -$res->getBody()->append("012345679"); -$res->send($tmp); - -rewind($tmp); -var_dump(stream_get_contents($tmp)); - -?> -Done ---EXPECTF-- -Test -string(%d) "HTTP/1.1 206 Partial Content%c -Accept-Ranges: bytes%c -X-Powered-By: %s%c -Content-Type: text/plain%c -Content-Range: bytes 2-4/9%c -%c -234" -Done diff --git a/tests/response008.phpt b/tests/response008.phpt deleted file mode 100644 index 379ab57..0000000 --- a/tests/response008.phpt +++ /dev/null @@ -1,30 +0,0 @@ ---TEST-- -env response stream message ---SKIPIF-- - ---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($f); - -rewind($f); -var_dump(stream_get_contents($f)); - ---EXPECTREGEX-- -string\(\d+\) "HTTP\/1\.1 200 OK -Accept-Ranges: bytes -Foo: bar -Content-Encoding: gzip -Vary: Accept-Encoding -ETag: "\w+-\w+-\w+" -Last-Modified: \w+, \d+ \w+ \d{4} \d\d:\d\d:\d\d GMT - -\x1f\x8b\x08.+ diff --git a/tests/response009.phpt b/tests/response009.phpt deleted file mode 100644 index 9a0e23d..0000000 --- a/tests/response009.phpt +++ /dev/null @@ -1,39 +0,0 @@ ---TEST-- -http response stream cache negative ---SKIPIF-- - ---GET-- -a=b ---ENV-- -HTTP_IF_MODIFIED_SINCE=Fri, 13 Feb 2009 23:31:30 GMT -HTTP_IF_NONE_MATCH=0000-00-0000 ---FILE-- -setBody(new http\Message\Body(fopen(__FILE__,"rb"))); -$r->setEtag("abc"); -$r->setLastModified(1234567891); -$r->send($f); -rewind($f); -var_dump(stream_get_contents($f)); -?> ---EXPECTF-- -string(%d) "HTTP/1.1 200 OK%c -Accept-Ranges: bytes%c -X-Powered-By: %s%c -ETag: "abc"%c -Last-Modified: %s%c -%c -setBody(new http\Message\Body(fopen(__FILE__,"rb"))); -$r->setEtag("abc"); -$r->setLastModified(1234567891); -$r->send($f); -rewind($f); -var_dump(stream_get_contents($f)); -?> -" - diff --git a/tests/response010.phpt b/tests/response010.phpt deleted file mode 100644 index 8a3c1ae..0000000 --- a/tests/response010.phpt +++ /dev/null @@ -1,29 +0,0 @@ ---TEST-- -response stream ranges ---SKIPIF-- - ---ENV-- -HTTP_RANGE=bytes=2-4 ---GET-- -a=b ---FILE-- -setContentType("text/plain"); -$r->setContentDisposition( - array("attachment" => array(array("filename" => basename(__FILE__)))) -); -$r->setBody(new http\Message\Body(fopen(__FILE__, "rb"))); -$r->send($f); -rewind($f); -var_dump(stream_get_contents($f)); -?> ---EXPECTF-- -string(%i) "HTTP/1.1 206 Partial Content%c -Accept-Ranges: bytes%c -X-Powered-By: PHP/%s%c -Content-Type: text/plain%c -Content-Range: bytes 2-4/311%c -%c -php" diff --git a/tests/response011.phpt b/tests/response011.phpt deleted file mode 100644 index 701da22..0000000 --- a/tests/response011.phpt +++ /dev/null @@ -1,24 +0,0 @@ ---TEST-- -http response cache positive with env message ---SKIPIF-- - ---GET-- -dummy=1 ---FILE-- -setHeader("If-Modified-Since", "Fri, 13 Feb 2009 23:31:32 GMT"); -$r = new http\Env\Response; -$r->setEnvRequest($e); -$r->setBody(new http\Message\Body(fopen(__FILE__,"rb"))); -$r->setEtag("abc"); -$r->setLastModified(1234567891); -$r->isCachedByEtag("If-None-Match") and die("Huh? etag? really?\n"); -$r->isCachedByLastModified("If-Modified-Since") or die("yep, I should be cached"); -$r->send(); -?> ---EXPECTHEADERS-- -HTTP/1.1 304 Not Modified -ETag: "abc" -Last-Modified: Fri, 13 Feb 2009 23:31:31 GMT ---EXPECT-- diff --git a/tests/response012.phpt b/tests/response012.phpt deleted file mode 100644 index 5e9ed57..0000000 --- a/tests/response012.phpt +++ /dev/null @@ -1,29 +0,0 @@ ---TEST-- -response content disposition ---SKIPIF-- - ---GET-- -dummy=1 ---FILE-- -setContentDisposition(array("attachment"=>array("filename"=>basename(__FILE__)))); -$r->setBody(new http\Message\Body(fopen(__FILE__,"r"))); -$r->send(); - -?> ---EXPECTHEADERS-- -Content-Disposition: attachment;filename=response012.php ---EXPECT-- -setContentDisposition(array("attachment"=>array("filename"=>basename(__FILE__)))); -$r->setBody(new http\Message\Body(fopen(__FILE__,"r"))); -$r->send(); - -?> - diff --git a/tests/response013.phpt b/tests/response013.phpt deleted file mode 100644 index 75b4163..0000000 --- a/tests/response013.phpt +++ /dev/null @@ -1,29 +0,0 @@ ---TEST-- -response deflate ---SKIPIF-- - ---GET-- -dummy=1 ---FILE-- -setHeader("Accept-Encoding", "deflate"); - -$res = new http\Env\Response; -$res->setCacheControl("public, max-age=3600"); -$res->setContentEncoding(http\Env\Response::CONTENT_ENCODING_GZIP); -$res->getBody()->append("foobar"); - -$res->setEnvRequest($req); -$res->send(); -?> ---EXPECTHEADERS-- -Content-Encoding: deflate -Vary: Accept-Encoding -Cache-Control: public, max-age=3600 ---EXPECTREGEX-- -^\x78\x9c.+ - diff --git a/tests/response014.phpt b/tests/response014.phpt deleted file mode 100644 index 5478a61..0000000 --- a/tests/response014.phpt +++ /dev/null @@ -1,24 +0,0 @@ ---TEST-- -response invalid ranges ---SKIPIF-- - ---FILE-- -setHeader("Range", "bytes=321-123,123-0"); -$res = new http\Env\Response; -$res->getBody()->append("foobar"); -$res->setEnvRequest($req); -$res->send($f); -rewind($f); -var_dump(stream_get_contents($f)); ---EXPECTF-- -string(96) "HTTP/1.1 416 Requested Range Not Satisfiable -Accept-Ranges: bytes -Content-Range: bytes */6 - -" -