|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ProtocolsTests\Http; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use CommonTestClass; |
|
7
|
|
|
use kalanis\RemoteRequest\Protocols\Helper; |
|
8
|
|
|
use kalanis\RemoteRequest\Protocols\Http; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class AnswerDecodeTest extends CommonTestClass |
|
12
|
|
|
{ |
|
13
|
|
|
public function testStreamAbstract(): void |
|
14
|
|
|
{ |
|
15
|
|
|
$lib = new XDecoder(); |
|
16
|
|
|
$this->assertFalse($lib->canDecode('')); |
|
17
|
|
|
$this->assertTrue($lib->canDecode('custom')); |
|
18
|
|
|
|
|
19
|
|
|
$string = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
20
|
|
|
$stream = Helper::getMemStorage(); |
|
21
|
|
|
fwrite($stream, $string); |
|
22
|
|
|
rewind($stream); |
|
23
|
|
|
$this->assertEquals($string . '---!!!', stream_get_contents($lib->processDecode($stream), -1, 0)); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testChunks(): void |
|
27
|
|
|
{ |
|
28
|
|
|
$lib = new Http\Answer\DecodeStrings\Chunked(); |
|
29
|
|
|
$this->assertFalse($lib->canDecode('')); |
|
30
|
|
|
$this->assertTrue($lib->canDecode('chunked')); |
|
31
|
|
|
$string = "4\r\nWiki\r\n5\r\npedia\r\nE\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n"; |
|
32
|
|
|
$this->assertEquals("Wikipedia in\r\n\r\nchunks.", $lib->processDecode($string)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @requires extension zlib |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testCompress(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$lib = new Http\Answer\DecodeStrings\Compressed(); |
|
41
|
|
|
$this->assertFalse($lib->canDecode('')); |
|
42
|
|
|
$this->assertTrue($lib->canDecode('x-compress,deflate')); |
|
43
|
|
|
$string = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
44
|
|
|
$this->assertEquals($string, $lib->processDecode(gzcompress($string))); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @requires extension zlib |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testDeflate(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$lib = new Http\Answer\DecodeStrings\Deflated(); |
|
53
|
|
|
$this->assertFalse($lib->canDecode('')); |
|
54
|
|
|
$this->assertTrue($lib->canDecode('x-compress,deflate')); |
|
55
|
|
|
$string = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
56
|
|
|
$this->assertEquals($string, $lib->processDecode(gzdeflate($string))); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @requires extension zlib |
|
61
|
|
|
*/ |
|
62
|
|
|
public function testGZip(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$lib = new Http\Answer\DecodeStrings\Zipped(); |
|
65
|
|
|
$this->assertFalse($lib->canDecode('')); |
|
66
|
|
|
$this->assertTrue($lib->canDecode('x-compress,gzip')); |
|
67
|
|
|
$string = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
68
|
|
|
$this->assertEquals($string, $lib->processDecode(gzencode($string))); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @requires extension zlib |
|
73
|
|
|
*/ |
|
74
|
|
|
public function testStringCompress(): void |
|
75
|
|
|
{ |
|
76
|
|
|
$lib = new XStringDecoderCompress(); |
|
77
|
|
|
$lib->addStringDecoding(new Http\Answer\DecodeStrings\Compressed()); |
|
78
|
|
|
$lib->addStringDecoding(new Http\Answer\DecodeStrings\Zipped()); |
|
79
|
|
|
$string = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
80
|
|
|
$this->assertEquals($string, $lib->processStringDecode(gzcompress($string))); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @requires extension zlib |
|
85
|
|
|
*/ |
|
86
|
|
|
public function testStringRaw(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$lib = new XStringDecoderDeflate(); |
|
89
|
|
|
$lib->addStringDecoding(new Http\Answer\DecodeStrings\Compressed()); |
|
90
|
|
|
$lib->addStringDecoding(new Http\Answer\DecodeStrings\Zipped()); |
|
91
|
|
|
$string = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
92
|
|
|
$this->assertEquals($string, $lib->processStringDecode($string)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @requires extension zlib |
|
97
|
|
|
*/ |
|
98
|
|
|
public function testStreamRaw(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$lib = new XStreamDecoder(); |
|
101
|
|
|
$string = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
102
|
|
|
$stream = Helper::getMemStorage(); |
|
103
|
|
|
fwrite($stream, $string); |
|
104
|
|
|
rewind($stream); |
|
105
|
|
|
$this->assertEquals($string, stream_get_contents($lib->processStreamDecode($stream), -1, 0)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @requires extension zlib |
|
110
|
|
|
*/ |
|
111
|
|
|
public function testStreamThru(): void |
|
112
|
|
|
{ |
|
113
|
|
|
$lib = new XStreamDecoder(); |
|
114
|
|
|
$string = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
115
|
|
|
$stream = Helper::getMemStorage(); |
|
116
|
|
|
fwrite($stream, $string); |
|
117
|
|
|
rewind($stream); |
|
118
|
|
|
// nothing set |
|
119
|
|
|
$this->assertEquals($string, stream_get_contents($lib->processStreamDecode($stream), -1, 0)); |
|
120
|
|
|
|
|
121
|
|
|
// added one |
|
122
|
|
|
rewind($stream); |
|
123
|
|
|
$lib->addStreamDecoder(new XDecoder()); |
|
124
|
|
|
$this->assertEquals($string . '---!!!', stream_get_contents($lib->processStreamDecode($stream), -1, 0)); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
class XStreamDecoder |
|
130
|
|
|
{ |
|
131
|
|
|
use Http\Answer\DecodeStreams\TDecoding; |
|
132
|
|
|
|
|
133
|
|
|
public function getAllHeaders(): array |
|
134
|
|
|
{ |
|
135
|
|
|
return [ |
|
136
|
|
|
'Server' => ['PhpUnit/9.3.0'], |
|
137
|
|
|
'Content-Length' => ['25'], |
|
138
|
|
|
'Content-Type' => ['text/plain'], |
|
139
|
|
|
'Content-Encoding' => ['compress,deflate,gzip,custom'], |
|
140
|
|
|
'Transfer-Encoding' => ['chunked'], |
|
141
|
|
|
'Connection' => ['Closed'], |
|
142
|
|
|
]; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
class XStringDecoderCompress |
|
148
|
|
|
{ |
|
149
|
|
|
use Http\Answer\DecodeStrings\TDecoding; |
|
150
|
|
|
|
|
151
|
|
|
public function getAllHeaders(): array |
|
152
|
|
|
{ |
|
153
|
|
|
return [ |
|
154
|
|
|
'Server' => ['PhpUnit/9.3.0'], |
|
155
|
|
|
'Content-Length' => ['25'], |
|
156
|
|
|
'Content-Type' => ['text/plain'], |
|
157
|
|
|
'Content-Encoding' => ['compress'], |
|
158
|
|
|
'Transfer-Encoding' => ['chunked'], |
|
159
|
|
|
'Connection' => ['Closed'], |
|
160
|
|
|
]; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
class XStringDecoderDeflate |
|
166
|
|
|
{ |
|
167
|
|
|
use Http\Answer\DecodeStrings\TDecoding; |
|
168
|
|
|
|
|
169
|
|
|
public function getAllHeaders(): array |
|
170
|
|
|
{ |
|
171
|
|
|
return [ |
|
172
|
|
|
'Server' => ['PhpUnit/9.3.0'], |
|
173
|
|
|
'Content-Length' => ['25'], |
|
174
|
|
|
'Content-Type' => ['text/plain'], |
|
175
|
|
|
'Content-Encoding' => ['deflate'], |
|
176
|
|
|
'Transfer-Encoding' => ['chunked'], |
|
177
|
|
|
'Connection' => ['Closed'], |
|
178
|
|
|
]; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
class XDecoder extends Http\Answer\DecodeStreams\ADecoder |
|
184
|
|
|
{ |
|
185
|
|
|
protected $contentEncoded = ['custom']; |
|
186
|
|
|
|
|
187
|
|
|
public function processDecode($content) |
|
188
|
|
|
{ |
|
189
|
|
|
fseek($content, 0, SEEK_END); |
|
190
|
|
|
fwrite($content, '---!!!'); |
|
191
|
|
|
return $content; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|