| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- namespace Sabre\DAV;
- use Sabre\HTTP\Request;
- use Sabre\HTTP\Response;
- require_once 'Sabre/DAV/ClientMock.php';
- class ClientTest extends \PHPUnit_Framework_TestCase {
- function setUp() {
- if (!function_exists('curl_init')) {
- $this->markTestSkipped('CURL must be installed to test the client');
- }
- }
- function testConstruct() {
- $client = new ClientMock([
- 'baseUri' => '/',
- ]);
- $this->assertInstanceOf('Sabre\DAV\ClientMock', $client);
- }
- /**
- * @expectedException InvalidArgumentException
- */
- function testConstructNoBaseUri() {
- $client = new ClientMock([]);
- }
- function testAuth() {
- $client = new ClientMock([
- 'baseUri' => '/',
- 'userName' => 'foo',
- 'password' => 'bar',
- ]);
- $this->assertEquals("foo:bar", $client->curlSettings[CURLOPT_USERPWD]);
- $this->assertEquals(CURLAUTH_BASIC | CURLAUTH_DIGEST, $client->curlSettings[CURLOPT_HTTPAUTH]);
- }
- function testBasicAuth() {
- $client = new ClientMock([
- 'baseUri' => '/',
- 'userName' => 'foo',
- 'password' => 'bar',
- 'authType' => Client::AUTH_BASIC
- ]);
- $this->assertEquals("foo:bar", $client->curlSettings[CURLOPT_USERPWD]);
- $this->assertEquals(CURLAUTH_BASIC, $client->curlSettings[CURLOPT_HTTPAUTH]);
- }
- function testDigestAuth() {
- $client = new ClientMock([
- 'baseUri' => '/',
- 'userName' => 'foo',
- 'password' => 'bar',
- 'authType' => Client::AUTH_DIGEST
- ]);
- $this->assertEquals("foo:bar", $client->curlSettings[CURLOPT_USERPWD]);
- $this->assertEquals(CURLAUTH_DIGEST, $client->curlSettings[CURLOPT_HTTPAUTH]);
- }
- function testNTLMAuth() {
- $client = new ClientMock([
- 'baseUri' => '/',
- 'userName' => 'foo',
- 'password' => 'bar',
- 'authType' => Client::AUTH_NTLM
- ]);
- $this->assertEquals("foo:bar", $client->curlSettings[CURLOPT_USERPWD]);
- $this->assertEquals(CURLAUTH_NTLM, $client->curlSettings[CURLOPT_HTTPAUTH]);
- }
- function testProxy() {
- $client = new ClientMock([
- 'baseUri' => '/',
- 'proxy' => 'localhost:8888',
- ]);
- $this->assertEquals("localhost:8888", $client->curlSettings[CURLOPT_PROXY]);
- }
- function testEncoding() {
- $client = new ClientMock([
- 'baseUri' => '/',
- 'encoding' => Client::ENCODING_IDENTITY | Client::ENCODING_GZIP | Client::ENCODING_DEFLATE,
- ]);
- $this->assertEquals("identity,deflate,gzip", $client->curlSettings[CURLOPT_ENCODING]);
- }
- function testPropFind() {
- $client = new ClientMock([
- 'baseUri' => '/',
- ]);
- $responseBody = <<<XML
- <?xml version="1.0"?>
- <multistatus xmlns="DAV:">
- <response>
- <href>/foo</href>
- <propstat>
- <prop>
- <displayname>bar</displayname>
- </prop>
- <status>HTTP/1.1 200 OK</status>
- </propstat>
- </response>
- </multistatus>
- XML;
- $client->response = new Response(207, [], $responseBody);
- $result = $client->propFind('foo', ['{DAV:}displayname', '{urn:zim}gir']);
- $this->assertEquals(['{DAV:}displayname' => 'bar'], $result);
- $request = $client->request;
- $this->assertEquals('PROPFIND', $request->getMethod());
- $this->assertEquals('/foo', $request->getUrl());
- $this->assertEquals([
- 'Depth' => ['0'],
- 'Content-Type' => ['application/xml'],
- ], $request->getHeaders());
- }
- /**
- * @expectedException \Sabre\HTTP\ClientHttpException
- */
- function testPropFindError() {
- $client = new ClientMock([
- 'baseUri' => '/',
- ]);
- $client->response = new Response(405, []);
- $client->propFind('foo', ['{DAV:}displayname', '{urn:zim}gir']);
- }
- function testPropFindDepth1() {
- $client = new ClientMock([
- 'baseUri' => '/',
- ]);
- $responseBody = <<<XML
- <?xml version="1.0"?>
- <multistatus xmlns="DAV:">
- <response>
- <href>/foo</href>
- <propstat>
- <prop>
- <displayname>bar</displayname>
- </prop>
- <status>HTTP/1.1 200 OK</status>
- </propstat>
- </response>
- </multistatus>
- XML;
- $client->response = new Response(207, [], $responseBody);
- $result = $client->propFind('foo', ['{DAV:}displayname', '{urn:zim}gir'], 1);
- $this->assertEquals([
- '/foo' => [
- '{DAV:}displayname' => 'bar'
- ],
- ], $result);
- $request = $client->request;
- $this->assertEquals('PROPFIND', $request->getMethod());
- $this->assertEquals('/foo', $request->getUrl());
- $this->assertEquals([
- 'Depth' => ['1'],
- 'Content-Type' => ['application/xml'],
- ], $request->getHeaders());
- }
- function testPropPatch() {
- $client = new ClientMock([
- 'baseUri' => '/',
- ]);
- $responseBody = <<<XML
- <?xml version="1.0"?>
- <multistatus xmlns="DAV:">
- <response>
- <href>/foo</href>
- <propstat>
- <prop>
- <displayname>bar</displayname>
- </prop>
- <status>HTTP/1.1 200 OK</status>
- </propstat>
- </response>
- </multistatus>
- XML;
- $client->response = new Response(207, [], $responseBody);
- $result = $client->propPatch('foo', ['{DAV:}displayname' => 'hi', '{urn:zim}gir' => null], 1);
- $this->assertTrue($result);
- $request = $client->request;
- $this->assertEquals('PROPPATCH', $request->getMethod());
- $this->assertEquals('/foo', $request->getUrl());
- $this->assertEquals([
- 'Content-Type' => ['application/xml'],
- ], $request->getHeaders());
- }
- /**
- * @depends testPropPatch
- * @expectedException \Sabre\HTTP\ClientHttpException
- */
- function testPropPatchHTTPError() {
- $client = new ClientMock([
- 'baseUri' => '/',
- ]);
- $client->response = new Response(403, [], '');
- $client->propPatch('foo', ['{DAV:}displayname' => 'hi', '{urn:zim}gir' => null], 1);
- }
- /**
- * @depends testPropPatch
- * @expectedException Sabre\HTTP\ClientException
- */
- function testPropPatchMultiStatusError() {
- $client = new ClientMock([
- 'baseUri' => '/',
- ]);
- $responseBody = <<<XML
- <?xml version="1.0"?>
- <multistatus xmlns="DAV:">
- <response>
- <href>/foo</href>
- <propstat>
- <prop>
- <displayname />
- </prop>
- <status>HTTP/1.1 403 Forbidden</status>
- </propstat>
- </response>
- </multistatus>
- XML;
- $client->response = new Response(207, [], $responseBody);
- $client->propPatch('foo', ['{DAV:}displayname' => 'hi', '{urn:zim}gir' => null], 1);
- }
- function testOPTIONS() {
- $client = new ClientMock([
- 'baseUri' => '/',
- ]);
- $client->response = new Response(207, [
- 'DAV' => 'calendar-access, extended-mkcol',
- ]);
- $result = $client->options();
- $this->assertEquals(
- ['calendar-access', 'extended-mkcol'],
- $result
- );
- $request = $client->request;
- $this->assertEquals('OPTIONS', $request->getMethod());
- $this->assertEquals('/', $request->getUrl());
- $this->assertEquals([
- ], $request->getHeaders());
- }
- }
|