3 class EncodingStreamTest
extends PHPUnit_Framework_TestCase
{
4 function testChunkStatic() {
5 $file = file(__FILE__
);
8 function($data, $line) {
9 return $data . sprintf("%lx\r\n%s\r\n", strlen($line), $line);
13 $this->assertEquals(implode("", $file), http\Encoding\Stream\Dechunk
::decode($cenc));
16 function testChunkNoteEncoded() {
17 $s = "this is apparently not encodded\n";
18 $this->assertEquals($s, @http\Encoding\Stream\Dechunk
::decode($s));
21 function testChunkNotEncodedNotice() {
22 error_reporting(E_ALL
);
23 $this->setExpectedException("PHPUnit_Framework_Error_Notice",
24 "Data does not seem to be chunked encoded");
25 $s = "this is apparently not encodded\n";
26 $this->assertEquals($s, http\Encoding\Stream\Dechunk
::decode($s));
29 function testChunkNotEncodedFail() {
30 $s = "3\nis \nbetter than\n1\n";
31 $this->assertNotEquals($s, @http\Encoding\Stream\Dechunk
::decode($s));
34 function testChunkNotEncodedWarning1() {
35 $this->setExpectedException("PHPUnit_Framework_Error_Warning",
36 "Expected LF at pos 8 of 20 but got 0x74");
37 $s = "3\nis \nbetter than\n1\n";
38 http\Encoding\Stream\Dechunk
::decode($s);
41 function testChunkNotEncodedWarning2() {
42 $this->setExpectedException("PHPUnit_Framework_Error_Warning",
43 "Expected CRLF at pos 10 of 24 but got 0x74 0x74");
44 $s = "3\r\nis \r\nbetter than\r\n1\r\n";
45 http\Encoding\Stream\Dechunk
::decode($s);
48 function testChunkNotEncodedWarning3() {
49 $this->setExpectedException("PHPUnit_Framework_Error_Warning",
50 "Expected chunk size at pos 6 of 27 but got trash");
51 $s = "3\nis \nreally better than\n1\n";
52 http\Encoding\Stream\Dechunk
::decode($s);
55 function testChunkFlush() {
56 $dech = new http\Encoding\Stream\
Dechunk(http\Encoding\Stream
::FLUSH_FULL
);
57 $file = file(__FILE__
);
59 foreach ($file as $i => $line) {
62 $data .= $dech->update(sprintf("%lx\r\n%s\r\n", strlen($line), $line));
64 $data .= $dech->update(sprintf("%lx\r\n", strlen($line)));
65 $data .= $dech->flush();
66 $data .= $dech->update($line);
67 $data .= $dech->flush();
68 $data .= $dech->update("\r\n");
71 $this->assertFalse($dech->done());
73 $data .= $dech->update("0\r\n");
74 $this->assertTrue($dech->done());
75 $data .= $dech->finish();
76 $this->assertEquals(implode("", $file), $data);
79 function testZlibStatic() {
80 $file = file_get_contents(__FILE__
);
81 $this->assertEquals($file,
82 http\Encoding\Stream\Inflate
::decode(
83 http\Encoding\Stream\Deflate
::encode(
84 $file, http\Encoding\Stream\Deflate
::TYPE_GZIP
88 $this->assertEquals($file,
89 http\Encoding\Stream\Inflate
::decode(
90 http\Encoding\Stream\Deflate
::encode(
91 $file, http\Encoding\Stream\Deflate
::TYPE_ZLIB
95 $this->assertEquals($file,
96 http\Encoding\Stream\Inflate
::decode(
97 http\Encoding\Stream\Deflate
::encode(
98 $file, http\Encoding\Stream\Deflate
::TYPE_RAW
104 function testZlibAutoFlush() {
105 $defl = new http\Encoding\Stream\
Deflate(http\Encoding\Stream
::FLUSH_FULL
);
106 $infl = new http\Encoding\Stream\Inflate
;
108 for ($f = fopen(__FILE__
, "rb"); !feof($f); $data = fread($f, 0x100)) {
112 $this->assertEquals($data, $infl->update($defl->update($data)));
116 echo $infl->update($defl->finish());
117 echo $infl->finish();
120 function testZlibWithoutFlush() {
121 $defl = new http\Encoding\Stream\Deflate
;
122 $infl = new http\Encoding\Stream\Inflate
;
123 $file = file(__FILE__
);
125 foreach ($file as $line) {
128 if (strlen($temp = $defl->update($line))) {
129 foreach(str_split($temp) as $byte) {
130 $data .= $infl->update($byte);
134 if (strlen($temp = $defl->finish())) {
135 $data .= $infl->update($temp);
137 $data .= $infl->finish();
138 $this->assertEquals(implode("", $file), $data);
141 function testZlibWithExplicitFlush() {
142 $defl = new http\Encoding\Stream\Deflate
;
143 $infl = new http\Encoding\Stream\Inflate
;
144 $file = file(__FILE__
);
146 foreach ($file as $line) {
147 $data .= $infl->flush();
148 if (strlen($temp = $defl->update($line))) {
149 $data .= $infl->update($temp);
150 $data .= $infl->flush();
152 if (strlen($temp = $defl->flush())) {
153 $data .= $infl->update($temp);
154 $data .= $infl->flush();
156 $this->assertTrue($defl->done());
158 if (strlen($temp = $defl->finish())) {
159 $data .= $infl->update($temp);
161 $this->assertTrue($defl->done());
162 $data .= $infl->finish();
163 $this->assertTrue($infl->done());
164 $this->assertEquals(implode("", $file), $data);
167 function testInflateError() {
168 $this->setExpectedException("PHPUnit_Framework_Error_Warning",
169 "Could not inflate data: data error");
170 http\Encoding\Stream\Inflate
::decode("if this goes through, something's pretty wrong");