3 require_once 'Sabre/HTTP/ResponseMock.php';
4 require_once 'Sabre/DAV/AbstractServer.php';
5 require_once 'Sabre/DAV/Exception.php';
7 class Sabre_DAV_ServerSimpleTest extends Sabre_DAV_AbstractServer{
9 function testConstructArray() {
12 new Sabre_DAV_SimpleCollection('hello')
15 $server = new Sabre_DAV_Server($nodes);
16 $this->assertEquals($nodes[0], $server->tree->getNodeForPath('hello'));
21 * @expectedException Sabre_DAV_Exception
23 function testConstructIncorrectObj() {
26 new Sabre_DAV_SimpleCollection('hello'),
30 $server = new Sabre_DAV_Server($nodes);
35 * @expectedException Sabre_DAV_Exception
37 function testConstructInvalidArg() {
39 $server = new Sabre_DAV_Server(1);
46 'REQUEST_URI' => '/test.txt',
47 'REQUEST_METHOD' => 'GET',
50 $request = new Sabre_HTTP_Request($serverVars);
51 $this->server->httpRequest = ($request);
52 $this->server->exec();
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'))),
59 $this->response->headers
62 $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
63 $this->assertEquals('Test contents', stream_get_contents($this->response->body));
67 function testGetDoesntExist() {
70 'REQUEST_URI' => '/test.txt_randomblbla',
71 'REQUEST_METHOD' => 'GET',
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);
81 function testGetDoesntExist2() {
84 'REQUEST_URI' => '/test.txt/randomblbla',
85 'REQUEST_METHOD' => 'GET',
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);
96 * This test should have the exact same result as testGet.
98 * The idea is that double slashes // are converted to single ones /
101 function testGetDoubleSlash() {
104 'REQUEST_URI' => '//test.txt',
105 'REQUEST_METHOD' => 'GET',
108 $request = new Sabre_HTTP_Request($serverVars);
109 $this->server->httpRequest = ($request);
110 $this->server->exec();
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'))),
117 $this->response->headers
120 $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
121 $this->assertEquals('Test contents', stream_get_contents($this->response->body));
126 function testHEAD() {
129 'REQUEST_URI' => '/test.txt',
130 'REQUEST_METHOD' => 'HEAD',
133 $request = new Sabre_HTTP_Request($serverVars);
134 $this->server->httpRequest = ($request);
135 $this->server->exec();
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'))),
142 $this->response->headers
145 $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
146 $this->assertEquals('', $this->response->body);
153 'REQUEST_URI' => '/testput.txt',
154 'REQUEST_METHOD' => 'PUT',
157 $request = new Sabre_HTTP_Request($serverVars);
158 $request->setBody('Testing new file');
159 $this->server->httpRequest = ($request);
160 $this->server->exec();
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);
168 $this->assertEquals('Testing new file',file_get_contents($this->tempDir . '/testput.txt'));
172 function testPutAlreadyExists() {
175 'REQUEST_URI' => '/test.txt',
176 'REQUEST_METHOD' => 'PUT',
177 'HTTP_IF_NONE_MATCH' => '*',
180 $request = new Sabre_HTTP_Request($serverVars);
181 $request->setBody('Testing new file');
182 $this->server->httpRequest = ($request);
183 $this->server->exec();
185 $this->assertEquals(array(
186 'Content-Type' => 'application/xml; charset=utf-8',
187 ),$this->response->headers);
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'));
194 function testPutUpdate() {
197 'REQUEST_URI' => '/test.txt',
198 'REQUEST_METHOD' => 'PUT',
201 $request = new Sabre_HTTP_Request($serverVars);
202 $request->setBody('Testing updated file');
203 $this->server->httpRequest = ($request);
204 $this->server->exec();
206 $this->assertEquals('0', $this->response->headers['Content-Length']);
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'));
214 function testPutContentRange() {
217 'REQUEST_URI' => '/testput.txt',
218 'REQUEST_METHOD' => 'PUT',
219 'HTTP_CONTENT_RANGE' => 'bytes/100-200',
222 $request = new Sabre_HTTP_Request($serverVars);
223 $request->setBody('Testing new file');
224 $this->server->httpRequest = ($request);
225 $this->server->exec();
227 $this->assertEquals('HTTP/1.1 501 Not Implemented',$this->response->status);
232 function testDelete() {
235 'REQUEST_URI' => '/test.txt',
236 'REQUEST_METHOD' => 'DELETE',
239 $request = new Sabre_HTTP_Request($serverVars);
240 $this->server->httpRequest = ($request);
241 $this->server->exec();
243 $this->assertEquals(array(
244 'Content-Length' => '0',
245 ),$this->response->headers);
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'));
253 function testDeleteDirectory() {
256 'REQUEST_URI' => '/testcol',
257 'REQUEST_METHOD' => 'DELETE',
260 mkdir($this->tempDir.'/testcol');
261 file_put_contents($this->tempDir.'/testcol/test.txt','Hi! I\'m a file with a short lifespan');
263 $request = new Sabre_HTTP_Request($serverVars);
264 $this->server->httpRequest = ($request);
265 $this->server->exec();
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'));
276 function testOptions() {
279 'REQUEST_URI' => '/',
280 'REQUEST_METHOD' => 'OPTIONS',
283 $request = new Sabre_HTTP_Request($serverVars);
284 $this->server->httpRequest = ($request);
285 $this->server->exec();
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);
296 $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
297 $this->assertEquals('', $this->response->body);
301 function testNonExistantMethod() {
304 'REQUEST_URI' => '/',
305 'REQUEST_METHOD' => 'BLABLA',
308 $request = new Sabre_HTTP_Request($serverVars);
309 $this->server->httpRequest = ($request);
310 $this->server->exec();
312 $this->assertEquals(array(
313 'Content-Type' => 'application/xml; charset=utf-8',
314 ),$this->response->headers);
316 $this->assertEquals('HTTP/1.1 501 Not Implemented',$this->response->status);
321 function testGETOnCollection() {
324 'REQUEST_URI' => '/',
325 'REQUEST_METHOD' => 'GET',
328 $request = new Sabre_HTTP_Request($serverVars);
329 $this->server->httpRequest = ($request);
330 $this->server->exec();
332 $this->assertEquals(array(
333 'Content-Type' => 'application/xml; charset=utf-8',
334 ),$this->response->headers);
336 $this->assertEquals('HTTP/1.1 501 Not Implemented',$this->response->status);
340 function testHEADOnCollection() {
343 'REQUEST_URI' => '/',
344 'REQUEST_METHOD' => 'HEAD',
347 $request = new Sabre_HTTP_Request($serverVars);
348 $this->server->httpRequest = ($request);
349 $this->server->exec();
351 $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
355 function testBaseUri() {
358 'REQUEST_URI' => '/blabla/test.txt',
359 'REQUEST_METHOD' => 'GET',
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();
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'))),
373 $this->response->headers
376 $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
377 $this->assertEquals('Test contents', stream_get_contents($this->response->body));
381 function testBaseUriAddSlash() {
387 '/foo/bar' => '/foo/bar/',
388 '/foo/bar/' => '/foo/bar/',
391 foreach($tests as $test=>$result) {
392 $this->server->setBaseUri($test);
394 $this->assertEquals($result, $this->server->getBaseUri());
400 function testCalculateUri() {
403 'http://www.example.org/root/somepath',
408 $this->server->setBaseUri('/root/');
410 foreach($uris as $uri) {
412 $this->assertEquals('somepath',$this->server->calculateUri($uri));
416 $this->server->setBaseUri('/root');
418 foreach($uris as $uri) {
420 $this->assertEquals('somepath',$this->server->calculateUri($uri));
424 $this->assertEquals('', $this->server->calculateUri('/root'));
428 function testCalculateUriSpecialChars() {
431 'http://www.example.org/root/%C3%A0fo%C3%B3',
432 '/root/%C3%A0fo%C3%B3',
433 '/root/%C3%A0fo%C3%B3/'
436 $this->server->setBaseUri('/root/');
438 foreach($uris as $uri) {
440 $this->assertEquals("\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri));
444 $this->server->setBaseUri('/root');
446 foreach($uris as $uri) {
448 $this->assertEquals("\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri));
452 $this->server->setBaseUri('/');
454 foreach($uris as $uri) {
456 $this->assertEquals("root/\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri));
462 function testBaseUriCheck() {
465 'http://www.example.org/root/somepath',
472 $this->server->setBaseUri('root/');
473 $this->server->calculateUri('/root/testuri');
475 $this->fail('Expected an exception');
477 } catch (Sabre_DAV_Exception_Forbidden $e) {
486 * @covers Sabre_DAV_Server::guessBaseUri
488 function testGuessBaseUri() {
491 'REQUEST_URI' => '/index.php/root',
492 'PATH_INFO' => '/root',
495 $httpRequest = new Sabre_HTTP_Request($serverVars);
496 $server = new Sabre_DAV_Server();
497 $server->httpRequest = $httpRequest;
499 $this->assertEquals('/index.php/', $server->guessBaseUri());
504 * @depends testGuessBaseUri
505 * @covers Sabre_DAV_Server::guessBaseUri
507 function testGuessBaseUriPercentEncoding() {
510 'REQUEST_URI' => '/index.php/dir/path2/path%20with%20spaces',
511 'PATH_INFO' => '/dir/path2/path with spaces',
514 $httpRequest = new Sabre_HTTP_Request($serverVars);
515 $server = new Sabre_DAV_Server();
516 $server->httpRequest = $httpRequest;
518 $this->assertEquals('/index.php/', $server->guessBaseUri());
523 * @depends testGuessBaseUri
524 * @covers Sabre_DAV_Server::guessBaseUri
527 function testGuessBaseUriPercentEncoding2() {
529 $this->markTestIncomplete('This behaviour is not yet implemented');
531 'REQUEST_URI' => '/some%20directory+mixed/index.php/dir/path2/path%20with%20spaces',
532 'PATH_INFO' => '/dir/path2/path with spaces',
535 $httpRequest = new Sabre_HTTP_Request($serverVars);
536 $server = new Sabre_DAV_Server();
537 $server->httpRequest = $httpRequest;
539 $this->assertEquals('/some%20directory+mixed/index.php/', $server->guessBaseUri());
543 function testGuessBaseUri2() {
546 'REQUEST_URI' => '/index.php/root/',
547 'PATH_INFO' => '/root/',
550 $httpRequest = new Sabre_HTTP_Request($serverVars);
551 $server = new Sabre_DAV_Server();
552 $server->httpRequest = $httpRequest;
554 $this->assertEquals('/index.php/', $server->guessBaseUri());
558 function testGuessBaseUriNoPathInfo() {
561 'REQUEST_URI' => '/index.php/root',
564 $httpRequest = new Sabre_HTTP_Request($serverVars);
565 $server = new Sabre_DAV_Server();
566 $server->httpRequest = $httpRequest;
568 $this->assertEquals('/', $server->guessBaseUri());
572 function testGuessBaseUriNoPathInfo2() {
575 'REQUEST_URI' => '/a/b/c/test.php',
578 $httpRequest = new Sabre_HTTP_Request($serverVars);
579 $server = new Sabre_DAV_Server();
580 $server->httpRequest = $httpRequest;
582 $this->assertEquals('/', $server->guessBaseUri());
588 * @covers Sabre_DAV_Server::guessBaseUri
589 * @depends testGuessBaseUri
591 function testGuessBaseUriQueryString() {
594 'REQUEST_URI' => '/index.php/root?query_string=blabla',
595 'PATH_INFO' => '/root',
598 $httpRequest = new Sabre_HTTP_Request($serverVars);
599 $server = new Sabre_DAV_Server();
600 $server->httpRequest = $httpRequest;
602 $this->assertEquals('/index.php/', $server->guessBaseUri());
606 function testTriggerException() {
608 $this->server->subscribeEvent('beforeMethod',array($this,'exceptionTrigger'));
609 $this->server->exec();
611 $this->assertEquals(array(
612 'Content-Type' => 'application/xml; charset=utf-8',
613 ),$this->response->headers);
615 $this->assertEquals('HTTP/1.1 500 Internal Server Error',$this->response->status);
619 function exceptionTrigger() {
621 throw new Sabre_DAV_Exception('Hola');
625 function testReportNotFound() {
628 'REQUEST_URI' => '/',
629 'REQUEST_METHOD' => 'REPORT',
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();
637 $this->assertEquals(array(
638 'Content-Type' => 'application/xml; charset=utf-8',
640 $this->response->headers
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);
647 function testReportIntercepted() {
650 'REQUEST_URI' => '/',
651 'REQUEST_METHOD' => 'REPORT',
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();
660 $this->assertEquals(array(
661 'testheader' => 'testvalue',
663 $this->response->headers
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);
670 function reportHandler($reportName) {
672 if ($reportName=='{http://www.rooftopsolutions.nl/NS}myreport') {
673 $this->server->httpResponse->sendStatus(418);
674 $this->server->httpResponse->setHeader('testheader','testvalue');
681 function testGetPropertiesForChildren() {
683 $result = $this->server->getPropertiesForChildren('',array(
684 '{DAV:}getcontentlength',
688 'test.txt' => array('{DAV:}getcontentlength' => 13),
692 $this->assertEquals($expected,$result);