encoding tests & fixes
[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 $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);
26 }
27
28 function testChunkNotEncodedFail() {
29 $s = "3\nis \nbetter than\n1\n";
30 $this->assertNotEquals($s, @http\Encoding\Stream\Dechunk::decode($s));
31 }
32
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);
38 }
39
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);
45 }
46
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);
52 }
53
54 function testChunkFlush() {
55 $dech = new http\Encoding\Stream\Dechunk(http\Encoding\Stream::FLUSH_FULL);
56 $file = file(__FILE__);
57 $data = "";
58 foreach ($file as $i => $line) {
59 $dech = clone $dech;
60 if ($i % 2) {
61 $data .= $dech->update(sprintf("%lx\r\n%s\r\n", strlen($line), $line));
62 } else {
63 $data .= $dech->update(sprintf("%lx\r\n", strlen($line)));
64 $data .= $dech->update($line);
65 $data .= $dech->update("\r\n");
66 }
67 $this->assertFalse($dech->done());
68 }
69 $data .= $dech->update("0\r\n");
70 $this->assertTrue($dech->done());
71 $data .= $dech->finish();
72 $this->assertEquals(implode("", $file), $data);
73 }
74
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
81 )
82 )
83 );
84 $this->assertEquals($file,
85 http\Encoding\Stream\Inflate::decode(
86 http\Encoding\Stream\Deflate::encode(
87 $file, http\Encoding\Stream\Deflate::TYPE_ZLIB
88 )
89 )
90 );
91 $this->assertEquals($file,
92 http\Encoding\Stream\Inflate::decode(
93 http\Encoding\Stream\Deflate::encode(
94 $file, http\Encoding\Stream\Deflate::TYPE_RAW
95 )
96 )
97 );
98 }
99
100 function testZlibAutoFlush() {
101 $defl = new http\Encoding\Stream\Deflate(http\Encoding\Stream::FLUSH_FULL);
102 $infl = new http\Encoding\Stream\Inflate;
103
104 for ($f = fopen(__FILE__, "rb"); !feof($f); $data = fread($f, 0x100)) {
105 $infl = clone $infl;
106 $defl = clone $defl;
107 if (isset($data)) {
108 $this->assertEquals($data, $infl->update($defl->update($data)));
109 }
110 }
111
112 echo $infl->update($defl->finish());
113 echo $infl->finish();
114 }
115
116 function testZlibWithoutFlush() {
117 $defl = new http\Encoding\Stream\Deflate;
118 $infl = new http\Encoding\Stream\Inflate;
119 $file = file(__FILE__);
120 $data = "";
121 foreach ($file as $line) {
122 $infl = clone $infl;
123 $defl = clone $defl;
124 if (strlen($temp = $defl->update($line))) {
125 foreach(str_split($temp) as $byte) {
126 $data .= $infl->update($byte);
127 }
128 }
129 }
130 if (strlen($temp = $defl->finish())) {
131 $data .= $infl->update($temp);
132 }
133 $data .= $infl->finish();
134 $this->assertEquals(implode("", $file), $data);
135 }
136
137 function testZlibWithExplicitFlush() {
138 $defl = new http\Encoding\Stream\Deflate;
139 $infl = new http\Encoding\Stream\Inflate;
140 $file = file(__FILE__);
141 $data = "";
142 foreach ($file as $line) {
143 if (strlen($temp = $defl->update($line))) {
144 $data .= $infl->update($temp);
145 }
146 if (strlen($temp = $defl->flush())) {
147 $data .= $infl->update($temp);
148 }
149 $this->assertTrue($defl->done());
150 }
151 if (strlen($temp = $defl->finish())) {
152 $data .= $infl->update($temp);
153 }
154 $this->assertTrue($defl->done());
155 $data .= $infl->finish();
156 $this->assertTrue($infl->done());
157 $this->assertEquals(implode("", $file), $data);
158 }
159
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");
164 }
165 }