X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=phpunit%2FMessageTest.php;h=485d431e4cfb0b037ce45dc4c587a7f8841e6acf;hb=87702149e0c422104aec0ddb7391f91392be0376;hp=6f425245f413f8365fb2fa76f725b7ecc3caa5e8;hpb=05ae9a5d01e29d7174152b1b46a5af09833d71ef;p=m6w6%2Fext-http diff --git a/phpunit/MessageTest.php b/phpunit/MessageTest.php index 6f42524..485d431 100644 --- a/phpunit/MessageTest.php +++ b/phpunit/MessageTest.php @@ -46,6 +46,13 @@ class MessageTest extends PHPUnit_Framework_TestCase $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()); @@ -155,5 +162,122 @@ class MessageTest extends PHPUnit_Framework_TestCase $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_Warning"); + $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.=$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 + ); + } }