| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
require_once 'PHPUnit/Framework.php';
|
|
|
4 |
require_once 'HTTP/Download.php';
|
|
|
5 |
require_once 'HTTP/Request.php';
|
|
|
6 |
|
|
|
7 |
class HTTP_DownloadTest extends PHPUnit_Framework_TestCase {
|
|
|
8 |
|
|
|
9 |
function setUp()
|
|
|
10 |
{
|
|
|
11 |
$this->testScript = 'http://local/www/mike/pear/HTTP_Download/tests/send.php';
|
|
|
12 |
}
|
|
|
13 |
|
|
|
14 |
function tearDown()
|
|
|
15 |
{
|
|
|
16 |
}
|
|
|
17 |
|
|
|
18 |
function testHTTP_Download()
|
|
|
19 |
{
|
|
|
20 |
$this->assertTrue(is_a($h = &new HTTP_Download, 'HTTP_Download'));
|
|
|
21 |
$this->assertTrue(is_a($h->HTTP, 'HTTP_Header'));
|
|
|
22 |
unset($h);
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
function testsetFile()
|
|
|
26 |
{
|
|
|
27 |
$h = &new HTTP_Download;
|
|
|
28 |
$this->assertFalse(PEAR::isError($h->setFile(dirname(__FILE__) . '/data.txt')), '$h->setFile("data.txt")');
|
|
|
29 |
$this->assertEquals(realpath(dirname(__FILE__) . '/data.txt'), $h->file, '$h->file == "data.txt');
|
|
|
30 |
$this->assertTrue(PEAR::isError($h->setFile('nonexistant', false)), '$h->setFile("nonexistant")');
|
|
|
31 |
unset($h);
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
function testsetData()
|
|
|
35 |
{
|
|
|
36 |
$h = &new HTTP_Download;
|
|
|
37 |
$this->assertTrue(null === $h->setData('foo'), 'null === $h->setData("foo")');
|
|
|
38 |
$this->assertEquals('foo', $h->data, '$h->data == "foo"');
|
|
|
39 |
unset($h);
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
function testsetResource()
|
|
|
43 |
{
|
|
|
44 |
$h = &new HTTP_Download;
|
|
|
45 |
$this->assertFalse(PEAR::isError($h->setResource($f = fopen(dirname(__FILE__) . '/data.txt', 'r'))), '$h->setResource(fopen("data.txt","r"))');
|
|
|
46 |
$this->assertEquals($f, $h->handle, '$h->handle == $f');
|
|
|
47 |
fclose($f); $f = -1;
|
|
|
48 |
$this->assertTrue(PEAR::isError($h->setResource($f)), '$h->setResource($f = -1)');
|
|
|
49 |
unset($h);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
function testsetGzip()
|
|
|
53 |
{
|
|
|
54 |
$h = &new HTTP_Download;
|
|
|
55 |
$this->assertFalse(PEAR::isError($h->setGzip(false)), '$h->setGzip(false)');
|
|
|
56 |
$this->assertFalse($h->gzip, '$h->gzip');
|
|
|
57 |
if (PEAR::loadExtension('zlib')) {
|
|
|
58 |
$this->assertFalse(PEAR::isError($h->setGzip(true)), '$h->setGzip(true) without ext/zlib');
|
|
|
59 |
$this->assertTrue($h->gzip, '$h->gzip');
|
|
|
60 |
} else {
|
|
|
61 |
$this->assertTrue(PEAR::isError($h->setGzip(true)), '$h->setGzip(true) with ext/zlib');
|
|
|
62 |
$this->assertFalse($h->gzip, '$h->gzip');
|
|
|
63 |
}
|
|
|
64 |
unset($h);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
function testsetContentType()
|
|
|
68 |
{
|
|
|
69 |
$h = &new HTTP_Download;
|
|
|
70 |
$this->assertFalse(PEAR::isError($h->setContentType('text/html;charset=iso-8859-1')), '$h->setContentType("text/html;charset=iso-8859-1")');
|
|
|
71 |
$this->assertTrue(PEAR::isError($h->setContentType('##++***!§§§§?°°^^}][{')), '$h->setContentType("some weird characters")');
|
|
|
72 |
$this->assertEquals('text/html;charset=iso-8859-1', $h->headers['Content-Type'], '$h->headers["Content-Type"] == "text/html;charset=iso-8859-1"');
|
|
|
73 |
unset($h);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
function testguessContentType()
|
|
|
77 |
{
|
|
|
78 |
$h = &new HTTP_Download(array('file' => dirname(__FILE__) . '/data.txt'));
|
|
|
79 |
$e = $h->guessContentType();
|
|
|
80 |
if (PEAR::isError($e) && $e->getCode() != HTTP_DOWNLOAD_E_NO_EXT_MMAGIC) {
|
|
|
81 |
$this->assertTrue(false, $e->getMessage());
|
|
|
82 |
}
|
|
|
83 |
unset($h, $e);
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
function _send($op)
|
|
|
87 |
{
|
|
|
88 |
if (!file_get_contents($this->testScript)) {
|
|
|
89 |
$this->markTestSkipped($this->testScript . " is not available");
|
|
|
90 |
}
|
|
|
91 |
$complete = str_repeat('1234567890',10);
|
|
|
92 |
|
|
|
93 |
$r = &new HTTP_Request($this->testScript);
|
|
|
94 |
foreach (array('file', 'resource', 'data') as $what) {
|
|
|
95 |
$r->reset($this->testScript);
|
|
|
96 |
|
|
|
97 |
// without range
|
|
|
98 |
$r->addQueryString('op', $op);
|
|
|
99 |
$r->addQueryString('what', $what);
|
|
|
100 |
$r->addQueryString('buffersize', 33);
|
|
|
101 |
$r->sendRequest();
|
|
|
102 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
|
|
|
103 |
$this->assertEquals($complete, $r->getResponseBody(), $what);
|
|
|
104 |
|
|
|
105 |
// range 1-5
|
|
|
106 |
$r->addHeader('Range', 'bytes=1-5');
|
|
|
107 |
$r->sendRequest();
|
|
|
108 |
$this->assertEquals(206, $r->getResponseCode(), 'HTTP 206 Partial Content');
|
|
|
109 |
$this->assertEquals('23456', $r->getResponseBody(), $what);
|
|
|
110 |
|
|
|
111 |
// range -5
|
|
|
112 |
$r->addHeader('Range', 'bytes=-5');
|
|
|
113 |
$r->sendRequest();
|
|
|
114 |
$this->assertEquals(206, $r->getResponseCode(), 'HTTP 206 Partial Content');
|
|
|
115 |
$this->assertEquals('67890', $r->getResponseBody(), $what);
|
|
|
116 |
|
|
|
117 |
// range 95-
|
|
|
118 |
$r->addHeader('Range', 'bytes=95-');
|
|
|
119 |
$r->sendRequest();
|
|
|
120 |
$this->assertEquals(206, $r->getResponseCode(), 'HTTP 206 Partial Content');
|
|
|
121 |
$this->assertEquals('67890', $r->getResponseBody(), $what);
|
|
|
122 |
$this->assertTrue(preg_match('/^bytes 95-\d+/', $r->getResponseHeader('content-range')), 'bytes keyword in Content-Range header');
|
|
|
123 |
|
|
|
124 |
// multiple non-overlapping ranges
|
|
|
125 |
$r->addHeader('Range', 'bytes=2-23,45-51, 24-44');
|
|
|
126 |
$r->sendRequest();
|
|
|
127 |
$this->assertEquals(206, $r->getResponseCode(), 'HTTP 206 Partial Content');
|
|
|
128 |
$this->assertTrue(preg_match('/^multipart\/byteranges; boundary=HTTP_DOWNLOAD-[a-f0-9.]{23}$/', $r->getResponseHeader('content-type')), 'Content-Type header: multipart/byteranges');
|
|
|
129 |
$this->assertTrue(preg_match('/Content-Range: bytes 2-23/', $r->getResponseBody()), 'bytes keyword in Content-Range header');
|
|
|
130 |
|
|
|
131 |
// multiple overlapping ranges
|
|
|
132 |
$r->addHeader('Range', 'bytes=2-23,45-51,22-46');
|
|
|
133 |
$r->sendRequest();
|
|
|
134 |
$this->assertEquals(206, $r->getResponseCode(), 'HTTP 206 Partial Content');
|
|
|
135 |
$this->assertEquals('bytes 2-51/100', $r->getResponseHeader('content-range'), 'bytes keyword in Content-Range header');
|
|
|
136 |
|
|
|
137 |
// Invalid range #1 (54-51)
|
|
|
138 |
$r->addHeader('Range', 'bytes=2-23,54-51,22-46');
|
|
|
139 |
$r->sendRequest();
|
|
|
140 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
|
|
|
141 |
$this->assertEquals('100', $r->getResponseHeader('content-length'), 'full content');
|
|
|
142 |
$this->assertEquals($complete, $r->getResponseBody(), $what);
|
|
|
143 |
|
|
|
144 |
// Invalid range #2 (maformed range)
|
|
|
145 |
$r->addHeader('Range', 'bytes=2-23 24-');
|
|
|
146 |
$r->sendRequest();
|
|
|
147 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
|
|
|
148 |
$this->assertEquals('100', $r->getResponseHeader('content-length'), 'full content');
|
|
|
149 |
$this->assertEquals($complete, $r->getResponseBody(), $what);
|
|
|
150 |
|
|
|
151 |
// Invalid range #3 (451-510)
|
|
|
152 |
$r->addHeader('Range', 'bytes=451-510, -0');
|
|
|
153 |
$r->sendRequest();
|
|
|
154 |
$this->assertEquals(416, $r->getResponseCode(), 'HTTP 416 Unsatisfiable range');
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
// Stream
|
|
|
158 |
$what = 'stream';
|
|
|
159 |
$r->reset($this->testScript);
|
|
|
160 |
|
|
|
161 |
// without range
|
|
|
162 |
$r->addQueryString('op', $op);
|
|
|
163 |
$r->addQueryString('what', $what);
|
|
|
164 |
$r->addQueryString('buffersize', 33);
|
|
|
165 |
$r->sendRequest();
|
|
|
166 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
|
|
|
167 |
$this->assertEquals($complete, $r->getResponseBody(), $what);
|
|
|
168 |
$this->assertFalse($r->getResponseHeader('content-range'), 'No range');
|
|
|
169 |
|
|
|
170 |
// range 1-5
|
|
|
171 |
$r->addHeader('Range', 'bytes=1-5');
|
|
|
172 |
$r->sendRequest();
|
|
|
173 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
|
|
|
174 |
$this->assertEquals($complete, $r->getResponseBody(), $what);
|
|
|
175 |
$this->assertFalse($r->getResponseHeader('content-length'), 'Length unknown');
|
|
|
176 |
$this->assertFalse($r->getResponseHeader('content-range'), 'No range');
|
|
|
177 |
|
|
|
178 |
// range -5
|
|
|
179 |
$r->addHeader('Range', 'bytes=-5');
|
|
|
180 |
$r->sendRequest();
|
|
|
181 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
|
|
|
182 |
$this->assertEquals($complete, $r->getResponseBody(), $what);
|
|
|
183 |
$this->assertFalse($r->getResponseHeader('content-length'), 'Length unknown');
|
|
|
184 |
$this->assertFalse($r->getResponseHeader('content-range'), 'No range');
|
|
|
185 |
|
|
|
186 |
// range 95-
|
|
|
187 |
$r->addHeader('Range', 'bytes=95-');
|
|
|
188 |
$r->sendRequest();
|
|
|
189 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
|
|
|
190 |
$this->assertEquals($complete, $r->getResponseBody(), $what);
|
|
|
191 |
$this->assertFalse($r->getResponseHeader('content-length'), 'Length unknown');
|
|
|
192 |
$this->assertFalse($r->getResponseHeader('content-range'), 'No range');
|
|
|
193 |
|
|
|
194 |
unset($r);
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
function testsend()
|
|
|
198 |
{
|
|
|
199 |
$this->_send('send');
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
function teststaticSend()
|
|
|
203 |
{
|
|
|
204 |
$this->_send('static');
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
function testsendArchive()
|
|
|
208 |
{
|
|
|
209 |
if (!file_get_contents($this->testScript)) {
|
|
|
210 |
$this->markTestSkipped($this->testScript . " is not available");
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
$r = &new HTTP_Request($this->testScript);
|
|
|
214 |
foreach (array('tar', 'tgz', 'zip', 'bz2') as $type) {
|
|
|
215 |
$r->addQueryString('type', $type);
|
|
|
216 |
$r->addQueryString('op', 'arch');
|
|
|
217 |
|
|
|
218 |
$r->addQueryString('what', 'data.txt');
|
|
|
219 |
$r->sendRequest();
|
|
|
220 |
$this->assertEquals(200, $r->getResponseCode(), 'HTTP 200 Ok');
|
|
|
221 |
$this->assertTrue($r->getResponseHeader('content-length') > 0, 'Content-Length > 0');
|
|
|
222 |
$this->assertTrue(preg_match('/application\/x-(tar|gzip|bzip2|zip)/', $t = $r->getResponseHeader('content-type')), 'Reasonable Content-Type for '. $type .' (actual: '. $t .')');
|
|
|
223 |
}
|
|
|
224 |
unset($r);
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
}
|