ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ServerRangeTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\DAV;
4 
5 use DateTime;
6 use Sabre\HTTP;
7 
16 
17  protected $setupFiles = true;
18 
22  protected $lastModified;
23 
24  function setUp() {
25 
26  parent::setUp();
27  $this->server->createFile('files/test.txt', 'Test contents');
28 
29  $this->lastModified = HTTP\Util::toHTTPDate(
30  new DateTime('@' . $this->server->tree->getNodeForPath('files/test.txt')->getLastModified())
31  );
32 
33  $stream = popen('echo "Test contents"', 'r');
34  $streamingFile = new Mock\StreamingFile(
35  'no-seeking.txt',
36  $stream
37  );
38  $streamingFile->setSize(12);
39  $this->server->tree->getNodeForPath('files')->addNode($streamingFile);
40 
41  }
42 
43  function testRange() {
44 
45  $request = new HTTP\Request('GET', '/files/test.txt', ['Range' => 'bytes=2-5']);
46  $response = $this->request($request);
47 
48  $this->assertEquals([
49  'X-Sabre-Version' => [Version::VERSION],
50  'Content-Type' => ['application/octet-stream'],
51  'Content-Length' => [4],
52  'Content-Range' => ['bytes 2-5/13'],
53  'ETag' => ['"' . md5('Test contents') . '"'],
54  'Last-Modified' => [$this->lastModified],
55  ],
56  $response->getHeaders()
57  );
58  $this->assertEquals(206, $response->getStatus());
59  $this->assertEquals('st c', $response->getBodyAsString());
60 
61  }
62 
66  function testStartRange() {
67 
68  $request = new HTTP\Request('GET', '/files/test.txt', ['Range' => 'bytes=2-']);
69  $response = $this->request($request);
70 
71  $this->assertEquals([
72  'X-Sabre-Version' => [Version::VERSION],
73  'Content-Type' => ['application/octet-stream'],
74  'Content-Length' => [11],
75  'Content-Range' => ['bytes 2-12/13'],
76  'ETag' => ['"' . md5('Test contents') . '"'],
77  'Last-Modified' => [$this->lastModified],
78  ],
79  $response->getHeaders()
80  );
81 
82  $this->assertEquals(206, $response->getStatus());
83  $this->assertEquals('st contents', $response->getBodyAsString());
84 
85  }
86 
90  function testEndRange() {
91 
92  $request = new HTTP\Request('GET', '/files/test.txt', ['Range' => 'bytes=-8']);
93  $response = $this->request($request);
94 
95  $this->assertEquals([
96  'X-Sabre-Version' => [Version::VERSION],
97  'Content-Type' => ['application/octet-stream'],
98  'Content-Length' => [8],
99  'Content-Range' => ['bytes 5-12/13'],
100  'ETag' => ['"' . md5('Test contents') . '"'],
101  'Last-Modified' => [$this->lastModified],
102  ],
103  $response->getHeaders()
104  );
105 
106  $this->assertEquals(206, $response->getStatus());
107  $this->assertEquals('contents', $response->getBodyAsString());
108 
109  }
110 
114  function testTooHighRange() {
115 
116  $request = new HTTP\Request('GET', '/files/test.txt', ['Range' => 'bytes=100-200']);
117  $response = $this->request($request);
118 
119  $this->assertEquals(416, $response->getStatus());
120 
121  }
122 
126  function testCrazyRange() {
127 
128  $request = new HTTP\Request('GET', '/files/test.txt', ['Range' => 'bytes=8-4']);
129  $response = $this->request($request);
130 
131  $this->assertEquals(416, $response->getStatus());
132 
133  }
134 
136 
137  $request = new HTTP\Request('GET', '/files/no-seeking.txt', ['Range' => 'bytes=2-5']);
138  $response = $this->request($request);
139 
140  $this->assertEquals(206, $response->getStatus(), $response);
141  $this->assertEquals([
142  'X-Sabre-Version' => [Version::VERSION],
143  'Content-Type' => ['application/octet-stream'],
144  'Content-Length' => [4],
145  'Content-Range' => ['bytes 2-5/12'],
146  // 'ETag' => ['"' . md5('Test contents') . '"'],
147  'Last-Modified' => [$this->lastModified],
148  ],
149  $response->getHeaders()
150  );
151 
152  $this->assertEquals('st c', $response->getBodyAsString());
153 
154  }
155 
159  function testIfRangeEtag() {
160 
161  $request = new HTTP\Request('GET', '/files/test.txt', [
162  'Range' => 'bytes=2-5',
163  'If-Range' => '"' . md5('Test contents') . '"',
164  ]);
165  $response = $this->request($request);
166 
167  $this->assertEquals([
168  'X-Sabre-Version' => [Version::VERSION],
169  'Content-Type' => ['application/octet-stream'],
170  'Content-Length' => [4],
171  'Content-Range' => ['bytes 2-5/13'],
172  'ETag' => ['"' . md5('Test contents') . '"'],
173  'Last-Modified' => [$this->lastModified],
174  ],
175  $response->getHeaders()
176  );
177 
178  $this->assertEquals(206, $response->getStatus());
179  $this->assertEquals('st c', $response->getBodyAsString());
180 
181  }
182 
187 
188  $request = new HTTP\Request('GET', '/files/test.txt', [
189  'Range' => 'bytes=2-5',
190  'If-Range' => '"foobar"',
191  ]);
192  $response = $this->request($request);
193 
194  $this->assertEquals([
195  'X-Sabre-Version' => [Version::VERSION],
196  'Content-Type' => ['application/octet-stream'],
197  'Content-Length' => [13],
198  'ETag' => ['"' . md5('Test contents') . '"'],
199  'Last-Modified' => [$this->lastModified],
200  ],
201  $response->getHeaders()
202  );
203 
204  $this->assertEquals(200, $response->getStatus());
205  $this->assertEquals('Test contents', $response->getBodyAsString());
206 
207  }
208 
213 
214  $request = new HTTP\Request('GET', '/files/test.txt', [
215  'Range' => 'bytes=2-5',
216  'If-Range' => 'tomorrow',
217  ]);
218  $response = $this->request($request);
219 
220  $this->assertEquals([
221  'X-Sabre-Version' => [Version::VERSION],
222  'Content-Type' => ['application/octet-stream'],
223  'Content-Length' => [4],
224  'Content-Range' => ['bytes 2-5/13'],
225  'ETag' => ['"' . md5('Test contents') . '"'],
226  'Last-Modified' => [$this->lastModified],
227  ],
228  $response->getHeaders()
229  );
230 
231  $this->assertEquals(206, $response->getStatus());
232  $this->assertEquals('st c', $response->getBodyAsString());
233 
234  }
235 
240 
241  $request = new HTTP\Request('GET', '/files/test.txt', [
242  'Range' => 'bytes=2-5',
243  'If-Range' => '-2 years',
244  ]);
245  $response = $this->request($request);
246 
247  $this->assertEquals([
248  'X-Sabre-Version' => [Version::VERSION],
249  'Content-Type' => ['application/octet-stream'],
250  'Content-Length' => [13],
251  'ETag' => ['"' . md5('Test contents') . '"'],
252  'Last-Modified' => [$this->lastModified],
253  ],
254  $response->getHeaders()
255  );
256 
257  $this->assertEquals(200, $response->getStatus());
258  $this->assertEquals('Test contents', $response->getBodyAsString());
259 
260  }
261 
262 }
foreach($paths as $path) $request
Definition: asyncclient.php:32
testIfRangeEtagIncorrect()
testIfRangeEtag
static toHTTPDate(\DateTime $dateTime)
Transforms a DateTime object to HTTP&#39;s most common date format.
Definition: Util.php:69
This file tests HTTP requests that use the Range: header.
const VERSION
Full version number.
Definition: Version.php:17
$stream
PHP stream implementation.
testIfRangeModificationDate()
testIfRangeEtag
This class may be used as a basis for other webdav-related unittests.
testIfRangeModificationDateModified()
testIfRangeModificationDate
$lastModified
We need this string a lot.
Mock Streaming File File.
request($request, $expectedStatus=null)
Makes a request, and returns a response object.
$response