From: Michael Wallner Date: Thu, 10 Jul 2014 11:07:52 +0000 (+0200) Subject: unfold message body tests X-Git-Tag: RELEASE_2_0_7~11 X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=commitdiff_plain;h=5b46115fea57ad7d842f9c713ca854da24d842cd unfold message body tests --- diff --git a/phpunit/MessageBodyTest.php b/phpunit/MessageBodyTest.php deleted file mode 100644 index 3aaf3eb..0000000 --- a/phpunit/MessageBodyTest.php +++ /dev/null @@ -1,135 +0,0 @@ -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( - (object) array( - "size" => 0, - "mtime" => 0, - "atime" => 0, - "ctime" => 0, - ), - $this->temp->stat() - ); - } - - function testAppendError() { - $this->setExpectedException("http\Exception\RuntimeException"); - $this->file->append("nope"); - } - function testAppend() { - $this->temp->append("yes"); - } - - function testAddForm() { - $this->temp->addForm( - 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: form-data; 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 testAddPart() { - $this->temp->addPart(new http\Message("This: is a header\n\nand this is the data\n")); - $this->assertStringMatchesFormat( - "--%x.%x\r\n". - "This: is a header\r\n". - "Content-Length: 21\r\n". - "\r\n". - "and this is the data\n\r\n". - "--%x.%x--\r\n". - "", - str_replace("\r", "", $this->temp) - ); - } - - function testEtag() { - $s = stat(__FILE__); - $this->assertEquals( - sprintf( - "%lx-%lx-%lx", - $s["ino"],$s["mtime"],$s["size"] - ), - $this->file->etag() - ); - $this->assertEquals(crc32(""), $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); - } - - function testClone() { - $this->assertEquals((string) $this->file, (string) clone $this->file); - } - - function testGetResource() { - $stream = $this->file->getResource(); - $this->assertTrue(is_resource($stream)); - $stat = fstat($stream); - $this->assertEquals(filesize(__FILE__), $stat["size"]); - } -} diff --git a/tests/messagebody001.phpt b/tests/messagebody001.phpt new file mode 100644 index 0000000..6f1e10a --- /dev/null +++ b/tests/messagebody001.phpt @@ -0,0 +1,34 @@ +--TEST-- +message body stat +--SKIPIF-- + +--FILE-- +stat("size")); +var_dump(filemtime(__FILE__) === $file->stat("mtime")); +var_dump(fileatime(__FILE__) === $file->stat("atime")); +var_dump(filectime(__FILE__) === $file->stat("ctime")); +var_dump( + (object) array( + "size" => 0, + "mtime" => 0, + "atime" => 0, + "ctime" => 0, + ) == $temp->stat() +); +?> +DONE +--EXPECT-- +Test +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +DONE diff --git a/tests/messagebody002.phpt b/tests/messagebody002.phpt new file mode 100644 index 0000000..fe28e28 --- /dev/null +++ b/tests/messagebody002.phpt @@ -0,0 +1,21 @@ +--TEST-- +message body append +--SKIPIF-- + +--FILE-- +append("yes"); + +var_dump((string) $temp); + +?> +DONE +--EXPECT-- +Test +string(3) "yes" +DONE diff --git a/tests/messagebody003.phpt b/tests/messagebody003.phpt new file mode 100644 index 0000000..d6f9ce1 --- /dev/null +++ b/tests/messagebody003.phpt @@ -0,0 +1,26 @@ +--TEST-- +message body append error +--SKIPIF-- + +--FILE-- +append("nope"); +} catch (Exception $e) { + echo $e, "\n"; +} + +?> +DONE +--EXPECTF-- +Test +exception 'http\Exception\RuntimeException' with message 'http\Message\Body::append(): Failed to append 4 bytes to body; wrote 0' in /home/mike/src/ext-http.git/tests/messagebody003.php:6 +Stack trace: +#0 %s(%d): http\Message\Body->append('nope') +#1 {main} +DONE diff --git a/tests/messagebody004.phpt b/tests/messagebody004.phpt new file mode 100644 index 0000000..64254d4 --- /dev/null +++ b/tests/messagebody004.phpt @@ -0,0 +1,81 @@ +--TEST-- +message body add form +--SKIPIF-- + +--FILE-- +addForm( + array( + "foo" => "bar", + "more" => array( + "bah", "baz", "fuz" + ), + ), + array( + array( + "file" => __FILE__, + "name" => "upload", + "type" => "text/plain", + ) + ) +); + +echo $temp; + +?> +DONE +--EXPECTF-- +Test +--%x.%x +Content-Disposition: form-data; name="foo" + +bar +--%x.%x +Content-Disposition: form-data; name="more[0]" + +bah +--%x.%x +Content-Disposition: form-data; name="more[1]" + +baz +--%x.%x +Content-Disposition: form-data; name="more[2]" + +fuz +--%x.%x +Content-Disposition: form-data; name="upload"; filename="%s" +Content-Transfer-Encoding: binary +Content-Type: text/plain + +addForm( + array( + "foo" => "bar", + "more" => array( + "bah", "baz", "fuz" + ), + ), + array( + array( + "file" => __FILE__, + "name" => "upload", + "type" => "text/plain", + ) + ) +); + +echo $temp; + +?> +DONE + +--%x.%x-- +DONE diff --git a/tests/messagebody005.phpt b/tests/messagebody005.phpt new file mode 100644 index 0000000..2d1d8b2 --- /dev/null +++ b/tests/messagebody005.phpt @@ -0,0 +1,26 @@ +--TEST-- +message body add part +--SKIPIF-- + +--FILE-- +addPart(new http\Message("This: is a header\n\nand this is the data\n")); +echo $temp; + +?> +DONE +--EXPECTF-- +Test +--%x.%x +This: is a header +Content-Length: 21 + +and this is the data + +--%x.%x-- +DONE diff --git a/tests/messagebody006.phpt b/tests/messagebody006.phpt new file mode 100644 index 0000000..a02057e --- /dev/null +++ b/tests/messagebody006.phpt @@ -0,0 +1,30 @@ +--TEST-- +message body etag +--SKIPIF-- + +--INI-- +http.etag.mode = crc32b +--FILE-- +etag() +); +var_dump($temp->etag()); + +?> +DONE +--EXPECT-- +Test +bool(true) +string(8) "00000000" +DONE diff --git a/tests/messagebody007.phpt b/tests/messagebody007.phpt new file mode 100644 index 0000000..dc010da --- /dev/null +++ b/tests/messagebody007.phpt @@ -0,0 +1,21 @@ +--TEST-- +message body to stream +--SKIPIF-- + +--FILE-- +toStream($f = fopen("php://temp", "w")); +fseek($f, 0, SEEK_SET); +var_dump(file_get_contents(__FILE__) === stream_get_contents($f)); + +?> +DONE +--EXPECT-- +Test +bool(true) +DONE diff --git a/tests/messagebody008.phpt b/tests/messagebody008.phpt new file mode 100644 index 0000000..cb9612c --- /dev/null +++ b/tests/messagebody008.phpt @@ -0,0 +1,23 @@ +--TEST-- +message body to callback +--SKIPIF-- + +--FILE-- +toCallback( + function($body, $string) use (&$s) { $s.=$string; } +); +var_dump($s === (string) $file); + +?> +DONE +--EXPECT-- +Test +bool(true) +DONE diff --git a/tests/messagebody009.phpt b/tests/messagebody009.phpt new file mode 100644 index 0000000..a7a7f43 --- /dev/null +++ b/tests/messagebody009.phpt @@ -0,0 +1,19 @@ +--TEST-- +message body clone +--SKIPIF-- + +--FILE-- + +DONE +--EXPECT-- +Test +bool(true) +DONE diff --git a/tests/messagebody010.phpt b/tests/messagebody010.phpt new file mode 100644 index 0000000..b50898d --- /dev/null +++ b/tests/messagebody010.phpt @@ -0,0 +1,23 @@ +--TEST-- +message body resource +--SKIPIF-- + +--FILE-- +getResource(); +var_dump(is_resource($stream)); +$stat = fstat($stream); +var_dump(filesize(__FILE__) === $stat["size"]); + +?> +DONE +--EXPECT-- +Test +bool(true) +bool(true) +DONE