]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/DAV/ClientTest.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / DAV / ClientTest.php
1 <?php
2
3 require_once 'Sabre/DAV/ClientMock.php';
4
5 class Sabre_DAV_ClientTest extends PHPUnit_Framework_TestCase {
6
7     function testConstruct() {
8
9         $client = new Sabre_DAV_ClientMock(array(
10             'baseUri' => '/',
11         ));
12
13     }
14
15     /**
16      * @expectedException InvalidArgumentException
17      */
18     function testConstructNoBaseUri() {
19
20         $client = new Sabre_DAV_ClientMock(array());
21
22     }
23
24     function testRequest() {
25
26         $client = new Sabre_DAV_ClientMock(array(
27             'baseUri' => 'http://example.org/foo/bar/',
28         ));
29
30         $responseBlob = array(
31             "HTTP/1.1 200 OK",
32             "Content-Type: text/plain",
33             "",
34             "Hello there!"
35         );
36
37         $client->response = array(
38             implode("\r\n", $responseBlob),
39             array(
40                 'header_size' => 45,
41                 'http_code' => 200,
42             ),
43             0,
44             ""
45         );
46
47         $result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
48
49         $this->assertEquals('http://example.org/foo/bar/baz', $client->url);
50         $this->assertEquals(array(
51             CURLOPT_RETURNTRANSFER => true,
52             CURLOPT_FOLLOWLOCATION => true,
53             CURLOPT_MAXREDIRS => 5,
54             CURLOPT_CUSTOMREQUEST => 'POST',
55             CURLOPT_POSTFIELDS => 'sillybody',
56             CURLOPT_HEADER => true,
57             CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
58         ), $client->curlSettings);
59
60         $this->assertEquals(array(
61             'statusCode' => 200,
62             'headers' => array(
63                 'content-type' => 'text/plain',
64             ),
65             'body' => 'Hello there!'
66         ), $result);
67
68
69     }
70
71
72     function testRequestProxy() {
73
74         $client = new Sabre_DAV_ClientMock(array(
75             'baseUri' => 'http://example.org/foo/bar/',
76             'proxy' => 'http://localhost:8000/',
77         ));
78
79         $responseBlob = array(
80             "HTTP/1.1 200 OK",
81             "Content-Type: text/plain",
82             "",
83             "Hello there!"
84         );
85
86         $client->response = array(
87             implode("\r\n", $responseBlob),
88             array(
89                 'header_size' => 45,
90                 'http_code' => 200,
91             ),
92             0,
93             ""
94         );
95
96         $result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
97
98         $this->assertEquals('http://example.org/foo/bar/baz', $client->url);
99         $this->assertEquals(array(
100             CURLOPT_RETURNTRANSFER => true,
101             CURLOPT_FOLLOWLOCATION => true,
102             CURLOPT_MAXREDIRS => 5,
103             CURLOPT_CUSTOMREQUEST => 'POST',
104             CURLOPT_POSTFIELDS => 'sillybody',
105             CURLOPT_HEADER => true,
106             CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
107             CURLOPT_PROXY => 'http://localhost:8000/',
108         ), $client->curlSettings);
109
110         $this->assertEquals(array(
111             'statusCode' => 200,
112             'headers' => array(
113                 'content-type' => 'text/plain',
114             ),
115             'body' => 'Hello there!'
116         ), $result);
117
118     }
119
120
121     function testRequestAuth() {
122
123         $client = new Sabre_DAV_ClientMock(array(
124             'baseUri' => 'http://example.org/foo/bar/',
125             'userName' => 'user',
126             'password' => 'password',
127         ));
128
129         $responseBlob = array(
130             "HTTP/1.1 200 OK",
131             "Content-Type: text/plain",
132             "",
133             "Hello there!"
134         );
135
136         $client->response = array(
137             implode("\r\n", $responseBlob),
138             array(
139                 'header_size' => 45,
140                 'http_code' => 200,
141             ),
142             0,
143             ""
144         );
145
146         $result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
147
148         $this->assertEquals('http://example.org/foo/bar/baz', $client->url);
149         $this->assertEquals(array(
150             CURLOPT_RETURNTRANSFER => true,
151             CURLOPT_FOLLOWLOCATION => true,
152             CURLOPT_MAXREDIRS => 5,
153             CURLOPT_CUSTOMREQUEST => 'POST',
154             CURLOPT_POSTFIELDS => 'sillybody',
155             CURLOPT_HEADER => true,
156             CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
157             CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_DIGEST,
158             CURLOPT_USERPWD => 'user:password'
159         ), $client->curlSettings);
160
161         $this->assertEquals(array(
162             'statusCode' => 200,
163             'headers' => array(
164                 'content-type' => 'text/plain',
165             ),
166             'body' => 'Hello there!'
167         ), $result);
168
169     }
170
171     function testRequestAuthBasic() {
172
173         $client = new Sabre_DAV_ClientMock(array(
174             'baseUri' => 'http://example.org/foo/bar/',
175             'userName' => 'user',
176             'password' => 'password',
177             'authType' => Sabre_DAV_Client::AUTH_BASIC,
178         ));
179
180         $responseBlob = array(
181             "HTTP/1.1 200 OK",
182             "Content-Type: text/plain",
183             "",
184             "Hello there!"
185         );
186
187         $client->response = array(
188             implode("\r\n", $responseBlob),
189             array(
190                 'header_size' => 45,
191                 'http_code' => 200,
192             ),
193             0,
194             ""
195         );
196
197         $result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
198
199         $this->assertEquals('http://example.org/foo/bar/baz', $client->url);
200         $this->assertEquals(array(
201             CURLOPT_RETURNTRANSFER => true,
202             CURLOPT_FOLLOWLOCATION => true,
203             CURLOPT_MAXREDIRS => 5,
204             CURLOPT_CUSTOMREQUEST => 'POST',
205             CURLOPT_POSTFIELDS => 'sillybody',
206             CURLOPT_HEADER => true,
207             CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
208             CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
209             CURLOPT_USERPWD => 'user:password'
210         ), $client->curlSettings);
211
212         $this->assertEquals(array(
213             'statusCode' => 200,
214             'headers' => array(
215                 'content-type' => 'text/plain',
216             ),
217             'body' => 'Hello there!'
218         ), $result);
219
220     }
221
222     function testRequestAuthDigest() {
223
224         $client = new Sabre_DAV_ClientMock(array(
225             'baseUri' => 'http://example.org/foo/bar/',
226             'userName' => 'user',
227             'password' => 'password',
228             'authType' => Sabre_DAV_Client::AUTH_DIGEST,
229         ));
230
231         $responseBlob = array(
232             "HTTP/1.1 200 OK",
233             "Content-Type: text/plain",
234             "",
235             "Hello there!"
236         );
237
238         $client->response = array(
239             implode("\r\n", $responseBlob),
240             array(
241                 'header_size' => 45,
242                 'http_code' => 200,
243             ),
244             0,
245             ""
246         );
247
248         $result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
249
250         $this->assertEquals('http://example.org/foo/bar/baz', $client->url);
251         $this->assertEquals(array(
252             CURLOPT_RETURNTRANSFER => true,
253             CURLOPT_FOLLOWLOCATION => true,
254             CURLOPT_MAXREDIRS => 5,
255             CURLOPT_CUSTOMREQUEST => 'POST',
256             CURLOPT_POSTFIELDS => 'sillybody',
257             CURLOPT_HEADER => true,
258             CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
259             CURLOPT_HTTPAUTH => CURLAUTH_DIGEST,
260             CURLOPT_USERPWD => 'user:password'
261         ), $client->curlSettings);
262
263         $this->assertEquals(array(
264             'statusCode' => 200,
265             'headers' => array(
266                 'content-type' => 'text/plain',
267             ),
268             'body' => 'Hello there!'
269         ), $result);
270
271     }
272     function testRequestError() {
273
274         $client = new Sabre_DAV_ClientMock(array(
275             'baseUri' => 'http://example.org/foo/bar/',
276         ));
277
278         $responseBlob = array(
279             "HTTP/1.1 200 OK",
280             "Content-Type: text/plain",
281             "",
282             "Hello there!"
283         );
284
285         $client->response = array(
286             implode("\r\n", $responseBlob),
287             array(
288                 'header_size' => 45,
289                 'http_code' => 200,
290             ),
291             CURLE_COULDNT_CONNECT,
292             "Could not connect, or something"
293         );
294
295         $caught = false;
296         try {
297             $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
298         } catch (Sabre_DAV_Exception $e) {
299             $caught = true;
300         }
301         if (!$caught) {
302             $this->markTestFailed('Exception was not thrown');
303         }
304
305     }
306
307     function testRequestHTTPError() {
308
309         $client = new Sabre_DAV_ClientMock(array(
310             'baseUri' => 'http://example.org/foo/bar/',
311         ));
312
313         $responseBlob = array(
314             "HTTP/1.1 400 Bad Request",
315             "Content-Type: text/plain",
316             "",
317             "Hello there!"
318         );
319
320         $client->response = array(
321             implode("\r\n", $responseBlob),
322             array(
323                 'header_size' => 45,
324                 'http_code' => 400,
325             ),
326             0,
327             ""
328         );
329
330         $caught = false;
331         try {
332             $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
333         } catch (Sabre_DAV_Exception $e) {
334             $caught = true;
335         }
336         if (!$caught) {
337             $this->fail('Exception was not thrown');
338         }
339
340     }
341
342     function testRequestHTTP404() {
343
344         $client = new Sabre_DAV_ClientMock(array(
345             'baseUri' => 'http://example.org/foo/bar/',
346         ));
347
348         $responseBlob = array(
349             "HTTP/1.1 404 Not Found",
350             "Content-Type: text/plain",
351             "",
352             "Hello there!"
353         );
354
355         $client->response = array(
356             implode("\r\n", $responseBlob),
357             array(
358                 'header_size' => 45,
359                 'http_code' => 404,
360             ),
361             0,
362             ""
363         );
364
365         $caught = false;
366         try {
367             $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
368         } catch (Sabre_DAV_Exception_NotFound $e) {
369             $caught = true;
370         }
371         if (!$caught) {
372             $this->fail('Exception was not thrown');
373         }
374
375     }
376
377     /**
378      * @dataProvider supportedHTTPCodes
379      */
380     function testSpecificHTTPErrors($error) {
381
382         $client = new Sabre_DAV_ClientMock(array(
383             'baseUri' => 'http://example.org/foo/bar/',
384         ));
385
386         $responseBlob = array(
387             "HTTP/1.1 $error blabla",
388             "Content-Type: text/plain",
389             "",
390             "Hello there!"
391         );
392
393         $client->response = array(
394             implode("\r\n", $responseBlob),
395             array(
396                 'header_size' => 42,
397                 'http_code' => $error,
398             ),
399             0,
400             ""
401         );
402
403         $caught = false;
404         try {
405             $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
406         } catch (Sabre_DAV_Exception $e) {
407             $caught = true;
408             $this->assertEquals($e->getHTTPCode(), $error);
409         }
410         if (!$caught) {
411             $this->fail('Exception was not thrown');
412         }
413
414
415     }
416
417     public function supportedHTTPCodes() {
418
419         return array(
420             array(400),
421             array(401),
422             array(402),
423             array(403),
424             array(404),
425             array(405),
426             array(409),
427             array(412),
428             array(416),
429             array(500),
430             array(501),
431             array(507),
432         );
433
434     }
435
436     function testGetAbsoluteUrl() {
437
438         $client = new Sabre_DAV_ClientMock(array(
439             'baseUri' => 'http://example.org/foo/',
440         ));
441
442         $this->assertEquals(
443             'http://example.org/foo/bar',
444             $client->getAbsoluteUrl('bar')
445         );
446
447         $this->assertEquals(
448             'http://example.org/bar',
449             $client->getAbsoluteUrl('/bar')
450         );
451
452         $this->assertEquals(
453             'http://example.com/bar',
454             $client->getAbsoluteUrl('http://example.com/bar')
455         );
456
457     }
458
459     function testOptions() {
460
461         $client = new Sabre_DAV_ClientMock(array(
462             'baseUri' => 'http://example.org/foo/bar/',
463         ));
464
465         $responseBlob = array(
466             "HTTP/1.1 200 OK",
467             "DAV: feature1, feature2",
468             "",
469         );
470
471         $client->response = array(
472             implode("\r\n", $responseBlob),
473             array(
474                 'header_size' => 40,
475                 'http_code' => 200,
476             ),
477             0,
478             ""
479         );
480
481         $result = $client->options();
482         $this->assertEquals(
483             array('feature1', 'feature2'),
484             $result
485         );
486
487     }
488
489     function testOptionsNoDav() {
490
491         $client = new Sabre_DAV_ClientMock(array(
492             'baseUri' => 'http://example.org/foo/bar/',
493         ));
494
495         $responseBlob = array(
496             "HTTP/1.1 200 OK",
497             "",
498         );
499
500         $client->response = array(
501             implode("\r\n", $responseBlob),
502             array(
503                 'header_size' => 20,
504                 'http_code' => 200,
505             ),
506             0,
507             ""
508         );
509
510         $result = $client->options();
511         $this->assertEquals(
512             array(),
513             $result
514         );
515
516     }
517
518     /**
519      * @expectedException InvalidArgumentException
520      */
521     function testPropFindNoXML() {
522
523         $client = new Sabre_DAV_ClientMock(array(
524             'baseUri' => 'http://example.org/foo/bar/',
525         ));
526
527         $responseBlob = array(
528             "HTTP/1.1 200 OK",
529             "",
530         );
531
532         $client->response = array(
533             implode("\r\n", $responseBlob),
534             array(
535                 'header_size' => 20,
536                 'http_code' => 200,
537             ),
538             0,
539             ""
540         );
541
542         $client->propfind('', array('{DAV:}foo','{DAV:}bar'));
543
544     }
545
546     function testPropFind() {
547
548         $client = new Sabre_DAV_ClientMock(array(
549             'baseUri' => 'http://example.org/foo/bar/',
550         ));
551
552         $responseBlob = array(
553             "HTTP/1.1 200 OK",
554             "",
555             "<?xml version=\"1.0\"?>",
556             "<d:multistatus xmlns:d=\"DAV:\">",
557             "  <d:response>",
558             "    <d:href>/foo/bar/</d:href>",
559             "    <d:propstat>",
560             "      <d:prop>",
561             "         <d:foo>hello</d:foo>",
562             "      </d:prop>",
563             "      <d:status>HTTP/1.1 200 OK</d:status>",
564             "    </d:propstat>",
565             "    <d:propstat>",
566             "      <d:prop>",
567             "         <d:bar />",
568             "      </d:prop>",
569             "      <d:status>HTTP/1.1 404 Not Found</d:status>",
570             "    </d:propstat>",
571             "  </d:response>",
572             "</d:multistatus>",
573         );
574
575         $client->response = array(
576             implode("\r\n", $responseBlob),
577             array(
578                 'header_size' => 19,
579                 'http_code' => 200,
580             ),
581             0,
582             ""
583         );
584
585         $result = $client->propfind('', array('{DAV:}foo','{DAV:}bar'));
586
587         $this->assertEquals(array(
588             '{DAV:}foo' => 'hello',
589         ), $result);
590
591         $requestBody = array(
592             '<?xml version="1.0"?>',
593             '<d:propfind xmlns:d="DAV:">',
594             '  <d:prop>',
595             '    <d:foo />',
596             '    <d:bar />',
597             '  </d:prop>',
598             '</d:propfind>'
599         );
600         $requestBody = implode("\n", $requestBody);
601
602         $this->assertEquals($requestBody, $client->curlSettings[CURLOPT_POSTFIELDS]);
603
604     }
605
606     function testPropFindDepth1CustomProp() {
607
608         $client = new Sabre_DAV_ClientMock(array(
609             'baseUri' => 'http://example.org/foo/bar/',
610         ));
611
612         $responseBlob = array(
613             "HTTP/1.1 200 OK",
614             "",
615             "<?xml version=\"1.0\"?>",
616             "<d:multistatus xmlns:d=\"DAV:\" xmlns:x=\"urn:custom\">",
617             "  <d:response>",
618             "    <d:href>/foo/bar/</d:href>",
619             "    <d:propstat>",
620             "      <d:prop>",
621             "         <d:foo>hello</d:foo>",
622             "         <x:bar>world</x:bar>",
623             "      </d:prop>",
624             "      <d:status>HTTP/1.1 200 OK</d:status>",
625             "    </d:propstat>",
626             "  </d:response>",
627             "</d:multistatus>",
628         );
629
630         $client->response = array(
631             implode("\r\n", $responseBlob),
632             array(
633                 'header_size' => 19,
634                 'http_code' => 200,
635             ),
636             0,
637             ""
638         );
639
640         $result = $client->propfind('', array('{DAV:}foo','{urn:custom}bar'),1);
641
642         $this->assertEquals(array(
643             "/foo/bar/" => array(
644                 '{DAV:}foo' => 'hello',
645                 '{urn:custom}bar' => 'world',
646             ),
647         ), $result);
648
649         $requestBody = array(
650             '<?xml version="1.0"?>',
651             '<d:propfind xmlns:d="DAV:">',
652             '  <d:prop>',
653             '    <d:foo />',
654             '    <x:bar xmlns:x="urn:custom"/>',
655             '  </d:prop>',
656             '</d:propfind>'
657         );
658         $requestBody = implode("\n", $requestBody);
659
660         $this->assertEquals($requestBody, $client->curlSettings[CURLOPT_POSTFIELDS]);
661
662     }
663
664     function testPropPatch() {
665
666         $client = new Sabre_DAV_ClientMock(array(
667             'baseUri' => 'http://example.org/foo/bar/',
668         ));
669
670         $responseBlob = array(
671             "HTTP/1.1 200 OK",
672             "",
673         );
674
675         $client->response = array(
676             implode("\r\n", $responseBlob),
677             array(
678                 'header_size' => 20,
679                 'http_code' => 200,
680             ),
681             0,
682             ""
683         );
684
685         $client->proppatch('', array(
686             '{DAV:}foo' => 'newvalue',
687             '{urn:custom}foo' => 'newvalue2',
688             '{DAV:}bar' => null,
689             '{urn:custom}bar' => null,
690         ));
691
692         $requestBody = array(
693             '<?xml version="1.0"?>',
694             '<d:propertyupdate xmlns:d="DAV:">',
695             '<d:set><d:prop>',
696             '    <d:foo>newvalue</d:foo>',
697             '</d:prop></d:set>',
698             '<d:set><d:prop>',
699             '    <x:foo xmlns:x="urn:custom">newvalue2</x:foo>',
700             '</d:prop></d:set>',
701             '<d:remove><d:prop>',
702             '    <d:bar />',
703             '</d:prop></d:remove>',
704             '<d:remove><d:prop>',
705             '    <x:bar xmlns:x="urn:custom"/>',
706             '</d:prop></d:remove>',
707             '</d:propertyupdate>'
708         );
709         $requestBody = implode("\n", $requestBody);
710
711         $this->assertEquals($requestBody, $client->curlSettings[CURLOPT_POSTFIELDS]);
712
713     }
714
715     function testHEADRequest() {
716
717         $client = new Sabre_DAV_ClientMock(array(
718             'baseUri' => 'http://example.org/foo/bar/',
719         ));
720
721         $responseBlob = array(
722             "HTTP/1.1 200 OK",
723             "Content-Type: text/plain",
724             "",
725             "Hello there!"
726         );
727
728         $client->response = array(
729             implode("\r\n", $responseBlob),
730             array(
731                 'header_size' => 45,
732                 'http_code' => 200,
733             ),
734             0,
735             ""
736         );
737
738         $result = $client->request('HEAD', 'baz');
739
740         $this->assertEquals('http://example.org/foo/bar/baz', $client->url);
741         $this->assertEquals(array(
742             CURLOPT_RETURNTRANSFER => true,
743             CURLOPT_FOLLOWLOCATION => true,
744             CURLOPT_MAXREDIRS => 5,
745             CURLOPT_CUSTOMREQUEST => 'HEAD',
746             CURLOPT_NOBODY => true,
747             CURLOPT_HEADER => true,
748             CURLOPT_HTTPHEADER => array(),
749             CURLOPT_POSTFIELDS => null,
750         ), $client->curlSettings);
751
752     }
753
754     function testPUTRequest() {
755
756         $client = new Sabre_DAV_ClientMock(array(
757             'baseUri' => 'http://example.org/foo/bar/',
758         ));
759
760         $responseBlob = array(
761             "HTTP/1.1 200 OK",
762             "Content-Type: text/plain",
763             "",
764             "Hello there!"
765         );
766
767         $client->response = array(
768             implode("\r\n", $responseBlob),
769             array(
770                 'header_size' => 45,
771                 'http_code' => 200,
772             ),
773             0,
774             ""
775         );
776
777         $result = $client->request('PUT', 'bar','newcontent');
778
779         $this->assertEquals('http://example.org/foo/bar/bar', $client->url);
780         $this->assertEquals(array(
781             CURLOPT_RETURNTRANSFER => true,
782             CURLOPT_FOLLOWLOCATION => true,
783             CURLOPT_MAXREDIRS => 5,
784             CURLOPT_CUSTOMREQUEST => "PUT",
785             CURLOPT_POSTFIELDS => 'newcontent',
786             CURLOPT_HEADER => true,
787             CURLOPT_HTTPHEADER => array(),
788         ), $client->curlSettings);
789
790     }
791 }