]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/DAV/ServerRangeTest.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / DAV / ServerRangeTest.php
1 <?php
2
3 require_once 'Sabre/DAV/AbstractServer.php';
4
5 class Sabre_DAV_ServerRangeTest extends Sabre_DAV_AbstractServer{
6
7     protected function getRootNode() {
8
9         return new Sabre_DAV_FSExt_Directory(SABRE_TEMPDIR);
10
11     }
12
13     function testRange() {
14
15         $serverVars = array(
16             'REQUEST_URI'    => '/test.txt',
17             'REQUEST_METHOD' => 'GET',
18             'HTTP_RANGE'     => 'bytes=2-5',
19         );
20
21         $request = new Sabre_HTTP_Request($serverVars);
22         $this->server->httpRequest = ($request);
23         $this->server->exec();
24
25         $this->assertEquals(array(
26             'Content-Type' => 'application/octet-stream',
27             'Content-Length' => 4,
28             'Content-Range' => 'bytes 2-5/13',
29             'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
30             'ETag'          => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')). '"',
31             ),
32             $this->response->headers
33          );
34
35         $this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
36         $this->assertEquals('st c', stream_get_contents($this->response->body));
37
38     }
39
40     /**
41      * @depends testRange
42      */
43     function testStartRange() {
44
45         $serverVars = array(
46             'REQUEST_URI'    => '/test.txt',
47             'REQUEST_METHOD' => 'GET',
48             'HTTP_RANGE'     => 'bytes=2-',
49         );
50
51         $request = new Sabre_HTTP_Request($serverVars);
52         $this->server->httpRequest = ($request);
53         $this->server->exec();
54
55         $this->assertEquals(array(
56             'Content-Type' => 'application/octet-stream',
57             'Content-Length' => 11,
58             'Content-Range' => 'bytes 2-12/13',
59             'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
60             'ETag'          => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
61             ),
62             $this->response->headers
63          );
64
65         $this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
66         $this->assertEquals('st contents', stream_get_contents($this->response->body));
67
68     }
69
70     /**
71      * @depends testRange
72      */
73     function testEndRange() {
74
75         $serverVars = array(
76             'REQUEST_URI'    => '/test.txt',
77             'REQUEST_METHOD' => 'GET',
78             'HTTP_RANGE'     => 'bytes=-8',
79         );
80
81         $request = new Sabre_HTTP_Request($serverVars);
82         $this->server->httpRequest = ($request);
83         $this->server->exec();
84
85         $this->assertEquals(array(
86             'Content-Type' => 'application/octet-stream',
87             'Content-Length' => 8,
88             'Content-Range' => 'bytes 5-12/13',
89             'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
90             'ETag'          => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')). '"',
91             ),
92             $this->response->headers
93          );
94
95         $this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
96         $this->assertEquals('contents', stream_get_contents($this->response->body));
97
98     }
99
100     /**
101      * @depends testRange
102      */
103     function testTooHighRange() {
104
105         $serverVars = array(
106             'REQUEST_URI'    => '/test.txt',
107             'REQUEST_METHOD' => 'GET',
108             'HTTP_RANGE'     => 'bytes=100-200',
109         );
110
111         $request = new Sabre_HTTP_Request($serverVars);
112         $this->server->httpRequest = ($request);
113         $this->server->exec();
114
115         $this->assertEquals('HTTP/1.1 416 Requested Range Not Satisfiable',$this->response->status);
116
117     }
118
119     /**
120      * @depends testRange
121      */
122     function testCrazyRange() {
123
124         $serverVars = array(
125             'REQUEST_URI'    => '/test.txt',
126             'REQUEST_METHOD' => 'GET',
127             'HTTP_RANGE'     => 'bytes=8-4',
128         );
129
130         $request = new Sabre_HTTP_Request($serverVars);
131         $this->server->httpRequest = ($request);
132         $this->server->exec();
133
134         $this->assertEquals('HTTP/1.1 416 Requested Range Not Satisfiable',$this->response->status);
135
136     }
137
138     /**
139      * @depends testRange
140      * @covers Sabre_DAV_Server::httpGet
141      */
142     function testIfRangeEtag() {
143
144         $node = $this->server->tree->getNodeForPath('test.txt');
145
146         $serverVars = array(
147             'REQUEST_URI'    => '/test.txt',
148             'REQUEST_METHOD' => 'GET',
149             'HTTP_RANGE'     => 'bytes=2-5',
150             'HTTP_IF_RANGE'  => $node->getETag(),
151         );
152
153         $request = new Sabre_HTTP_Request($serverVars);
154         $this->server->httpRequest = ($request);
155         $this->server->exec();
156
157         $this->assertEquals(array(
158             'Content-Type' => 'application/octet-stream',
159             'Content-Length' => 4,
160             'Content-Range' => 'bytes 2-5/13',
161             'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
162             'ETag'          => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
163             ),
164             $this->response->headers
165          );
166
167         $this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
168         $this->assertEquals('st c', stream_get_contents($this->response->body));
169
170     }
171
172     /**
173      * @depends testRange
174      * @covers Sabre_DAV_Server::httpGet
175      */
176     function testIfRangeEtagIncorrect() {
177
178         $node = $this->server->tree->getNodeForPath('test.txt');
179
180         $serverVars = array(
181             'REQUEST_URI'    => '/test.txt',
182             'REQUEST_METHOD' => 'GET',
183             'HTTP_RANGE'     => 'bytes=2-5',
184             'HTTP_IF_RANGE'  => $node->getETag() . 'blabla',
185         );
186
187         $request = new Sabre_HTTP_Request($serverVars);
188         $this->server->httpRequest = ($request);
189         $this->server->exec();
190
191         $this->assertEquals(array(
192             'Content-Type' => 'application/octet-stream',
193             'Content-Length' => 13,
194             'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
195             'ETag'          => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
196             ),
197             $this->response->headers
198          );
199
200         $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
201         $this->assertEquals('Test contents', stream_get_contents($this->response->body));
202
203     }
204
205     /**
206      * @depends testRange
207      * @covers Sabre_DAV_Server::httpGet
208      */
209     function testIfRangeModificationDate() {
210
211         $node = $this->server->tree->getNodeForPath('test.txt');
212
213         $serverVars = array(
214             'REQUEST_URI'    => '/test.txt',
215             'REQUEST_METHOD' => 'GET',
216             'HTTP_RANGE'     => 'bytes=2-5',
217             'HTTP_IF_RANGE'  => 'tomorrow',
218         );
219
220         $request = new Sabre_HTTP_Request($serverVars);
221         $this->server->httpRequest = ($request);
222         $this->server->exec();
223
224         $this->assertEquals(array(
225             'Content-Type' => 'application/octet-stream',
226             'Content-Length' => 4,
227             'Content-Range' => 'bytes 2-5/13',
228             'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
229             'ETag'          => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
230             ),
231             $this->response->headers
232          );
233
234         $this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
235         $this->assertEquals('st c', stream_get_contents($this->response->body));
236
237     }
238
239     /**
240      * @depends testRange
241      * @covers Sabre_DAV_Server::httpGet
242      */
243     function testIfRangeModificationDateModified() {
244
245         $node = $this->server->tree->getNodeForPath('test.txt');
246
247         $serverVars = array(
248             'REQUEST_URI'    => '/test.txt',
249             'REQUEST_METHOD' => 'GET',
250             'HTTP_RANGE'     => 'bytes=2-5',
251             'HTTP_IF_RANGE'  => '-2 years',
252         );
253
254         $request = new Sabre_HTTP_Request($serverVars);
255         $this->server->httpRequest = ($request);
256         $this->server->exec();
257
258         $this->assertEquals(array(
259             'Content-Type' => 'application/octet-stream',
260             'Content-Length' => 13,
261             'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
262             'ETag'          => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
263             ),
264             $this->response->headers
265          );
266
267         $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
268         $this->assertEquals('Test contents', stream_get_contents($this->response->body));
269
270     }
271 }