-
Notifications
You must be signed in to change notification settings - Fork 3
/
HTTP_Download2Test.php
254 lines (207 loc) · 9.34 KB
/
HTTP_Download2Test.php
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
require_once dirname(__FILE__) . '/helper.inc';
require_once 'HTTP/Download2.php';
require_once 'HTTP/Request.php';
class HTTP_Download2Test extends PHPUnit_Framework_TestCase {
function setUp()
{
$this->testScript = 'http://local/www/mike/pear/HTTP_Download2/tests/send.php';
}
function testHTTP_Download2()
{
$this->assertTrue(is_a($h = new HTTP_Download2, 'HTTP_Download2'));
$this->assertTrue(is_a($h->HTTP, 'HTTP_Header'));
unset($h);
}
function testsetFile()
{
$h = new HTTP_Download2;
$h->setFile(dirname(__FILE__) . '/data.txt');
$this->assertEquals(realpath(dirname(__FILE__) . '/data.txt'), $h->file, '$h->file == "data.txt');
try {
$h->setFile('nonexistant', false); // '$h->setFile("nonexistant")'
$this->fail("Expected a HTTP_Download2_Exception");
} catch (HTTP_Download2_Exception $e) {
}
}
function testsetData()
{
$h = new HTTP_Download2;
$this->assertTrue(null === $h->setData('foo'), 'null === $h->setData("foo")');
$this->assertEquals('foo', $h->data, '$h->data == "foo"');
unset($h);
}
function testsetResource()
{
$h = new HTTP_Download2;
$h->setResource($f = fopen(dirname(__FILE__) . '/data.txt', 'r'));
$this->assertEquals($f, $h->handle, '$h->handle == $f');
fclose($f); $f = -1;
try {
$h->setResource($f); //, '$h->setResource($f = -1)');
$this->fail("Expected a HTTP_Download2_Exception");
} catch (HTTP_Download2_Exception $e) {
}
}
function testsetGzip()
{
$h = new HTTP_Download2;
$h->setGzip(false);
$this->assertFalse($h->gzip, '$h->gzip');
if (extension_loaded('zlib')) {
$h->setGzip(true);
$this->assertTrue($h->gzip, '$h->gzip');
} else {
try {
$h->setGzip(true);// '$h->setGzip(true) with ext/zlib');
$this->fail("Expected a HTTP_Download2_Exception");
} catch (HTTP_Download2_Exception $e) {
}
$this->assertFalse($h->gzip, '$h->gzip');
}
unset($h);
}
function testsetContentType()
{
$h = new HTTP_Download2;
$h->setContentType('text/html;charset=iso-8859-1');
try {
$h->setContentType('##++***!§§§§?°°^^}][{'); //, '$h->setContentType("some weird characters")');
$this->fail("Expected a HTTP_Download2_Exception");
} catch (HTTP_Download2_Exception $e) {
}
$this->assertEquals('text/html;charset=iso-8859-1', $h->headers['Content-Type'], '$h->headers["Content-Type"] == "text/html;charset=iso-8859-1"');
unset($h);
}
function testguessContentType()
{
$h = new HTTP_Download2(array('file' => dirname(__FILE__) . '/data.txt'));
try {
$h->guessContentType();
} catch (HTTP_Download2_Exception $e) {
if ($e->getCode() == HTTP_DOWNLOAD2_E_NO_EXT_MMAGIC) {
$this->markTestSkipped($e->getMessage());
}
$this->fail((string)$e);
}
}
function _send($op)
{
if (!@file_get_contents($this->testScript)) {
$this->markTestSkipped($this->testScript . " is not available");
}
$complete = str_repeat('1234567890',10);
$r = new HTTP_Request($this->testScript);
foreach (array('file', 'resource', 'data') as $what) {
$r->reset($this->testScript);
// without range
$r->addQueryString('op', $op);
$r->addQueryString('what', $what);
$r->addQueryString('buffersize', 33);
$r->sendRequest();
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
$this->assertEquals($complete, $r->getResponseBody(), $what);
// range 1-5
$r->addHeader('Range', 'bytes=1-5');
$r->sendRequest();
$this->assertEquals(206, $r->getResponseCode(), 'HTTP 206 Partial Content');
$this->assertEquals('23456', $r->getResponseBody(), $what);
// range -5
$r->addHeader('Range', 'bytes=-5');
$r->sendRequest();
$this->assertEquals(206, $r->getResponseCode(), 'HTTP 206 Partial Content');
$this->assertEquals('67890', $r->getResponseBody(), $what);
// range 95-
$r->addHeader('Range', 'bytes=95-');
$r->sendRequest();
$this->assertEquals(206, $r->getResponseCode(), 'HTTP 206 Partial Content');
$this->assertEquals('67890', $r->getResponseBody(), $what);
$this->assertTrue(preg_match('/^bytes 95-\d+/', $r->getResponseHeader('content-range')), 'bytes keyword in Content-Range header');
// multiple non-overlapping ranges
$r->addHeader('Range', 'bytes=2-23,45-51, 24-44');
$r->sendRequest();
$this->assertEquals(206, $r->getResponseCode(), 'HTTP 206 Partial Content');
$this->assertTrue(preg_match('/^multipart\/byteranges; boundary=HTTP_DOWNLOAD-[a-f0-9.]{23}$/', $r->getResponseHeader('content-type')), 'Content-Type header: multipart/byteranges');
$this->assertTrue(preg_match('/Content-Range: bytes 2-23/', $r->getResponseBody()), 'bytes keyword in Content-Range header');
// multiple overlapping ranges
$r->addHeader('Range', 'bytes=2-23,45-51,22-46');
$r->sendRequest();
$this->assertEquals(206, $r->getResponseCode(), 'HTTP 206 Partial Content');
$this->assertEquals('bytes 2-51/100', $r->getResponseHeader('content-range'), 'bytes keyword in Content-Range header');
// Invalid range #1 (54-51)
$r->addHeader('Range', 'bytes=2-23,54-51,22-46');
$r->sendRequest();
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
$this->assertEquals('100', $r->getResponseHeader('content-length'), 'full content');
$this->assertEquals($complete, $r->getResponseBody(), $what);
// Invalid range #2 (maformed range)
$r->addHeader('Range', 'bytes=2-23 24-');
$r->sendRequest();
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
$this->assertEquals('100', $r->getResponseHeader('content-length'), 'full content');
$this->assertEquals($complete, $r->getResponseBody(), $what);
// Invalid range #3 (451-510)
$r->addHeader('Range', 'bytes=451-510, -0');
$r->sendRequest();
$this->assertEquals(416, $r->getResponseCode(), 'HTTP 416 Unsatisfiable range');
}
// Stream
$what = 'stream';
$r->reset($this->testScript);
// without range
$r->addQueryString('op', $op);
$r->addQueryString('what', $what);
$r->addQueryString('buffersize', 33);
$r->sendRequest();
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
$this->assertEquals($complete, $r->getResponseBody(), $what);
$this->assertFalse($r->getResponseHeader('content-range'), 'No range');
// range 1-5
$r->addHeader('Range', 'bytes=1-5');
$r->sendRequest();
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
$this->assertEquals($complete, $r->getResponseBody(), $what);
$this->assertFalse($r->getResponseHeader('content-length'), 'Length unknown');
$this->assertFalse($r->getResponseHeader('content-range'), 'No range');
// range -5
$r->addHeader('Range', 'bytes=-5');
$r->sendRequest();
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
$this->assertEquals($complete, $r->getResponseBody(), $what);
$this->assertFalse($r->getResponseHeader('content-length'), 'Length unknown');
$this->assertFalse($r->getResponseHeader('content-range'), 'No range');
// range 95-
$r->addHeader('Range', 'bytes=95-');
$r->sendRequest();
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
$this->assertEquals($complete, $r->getResponseBody(), $what);
$this->assertFalse($r->getResponseHeader('content-length'), 'Length unknown');
$this->assertFalse($r->getResponseHeader('content-range'), 'No range');
unset($r);
}
function testsend()
{
$this->_send('send');
}
function teststaticSend()
{
$this->_send('static');
}
function testsendArchive()
{
if (!@file_get_contents($this->testScript)) {
$this->markTestSkipped($this->testScript . " is not available");
}
$r = new HTTP_Request($this->testScript);
foreach (array('tar', 'tgz', 'zip', 'bz2') as $type) {
$r->addQueryString('type', $type);
$r->addQueryString('op', 'arch');
$r->addQueryString('what', 'data.txt');
$r->sendRequest();
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
$this->assertTrue($r->getResponseHeader('content-length') > 0, 'Content-Length > 0');
$this->assertTrue(preg_match('/application\/x-(tar|gzip|bzip2|zip)/', $t = $r->getResponseHeader('content-type')), 'Reasonable Content-Type for '. $type .' (actual: '. $t .')');
}
unset($r);
}
}