From: Michael Wallner Date: Wed, 9 Jul 2014 15:21:48 +0000 (+0200) Subject: unfold encoding tests X-Git-Tag: RELEASE_2_0_7~14 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=cfe10ae9729afca2fcd218c4d11fc9af58c18567;p=m6w6%2Fext-http unfold encoding tests --- diff --git a/phpunit/EncodingTest.php b/phpunit/EncodingTest.php deleted file mode 100644 index 4642613..0000000 --- a/phpunit/EncodingTest.php +++ /dev/null @@ -1,172 +0,0 @@ -assertEquals(implode("", $file), http\Encoding\Stream\Dechunk::decode($cenc)); - } - - function testChunkNoteEncoded() { - $s = "this is apparently not encodded\n"; - $this->assertEquals($s, @http\Encoding\Stream\Dechunk::decode($s)); - } - - function testChunkNotEncodedNotice() { - error_reporting(E_ALL); - $this->setExpectedException("PHPUnit_Framework_Error_Notice", - "Data does not seem to be chunked encoded"); - $s = "this is apparently not encodded\n"; - $this->assertEquals($s, http\Encoding\Stream\Dechunk::decode($s)); - } - - function testChunkNotEncodedFail() { - $s = "3\nis \nbetter than\n1\n"; - $this->assertNotEquals($s, @http\Encoding\Stream\Dechunk::decode($s)); - } - - function testChunkNotEncodedWarning1() { - $this->setExpectedException("PHPUnit_Framework_Error_Warning", - "Expected LF at pos 8 of 20 but got 0x74"); - $s = "3\nis \nbetter than\n1\n"; - http\Encoding\Stream\Dechunk::decode($s); - } - - function testChunkNotEncodedWarning2() { - $this->setExpectedException("PHPUnit_Framework_Error_Warning", - "Expected CRLF at pos 10 of 24 but got 0x74 0x74"); - $s = "3\r\nis \r\nbetter than\r\n1\r\n"; - http\Encoding\Stream\Dechunk::decode($s); - } - - function testChunkNotEncodedWarning3() { - $this->setExpectedException("PHPUnit_Framework_Error_Warning", - "Expected chunk size at pos 6 of 27 but got trash"); - $s = "3\nis \nreally better than\n1\n"; - http\Encoding\Stream\Dechunk::decode($s); - } - - function testChunkFlush() { - $dech = new http\Encoding\Stream\Dechunk(http\Encoding\Stream::FLUSH_FULL); - $file = file(__FILE__); - $data = ""; - foreach ($file as $i => $line) { - $dech = clone $dech; - if ($i % 2) { - $data .= $dech->update(sprintf("%lx\r\n%s\r\n", strlen($line), $line)); - } else { - $data .= $dech->update(sprintf("%lx\r\n", strlen($line))); - $data .= $dech->flush(); - $data .= $dech->update($line); - $data .= $dech->flush(); - $data .= $dech->update("\r\n"); - } - $dech->flush(); - $this->assertFalse($dech->done()); - } - $data .= $dech->update("0\r\n"); - $this->assertTrue($dech->done()); - $data .= $dech->finish(); - $this->assertEquals(implode("", $file), $data); - } - - function testZlibStatic() { - $file = file_get_contents(__FILE__); - $this->assertEquals($file, - http\Encoding\Stream\Inflate::decode( - http\Encoding\Stream\Deflate::encode( - $file, http\Encoding\Stream\Deflate::TYPE_GZIP - ) - ) - ); - $this->assertEquals($file, - http\Encoding\Stream\Inflate::decode( - http\Encoding\Stream\Deflate::encode( - $file, http\Encoding\Stream\Deflate::TYPE_ZLIB - ) - ) - ); - $this->assertEquals($file, - http\Encoding\Stream\Inflate::decode( - http\Encoding\Stream\Deflate::encode( - $file, http\Encoding\Stream\Deflate::TYPE_RAW - ) - ) - ); - } - - function testZlibAutoFlush() { - $defl = new http\Encoding\Stream\Deflate(http\Encoding\Stream::FLUSH_FULL); - $infl = new http\Encoding\Stream\Inflate; - - for ($f = fopen(__FILE__, "rb"); !feof($f); $data = fread($f, 0x100)) { - $infl = clone $infl; - $defl = clone $defl; - if (isset($data)) { - $this->assertEquals($data, $infl->update($defl->update($data))); - } - } - - echo $infl->update($defl->finish()); - echo $infl->finish(); - } - - function testZlibWithoutFlush() { - $defl = new http\Encoding\Stream\Deflate; - $infl = new http\Encoding\Stream\Inflate; - $file = file(__FILE__); - $data = ""; - foreach ($file as $line) { - $infl = clone $infl; - $defl = clone $defl; - if (strlen($temp = $defl->update($line))) { - foreach(str_split($temp) as $byte) { - $data .= $infl->update($byte); - } - } - } - if (strlen($temp = $defl->finish())) { - $data .= $infl->update($temp); - } - $data .= $infl->finish(); - $this->assertEquals(implode("", $file), $data); - } - - function testZlibWithExplicitFlush() { - $defl = new http\Encoding\Stream\Deflate; - $infl = new http\Encoding\Stream\Inflate; - $file = file(__FILE__); - $data = ""; - foreach ($file as $line) { - $data .= $infl->flush(); - if (strlen($temp = $defl->update($line))) { - $data .= $infl->update($temp); - $data .= $infl->flush(); - } - if (strlen($temp = $defl->flush())) { - $data .= $infl->update($temp); - $data .= $infl->flush(); - } - $this->assertTrue($defl->done()); - } - if (strlen($temp = $defl->finish())) { - $data .= $infl->update($temp); - } - $this->assertTrue($defl->done()); - $data .= $infl->finish(); - $this->assertTrue($infl->done()); - $this->assertEquals(implode("", $file), $data); - } - - function testInflateError() { - $this->setExpectedException("PHPUnit_Framework_Error_Warning", - "Could not inflate data: data error"); - http\Encoding\Stream\Inflate::decode("if this goes through, something's pretty wrong"); - } -} diff --git a/tests/encstream001.phpt b/tests/encstream001.phpt new file mode 100644 index 0000000..5dc13d3 --- /dev/null +++ b/tests/encstream001.phpt @@ -0,0 +1,27 @@ +--TEST-- +encoding stream chunked static +--SKIPIF-- + +--FILE-- + +DONE +--EXPECT-- +Test +bool(true) +DONE + diff --git a/tests/encstream002.phpt b/tests/encstream002.phpt new file mode 100644 index 0000000..3be0aca --- /dev/null +++ b/tests/encstream002.phpt @@ -0,0 +1,21 @@ +--TEST-- +encoding stream chunked not encoded +--SKIPIF-- + +--FILE-- + +DONE +--EXPECTF-- +Test + +Notice: http\Encoding\Stream\Dechunk::decode(): Data does not seem to be chunked encoded in %s on line %d +bool(true) +DONE diff --git a/tests/encstream003.phpt b/tests/encstream003.phpt new file mode 100644 index 0000000..ad8c737 --- /dev/null +++ b/tests/encstream003.phpt @@ -0,0 +1,38 @@ +--TEST-- +encoding stream chunked error +--SKIPIF-- + +--FILE-- + +DONE +--EXPECTF-- +Test + +Warning: http\Encoding\Stream\Dechunk::decode(): Expected LF at pos 8 of 20 but got 0x74 in %s on line %d + +Warning: http\Encoding\Stream\Dechunk::decode(): Truncated message: chunk size 190 exceeds remaining data size 11 at pos 9 of 20 in %s on line %d +string(14) "is ter than +1 +" + +Warning: http\Encoding\Stream\Dechunk::decode(): Expected CRLF at pos 10 of 24 but got 0x74 0x74 in %s on line %d + +Warning: http\Encoding\Stream\Dechunk::decode(): Truncated message: chunk size 190 exceeds remaining data size 12 at pos 12 of 24 in %s on line %d +string(15) "is er than +1 +" + +Warning: http\Encoding\Stream\Dechunk::decode(): Expected chunk size at pos 6 of 27 but got trash in %s on line %d +bool(false) +DONE diff --git a/tests/encstream004.phpt b/tests/encstream004.phpt new file mode 100644 index 0000000..eb9da0f --- /dev/null +++ b/tests/encstream004.phpt @@ -0,0 +1,38 @@ +--TEST-- +encoding stream chunked flush +--SKIPIF-- + +--FILE-- + $line) { + $dech = clone $dech; + if ($i % 2) { + $data .= $dech->update(sprintf("%lx\r\n%s\r\n", strlen($line), $line)); + } else { + $data .= $dech->update(sprintf("%lx\r\n", strlen($line))); + $data .= $dech->flush(); + $data .= $dech->update($line); + $data .= $dech->flush(); + $data .= $dech->update("\r\n"); + } + $dech->flush(); + $dech->done() and printf("uh-oh done() reported true!\n"); +} +$data .= $dech->update("0\r\n"); +var_dump($dech->done()); +$data .= $dech->finish(); +var_dump(implode("", $file) === $data); +?> +DONE +--EXPECT-- +Test +bool(true) +bool(true) +DONE diff --git a/tests/encstream005.phpt b/tests/encstream005.phpt new file mode 100644 index 0000000..6455ed4 --- /dev/null +++ b/tests/encstream005.phpt @@ -0,0 +1,41 @@ +--TEST-- +encoding stream zlib static +--SKIPIF-- + +--FILE-- + +DONE +--EXPECT-- +Test +bool(true) +bool(true) +bool(true) +DONE diff --git a/tests/encstream006.phpt b/tests/encstream006.phpt new file mode 100644 index 0000000..5430bfa --- /dev/null +++ b/tests/encstream006.phpt @@ -0,0 +1,30 @@ +--TEST-- +encoding stream zlib auto flush +--SKIPIF-- + +--FILE-- +update($defl->update($data))) { + printf("uh-oh »%s« != »%s«\n", $data, $d); + } + } +} + +echo $infl->update($defl->finish()); +echo $infl->finish(); +?> +DONE +--EXPECT-- +Test +DONE diff --git a/tests/encstream007.phpt b/tests/encstream007.phpt new file mode 100644 index 0000000..5fdeffe --- /dev/null +++ b/tests/encstream007.phpt @@ -0,0 +1,34 @@ +--TEST-- +encoding stream zlib without flush +--SKIPIF-- + +--FILE-- +update($line))) { + foreach(str_split($temp) as $byte) { + $data .= $infl->update($byte); + } + } +} +if (strlen($temp = $defl->finish())) { + $data .= $infl->update($temp); +} +$data .= $infl->finish(); +var_dump(implode("", $file) === $data); +?> +DONE +--EXPECT-- +Test +bool(true) +DONE diff --git a/tests/encstream008.phpt b/tests/encstream008.phpt new file mode 100644 index 0000000..a46f9f0 --- /dev/null +++ b/tests/encstream008.phpt @@ -0,0 +1,42 @@ +--TEST-- +encoding stream zlib with explicit flush +--SKIPIF-- + +--FILE-- +flush(); + if (strlen($temp = $defl->update($line))) { + $data .= $infl->update($temp); + $data .= $infl->flush(); + } + if (strlen($temp = $defl->flush())) { + $data .= $infl->update($temp); + $data .= $infl->flush(); + } + $defl->done() or printf("uh-oh stream's not done yet!\n"); +} +if (strlen($temp = $defl->finish())) { + $data .= $infl->update($temp); +} +var_dump($defl->done()); +$data .= $infl->finish(); +var_dump($infl->done()); +var_dump(implode("", $file) === $data); + +?> +DONE +--EXPECT-- +Test +bool(true) +bool(true) +bool(true) +DONE diff --git a/tests/encstream009.phpt b/tests/encstream009.phpt new file mode 100644 index 0000000..fa51a8a --- /dev/null +++ b/tests/encstream009.phpt @@ -0,0 +1,20 @@ +--TEST-- +encoding stream zlib error +--SKIPIF-- + +--FILE-- + +DONE +--EXPECTF-- +Test + +Warning: http\Encoding\Stream\Inflate::decode(): Could not inflate data: data error in %s on line %d +bool(false) +DONE