PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Sabre/DAV/ServerRangeTest.php

https://github.com/agilastic/sabre-dav
PHP | 270 lines | 184 code | 62 blank | 24 comment | 0 complexity | b062c9650fc37e89cb94b9c3aef23fb4 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 = HTTP\Request::createFromServerArray($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('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 = HTTP\Request::createFromServerArray($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('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 = HTTP\Request::createFromServerArray($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('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 = HTTP\Request::createFromServerArray($serverVars);
  88. $this->server->httpRequest = ($request);
  89. $this->server->exec();
  90. $this->assertEquals('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 = HTTP\Request::createFromServerArray($serverVars);
  102. $this->server->httpRequest = ($request);
  103. $this->server->exec();
  104. $this->assertEquals('416 Requested Range Not Satisfiable',$this->response->status);
  105. }
  106. /**
  107. * @depends testRange
  108. */
  109. function testIfRangeEtag() {
  110. $node = $this->server->tree->getNodeForPath('test.txt');
  111. $serverVars = array(
  112. 'REQUEST_URI' => '/test.txt',
  113. 'REQUEST_METHOD' => 'GET',
  114. 'HTTP_RANGE' => 'bytes=2-5',
  115. 'HTTP_IF_RANGE' => $node->getETag(),
  116. );
  117. $request = HTTP\Request::createFromServerArray($serverVars);
  118. $this->server->httpRequest = ($request);
  119. $this->server->exec();
  120. $this->assertEquals(array(
  121. 'Content-Type' => 'application/octet-stream',
  122. 'Content-Length' => 4,
  123. 'Content-Range' => 'bytes 2-5/13',
  124. 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
  125. 'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
  126. ),
  127. $this->response->headers
  128. );
  129. $this->assertEquals('206 Partial Content',$this->response->status);
  130. $this->assertEquals('st c', stream_get_contents($this->response->body));
  131. }
  132. /**
  133. * @depends testRange
  134. */
  135. function testIfRangeEtagIncorrect() {
  136. $node = $this->server->tree->getNodeForPath('test.txt');
  137. $serverVars = array(
  138. 'REQUEST_URI' => '/test.txt',
  139. 'REQUEST_METHOD' => 'GET',
  140. 'HTTP_RANGE' => 'bytes=2-5',
  141. 'HTTP_IF_RANGE' => $node->getETag() . 'blabla',
  142. );
  143. $request = HTTP\Request::createFromServerArray($serverVars);
  144. $this->server->httpRequest = ($request);
  145. $this->server->exec();
  146. $this->assertEquals(array(
  147. 'Content-Type' => 'application/octet-stream',
  148. 'Content-Length' => 13,
  149. 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
  150. 'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
  151. ),
  152. $this->response->headers
  153. );
  154. $this->assertEquals('200 OK',$this->response->status);
  155. $this->assertEquals('Test contents', stream_get_contents($this->response->body));
  156. }
  157. /**
  158. * @depends testRange
  159. */
  160. function testIfRangeModificationDate() {
  161. $node = $this->server->tree->getNodeForPath('test.txt');
  162. $serverVars = array(
  163. 'REQUEST_URI' => '/test.txt',
  164. 'REQUEST_METHOD' => 'GET',
  165. 'HTTP_RANGE' => 'bytes=2-5',
  166. 'HTTP_IF_RANGE' => 'tomorrow',
  167. );
  168. $request = HTTP\Request::createFromServerArray($serverVars);
  169. $this->server->httpRequest = ($request);
  170. $this->server->exec();
  171. $this->assertEquals(array(
  172. 'Content-Type' => 'application/octet-stream',
  173. 'Content-Length' => 4,
  174. 'Content-Range' => 'bytes 2-5/13',
  175. 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
  176. 'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
  177. ),
  178. $this->response->headers
  179. );
  180. $this->assertEquals('206 Partial Content',$this->response->status);
  181. $this->assertEquals('st c', stream_get_contents($this->response->body));
  182. }
  183. /**
  184. * @depends testRange
  185. */
  186. function testIfRangeModificationDateModified() {
  187. $node = $this->server->tree->getNodeForPath('test.txt');
  188. $serverVars = array(
  189. 'REQUEST_URI' => '/test.txt',
  190. 'REQUEST_METHOD' => 'GET',
  191. 'HTTP_RANGE' => 'bytes=2-5',
  192. 'HTTP_IF_RANGE' => '-2 years',
  193. );
  194. $request = HTTP\Request::createFromServerArray($serverVars);
  195. $this->server->httpRequest = ($request);
  196. $this->server->exec();
  197. $this->assertEquals(array(
  198. 'Content-Type' => 'application/octet-stream',
  199. 'Content-Length' => 13,
  200. 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
  201. 'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
  202. ),
  203. $this->response->headers
  204. );
  205. $this->assertEquals('200 OK',$this->response->status);
  206. $this->assertEquals('Test contents', stream_get_contents($this->response->body));
  207. }
  208. }