1<?php 2 3namespace Sabre\DAV; 4use Sabre\HTTP; 5 6require_once 'Sabre/DAV/AbstractServer.php'; 7 8class ServerRangeTest extends AbstractServer{ 9 10 protected function getRootNode() { 11 12 return new FSExt\Directory(SABRE_TEMPDIR); 13 14 } 15 16 function testRange() { 17 18 $serverVars = [ 19 'REQUEST_URI' => '/test.txt', 20 'REQUEST_METHOD' => 'GET', 21 'HTTP_RANGE' => 'bytes=2-5', 22 ]; 23 $filename = SABRE_TEMPDIR . '/test.txt'; 24 25 $request = HTTP\Sapi::createFromServerArray($serverVars); 26 $this->server->httpRequest = ($request); 27 $this->server->exec(); 28 29 $this->assertEquals([ 30 'X-Sabre-Version' => [Version::VERSION], 31 'Content-Type' => ['application/octet-stream'], 32 'Content-Length' => [4], 33 'Content-Range' => ['bytes 2-5/13'], 34 'Last-Modified' => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))], 35 'ETag' => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'], 36 ], 37 $this->response->getHeaders() 38 ); 39 40 $this->assertEquals(206, $this->response->status); 41 $this->assertEquals('st c', stream_get_contents($this->response->body, 4)); 42 43 } 44 45 /** 46 * @depends testRange 47 */ 48 function testStartRange() { 49 50 $serverVars = [ 51 'REQUEST_URI' => '/test.txt', 52 'REQUEST_METHOD' => 'GET', 53 'HTTP_RANGE' => 'bytes=2-', 54 ]; 55 $filename = SABRE_TEMPDIR . '/test.txt'; 56 57 $request = HTTP\Sapi::createFromServerArray($serverVars); 58 $this->server->httpRequest = ($request); 59 $this->server->exec(); 60 61 $this->assertEquals([ 62 'X-Sabre-Version' => [Version::VERSION], 63 'Content-Type' => ['application/octet-stream'], 64 'Content-Length' => [11], 65 'Content-Range' => ['bytes 2-12/13'], 66 'Last-Modified' => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))], 67 'ETag' => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'], 68 ], 69 $this->response->getHeaders() 70 ); 71 72 $this->assertEquals(206, $this->response->status); 73 $this->assertEquals('st contents', stream_get_contents($this->response->body, 11)); 74 75 } 76 77 /** 78 * @depends testRange 79 */ 80 function testEndRange() { 81 82 $serverVars = [ 83 'REQUEST_URI' => '/test.txt', 84 'REQUEST_METHOD' => 'GET', 85 'HTTP_RANGE' => 'bytes=-8', 86 ]; 87 $filename = SABRE_TEMPDIR . '/test.txt'; 88 89 $request = HTTP\Sapi::createFromServerArray($serverVars); 90 $this->server->httpRequest = ($request); 91 $this->server->exec(); 92 93 $this->assertEquals([ 94 'X-Sabre-Version' => [Version::VERSION], 95 'Content-Type' => ['application/octet-stream'], 96 'Content-Length' => [8], 97 'Content-Range' => ['bytes 5-12/13'], 98 'Last-Modified' => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))], 99 'ETag' => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'], 100 ], 101 $this->response->getHeaders() 102 ); 103 104 $this->assertEquals(206, $this->response->status); 105 $this->assertEquals('contents', stream_get_contents($this->response->body, 8)); 106 107 } 108 109 /** 110 * @depends testRange 111 */ 112 function testTooHighRange() { 113 114 $serverVars = [ 115 'REQUEST_URI' => '/test.txt', 116 'REQUEST_METHOD' => 'GET', 117 'HTTP_RANGE' => 'bytes=100-200', 118 ]; 119 120 $request = HTTP\Sapi::createFromServerArray($serverVars); 121 $this->server->httpRequest = ($request); 122 $this->server->exec(); 123 124 $this->assertEquals(416, $this->response->status); 125 126 } 127 128 /** 129 * @depends testRange 130 */ 131 function testCrazyRange() { 132 133 $serverVars = [ 134 'REQUEST_URI' => '/test.txt', 135 'REQUEST_METHOD' => 'GET', 136 'HTTP_RANGE' => 'bytes=8-4', 137 ]; 138 139 $request = HTTP\Sapi::createFromServerArray($serverVars); 140 $this->server->httpRequest = ($request); 141 $this->server->exec(); 142 143 $this->assertEquals(416, $this->response->status); 144 145 } 146 147 /** 148 * @depends testRange 149 */ 150 function testIfRangeEtag() { 151 152 $node = $this->server->tree->getNodeForPath('test.txt'); 153 154 $serverVars = [ 155 'REQUEST_URI' => '/test.txt', 156 'REQUEST_METHOD' => 'GET', 157 'HTTP_RANGE' => 'bytes=2-5', 158 'HTTP_IF_RANGE' => $node->getETag(), 159 ]; 160 $filename = SABRE_TEMPDIR . '/test.txt'; 161 162 $request = HTTP\Sapi::createFromServerArray($serverVars); 163 $this->server->httpRequest = ($request); 164 $this->server->exec(); 165 166 $this->assertEquals([ 167 'X-Sabre-Version' => [Version::VERSION], 168 'Content-Type' => ['application/octet-stream'], 169 'Content-Length' => [4], 170 'Content-Range' => ['bytes 2-5/13'], 171 'Last-Modified' => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))], 172 'ETag' => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'], 173 ], 174 $this->response->getHeaders() 175 ); 176 177 $this->assertEquals(206, $this->response->status); 178 $this->assertEquals('st c', stream_get_contents($this->response->body, 4)); 179 180 } 181 182 /** 183 * @depends testRange 184 */ 185 function testIfRangeEtagIncorrect() { 186 187 $node = $this->server->tree->getNodeForPath('test.txt'); 188 189 $serverVars = [ 190 'REQUEST_URI' => '/test.txt', 191 'REQUEST_METHOD' => 'GET', 192 'HTTP_RANGE' => 'bytes=2-5', 193 'HTTP_IF_RANGE' => $node->getETag() . 'blabla', 194 ]; 195 $filename = SABRE_TEMPDIR . '/test.txt'; 196 197 $request = HTTP\Sapi::createFromServerArray($serverVars); 198 $this->server->httpRequest = ($request); 199 $this->server->exec(); 200 201 $this->assertEquals([ 202 'X-Sabre-Version' => [Version::VERSION], 203 'Content-Type' => ['application/octet-stream'], 204 'Content-Length' => [13], 205 'Last-Modified' => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))], 206 'ETag' => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'], 207 ], 208 $this->response->getHeaders() 209 ); 210 211 $this->assertEquals(200, $this->response->status); 212 $this->assertEquals('Test contents', stream_get_contents($this->response->body)); 213 214 } 215 216 /** 217 * @depends testRange 218 */ 219 function testIfRangeModificationDate() { 220 221 $node = $this->server->tree->getNodeForPath('test.txt'); 222 223 $serverVars = [ 224 'REQUEST_URI' => '/test.txt', 225 'REQUEST_METHOD' => 'GET', 226 'HTTP_RANGE' => 'bytes=2-5', 227 'HTTP_IF_RANGE' => 'tomorrow', 228 ]; 229 $filename = SABRE_TEMPDIR . '/test.txt'; 230 231 $request = HTTP\Sapi::createFromServerArray($serverVars); 232 $this->server->httpRequest = ($request); 233 $this->server->exec(); 234 235 $this->assertEquals([ 236 'X-Sabre-Version' => [Version::VERSION], 237 'Content-Type' => ['application/octet-stream'], 238 'Content-Length' => [4], 239 'Content-Range' => ['bytes 2-5/13'], 240 'Last-Modified' => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))], 241 'ETag' => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'], 242 ], 243 $this->response->getHeaders() 244 ); 245 246 $this->assertEquals(206, $this->response->status); 247 $this->assertEquals('st c', stream_get_contents($this->response->body, 4)); 248 249 } 250 251 /** 252 * @depends testRange 253 */ 254 function testIfRangeModificationDateModified() { 255 256 $node = $this->server->tree->getNodeForPath('test.txt'); 257 258 $serverVars = [ 259 'REQUEST_URI' => '/test.txt', 260 'REQUEST_METHOD' => 'GET', 261 'HTTP_RANGE' => 'bytes=2-5', 262 'HTTP_IF_RANGE' => '-2 years', 263 ]; 264 $filename = SABRE_TEMPDIR . '/test.txt'; 265 266 $request = HTTP\Sapi::createFromServerArray($serverVars); 267 $this->server->httpRequest = ($request); 268 $this->server->exec(); 269 270 $this->assertEquals([ 271 'X-Sabre-Version' => [Version::VERSION], 272 'Content-Type' => ['application/octet-stream'], 273 'Content-Length' => [13], 274 'Last-Modified' => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))], 275 'ETag' => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'], 276 ], 277 $this->response->getHeaders() 278 ); 279 280 $this->assertEquals(200, $this->response->status); 281 $this->assertEquals('Test contents', stream_get_contents($this->response->body)); 282 283 } 284} 285