ClientTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace Sabre\DAV;
  3. use Sabre\HTTP\Request;
  4. use Sabre\HTTP\Response;
  5. require_once 'Sabre/DAV/ClientMock.php';
  6. class ClientTest extends \PHPUnit_Framework_TestCase {
  7. function setUp() {
  8. if (!function_exists('curl_init')) {
  9. $this->markTestSkipped('CURL must be installed to test the client');
  10. }
  11. }
  12. function testConstruct() {
  13. $client = new ClientMock([
  14. 'baseUri' => '/',
  15. ]);
  16. $this->assertInstanceOf('Sabre\DAV\ClientMock', $client);
  17. }
  18. /**
  19. * @expectedException InvalidArgumentException
  20. */
  21. function testConstructNoBaseUri() {
  22. $client = new ClientMock([]);
  23. }
  24. function testAuth() {
  25. $client = new ClientMock([
  26. 'baseUri' => '/',
  27. 'userName' => 'foo',
  28. 'password' => 'bar',
  29. ]);
  30. $this->assertEquals("foo:bar", $client->curlSettings[CURLOPT_USERPWD]);
  31. $this->assertEquals(CURLAUTH_BASIC | CURLAUTH_DIGEST, $client->curlSettings[CURLOPT_HTTPAUTH]);
  32. }
  33. function testBasicAuth() {
  34. $client = new ClientMock([
  35. 'baseUri' => '/',
  36. 'userName' => 'foo',
  37. 'password' => 'bar',
  38. 'authType' => Client::AUTH_BASIC
  39. ]);
  40. $this->assertEquals("foo:bar", $client->curlSettings[CURLOPT_USERPWD]);
  41. $this->assertEquals(CURLAUTH_BASIC, $client->curlSettings[CURLOPT_HTTPAUTH]);
  42. }
  43. function testDigestAuth() {
  44. $client = new ClientMock([
  45. 'baseUri' => '/',
  46. 'userName' => 'foo',
  47. 'password' => 'bar',
  48. 'authType' => Client::AUTH_DIGEST
  49. ]);
  50. $this->assertEquals("foo:bar", $client->curlSettings[CURLOPT_USERPWD]);
  51. $this->assertEquals(CURLAUTH_DIGEST, $client->curlSettings[CURLOPT_HTTPAUTH]);
  52. }
  53. function testNTLMAuth() {
  54. $client = new ClientMock([
  55. 'baseUri' => '/',
  56. 'userName' => 'foo',
  57. 'password' => 'bar',
  58. 'authType' => Client::AUTH_NTLM
  59. ]);
  60. $this->assertEquals("foo:bar", $client->curlSettings[CURLOPT_USERPWD]);
  61. $this->assertEquals(CURLAUTH_NTLM, $client->curlSettings[CURLOPT_HTTPAUTH]);
  62. }
  63. function testProxy() {
  64. $client = new ClientMock([
  65. 'baseUri' => '/',
  66. 'proxy' => 'localhost:8888',
  67. ]);
  68. $this->assertEquals("localhost:8888", $client->curlSettings[CURLOPT_PROXY]);
  69. }
  70. function testEncoding() {
  71. $client = new ClientMock([
  72. 'baseUri' => '/',
  73. 'encoding' => Client::ENCODING_IDENTITY | Client::ENCODING_GZIP | Client::ENCODING_DEFLATE,
  74. ]);
  75. $this->assertEquals("identity,deflate,gzip", $client->curlSettings[CURLOPT_ENCODING]);
  76. }
  77. function testPropFind() {
  78. $client = new ClientMock([
  79. 'baseUri' => '/',
  80. ]);
  81. $responseBody = <<<XML
  82. <?xml version="1.0"?>
  83. <multistatus xmlns="DAV:">
  84. <response>
  85. <href>/foo</href>
  86. <propstat>
  87. <prop>
  88. <displayname>bar</displayname>
  89. </prop>
  90. <status>HTTP/1.1 200 OK</status>
  91. </propstat>
  92. </response>
  93. </multistatus>
  94. XML;
  95. $client->response = new Response(207, [], $responseBody);
  96. $result = $client->propFind('foo', ['{DAV:}displayname', '{urn:zim}gir']);
  97. $this->assertEquals(['{DAV:}displayname' => 'bar'], $result);
  98. $request = $client->request;
  99. $this->assertEquals('PROPFIND', $request->getMethod());
  100. $this->assertEquals('/foo', $request->getUrl());
  101. $this->assertEquals([
  102. 'Depth' => ['0'],
  103. 'Content-Type' => ['application/xml'],
  104. ], $request->getHeaders());
  105. }
  106. /**
  107. * @expectedException \Sabre\HTTP\ClientHttpException
  108. */
  109. function testPropFindError() {
  110. $client = new ClientMock([
  111. 'baseUri' => '/',
  112. ]);
  113. $client->response = new Response(405, []);
  114. $client->propFind('foo', ['{DAV:}displayname', '{urn:zim}gir']);
  115. }
  116. function testPropFindDepth1() {
  117. $client = new ClientMock([
  118. 'baseUri' => '/',
  119. ]);
  120. $responseBody = <<<XML
  121. <?xml version="1.0"?>
  122. <multistatus xmlns="DAV:">
  123. <response>
  124. <href>/foo</href>
  125. <propstat>
  126. <prop>
  127. <displayname>bar</displayname>
  128. </prop>
  129. <status>HTTP/1.1 200 OK</status>
  130. </propstat>
  131. </response>
  132. </multistatus>
  133. XML;
  134. $client->response = new Response(207, [], $responseBody);
  135. $result = $client->propFind('foo', ['{DAV:}displayname', '{urn:zim}gir'], 1);
  136. $this->assertEquals([
  137. '/foo' => [
  138. '{DAV:}displayname' => 'bar'
  139. ],
  140. ], $result);
  141. $request = $client->request;
  142. $this->assertEquals('PROPFIND', $request->getMethod());
  143. $this->assertEquals('/foo', $request->getUrl());
  144. $this->assertEquals([
  145. 'Depth' => ['1'],
  146. 'Content-Type' => ['application/xml'],
  147. ], $request->getHeaders());
  148. }
  149. function testPropPatch() {
  150. $client = new ClientMock([
  151. 'baseUri' => '/',
  152. ]);
  153. $responseBody = <<<XML
  154. <?xml version="1.0"?>
  155. <multistatus xmlns="DAV:">
  156. <response>
  157. <href>/foo</href>
  158. <propstat>
  159. <prop>
  160. <displayname>bar</displayname>
  161. </prop>
  162. <status>HTTP/1.1 200 OK</status>
  163. </propstat>
  164. </response>
  165. </multistatus>
  166. XML;
  167. $client->response = new Response(207, [], $responseBody);
  168. $result = $client->propPatch('foo', ['{DAV:}displayname' => 'hi', '{urn:zim}gir' => null], 1);
  169. $this->assertTrue($result);
  170. $request = $client->request;
  171. $this->assertEquals('PROPPATCH', $request->getMethod());
  172. $this->assertEquals('/foo', $request->getUrl());
  173. $this->assertEquals([
  174. 'Content-Type' => ['application/xml'],
  175. ], $request->getHeaders());
  176. }
  177. /**
  178. * @depends testPropPatch
  179. * @expectedException \Sabre\HTTP\ClientHttpException
  180. */
  181. function testPropPatchHTTPError() {
  182. $client = new ClientMock([
  183. 'baseUri' => '/',
  184. ]);
  185. $client->response = new Response(403, [], '');
  186. $client->propPatch('foo', ['{DAV:}displayname' => 'hi', '{urn:zim}gir' => null], 1);
  187. }
  188. /**
  189. * @depends testPropPatch
  190. * @expectedException Sabre\HTTP\ClientException
  191. */
  192. function testPropPatchMultiStatusError() {
  193. $client = new ClientMock([
  194. 'baseUri' => '/',
  195. ]);
  196. $responseBody = <<<XML
  197. <?xml version="1.0"?>
  198. <multistatus xmlns="DAV:">
  199. <response>
  200. <href>/foo</href>
  201. <propstat>
  202. <prop>
  203. <displayname />
  204. </prop>
  205. <status>HTTP/1.1 403 Forbidden</status>
  206. </propstat>
  207. </response>
  208. </multistatus>
  209. XML;
  210. $client->response = new Response(207, [], $responseBody);
  211. $client->propPatch('foo', ['{DAV:}displayname' => 'hi', '{urn:zim}gir' => null], 1);
  212. }
  213. function testOPTIONS() {
  214. $client = new ClientMock([
  215. 'baseUri' => '/',
  216. ]);
  217. $client->response = new Response(207, [
  218. 'DAV' => 'calendar-access, extended-mkcol',
  219. ]);
  220. $result = $client->options();
  221. $this->assertEquals(
  222. ['calendar-access', 'extended-mkcol'],
  223. $result
  224. );
  225. $request = $client->request;
  226. $this->assertEquals('OPTIONS', $request->getMethod());
  227. $this->assertEquals('/', $request->getUrl());
  228. $this->assertEquals([
  229. ], $request->getHeaders());
  230. }
  231. }