PluginTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace Sabre\DAV\Browser;
  3. use Sabre\DAV;
  4. use Sabre\HTTP;
  5. require_once 'Sabre/DAV/AbstractServer.php';
  6. class PluginTest extends DAV\AbstractServer{
  7. protected $plugin;
  8. function setUp() {
  9. parent::setUp();
  10. $this->server->addPlugin($this->plugin = new Plugin());
  11. $this->server->tree->getNodeForPath('')->createDirectory('dir2');
  12. }
  13. function testCollectionGet() {
  14. $request = new HTTP\Request('GET', '/dir');
  15. $this->server->httpRequest = $request;
  16. $this->server->exec();
  17. $this->assertEquals(200, $this->response->getStatus(), "Incorrect status received. Full response body: " . $this->response->getBodyAsString());
  18. $this->assertEquals(
  19. [
  20. 'X-Sabre-Version' => [DAV\Version::VERSION],
  21. 'Content-Type' => ['text/html; charset=utf-8'],
  22. 'Content-Security-Policy' => ["img-src 'self'; style-src 'self';"]
  23. ],
  24. $this->response->getHeaders()
  25. );
  26. $body = $this->response->getBodyAsString();
  27. $this->assertTrue(strpos($body, '<title>dir') !== false, $body);
  28. $this->assertTrue(strpos($body, '<a href="/dir/child.txt">')!==false);
  29. }
  30. /**
  31. * Adding the If-None-Match should have 0 effect, but it threw an error.
  32. */
  33. function testCollectionGetIfNoneMatch() {
  34. $request = new HTTP\Request('GET', '/dir');
  35. $request->setHeader('If-None-Match', '"foo-bar"');
  36. $this->server->httpRequest = $request;
  37. $this->server->exec();
  38. $this->assertEquals(200, $this->response->getStatus(), "Incorrect status received. Full response body: " . $this->response->getBodyAsString());
  39. $this->assertEquals(
  40. [
  41. 'X-Sabre-Version' => [DAV\Version::VERSION],
  42. 'Content-Type' => ['text/html; charset=utf-8'],
  43. 'Content-Security-Policy' => ["img-src 'self'; style-src 'self';"]
  44. ],
  45. $this->response->getHeaders()
  46. );
  47. $body = $this->response->getBodyAsString();
  48. $this->assertTrue(strpos($body, '<title>dir') !== false, $body);
  49. $this->assertTrue(strpos($body, '<a href="/dir/child.txt">')!==false);
  50. }
  51. function testCollectionGetRoot() {
  52. $request = new HTTP\Request('GET', '/');
  53. $this->server->httpRequest = ($request);
  54. $this->server->exec();
  55. $this->assertEquals(200, $this->response->status, "Incorrect status received. Full response body: " . $this->response->getBodyAsString());
  56. $this->assertEquals(
  57. [
  58. 'X-Sabre-Version' => [DAV\Version::VERSION],
  59. 'Content-Type' => ['text/html; charset=utf-8'],
  60. 'Content-Security-Policy' => ["img-src 'self'; style-src 'self';"]
  61. ],
  62. $this->response->getHeaders()
  63. );
  64. $body = $this->response->getBodyAsString();
  65. $this->assertTrue(strpos($body, '<title>/') !== false, $body);
  66. $this->assertTrue(strpos($body, '<a href="/dir/">')!==false);
  67. $this->assertTrue(strpos($body, '<span class="btn disabled">')!==false);
  68. }
  69. function testGETPassthru() {
  70. $request = new HTTP\Request('GET', '/random');
  71. $response = new HTTP\Response();
  72. $this->assertNull(
  73. $this->plugin->httpGet($request, $response)
  74. );
  75. }
  76. function testPostOtherContentType() {
  77. $request = new HTTP\Request('POST', '/', ['Content-Type' => 'text/xml']);
  78. $this->server->httpRequest = $request;
  79. $this->server->exec();
  80. $this->assertEquals(501, $this->response->status);
  81. }
  82. function testPostNoSabreAction() {
  83. $request = new HTTP\Request('POST', '/', ['Content-Type' => 'application/x-www-form-urlencoded']);
  84. $request->setPostData([]);
  85. $this->server->httpRequest = $request;
  86. $this->server->exec();
  87. $this->assertEquals(501, $this->response->status);
  88. }
  89. function testPostMkCol() {
  90. $serverVars = array(
  91. 'REQUEST_URI' => '/',
  92. 'REQUEST_METHOD' => 'POST',
  93. 'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
  94. );
  95. $postVars = array(
  96. 'sabreAction' => 'mkcol',
  97. 'name' => 'new_collection',
  98. );
  99. $request = HTTP\Sapi::createFromServerArray($serverVars);
  100. $request->setPostData($postVars);
  101. $this->server->httpRequest = $request;
  102. $this->server->exec();
  103. $this->assertEquals(302, $this->response->status);
  104. $this->assertEquals(array(
  105. 'X-Sabre-Version' => [DAV\Version::VERSION],
  106. 'Location' => ['/'],
  107. ), $this->response->getHeaders());
  108. $this->assertTrue(is_dir(SABRE_TEMPDIR . '/new_collection'));
  109. }
  110. function testGetAsset() {
  111. $request = new HTTP\Request('GET', '/?sabreAction=asset&assetName=favicon.ico');
  112. $this->server->httpRequest = $request;
  113. $this->server->exec();
  114. $this->assertEquals(200, $this->response->getStatus(), 'Error: ' . $this->response->body);
  115. $this->assertEquals([
  116. 'X-Sabre-Version' => [DAV\Version::VERSION],
  117. 'Content-Type' => ['image/vnd.microsoft.icon'],
  118. 'Content-Length' => ['4286'],
  119. 'Cache-Control' => ['public, max-age=1209600'],
  120. 'Content-Security-Policy' => ["img-src 'self'; style-src 'self';"]
  121. ], $this->response->getHeaders());
  122. }
  123. function testGetAsset404() {
  124. $request = new HTTP\Request('GET', '/?sabreAction=asset&assetName=flavicon.ico');
  125. $this->server->httpRequest = $request;
  126. $this->server->exec();
  127. $this->assertEquals(404, $this->response->getStatus(), 'Error: ' . $this->response->body);
  128. }
  129. function testGetAssetEscapeBasePath() {
  130. $request = new HTTP\Request('GET', '/?sabreAction=asset&assetName=./../assets/favicon.ico');
  131. $this->server->httpRequest = $request;
  132. $this->server->exec();
  133. $this->assertEquals(404, $this->response->getStatus(), 'Error: ' . $this->response->body);
  134. }
  135. }