1<?php 2 3namespace Sabre\DAV; 4 5use Sabre\HTTP; 6 7class ServerSimpleTest extends AbstractServer{ 8 9 function testConstructArray() { 10 11 $nodes = [ 12 new SimpleCollection('hello') 13 ]; 14 15 $server = new Server($nodes); 16 $this->assertEquals($nodes[0], $server->tree->getNodeForPath('hello')); 17 18 } 19 20 /** 21 * @expectedException Sabre\DAV\Exception 22 */ 23 function testConstructIncorrectObj() { 24 25 $nodes = [ 26 new SimpleCollection('hello'), 27 new \STDClass(), 28 ]; 29 30 $server = new Server($nodes); 31 32 } 33 34 /** 35 * @expectedException Sabre\DAV\Exception 36 */ 37 function testConstructInvalidArg() { 38 39 $server = new Server(1); 40 41 } 42 43 function testOptions() { 44 45 $request = new HTTP\Request('OPTIONS', '/'); 46 $this->server->httpRequest = $request; 47 $this->server->exec(); 48 49 $this->assertEquals([ 50 'DAV' => ['1, 3, extended-mkcol'], 51 'MS-Author-Via' => ['DAV'], 52 'Allow' => ['OPTIONS, GET, HEAD, DELETE, PROPFIND, PUT, PROPPATCH, COPY, MOVE, REPORT'], 53 'Accept-Ranges' => ['bytes'], 54 'Content-Length' => ['0'], 55 'X-Sabre-Version' => [Version::VERSION], 56 ],$this->response->getHeaders()); 57 58 $this->assertEquals(200, $this->response->status); 59 $this->assertEquals('', $this->response->body); 60 61 } 62 63 function testOptionsUnmapped() { 64 65 $request = new HTTP\Request('OPTIONS', '/unmapped'); 66 $this->server->httpRequest = $request; 67 68 $this->server->exec(); 69 70 $this->assertEquals([ 71 'DAV' => ['1, 3, extended-mkcol'], 72 'MS-Author-Via' => ['DAV'], 73 'Allow' => ['OPTIONS, GET, HEAD, DELETE, PROPFIND, PUT, PROPPATCH, COPY, MOVE, REPORT, MKCOL'], 74 'Accept-Ranges' => ['bytes'], 75 'Content-Length' => ['0'], 76 'X-Sabre-Version' => [Version::VERSION], 77 ],$this->response->getHeaders()); 78 79 $this->assertEquals(200, $this->response->status); 80 $this->assertEquals('', $this->response->body); 81 82 } 83 84 function testNonExistantMethod() { 85 86 $serverVars = [ 87 'REQUEST_URI' => '/', 88 'REQUEST_METHOD' => 'BLABLA', 89 ]; 90 91 $request = HTTP\Sapi::createFromServerArray($serverVars); 92 $this->server->httpRequest = ($request); 93 $this->server->exec(); 94 95 $this->assertEquals([ 96 'X-Sabre-Version' => [Version::VERSION], 97 'Content-Type' => ['application/xml; charset=utf-8'], 98 ],$this->response->getHeaders()); 99 100 $this->assertEquals(501, $this->response->status); 101 102 103 } 104 105 function testBaseUri() { 106 107 $serverVars = [ 108 'REQUEST_URI' => '/blabla/test.txt', 109 'REQUEST_METHOD' => 'GET', 110 ]; 111 $filename = $this->tempDir . '/test.txt'; 112 113 $request = HTTP\Sapi::createFromServerArray($serverVars); 114 $this->server->setBaseUri('/blabla/'); 115 $this->assertEquals('/blabla/',$this->server->getBaseUri()); 116 $this->server->httpRequest = ($request); 117 $this->server->exec(); 118 119 $this->assertEquals([ 120 'X-Sabre-Version' => [Version::VERSION], 121 'Content-Type' => ['application/octet-stream'], 122 'Content-Length' => [13], 123 'Last-Modified' => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))], 124 'ETag' => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'], 125 ], 126 $this->response->getHeaders() 127 ); 128 129 $this->assertEquals(200, $this->response->status); 130 $this->assertEquals('Test contents', stream_get_contents($this->response->body)); 131 132 } 133 134 function testBaseUriAddSlash() { 135 136 $tests = [ 137 '/' => '/', 138 '/foo' => '/foo/', 139 '/foo/' => '/foo/', 140 '/foo/bar' => '/foo/bar/', 141 '/foo/bar/' => '/foo/bar/', 142 ]; 143 144 foreach($tests as $test=>$result) { 145 $this->server->setBaseUri($test); 146 147 $this->assertEquals($result, $this->server->getBaseUri()); 148 149 } 150 151 } 152 153 function testCalculateUri() { 154 155 $uris = [ 156 'http://www.example.org/root/somepath', 157 '/root/somepath', 158 '/root/somepath/', 159 ]; 160 161 $this->server->setBaseUri('/root/'); 162 163 foreach($uris as $uri) { 164 165 $this->assertEquals('somepath',$this->server->calculateUri($uri)); 166 167 } 168 169 $this->server->setBaseUri('/root'); 170 171 foreach($uris as $uri) { 172 173 $this->assertEquals('somepath',$this->server->calculateUri($uri)); 174 175 } 176 177 $this->assertEquals('', $this->server->calculateUri('/root')); 178 179 } 180 181 function testCalculateUriSpecialChars() { 182 183 $uris = [ 184 'http://www.example.org/root/%C3%A0fo%C3%B3', 185 '/root/%C3%A0fo%C3%B3', 186 '/root/%C3%A0fo%C3%B3/' 187 ]; 188 189 $this->server->setBaseUri('/root/'); 190 191 foreach($uris as $uri) { 192 193 $this->assertEquals("\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri)); 194 195 } 196 197 $this->server->setBaseUri('/root'); 198 199 foreach($uris as $uri) { 200 201 $this->assertEquals("\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri)); 202 203 } 204 205 $this->server->setBaseUri('/'); 206 207 foreach($uris as $uri) { 208 209 $this->assertEquals("root/\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri)); 210 211 } 212 213 } 214 215 /** 216 * @expectedException \Sabre\DAV\Exception\Forbidden 217 */ 218 function testCalculateUriBreakout() { 219 220 $uri = '/path1/'; 221 222 $this->server->setBaseUri('/path2/'); 223 $this->server->calculateUri($uri); 224 225 } 226 227 /** 228 */ 229 function testGuessBaseUri() { 230 231 $serverVars = [ 232 'REQUEST_URI' => '/index.php/root', 233 'PATH_INFO' => '/root', 234 ]; 235 236 $httpRequest = HTTP\Sapi::createFromServerArray($serverVars); 237 $server = new Server(); 238 $server->httpRequest = $httpRequest; 239 240 $this->assertEquals('/index.php/', $server->guessBaseUri()); 241 242 } 243 244 /** 245 * @depends testGuessBaseUri 246 */ 247 function testGuessBaseUriPercentEncoding() { 248 249 $serverVars = [ 250 'REQUEST_URI' => '/index.php/dir/path2/path%20with%20spaces', 251 'PATH_INFO' => '/dir/path2/path with spaces', 252 ]; 253 254 $httpRequest = HTTP\Sapi::createFromServerArray($serverVars); 255 $server = new Server(); 256 $server->httpRequest = $httpRequest; 257 258 $this->assertEquals('/index.php/', $server->guessBaseUri()); 259 260 } 261 262 /** 263 * @depends testGuessBaseUri 264 */ 265 /* 266 function testGuessBaseUriPercentEncoding2() { 267 268 $this->markTestIncomplete('This behaviour is not yet implemented'); 269 $serverVars = [ 270 'REQUEST_URI' => '/some%20directory+mixed/index.php/dir/path2/path%20with%20spaces', 271 'PATH_INFO' => '/dir/path2/path with spaces', 272 ]; 273 274 $httpRequest = HTTP\Sapi::createFromServerArray($serverVars); 275 $server = new Server(); 276 $server->httpRequest = $httpRequest; 277 278 $this->assertEquals('/some%20directory+mixed/index.php/', $server->guessBaseUri()); 279 280 }*/ 281 282 function testGuessBaseUri2() { 283 284 $serverVars = [ 285 'REQUEST_URI' => '/index.php/root/', 286 'PATH_INFO' => '/root/', 287 ]; 288 289 $httpRequest = HTTP\Sapi::createFromServerArray($serverVars); 290 $server = new Server(); 291 $server->httpRequest = $httpRequest; 292 293 $this->assertEquals('/index.php/', $server->guessBaseUri()); 294 295 } 296 297 function testGuessBaseUriNoPathInfo() { 298 299 $serverVars = [ 300 'REQUEST_URI' => '/index.php/root', 301 ]; 302 303 $httpRequest = HTTP\Sapi::createFromServerArray($serverVars); 304 $server = new Server(); 305 $server->httpRequest = $httpRequest; 306 307 $this->assertEquals('/', $server->guessBaseUri()); 308 309 } 310 311 function testGuessBaseUriNoPathInfo2() { 312 313 $serverVars = [ 314 'REQUEST_URI' => '/a/b/c/test.php', 315 ]; 316 317 $httpRequest = HTTP\Sapi::createFromServerArray($serverVars); 318 $server = new Server(); 319 $server->httpRequest = $httpRequest; 320 321 $this->assertEquals('/', $server->guessBaseUri()); 322 323 } 324 325 326 /** 327 * @depends testGuessBaseUri 328 */ 329 function testGuessBaseUriQueryString() { 330 331 $serverVars = [ 332 'REQUEST_URI' => '/index.php/root?query_string=blabla', 333 'PATH_INFO' => '/root', 334 ]; 335 336 $httpRequest = HTTP\Sapi::createFromServerArray($serverVars); 337 $server = new Server(); 338 $server->httpRequest = $httpRequest; 339 340 $this->assertEquals('/index.php/', $server->guessBaseUri()); 341 342 } 343 344 /** 345 * @depends testGuessBaseUri 346 * @expectedException \Sabre\DAV\Exception 347 */ 348 function testGuessBaseUriBadConfig() { 349 350 $serverVars = [ 351 'REQUEST_URI' => '/index.php/root/heyyy', 352 'PATH_INFO' => '/root', 353 ]; 354 355 $httpRequest = HTTP\Sapi::createFromServerArray($serverVars); 356 $server = new Server(); 357 $server->httpRequest = $httpRequest; 358 359 $server->guessBaseUri(); 360 361 } 362 363 function testTriggerException() { 364 365 $serverVars = [ 366 'REQUEST_URI' => '/', 367 'REQUEST_METHOD' => 'FOO', 368 ]; 369 370 $httpRequest = HTTP\Sapi::createFromServerArray($serverVars); 371 $this->server->httpRequest = $httpRequest; 372 $this->server->on('beforeMethod', [$this,'exceptionTrigger']); 373 $this->server->exec(); 374 375 $this->assertEquals([ 376 'Content-Type' => ['application/xml; charset=utf-8'], 377 ],$this->response->getHeaders()); 378 379 $this->assertEquals(500, $this->response->status); 380 381 } 382 383 function exceptionTrigger() { 384 385 throw new Exception('Hola'); 386 387 } 388 389 function testReportNotFound() { 390 391 $serverVars = [ 392 'REQUEST_URI' => '/', 393 'REQUEST_METHOD' => 'REPORT', 394 ]; 395 396 $request = HTTP\Sapi::createFromServerArray($serverVars); 397 $this->server->httpRequest = ($request); 398 $this->server->httpRequest->setBody('<?xml version="1.0"?><bla:myreport xmlns:bla="http://www.rooftopsolutions.nl/NS"></bla:myreport>'); 399 $this->server->exec(); 400 401 $this->assertEquals([ 402 'X-Sabre-Version' => [Version::VERSION], 403 'Content-Type' => ['application/xml; charset=utf-8'], 404 ], 405 $this->response->getHeaders() 406 ); 407 408 $this->assertEquals(415, $this->response->status, 'We got an incorrect status back. Full response body follows: ' . $this->response->body); 409 410 } 411 412 function testReportIntercepted() { 413 414 $serverVars = [ 415 'REQUEST_URI' => '/', 416 'REQUEST_METHOD' => 'REPORT', 417 ]; 418 419 $request = HTTP\Sapi::createFromServerArray($serverVars); 420 $this->server->httpRequest = ($request); 421 $this->server->httpRequest->setBody('<?xml version="1.0"?><bla:myreport xmlns:bla="http://www.rooftopsolutions.nl/NS"></bla:myreport>'); 422 $this->server->on('report', [$this,'reportHandler']); 423 $this->server->exec(); 424 425 $this->assertEquals([ 426 'X-Sabre-Version' => [Version::VERSION], 427 'testheader' => ['testvalue'], 428 ], 429 $this->response->getHeaders() 430 ); 431 432 $this->assertEquals(418, $this->response->status,'We got an incorrect status back. Full response body follows: ' . $this->response->body); 433 434 } 435 436 function reportHandler($reportName) { 437 438 if ($reportName=='{http://www.rooftopsolutions.nl/NS}myreport') { 439 $this->server->httpResponse->setStatus(418); 440 $this->server->httpResponse->setHeader('testheader','testvalue'); 441 return false; 442 } 443 else return; 444 445 } 446 447 function testGetPropertiesForChildren() { 448 449 $result = $this->server->getPropertiesForChildren('',[ 450 '{DAV:}getcontentlength', 451 ]); 452 453 $expected = [ 454 'test.txt' => ['{DAV:}getcontentlength' => 13], 455 'dir/' => [], 456 ]; 457 458 $this->assertEquals($expected,$result); 459 460 } 461 462} 463