PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Sabre/DAV/ServerRangeTest.php

https://github.com/KOLANICH/SabreDAV
PHP | 274 lines | 184 code | 62 blank | 28 comment | 0 complexity | 2e913d23344cdc8f3bbcad503756c0bb MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\DAV;
  3. use Sabre\HTTP;
  4. require_once 'Sabre/DAV/AbstractServer.php';
  5. class ServerRangeTest extends AbstractServer{
  6. protected function getRootNode() {
  7. return new FSExt\Directory(SABRE_TEMPDIR);
  8. }
  9. function testRange() {
  10. $serverVars = array(
  11. 'REQUEST_URI' => '/test.txt',
  12. 'REQUEST_METHOD' => 'GET',
  13. 'HTTP_RANGE' => 'bytes=2-5',
  14. );
  15. $request = new HTTP\Request($serverVars);
  16. $this->server->httpRequest = ($request);
  17. $this->server->exec();
  18. $this->assertEquals(array(
  19. 'Content-Type' => 'application/octet-stream',
  20. 'Content-Length' => 4,
  21. 'Content-Range' => 'bytes 2-5/13',
  22. 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
  23. 'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')). '"',
  24. ),
  25. $this->response->headers
  26. );
  27. $this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
  28. $this->assertEquals('st c', stream_get_contents($this->response->body));
  29. }
  30. /**
  31. * @depends testRange
  32. */
  33. function testStartRange() {
  34. $serverVars = array(
  35. 'REQUEST_URI' => '/test.txt',
  36. 'REQUEST_METHOD' => 'GET',
  37. 'HTTP_RANGE' => 'bytes=2-',
  38. );
  39. $request = new HTTP\Request($serverVars);
  40. $this->server->httpRequest = ($request);
  41. $this->server->exec();
  42. $this->assertEquals(array(
  43. 'Content-Type' => 'application/octet-stream',
  44. 'Content-Length' => 11,
  45. 'Content-Range' => 'bytes 2-12/13',
  46. 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
  47. 'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
  48. ),
  49. $this->response->headers
  50. );
  51. $this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
  52. $this->assertEquals('st contents', stream_get_contents($this->response->body));
  53. }
  54. /**
  55. * @depends testRange
  56. */
  57. function testEndRange() {
  58. $serverVars = array(
  59. 'REQUEST_URI' => '/test.txt',
  60. 'REQUEST_METHOD' => 'GET',
  61. 'HTTP_RANGE' => 'bytes=-8',
  62. );
  63. $request = new HTTP\Request($serverVars);
  64. $this->server->httpRequest = ($request);
  65. $this->server->exec();
  66. $this->assertEquals(array(
  67. 'Content-Type' => 'application/octet-stream',
  68. 'Content-Length' => 8,
  69. 'Content-Range' => 'bytes 5-12/13',
  70. 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
  71. 'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')). '"',
  72. ),
  73. $this->response->headers
  74. );
  75. $this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
  76. $this->assertEquals('contents', stream_get_contents($this->response->body));
  77. }
  78. /**
  79. * @depends testRange
  80. */
  81. function testTooHighRange() {
  82. $serverVars = array(
  83. 'REQUEST_URI' => '/test.txt',
  84. 'REQUEST_METHOD' => 'GET',
  85. 'HTTP_RANGE' => 'bytes=100-200',
  86. );
  87. $request = new HTTP\Request($serverVars);
  88. $this->server->httpRequest = ($request);
  89. $this->server->exec();
  90. $this->assertEquals('HTTP/1.1 416 Requested Range Not Satisfiable',$this->response->status);
  91. }
  92. /**
  93. * @depends testRange
  94. */
  95. function testCrazyRange() {
  96. $serverVars = array(
  97. 'REQUEST_URI' => '/test.txt',
  98. 'REQUEST_METHOD' => 'GET',
  99. 'HTTP_RANGE' => 'bytes=8-4',
  100. );
  101. $request = new HTTP\Request($serverVars);
  102. $this->server->httpRequest = ($request);
  103. $this->server->exec();
  104. $this->assertEquals('HTTP/1.1 416 Requested Range Not Satisfiable',$this->response->status);
  105. }
  106. /**
  107. * @depends testRange
  108. * @covers \Sabre\DAV\Server::httpGet
  109. */
  110. function testIfRangeEtag() {
  111. $node = $this->server->tree->getNodeForPath('test.txt');
  112. $serverVars = array(
  113. 'REQUEST_URI' => '/test.txt',
  114. 'REQUEST_METHOD' => 'GET',
  115. 'HTTP_RANGE' => 'bytes=2-5',
  116. 'HTTP_IF_RANGE' => $node->getETag(),
  117. );
  118. $request = new HTTP\Request($serverVars);
  119. $this->server->httpRequest = ($request);
  120. $this->server->exec();
  121. $this->assertEquals(array(
  122. 'Content-Type' => 'application/octet-stream',
  123. 'Content-Length' => 4,
  124. 'Content-Range' => 'bytes 2-5/13',
  125. 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
  126. 'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
  127. ),
  128. $this->response->headers
  129. );
  130. $this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
  131. $this->assertEquals('st c', stream_get_contents($this->response->body));
  132. }
  133. /**
  134. * @depends testRange
  135. * @covers \Sabre\DAV\Server::httpGet
  136. */
  137. function testIfRangeEtagIncorrect() {
  138. $node = $this->server->tree->getNodeForPath('test.txt');
  139. $serverVars = array(
  140. 'REQUEST_URI' => '/test.txt',
  141. 'REQUEST_METHOD' => 'GET',
  142. 'HTTP_RANGE' => 'bytes=2-5',
  143. 'HTTP_IF_RANGE' => $node->getETag() . 'blabla',
  144. );
  145. $request = new HTTP\Request($serverVars);
  146. $this->server->httpRequest = ($request);
  147. $this->server->exec();
  148. $this->assertEquals(array(
  149. 'Content-Type' => 'application/octet-stream',
  150. 'Content-Length' => 13,
  151. 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
  152. 'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
  153. ),
  154. $this->response->headers
  155. );
  156. $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
  157. $this->assertEquals('Test contents', stream_get_contents($this->response->body));
  158. }
  159. /**
  160. * @depends testRange
  161. * @covers \Sabre\DAV\Server::httpGet
  162. */
  163. function testIfRangeModificationDate() {
  164. $node = $this->server->tree->getNodeForPath('test.txt');
  165. $serverVars = array(
  166. 'REQUEST_URI' => '/test.txt',
  167. 'REQUEST_METHOD' => 'GET',
  168. 'HTTP_RANGE' => 'bytes=2-5',
  169. 'HTTP_IF_RANGE' => 'tomorrow',
  170. );
  171. $request = new HTTP\Request($serverVars);
  172. $this->server->httpRequest = ($request);
  173. $this->server->exec();
  174. $this->assertEquals(array(
  175. 'Content-Type' => 'application/octet-stream',
  176. 'Content-Length' => 4,
  177. 'Content-Range' => 'bytes 2-5/13',
  178. 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
  179. 'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
  180. ),
  181. $this->response->headers
  182. );
  183. $this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
  184. $this->assertEquals('st c', stream_get_contents($this->response->body));
  185. }
  186. /**
  187. * @depends testRange
  188. * @covers \Sabre\DAV\Server::httpGet
  189. */
  190. function testIfRangeModificationDateModified() {
  191. $node = $this->server->tree->getNodeForPath('test.txt');
  192. $serverVars = array(
  193. 'REQUEST_URI' => '/test.txt',
  194. 'REQUEST_METHOD' => 'GET',
  195. 'HTTP_RANGE' => 'bytes=2-5',
  196. 'HTTP_IF_RANGE' => '-2 years',
  197. );
  198. $request = new HTTP\Request($serverVars);
  199. $this->server->httpRequest = ($request);
  200. $this->server->exec();
  201. $this->assertEquals(array(
  202. 'Content-Type' => 'application/octet-stream',
  203. 'Content-Length' => 13,
  204. 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
  205. 'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
  206. ),
  207. $this->response->headers
  208. );
  209. $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
  210. $this->assertEquals('Test contents', stream_get_contents($this->response->body));
  211. }
  212. }