let DEV_2 be trunk
[m6w6/ext-http] / phpunit / EncodingTest.php
1 <?php
2
3 class EncodingStreamTest extends PHPUnit_Framework_TestCase {
4 function testChunkStatic() {
5 $file = file(__FILE__);
6 $cenc = array_reduce(
7 $file,
8 function($data, $line) {
9 return $data . sprintf("%lx\r\n%s\r\n", strlen($line), $line);
10 }
11 ) . "0\r\n";
12
13 $this->assertEquals(implode("", $file), http\Encoding\Stream\Dechunk::decode($cenc));
14 }
15
16 function testChunkNoteEncoded() {
17 $s = "this is apparently not encodded\n";
18 $this->assertEquals($s, @http\Encoding\Stream\Dechunk::decode($s));
19 }
20
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));
27 }
28
29 function testChunkNotEncodedFail() {
30 $s = "3\nis \nbetter than\n1\n";
31 $this->assertNotEquals($s, @http\Encoding\Stream\Dechunk::decode($s));
32 }
33
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);
39 }
40
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);
46 }
47
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);
53 }
54
55 function testChunkFlush() {
56 $dech = new http\Encoding\Stream\Dechunk(http\Encoding\Stream::FLUSH_FULL);
57 $file = file(__FILE__);
58 $data = "";
59 foreach ($file as $i => $line) {
60 $dech = clone $dech;
61 if ($i % 2) {
62 $data .= $dech->update(sprintf("%lx\r\n%s\r\n", strlen($line), $line));
63 } else {
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");
69 }
70 $dech->flush();
71 $this->assertFalse($dech->done());
72 }
73 $data .= $dech->update("0\r\n");
74 $this->assertTrue($dech->done());
75 $data .= $dech->finish();
76 $this->assertEquals(implode("", $file), $data);
77 }
78
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
85 )
86 )
87 );
88 $this->assertEquals($file,
89 http\Encoding\Stream\Inflate::decode(
90 http\Encoding\Stream\Deflate::encode(
91 $file, http\Encoding\Stream\Deflate::TYPE_ZLIB
92 )
93 )
94 );
95 $this->assertEquals($file,
96 http\Encoding\Stream\Inflate::decode(
97 http\Encoding\Stream\Deflate::encode(
98 $file, http\Encoding\Stream\Deflate::TYPE_RAW
99 )
100 )
101 );
102 }
103
104 function testZlibAutoFlush() {
105 $defl = new http\Encoding\Stream\Deflate(http\Encoding\Stream::FLUSH_FULL);
106 $infl = new http\Encoding\Stream\Inflate;
107
108 for ($f = fopen(__FILE__, "rb"); !feof($f); $data = fread($f, 0x100)) {
109 $infl = clone $infl;
110 $defl = clone $defl;
111 if (isset($data)) {
112 $this->assertEquals($data, $infl->update($defl->update($data)));
113 }
114 }
115
116 echo $infl->update($defl->finish());
117 echo $infl->finish();
118 }
119
120 function testZlibWithoutFlush() {
121 $defl = new http\Encoding\Stream\Deflate;
122 $infl = new http\Encoding\Stream\Inflate;
123 $file = file(__FILE__);
124 $data = "";
125 foreach ($file as $line) {
126 $infl = clone $infl;
127 $defl = clone $defl;
128 if (strlen($temp = $defl->update($line))) {
129 foreach(str_split($temp) as $byte) {
130 $data .= $infl->update($byte);
131 }
132 }
133 }
134 if (strlen($temp = $defl->finish())) {
135 $data .= $infl->update($temp);
136 }
137 $data .= $infl->finish();
138 $this->assertEquals(implode("", $file), $data);
139 }
140
141 function testZlibWithExplicitFlush() {
142 $defl = new http\Encoding\Stream\Deflate;
143 $infl = new http\Encoding\Stream\Inflate;
144 $file = file(__FILE__);
145 $data = "";
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();
151 }
152 if (strlen($temp = $defl->flush())) {
153 $data .= $infl->update($temp);
154 $data .= $infl->flush();
155 }
156 $this->assertTrue($defl->done());
157 }
158 if (strlen($temp = $defl->finish())) {
159 $data .= $infl->update($temp);
160 }
161 $this->assertTrue($defl->done());
162 $data .= $infl->finish();
163 $this->assertTrue($infl->done());
164 $this->assertEquals(implode("", $file), $data);
165 }
166
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");
171 }
172 }