File: EncodingTest.php

package info (click to toggle)
php-pecl-http 2.0.4-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,436 kB
  • ctags: 1,839
  • sloc: ansic: 15,322; php: 726; xml: 254; makefile: 10; pascal: 3
file content (172 lines) | stat: -rw-r--r-- 6,078 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php

class EncodingStreamTest extends PHPUnit_Framework_TestCase {
    function testChunkStatic() {
        $file = file(__FILE__);
        $cenc = array_reduce(
            $file,
            function($data, $line) {
                return $data . sprintf("%lx\r\n%s\r\n", strlen($line), $line);
            }
        ) . "0\r\n";

        $this->assertEquals(implode("", $file), http\Encoding\Stream\Dechunk::decode($cenc));
    }

    function testChunkNoteEncoded() {
        $s = "this is apparently not encodded\n";
        $this->assertEquals($s, @http\Encoding\Stream\Dechunk::decode($s));
    }

    function testChunkNotEncodedNotice() {
	    error_reporting(E_ALL);
        $this->setExpectedException("PHPUnit_Framework_Error_Notice", 
            "Data does not seem to be chunked encoded");
        $s = "this is apparently not encodded\n";
        $this->assertEquals($s, http\Encoding\Stream\Dechunk::decode($s));
    }

    function testChunkNotEncodedFail() {
        $s = "3\nis \nbetter than\n1\n";
        $this->assertNotEquals($s, @http\Encoding\Stream\Dechunk::decode($s));
    }

    function testChunkNotEncodedWarning1() {
        $this->setExpectedException("PHPUnit_Framework_Error_Warning", 
            "Expected LF at pos 8 of 20 but got 0x74");
        $s = "3\nis \nbetter than\n1\n";
        http\Encoding\Stream\Dechunk::decode($s);
    }

    function testChunkNotEncodedWarning2() {
        $this->setExpectedException("PHPUnit_Framework_Error_Warning", 
            "Expected CRLF at pos 10 of 24 but got 0x74 0x74");
        $s = "3\r\nis \r\nbetter than\r\n1\r\n";
        http\Encoding\Stream\Dechunk::decode($s);
    }

    function testChunkNotEncodedWarning3() {
        $this->setExpectedException("PHPUnit_Framework_Error_Warning", 
            "Expected chunk size at pos 6 of 27 but got trash");
        $s = "3\nis \nreally better than\n1\n";
        http\Encoding\Stream\Dechunk::decode($s);
    }

    function testChunkFlush() {
        $dech = new http\Encoding\Stream\Dechunk(http\Encoding\Stream::FLUSH_FULL);
        $file = file(__FILE__);
        $data = "";
        foreach ($file as $i => $line) {
            $dech = clone $dech;
            if ($i % 2) {
                $data .= $dech->update(sprintf("%lx\r\n%s\r\n", strlen($line), $line));
            } else {
                $data .= $dech->update(sprintf("%lx\r\n", strlen($line)));
                $data .= $dech->flush();
                $data .= $dech->update($line);
                $data .= $dech->flush();
                $data .= $dech->update("\r\n");
            }
            $dech->flush();
            $this->assertFalse($dech->done());
        }
        $data .= $dech->update("0\r\n");
        $this->assertTrue($dech->done());
        $data .= $dech->finish();
        $this->assertEquals(implode("", $file), $data);
    }

    function testZlibStatic() {
        $file = file_get_contents(__FILE__);
        $this->assertEquals($file, 
            http\Encoding\Stream\Inflate::decode(
                http\Encoding\Stream\Deflate::encode(
                    $file, http\Encoding\Stream\Deflate::TYPE_GZIP
                )
            )
        );
        $this->assertEquals($file, 
            http\Encoding\Stream\Inflate::decode(
                http\Encoding\Stream\Deflate::encode(
                    $file, http\Encoding\Stream\Deflate::TYPE_ZLIB
                )
            )
        );
        $this->assertEquals($file, 
            http\Encoding\Stream\Inflate::decode(
                http\Encoding\Stream\Deflate::encode(
                    $file, http\Encoding\Stream\Deflate::TYPE_RAW
                )
            )
        );
    }

    function testZlibAutoFlush() {
        $defl = new http\Encoding\Stream\Deflate(http\Encoding\Stream::FLUSH_FULL);
        $infl = new http\Encoding\Stream\Inflate;

        for ($f = fopen(__FILE__, "rb"); !feof($f); $data = fread($f, 0x100)) {
            $infl = clone $infl;
            $defl = clone $defl;
            if (isset($data)) {
                $this->assertEquals($data, $infl->update($defl->update($data)));
            }
        }

        echo $infl->update($defl->finish());
        echo $infl->finish();
    }

    function testZlibWithoutFlush() {
        $defl = new http\Encoding\Stream\Deflate;
        $infl = new http\Encoding\Stream\Inflate;
        $file = file(__FILE__);
        $data = "";
        foreach ($file as $line) {
            $infl = clone $infl;
            $defl = clone $defl;
            if (strlen($temp = $defl->update($line))) {
                foreach(str_split($temp) as $byte) {
                    $data .= $infl->update($byte);
                }
            }
        }
        if (strlen($temp = $defl->finish())) {
            $data .= $infl->update($temp);
        }
        $data .= $infl->finish();
        $this->assertEquals(implode("", $file), $data);
    }

    function testZlibWithExplicitFlush() {
        $defl = new http\Encoding\Stream\Deflate;
        $infl = new http\Encoding\Stream\Inflate;
        $file = file(__FILE__);
        $data = "";
        foreach ($file as $line) {
        	$data .= $infl->flush();
            if (strlen($temp = $defl->update($line))) {
                $data .= $infl->update($temp);
	        	$data .= $infl->flush();
            }
            if (strlen($temp = $defl->flush())) {
                $data .= $infl->update($temp);
	        	$data .= $infl->flush();
            }
            $this->assertTrue($defl->done());
        }
        if (strlen($temp = $defl->finish())) {
            $data .= $infl->update($temp);
        }
        $this->assertTrue($defl->done());
        $data .= $infl->finish();
        $this->assertTrue($infl->done());
        $this->assertEquals(implode("", $file), $data);
    }

    function testInflateError() {
        $this->setExpectedException("PHPUnit_Framework_Error_Warning",
            "Could not inflate data: data error");
        http\Encoding\Stream\Inflate::decode("if this goes through, something's pretty wrong");
    }
}