tests++
[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->update($line);
66 $data .= $dech->update("\r\n");
67 }
68 $this->assertFalse($dech->done());
69 }
70 $data .= $dech->update("0\r\n");
71 $this->assertTrue($dech->done());
72 $data .= $dech->finish();
73 $this->assertEquals(implode("", $file), $data);
74 }
75
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
82 )
83 )
84 );
85 $this->assertEquals($file,
86 http\Encoding\Stream\Inflate::decode(
87 http\Encoding\Stream\Deflate::encode(
88 $file, http\Encoding\Stream\Deflate::TYPE_ZLIB
89 )
90 )
91 );
92 $this->assertEquals($file,
93 http\Encoding\Stream\Inflate::decode(
94 http\Encoding\Stream\Deflate::encode(
95 $file, http\Encoding\Stream\Deflate::TYPE_RAW
96 )
97 )
98 );
99 }
100
101 function testZlibAutoFlush() {
102 $defl = new http\Encoding\Stream\Deflate(http\Encoding\Stream::FLUSH_FULL);
103 $infl = new http\Encoding\Stream\Inflate;
104
105 for ($f = fopen(__FILE__, "rb"); !feof($f); $data = fread($f, 0x100)) {
106 $infl = clone $infl;
107 $defl = clone $defl;
108 if (isset($data)) {
109 $this->assertEquals($data, $infl->update($defl->update($data)));
110 }
111 }
112
113 echo $infl->update($defl->finish());
114 echo $infl->finish();
115 }
116
117 function testZlibWithoutFlush() {
118 $defl = new http\Encoding\Stream\Deflate;
119 $infl = new http\Encoding\Stream\Inflate;
120 $file = file(__FILE__);
121 $data = "";
122 foreach ($file as $line) {
123 $infl = clone $infl;
124 $defl = clone $defl;
125 if (strlen($temp = $defl->update($line))) {
126 foreach(str_split($temp) as $byte) {
127 $data .= $infl->update($byte);
128 }
129 }
130 }
131 if (strlen($temp = $defl->finish())) {
132 $data .= $infl->update($temp);
133 }
134 $data .= $infl->finish();
135 $this->assertEquals(implode("", $file), $data);
136 }
137
138 function testZlibWithExplicitFlush() {
139 $defl = new http\Encoding\Stream\Deflate;
140 $infl = new http\Encoding\Stream\Inflate;
141 $file = file(__FILE__);
142 $data = "";
143 foreach ($file as $line) {
144 if (strlen($temp = $defl->update($line))) {
145 $data .= $infl->update($temp);
146 }
147 if (strlen($temp = $defl->flush())) {
148 $data .= $infl->update($temp);
149 }
150 $this->assertTrue($defl->done());
151 }
152 if (strlen($temp = $defl->finish())) {
153 $data .= $infl->update($temp);
154 }
155 $this->assertTrue($defl->done());
156 $data .= $infl->finish();
157 $this->assertTrue($infl->done());
158 $this->assertEquals(implode("", $file), $data);
159 }
160
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");
165 }
166 }