]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/DAV/ServerSimpleTest.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / DAV / ServerSimpleTest.php
1 <?php
2
3 require_once 'Sabre/HTTP/ResponseMock.php';
4 require_once 'Sabre/DAV/AbstractServer.php';
5 require_once 'Sabre/DAV/Exception.php';
6
7 class Sabre_DAV_ServerSimpleTest extends Sabre_DAV_AbstractServer{
8
9     function testConstructArray() {
10
11         $nodes = array(
12             new Sabre_DAV_SimpleCollection('hello')
13         );
14
15         $server = new Sabre_DAV_Server($nodes);
16         $this->assertEquals($nodes[0], $server->tree->getNodeForPath('hello'));
17
18     }
19
20     /**
21      * @expectedException Sabre_DAV_Exception
22      */
23     function testConstructIncorrectObj() {
24
25         $nodes = array(
26             new Sabre_DAV_SimpleCollection('hello'),
27             new STDClass(),
28         );
29
30         $server = new Sabre_DAV_Server($nodes);
31
32     }
33
34     /**
35      * @expectedException Sabre_DAV_Exception
36      */
37     function testConstructInvalidArg() {
38
39         $server = new Sabre_DAV_Server(1);
40
41     }
42
43     function testGet() {
44
45         $serverVars = array(
46             'REQUEST_URI'    => '/test.txt',
47             'REQUEST_METHOD' => 'GET',
48         );
49
50         $request = new Sabre_HTTP_Request($serverVars);
51         $this->server->httpRequest = ($request);
52         $this->server->exec();
53
54         $this->assertEquals(array(
55             'Content-Type' => 'application/octet-stream',
56             'Content-Length' => 13,
57             'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
58             ),
59             $this->response->headers
60          );
61
62         $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
63         $this->assertEquals('Test contents', stream_get_contents($this->response->body));
64
65     }
66
67     function testGetDoesntExist() {
68
69         $serverVars = array(
70             'REQUEST_URI'    => '/test.txt_randomblbla',
71             'REQUEST_METHOD' => 'GET',
72         );
73
74         $request = new Sabre_HTTP_Request($serverVars);
75         $this->server->httpRequest = ($request);
76         $this->server->exec();
77         $this->assertEquals('HTTP/1.1 404 Not Found',$this->response->status);
78
79     }
80
81     function testGetDoesntExist2() {
82
83         $serverVars = array(
84             'REQUEST_URI'    => '/test.txt/randomblbla',
85             'REQUEST_METHOD' => 'GET',
86         );
87
88         $request = new Sabre_HTTP_Request($serverVars);
89         $this->server->httpRequest = ($request);
90         $this->server->exec();
91         $this->assertEquals('HTTP/1.1 404 Not Found',$this->response->status);
92
93     }
94
95     /**
96      * This test should have the exact same result as testGet.
97      *
98      * The idea is that double slashes // are converted to single ones /
99      *
100      */
101     function testGetDoubleSlash() {
102
103         $serverVars = array(
104             'REQUEST_URI'    => '//test.txt',
105             'REQUEST_METHOD' => 'GET',
106         );
107
108         $request = new Sabre_HTTP_Request($serverVars);
109         $this->server->httpRequest = ($request);
110         $this->server->exec();
111
112         $this->assertEquals(array(
113             'Content-Type' => 'application/octet-stream',
114             'Content-Length' => 13,
115             'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
116             ),
117             $this->response->headers
118          );
119
120         $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
121         $this->assertEquals('Test contents', stream_get_contents($this->response->body));
122
123     }
124
125
126     function testHEAD() {
127
128         $serverVars = array(
129             'REQUEST_URI'    => '/test.txt',
130             'REQUEST_METHOD' => 'HEAD',
131         );
132
133         $request = new Sabre_HTTP_Request($serverVars);
134         $this->server->httpRequest = ($request);
135         $this->server->exec();
136
137         $this->assertEquals(array(
138             'Content-Type' => 'application/octet-stream',
139             'Content-Length' => 13,
140             'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' .  filemtime($this->tempDir . '/test.txt'))),
141             ),
142             $this->response->headers
143          );
144
145         $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
146         $this->assertEquals('', $this->response->body);
147
148     }
149
150     function testPut() {
151
152         $serverVars = array(
153             'REQUEST_URI'    => '/testput.txt',
154             'REQUEST_METHOD' => 'PUT',
155         );
156
157         $request = new Sabre_HTTP_Request($serverVars);
158         $request->setBody('Testing new file');
159         $this->server->httpRequest = ($request);
160         $this->server->exec();
161
162         $this->assertEquals('', $this->response->body);
163         $this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
164         $this->assertEquals(array(
165             "Content-Length" => "0",
166         ), $this->response->headers);
167
168         $this->assertEquals('Testing new file',file_get_contents($this->tempDir . '/testput.txt'));
169
170     }
171
172     function testPutAlreadyExists() {
173
174         $serverVars = array(
175             'REQUEST_URI'    => '/test.txt',
176             'REQUEST_METHOD' => 'PUT',
177             'HTTP_IF_NONE_MATCH' => '*',
178         );
179
180         $request = new Sabre_HTTP_Request($serverVars);
181         $request->setBody('Testing new file');
182         $this->server->httpRequest = ($request);
183         $this->server->exec();
184
185         $this->assertEquals(array(
186             'Content-Type' => 'application/xml; charset=utf-8',
187         ),$this->response->headers);
188
189         $this->assertEquals('HTTP/1.1 412 Precondition failed',$this->response->status);
190         $this->assertNotEquals('Testing new file',file_get_contents($this->tempDir . '/test.txt'));
191
192     }
193
194     function testPutUpdate() {
195
196         $serverVars = array(
197             'REQUEST_URI'    => '/test.txt',
198             'REQUEST_METHOD' => 'PUT',
199         );
200
201         $request = new Sabre_HTTP_Request($serverVars);
202         $request->setBody('Testing updated file');
203         $this->server->httpRequest = ($request);
204         $this->server->exec();
205
206         $this->assertEquals('0', $this->response->headers['Content-Length']);
207
208         $this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
209         $this->assertEquals('', $this->response->body);
210         $this->assertEquals('Testing updated file',file_get_contents($this->tempDir . '/test.txt'));
211
212     }
213
214     function testPutContentRange() {
215
216         $serverVars = array(
217             'REQUEST_URI'    => '/testput.txt',
218             'REQUEST_METHOD' => 'PUT',
219             'HTTP_CONTENT_RANGE' => 'bytes/100-200',
220         );
221
222         $request = new Sabre_HTTP_Request($serverVars);
223         $request->setBody('Testing new file');
224         $this->server->httpRequest = ($request);
225         $this->server->exec();
226
227         $this->assertEquals('HTTP/1.1 501 Not Implemented',$this->response->status);
228
229     }
230
231
232     function testDelete() {
233
234         $serverVars = array(
235             'REQUEST_URI'    => '/test.txt',
236             'REQUEST_METHOD' => 'DELETE',
237         );
238
239         $request = new Sabre_HTTP_Request($serverVars);
240         $this->server->httpRequest = ($request);
241         $this->server->exec();
242
243         $this->assertEquals(array(
244             'Content-Length' => '0',
245         ),$this->response->headers);
246
247         $this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
248         $this->assertEquals('', $this->response->body);
249         $this->assertFalse(file_exists($this->tempDir . '/test.txt'));
250
251     }
252
253     function testDeleteDirectory() {
254
255         $serverVars = array(
256             'REQUEST_URI'    => '/testcol',
257             'REQUEST_METHOD' => 'DELETE',
258         );
259
260         mkdir($this->tempDir.'/testcol');
261         file_put_contents($this->tempDir.'/testcol/test.txt','Hi! I\'m a file with a short lifespan');
262
263         $request = new Sabre_HTTP_Request($serverVars);
264         $this->server->httpRequest = ($request);
265         $this->server->exec();
266
267         $this->assertEquals(array(
268             'Content-Length' => '0',
269         ),$this->response->headers);
270         $this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
271         $this->assertEquals('', $this->response->body);
272         $this->assertFalse(file_exists($this->tempDir . '/col'));
273
274     }
275
276     function testOptions() {
277
278         $serverVars = array(
279             'REQUEST_URI'    => '/',
280             'REQUEST_METHOD' => 'OPTIONS',
281         );
282
283         $request = new Sabre_HTTP_Request($serverVars);
284         $this->server->httpRequest = ($request);
285         $this->server->exec();
286
287         $this->assertEquals(array(
288             'DAV'            => '1, 3, extended-mkcol',
289             'MS-Author-Via'  => 'DAV',
290             'Allow'          => 'OPTIONS, GET, HEAD, DELETE, PROPFIND, PUT, PROPPATCH, COPY, MOVE, REPORT',
291             'Accept-Ranges'  => 'bytes',
292             'Content-Length' => '0',
293             'X-Sabre-Version' => Sabre_DAV_Version::VERSION,
294         ),$this->response->headers);
295
296         $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
297         $this->assertEquals('', $this->response->body);
298
299
300     }
301     function testNonExistantMethod() {
302
303         $serverVars = array(
304             'REQUEST_URI'    => '/',
305             'REQUEST_METHOD' => 'BLABLA',
306         );
307
308         $request = new Sabre_HTTP_Request($serverVars);
309         $this->server->httpRequest = ($request);
310         $this->server->exec();
311
312         $this->assertEquals(array(
313             'Content-Type' => 'application/xml; charset=utf-8',
314         ),$this->response->headers);
315
316         $this->assertEquals('HTTP/1.1 501 Not Implemented',$this->response->status);
317
318
319     }
320
321     function testGETOnCollection() {
322
323         $serverVars = array(
324             'REQUEST_URI'    => '/',
325             'REQUEST_METHOD' => 'GET',
326         );
327
328         $request = new Sabre_HTTP_Request($serverVars);
329         $this->server->httpRequest = ($request);
330         $this->server->exec();
331
332         $this->assertEquals(array(
333             'Content-Type' => 'application/xml; charset=utf-8',
334         ),$this->response->headers);
335
336         $this->assertEquals('HTTP/1.1 501 Not Implemented',$this->response->status);
337
338     }
339
340     function testHEADOnCollection() {
341
342         $serverVars = array(
343             'REQUEST_URI'    => '/',
344             'REQUEST_METHOD' => 'HEAD',
345         );
346
347         $request = new Sabre_HTTP_Request($serverVars);
348         $this->server->httpRequest = ($request);
349         $this->server->exec();
350
351         $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
352
353     }
354
355     function testBaseUri() {
356
357         $serverVars = array(
358             'REQUEST_URI'    => '/blabla/test.txt',
359             'REQUEST_METHOD' => 'GET',
360         );
361
362         $request = new Sabre_HTTP_Request($serverVars);
363         $this->server->setBaseUri('/blabla/');
364         $this->assertEquals('/blabla/',$this->server->getBaseUri());
365         $this->server->httpRequest = ($request);
366         $this->server->exec();
367
368         $this->assertEquals(array(
369             'Content-Type' => 'application/octet-stream',
370             'Content-Length' => 13,
371             'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
372             ),
373             $this->response->headers
374          );
375
376         $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
377         $this->assertEquals('Test contents', stream_get_contents($this->response->body));
378
379     }
380
381     function testBaseUriAddSlash() {
382
383         $tests = array(
384             '/'         => '/',
385             '/foo'      => '/foo/',
386             '/foo/'     => '/foo/',
387             '/foo/bar'  => '/foo/bar/',
388             '/foo/bar/' => '/foo/bar/',
389         );
390
391         foreach($tests as $test=>$result) {
392             $this->server->setBaseUri($test);
393
394             $this->assertEquals($result, $this->server->getBaseUri());
395
396         }
397
398     }
399
400     function testCalculateUri() {
401
402         $uris = array(
403             'http://www.example.org/root/somepath',
404             '/root/somepath',
405             '/root/somepath/',
406         );
407
408         $this->server->setBaseUri('/root/');
409
410         foreach($uris as $uri) {
411
412             $this->assertEquals('somepath',$this->server->calculateUri($uri));
413
414         }
415
416         $this->server->setBaseUri('/root');
417
418         foreach($uris as $uri) {
419
420             $this->assertEquals('somepath',$this->server->calculateUri($uri));
421
422         }
423
424         $this->assertEquals('', $this->server->calculateUri('/root'));
425
426     }
427
428     function testCalculateUriSpecialChars() {
429
430         $uris = array(
431             'http://www.example.org/root/%C3%A0fo%C3%B3',
432             '/root/%C3%A0fo%C3%B3',
433             '/root/%C3%A0fo%C3%B3/'
434         );
435
436         $this->server->setBaseUri('/root/');
437
438         foreach($uris as $uri) {
439
440             $this->assertEquals("\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri));
441
442         }
443
444         $this->server->setBaseUri('/root');
445
446         foreach($uris as $uri) {
447
448             $this->assertEquals("\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri));
449
450         }
451
452         $this->server->setBaseUri('/');
453
454         foreach($uris as $uri) {
455
456             $this->assertEquals("root/\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri));
457
458         }
459
460     }
461
462     function testBaseUriCheck() {
463
464         $uris = array(
465             'http://www.example.org/root/somepath',
466             '/root/somepath',
467             '/root/somepath/'
468         );
469
470         try {
471
472             $this->server->setBaseUri('root/');
473             $this->server->calculateUri('/root/testuri');
474
475             $this->fail('Expected an exception');
476
477         } catch (Sabre_DAV_Exception_Forbidden $e) {
478
479             // This was expected
480
481         }
482
483     }
484
485     /**
486      * @covers Sabre_DAV_Server::guessBaseUri
487      */
488     function testGuessBaseUri() {
489
490         $serverVars = array(
491             'REQUEST_URI' => '/index.php/root',
492             'PATH_INFO'   => '/root',
493         );
494
495         $httpRequest = new Sabre_HTTP_Request($serverVars);
496         $server = new Sabre_DAV_Server();
497         $server->httpRequest = $httpRequest;
498
499         $this->assertEquals('/index.php/', $server->guessBaseUri());
500
501     }
502
503     /**
504      * @depends testGuessBaseUri
505      * @covers Sabre_DAV_Server::guessBaseUri
506      */
507     function testGuessBaseUriPercentEncoding() {
508
509         $serverVars = array(
510             'REQUEST_URI' => '/index.php/dir/path2/path%20with%20spaces',
511             'PATH_INFO'   => '/dir/path2/path with spaces',
512         );
513
514         $httpRequest = new Sabre_HTTP_Request($serverVars);
515         $server = new Sabre_DAV_Server();
516         $server->httpRequest = $httpRequest;
517
518         $this->assertEquals('/index.php/', $server->guessBaseUri());
519
520     }
521
522     /**
523      * @depends testGuessBaseUri
524      * @covers Sabre_DAV_Server::guessBaseUri
525      */
526     /*
527     function testGuessBaseUriPercentEncoding2() {
528
529         $this->markTestIncomplete('This behaviour is not yet implemented');
530         $serverVars = array(
531             'REQUEST_URI' => '/some%20directory+mixed/index.php/dir/path2/path%20with%20spaces',
532             'PATH_INFO'   => '/dir/path2/path with spaces',
533         );
534
535         $httpRequest = new Sabre_HTTP_Request($serverVars);
536         $server = new Sabre_DAV_Server();
537         $server->httpRequest = $httpRequest;
538
539         $this->assertEquals('/some%20directory+mixed/index.php/', $server->guessBaseUri());
540
541     }*/
542
543     function testGuessBaseUri2() {
544
545         $serverVars = array(
546             'REQUEST_URI' => '/index.php/root/',
547             'PATH_INFO'   => '/root/',
548         );
549
550         $httpRequest = new Sabre_HTTP_Request($serverVars);
551         $server = new Sabre_DAV_Server();
552         $server->httpRequest = $httpRequest;
553
554         $this->assertEquals('/index.php/', $server->guessBaseUri());
555
556     }
557
558     function testGuessBaseUriNoPathInfo() {
559
560         $serverVars = array(
561             'REQUEST_URI' => '/index.php/root',
562         );
563
564         $httpRequest = new Sabre_HTTP_Request($serverVars);
565         $server = new Sabre_DAV_Server();
566         $server->httpRequest = $httpRequest;
567
568         $this->assertEquals('/', $server->guessBaseUri());
569
570     }
571
572     function testGuessBaseUriNoPathInfo2() {
573
574         $serverVars = array(
575             'REQUEST_URI' => '/a/b/c/test.php',
576         );
577
578         $httpRequest = new Sabre_HTTP_Request($serverVars);
579         $server = new Sabre_DAV_Server();
580         $server->httpRequest = $httpRequest;
581
582         $this->assertEquals('/', $server->guessBaseUri());
583
584     }
585
586
587     /**
588      * @covers Sabre_DAV_Server::guessBaseUri
589      * @depends testGuessBaseUri
590      */
591     function testGuessBaseUriQueryString() {
592
593         $serverVars = array(
594             'REQUEST_URI' => '/index.php/root?query_string=blabla',
595             'PATH_INFO'   => '/root',
596         );
597
598         $httpRequest = new Sabre_HTTP_Request($serverVars);
599         $server = new Sabre_DAV_Server();
600         $server->httpRequest = $httpRequest;
601
602         $this->assertEquals('/index.php/', $server->guessBaseUri());
603
604     }
605
606     function testTriggerException() {
607
608         $this->server->subscribeEvent('beforeMethod',array($this,'exceptionTrigger'));
609         $this->server->exec();
610
611         $this->assertEquals(array(
612             'Content-Type' => 'application/xml; charset=utf-8',
613         ),$this->response->headers);
614
615         $this->assertEquals('HTTP/1.1 500 Internal Server Error',$this->response->status);
616
617     }
618
619     function exceptionTrigger() {
620
621         throw new Sabre_DAV_Exception('Hola');
622
623     }
624
625     function testReportNotFound() {
626
627         $serverVars = array(
628             'REQUEST_URI'    => '/',
629             'REQUEST_METHOD' => 'REPORT',
630         );
631
632         $request = new Sabre_HTTP_Request($serverVars);
633         $this->server->httpRequest = ($request);
634         $this->server->httpRequest->setBody('<?xml version="1.0"?><bla:myreport xmlns:bla="http://www.rooftopsolutions.nl/NS"></bla:myreport>');
635         $this->server->exec();
636
637         $this->assertEquals(array(
638             'Content-Type' => 'application/xml; charset=utf-8',
639             ),
640             $this->response->headers
641          );
642
643         $this->assertEquals('HTTP/1.1 501 Not Implemented',$this->response->status,'We got an incorrect status back. Full response body follows: ' . $this->response->body);
644
645     }
646
647     function testReportIntercepted() {
648
649         $serverVars = array(
650             'REQUEST_URI'    => '/',
651             'REQUEST_METHOD' => 'REPORT',
652         );
653
654         $request = new Sabre_HTTP_Request($serverVars);
655         $this->server->httpRequest = ($request);
656         $this->server->httpRequest->setBody('<?xml version="1.0"?><bla:myreport xmlns:bla="http://www.rooftopsolutions.nl/NS"></bla:myreport>');
657         $this->server->subscribeEvent('report',array($this,'reportHandler'));
658         $this->server->exec();
659
660         $this->assertEquals(array(
661             'testheader' => 'testvalue',
662             ),
663             $this->response->headers
664         );
665
666         $this->assertEquals('HTTP/1.1 418 I\'m a teapot',$this->response->status,'We got an incorrect status back. Full response body follows: ' . $this->response->body);
667
668     }
669
670     function reportHandler($reportName) {
671
672         if ($reportName=='{http://www.rooftopsolutions.nl/NS}myreport') {
673             $this->server->httpResponse->sendStatus(418);
674             $this->server->httpResponse->setHeader('testheader','testvalue');
675             return false;
676         }
677         else return;
678
679     }
680
681     function testGetPropertiesForChildren() {
682
683         $result = $this->server->getPropertiesForChildren('',array(
684             '{DAV:}getcontentlength',
685         ));
686
687         $expected = array(
688             'test.txt' => array('{DAV:}getcontentlength' => 13),
689             'dir/' => array(),
690         );
691
692         $this->assertEquals($expected,$result);
693
694     }
695
696 }