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 $this->setExpectedException("PHPUnit_Framework_Error_Notice",
23 "Data does not seem to be chunked encoded");
24 $s = "this is apparently not encodded\n";
25 http\Encoding\Stream\Dechunk
::decode($s);
28 function testChunkNotEncodedFail() {
29 $s = "3\nis \nbetter than\n1\n";
30 $this->assertNotEquals($s, @http\Encoding\Stream\Dechunk
::decode($s));
33 function testChunkNotEncodedWarning1() {
34 $this->setExpectedException("PHPUnit_Framework_Error_Warning",
35 "Expected LF at pos 8 of 20 but got 0x74");
36 $s = "3\nis \nbetter than\n1\n";
37 http\Encoding\Stream\Dechunk
::decode($s);
40 function testChunkNotEncodedWarning2() {
41 $this->setExpectedException("PHPUnit_Framework_Error_Warning",
42 "Expected CRLF at pos 10 of 24 but got 0x74 0x74");
43 $s = "3\r\nis \r\nbetter than\r\n1\r\n";
44 http\Encoding\Stream\Dechunk
::decode($s);
47 function testChunkNotEncodedWarning3() {
48 $this->setExpectedException("PHPUnit_Framework_Error_Warning",
49 "Expected chunk size at pos 6 of 27 but got trash");
50 $s = "3\nis \nreally better than\n1\n";
51 http\Encoding\Stream\Dechunk
::decode($s);
54 function testChunkFlush() {
55 $dech = new http\Encoding\Stream\
Dechunk(http\Encoding\Stream
::FLUSH_FULL
);
56 $file = file(__FILE__
);
58 foreach ($file as $i => $line) {
61 $data .= $dech->update(sprintf("%lx\r\n%s\r\n", strlen($line), $line));
63 $data .= $dech->update(sprintf("%lx\r\n", strlen($line)));
64 $data .= $dech->update($line);
65 $data .= $dech->update("\r\n");
67 $this->assertFalse($dech->done());
69 $data .= $dech->update("0\r\n");
70 $this->assertTrue($dech->done());
71 $data .= $dech->finish();
72 $this->assertEquals(implode("", $file), $data);
75 function testZlibStatic() {
76 $file = file_get_contents(__FILE__
);
77 $this->assertEquals($file,
78 http\Encoding\Stream\Inflate
::decode(
79 http\Encoding\Stream\Deflate
::encode(
80 $file, http\Encoding\Stream\Deflate
::TYPE_GZIP
84 $this->assertEquals($file,
85 http\Encoding\Stream\Inflate
::decode(
86 http\Encoding\Stream\Deflate
::encode(
87 $file, http\Encoding\Stream\Deflate
::TYPE_ZLIB
91 $this->assertEquals($file,
92 http\Encoding\Stream\Inflate
::decode(
93 http\Encoding\Stream\Deflate
::encode(
94 $file, http\Encoding\Stream\Deflate
::TYPE_RAW
100 function testZlibAutoFlush() {
101 $defl = new http\Encoding\Stream\
Deflate(http\Encoding\Stream
::FLUSH_FULL
);
102 $infl = new http\Encoding\Stream\Inflate
;
104 for ($f = fopen(__FILE__
, "rb"); !feof($f); $data = fread($f, 0x100)) {
108 $this->assertEquals($data, $infl->update($defl->update($data)));
112 echo $infl->update($defl->finish());
113 echo $infl->finish();
116 function testZlibWithoutFlush() {
117 $defl = new http\Encoding\Stream\Deflate
;
118 $infl = new http\Encoding\Stream\Inflate
;
119 $file = file(__FILE__
);
121 foreach ($file as $line) {
124 if (strlen($temp = $defl->update($line))) {
125 foreach(str_split($temp) as $byte) {
126 $data .= $infl->update($byte);
130 if (strlen($temp = $defl->finish())) {
131 $data .= $infl->update($temp);
133 $data .= $infl->finish();
134 $this->assertEquals(implode("", $file), $data);
137 function testZlibWithExplicitFlush() {
138 $defl = new http\Encoding\Stream\Deflate
;
139 $infl = new http\Encoding\Stream\Inflate
;
140 $file = file(__FILE__
);
142 foreach ($file as $line) {
143 if (strlen($temp = $defl->update($line))) {
144 $data .= $infl->update($temp);
146 if (strlen($temp = $defl->flush())) {
147 $data .= $infl->update($temp);
149 $this->assertTrue($defl->done());
151 if (strlen($temp = $defl->finish())) {
152 $data .= $infl->update($temp);
154 $this->assertTrue($defl->done());
155 $data .= $infl->finish();
156 $this->assertTrue($infl->done());
157 $this->assertEquals(implode("", $file), $data);
160 function testInflateError() {
161 $this->setExpectedException("PHPUnit_Framework_Error_Warning",
162 "Could not inflate data: data error");
163 http\Encoding\Stream\Inflate
::decode("if this goes through, something's pretty wrong");