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->update($line);
66 $data .= $dech->update("\r\n");
68 $this->assertFalse($dech->done());
70 $data .= $dech->update("0\r\n");
71 $this->assertTrue($dech->done());
72 $data .= $dech->finish();
73 $this->assertEquals(implode("", $file), $data);
76 function testZlibStatic() {
77 $file = file_get_contents(__FILE__
);
78 $this->assertEquals($file,
79 http\Encoding\Stream\Inflate
::decode(
80 http\Encoding\Stream\Deflate
::encode(
81 $file, http\Encoding\Stream\Deflate
::TYPE_GZIP
85 $this->assertEquals($file,
86 http\Encoding\Stream\Inflate
::decode(
87 http\Encoding\Stream\Deflate
::encode(
88 $file, http\Encoding\Stream\Deflate
::TYPE_ZLIB
92 $this->assertEquals($file,
93 http\Encoding\Stream\Inflate
::decode(
94 http\Encoding\Stream\Deflate
::encode(
95 $file, http\Encoding\Stream\Deflate
::TYPE_RAW
101 function testZlibAutoFlush() {
102 $defl = new http\Encoding\Stream\
Deflate(http\Encoding\Stream
::FLUSH_FULL
);
103 $infl = new http\Encoding\Stream\Inflate
;
105 for ($f = fopen(__FILE__
, "rb"); !feof($f); $data = fread($f, 0x100)) {
109 $this->assertEquals($data, $infl->update($defl->update($data)));
113 echo $infl->update($defl->finish());
114 echo $infl->finish();
117 function testZlibWithoutFlush() {
118 $defl = new http\Encoding\Stream\Deflate
;
119 $infl = new http\Encoding\Stream\Inflate
;
120 $file = file(__FILE__
);
122 foreach ($file as $line) {
125 if (strlen($temp = $defl->update($line))) {
126 foreach(str_split($temp) as $byte) {
127 $data .= $infl->update($byte);
131 if (strlen($temp = $defl->finish())) {
132 $data .= $infl->update($temp);
134 $data .= $infl->finish();
135 $this->assertEquals(implode("", $file), $data);
138 function testZlibWithExplicitFlush() {
139 $defl = new http\Encoding\Stream\Deflate
;
140 $infl = new http\Encoding\Stream\Inflate
;
141 $file = file(__FILE__
);
143 foreach ($file as $line) {
144 if (strlen($temp = $defl->update($line))) {
145 $data .= $infl->update($temp);
147 if (strlen($temp = $defl->flush())) {
148 $data .= $infl->update($temp);
150 $this->assertTrue($defl->done());
152 if (strlen($temp = $defl->finish())) {
153 $data .= $infl->update($temp);
155 $this->assertTrue($defl->done());
156 $data .= $infl->finish();
157 $this->assertTrue($infl->done());
158 $this->assertEquals(implode("", $file), $data);
161 function testInflateError() {
162 $this->setExpectedException("PHPUnit_Framework_Error_Warning",
163 "Could not inflate data: data error");
164 http\Encoding\Stream\Inflate
::decode("if this goes through, something's pretty wrong");