3 require_once 'Sabre/DAV/ClientMock.php';
5 class Sabre_DAV_ClientTest extends PHPUnit_Framework_TestCase {
7 function testConstruct() {
9 $client = new Sabre_DAV_ClientMock(array(
16 * @expectedException InvalidArgumentException
18 function testConstructNoBaseUri() {
20 $client = new Sabre_DAV_ClientMock(array());
24 function testRequest() {
26 $client = new Sabre_DAV_ClientMock(array(
27 'baseUri' => 'http://example.org/foo/bar/',
30 $responseBlob = array(
32 "Content-Type: text/plain",
37 $client->response = array(
38 implode("\r\n", $responseBlob),
47 $result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
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);
60 $this->assertEquals(array(
63 'content-type' => 'text/plain',
65 'body' => 'Hello there!'
72 function testRequestProxy() {
74 $client = new Sabre_DAV_ClientMock(array(
75 'baseUri' => 'http://example.org/foo/bar/',
76 'proxy' => 'http://localhost:8000/',
79 $responseBlob = array(
81 "Content-Type: text/plain",
86 $client->response = array(
87 implode("\r\n", $responseBlob),
96 $result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
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);
110 $this->assertEquals(array(
113 'content-type' => 'text/plain',
115 'body' => 'Hello there!'
121 function testRequestAuth() {
123 $client = new Sabre_DAV_ClientMock(array(
124 'baseUri' => 'http://example.org/foo/bar/',
125 'userName' => 'user',
126 'password' => 'password',
129 $responseBlob = array(
131 "Content-Type: text/plain",
136 $client->response = array(
137 implode("\r\n", $responseBlob),
146 $result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
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);
161 $this->assertEquals(array(
164 'content-type' => 'text/plain',
166 'body' => 'Hello there!'
171 function testRequestAuthBasic() {
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,
180 $responseBlob = array(
182 "Content-Type: text/plain",
187 $client->response = array(
188 implode("\r\n", $responseBlob),
197 $result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
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);
212 $this->assertEquals(array(
215 'content-type' => 'text/plain',
217 'body' => 'Hello there!'
222 function testRequestAuthDigest() {
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,
231 $responseBlob = array(
233 "Content-Type: text/plain",
238 $client->response = array(
239 implode("\r\n", $responseBlob),
248 $result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
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);
263 $this->assertEquals(array(
266 'content-type' => 'text/plain',
268 'body' => 'Hello there!'
272 function testRequestError() {
274 $client = new Sabre_DAV_ClientMock(array(
275 'baseUri' => 'http://example.org/foo/bar/',
278 $responseBlob = array(
280 "Content-Type: text/plain",
285 $client->response = array(
286 implode("\r\n", $responseBlob),
291 CURLE_COULDNT_CONNECT,
292 "Could not connect, or something"
297 $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
298 } catch (Sabre_DAV_Exception $e) {
302 $this->markTestFailed('Exception was not thrown');
307 function testRequestHTTPError() {
309 $client = new Sabre_DAV_ClientMock(array(
310 'baseUri' => 'http://example.org/foo/bar/',
313 $responseBlob = array(
314 "HTTP/1.1 400 Bad Request",
315 "Content-Type: text/plain",
320 $client->response = array(
321 implode("\r\n", $responseBlob),
332 $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
333 } catch (Sabre_DAV_Exception $e) {
337 $this->fail('Exception was not thrown');
342 function testRequestHTTP404() {
344 $client = new Sabre_DAV_ClientMock(array(
345 'baseUri' => 'http://example.org/foo/bar/',
348 $responseBlob = array(
349 "HTTP/1.1 404 Not Found",
350 "Content-Type: text/plain",
355 $client->response = array(
356 implode("\r\n", $responseBlob),
367 $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
368 } catch (Sabre_DAV_Exception_NotFound $e) {
372 $this->fail('Exception was not thrown');
378 * @dataProvider supportedHTTPCodes
380 function testSpecificHTTPErrors($error) {
382 $client = new Sabre_DAV_ClientMock(array(
383 'baseUri' => 'http://example.org/foo/bar/',
386 $responseBlob = array(
387 "HTTP/1.1 $error blabla",
388 "Content-Type: text/plain",
393 $client->response = array(
394 implode("\r\n", $responseBlob),
397 'http_code' => $error,
405 $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
406 } catch (Sabre_DAV_Exception $e) {
408 $this->assertEquals($e->getHTTPCode(), $error);
411 $this->fail('Exception was not thrown');
417 public function supportedHTTPCodes() {
436 function testGetAbsoluteUrl() {
438 $client = new Sabre_DAV_ClientMock(array(
439 'baseUri' => 'http://example.org/foo/',
443 'http://example.org/foo/bar',
444 $client->getAbsoluteUrl('bar')
448 'http://example.org/bar',
449 $client->getAbsoluteUrl('/bar')
453 'http://example.com/bar',
454 $client->getAbsoluteUrl('http://example.com/bar')
459 function testOptions() {
461 $client = new Sabre_DAV_ClientMock(array(
462 'baseUri' => 'http://example.org/foo/bar/',
465 $responseBlob = array(
467 "DAV: feature1, feature2",
471 $client->response = array(
472 implode("\r\n", $responseBlob),
481 $result = $client->options();
483 array('feature1', 'feature2'),
489 function testOptionsNoDav() {
491 $client = new Sabre_DAV_ClientMock(array(
492 'baseUri' => 'http://example.org/foo/bar/',
495 $responseBlob = array(
500 $client->response = array(
501 implode("\r\n", $responseBlob),
510 $result = $client->options();
519 * @expectedException InvalidArgumentException
521 function testPropFindNoXML() {
523 $client = new Sabre_DAV_ClientMock(array(
524 'baseUri' => 'http://example.org/foo/bar/',
527 $responseBlob = array(
532 $client->response = array(
533 implode("\r\n", $responseBlob),
542 $client->propfind('', array('{DAV:}foo','{DAV:}bar'));
546 function testPropFind() {
548 $client = new Sabre_DAV_ClientMock(array(
549 'baseUri' => 'http://example.org/foo/bar/',
552 $responseBlob = array(
555 "<?xml version=\"1.0\"?>",
556 "<d:multistatus xmlns:d=\"DAV:\">",
558 " <d:href>/foo/bar/</d:href>",
561 " <d:foo>hello</d:foo>",
563 " <d:status>HTTP/1.1 200 OK</d:status>",
569 " <d:status>HTTP/1.1 404 Not Found</d:status>",
575 $client->response = array(
576 implode("\r\n", $responseBlob),
585 $result = $client->propfind('', array('{DAV:}foo','{DAV:}bar'));
587 $this->assertEquals(array(
588 '{DAV:}foo' => 'hello',
591 $requestBody = array(
592 '<?xml version="1.0"?>',
593 '<d:propfind xmlns:d="DAV:">',
600 $requestBody = implode("\n", $requestBody);
602 $this->assertEquals($requestBody, $client->curlSettings[CURLOPT_POSTFIELDS]);
606 function testPropFindDepth1CustomProp() {
608 $client = new Sabre_DAV_ClientMock(array(
609 'baseUri' => 'http://example.org/foo/bar/',
612 $responseBlob = array(
615 "<?xml version=\"1.0\"?>",
616 "<d:multistatus xmlns:d=\"DAV:\" xmlns:x=\"urn:custom\">",
618 " <d:href>/foo/bar/</d:href>",
621 " <d:foo>hello</d:foo>",
622 " <x:bar>world</x:bar>",
624 " <d:status>HTTP/1.1 200 OK</d:status>",
630 $client->response = array(
631 implode("\r\n", $responseBlob),
640 $result = $client->propfind('', array('{DAV:}foo','{urn:custom}bar'),1);
642 $this->assertEquals(array(
643 "/foo/bar/" => array(
644 '{DAV:}foo' => 'hello',
645 '{urn:custom}bar' => 'world',
649 $requestBody = array(
650 '<?xml version="1.0"?>',
651 '<d:propfind xmlns:d="DAV:">',
654 ' <x:bar xmlns:x="urn:custom"/>',
658 $requestBody = implode("\n", $requestBody);
660 $this->assertEquals($requestBody, $client->curlSettings[CURLOPT_POSTFIELDS]);
664 function testPropPatch() {
666 $client = new Sabre_DAV_ClientMock(array(
667 'baseUri' => 'http://example.org/foo/bar/',
670 $responseBlob = array(
675 $client->response = array(
676 implode("\r\n", $responseBlob),
685 $client->proppatch('', array(
686 '{DAV:}foo' => 'newvalue',
687 '{urn:custom}foo' => 'newvalue2',
689 '{urn:custom}bar' => null,
692 $requestBody = array(
693 '<?xml version="1.0"?>',
694 '<d:propertyupdate xmlns:d="DAV:">',
696 ' <d:foo>newvalue</d:foo>',
699 ' <x:foo xmlns:x="urn:custom">newvalue2</x:foo>',
701 '<d:remove><d:prop>',
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>'
709 $requestBody = implode("\n", $requestBody);
711 $this->assertEquals($requestBody, $client->curlSettings[CURLOPT_POSTFIELDS]);
715 function testHEADRequest() {
717 $client = new Sabre_DAV_ClientMock(array(
718 'baseUri' => 'http://example.org/foo/bar/',
721 $responseBlob = array(
723 "Content-Type: text/plain",
728 $client->response = array(
729 implode("\r\n", $responseBlob),
738 $result = $client->request('HEAD', 'baz');
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);
754 function testPUTRequest() {
756 $client = new Sabre_DAV_ClientMock(array(
757 'baseUri' => 'http://example.org/foo/bar/',
760 $responseBlob = array(
762 "Content-Type: text/plain",
767 $client->response = array(
768 implode("\r\n", $responseBlob),
777 $result = $client->request('PUT', 'bar','newcontent');
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);