Debian Codegraph @

tests/phpunit/unit/includes/filebackend/HTTPFileStreamerTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Utils\MWTimestamp;
4 use PHPUnit\Framework\TestCase;
5 use Wikimedia\FileBackend\HTTPFileStreamer;
6
7 /**
8 * @covers \Wikimedia\FileBackend\HTTPFileStreamer
9 */
10 class HTTPFileStreamerTest extends TestCase {
11
12 private const FILE = MW_INSTALL_PATH . '/tests/phpunit/data/media/test.jpg';
13
14 /** @var int */
15 private $obLevel = null;
16
17 protected function setUp(): void {
18 $this->obLevel = ob_get_level();
19 ob_start();
20 parent::setUp();
21 }
22
23 protected function tearDown(): void {
24 while ( ob_get_level() > $this->obLevel ) {
25 ob_end_clean();
26 }
27 parent::tearDown();
28 }
29
30 /**
31 * @dataProvider providePreprocessHeaders
32 */
33 public function testPreprocessHeaders( array $input, array $expectedRaw, array $expectedOpt ) {
34 [ $actualRaw, $actualOpt ] = HTTPFileStreamer::preprocessHeaders( $input );
35 $this->assertSame( $expectedRaw, $actualRaw );
36 $this->assertSame( $expectedOpt, $actualOpt );
37 }
38
39 public static function providePreprocessHeaders() {
40 return [
41 [
42 [ 'Vary' => 'cookie', 'Cache-Control' => 'private' ],
43 [ 'Vary: cookie', 'Cache-Control: private' ],
44 [],
45 ],
46 [
47 [
48 'Range' => 'bytes=(123-456)',
49 'Content-Type' => 'video/mp4',
50 'If-Modified-Since' => 'Wed, 21 Oct 2015 07:28:00 GMT',
51 ],
52 [ 'Content-Type: video/mp4' ],
53 [ 'range' => 'bytes=(123-456)', 'if-modified-since' => 'Wed, 21 Oct 2015 07:28:00 GMT' ],
54 ],
55 ];
56 }
57
58 private function makeStreamerParams( &$actual ) {
59 $actual = [
60 'reset' => 0,
61 'file' => null,
62 'range' => null,
63 'headers' => [],
64 'status' => 200,
65 ];
66
67 return [
68 'obResetFunc' => static function () use ( &$actual ) {
69 $actual['reset']++;
70 },
71 'streamMimeFunc' => static function () {
72 return 'test/test';
73 },
74 'headerFunc' => static function ( $header ) use ( &$actual ) {
75 if ( preg_match( '/^HTTP.*? (\d+)/', $header, $m ) ) {
76 $actual['status'] = (int)$m[1];
77 }
78
79 $actual['headers'][] = $header;
80 },
81 ];
82 }
83
84 public static function provideStream() {
85 $mtime = filemtime( self::FILE );
86 $size = filesize( self::FILE );
87
88 $modified = MWTimestamp::convert( TS_RFC2822, $mtime );
89
90 yield 'simple stream' => [
91 [],
92 [],
93 [
94 "Last-Modified: $modified",
95 'Content-type: test/test',
96 "Content-Length: $size"
97 ]
98 ];
99
100 yield 'extra header' => [
101 [ 'Extra: yes' ],
102 [],
103 [
104 'Extra: yes',
105 "Last-Modified: $modified",
106 'Content-type: test/test',
107 "Content-Length: $size"
108 ]
109 ];
110
111 yield 'modified' => [
112 [],
113 [
114 'if-modified-since' => MWTimestamp::convert( TS_RFC2822, $mtime - 1 )
115 ],
116 [
117 "Last-Modified: $modified",
118 'Content-type: test/test',
119 "Content-Length: $size"
120 ]
121 ];
122
123 yield 'not modified' => [
124 [],
125 [
126 'if-modified-since' => MWTimestamp::convert( TS_RFC2822, $mtime + 1 )
127 ],
128 [
129 'HTTP/1.1 304 Not Modified'
130 ]
131 ];
132 }
133
134 /**
135 * @dataProvider provideStream
136 */
137 public function testStream( $extraHeaders, $reqHeaders, $expectedHeaders ) {
138 $params = $this->makeStreamerParams( $actual );
139
140 $streamer = new HTTPFileStreamer( self::FILE, $params );
141 $ok = $streamer->stream( $extraHeaders, true, $reqHeaders );
142
143 $this->assertTrue( $ok );
144
145 if ( !isset( $actual['status'] ) ) {
146 $this->assertSame( self::FILE, $actual['file'] );
147 }
148 $this->assertSame( 1, $actual['reset'] );
149
150 foreach ( $expectedHeaders as $exp ) {
151 $this->assertContains( $exp, $actual['headers'] );
152 }
153 }
154
155 public static function provideStream_range() {
156 $filesize = filesize( self::FILE );
157 $length = $filesize;
158 $start = 0;
159 $end = $length - 1;
160
161 yield 'all' => [
162 'bytes=-',
163 206,
164 [
165 "Content-Length: $length",
166 "Content-Range: bytes $start-$end/$filesize",
167 ],
168 [ $start, $end, $length ]
169 ];
170
171 $length = 100;
172 $start = 0;
173 $end = $length - 1;
174 yield 'prefix' => [
175 'bytes=0-99',
176 206,
177 [
178 "Content-Length: $length",
179 "Content-Range: bytes $start-$end/$filesize",
180 ],
181 [ $start, $end, $length ]
182 ];
183
184 $length = 100;
185 $start = 100;
186 $end = $start + $length - 1;
187 yield 'middle' => [
188 'bytes=100-199',
189 206,
190 [
191 "Content-Length: $length",
192 "Content-Range: bytes $start-$end/$filesize",
193 ],
194 [ $start, $end, $length ]
195 ];
196
197 $length = 100;
198 $start = $filesize - $length;
199 $end = $filesize - 1;
200 yield 'suffix' => [
201 'bytes=-100',
202 206,
203 [
204 "Content-Length: $length",
205 "Content-Range: bytes $start-$end/$filesize",
206 ],
207 [ $start, $end, $length ]
208 ];
209
210 $length = $filesize - 100;
211 $start = 100;
212 $end = $start + $length - 1;
213 yield 'remaining' => [
214 'bytes=100-',
215 206,
216 [
217 "Content-Length: $length",
218 "Content-Range: bytes $start-$end/$filesize",
219 ],
220 [ $start, $end, $length ]
221 ];
222
223 yield 'impossible' => [
224 'bytes=1000-2000',
225 416,
226 [ 'Cache-Control: no-cache' ],
227 null
228 ];
229
230 yield 'unrecognized' => [
231 'foo=1000-2000',
232 200,
233 [ "Content-Length: $filesize" ],
234 null
235 ];
236 }
237
238 /**
239 * Check parsing of the Range header and corresponding response headers.
240 *
241 * @dataProvider provideStream_range
242 */
243 public function testStream_range(
244 $range,
245 $expectedStatus,
246 $expectedHeaders,
247 $expectedRange
248 ) {
249 $params = $this->makeStreamerParams( $actual );
250
251 $streamer = new HTTPFileStreamer( self::FILE, $params );
252 $streamer->stream( [], true, [ 'range' => $range ] );
253
254 $this->assertSame( $expectedStatus, $actual['status'] );
255
256 foreach ( $expectedHeaders as $exp ) {
257 $this->assertContains( $exp, $actual['headers'] );
258 }
259
260 if ( $expectedStatus < 300 ) {
261 [ $start, , $length ] = $expectedRange;
262 $this->assertBufferContainsFile( $start, $length );
263 }
264 }
265
266 /**
267 * Check we ar ereaching the correct chunks from the file
268 */
269 public function testStream_chunks() {
270 $data = file_get_contents( self::FILE );
271
272 $params = $this->makeStreamerParams( $actual );
273
274 $streamer = new HTTPFileStreamer( self::FILE, $params );
275
276 // grab a chunk from the middle
277 ob_start();
278 $streamer->stream( [], true, [ 'range' => 'bytes=100-199' ] );
279 $chunk1 = ob_get_clean();
280
281 // get the start
282 ob_start();
283 $streamer->stream( [], true, [ 'range' => 'bytes=0-99' ] );
284 $chunk2 = ob_get_clean();
285
286 // fetch the rest
287 ob_start();
288 $streamer->stream( [], true, [ 'range' => 'bytes=200-' ] );
289 $chunk3 = ob_get_clean();
290
291 $this->assertSame( $data, $chunk2 . $chunk1 . $chunk3 );
292 }
293
294 public function testStream_404() {
295 $params = $this->makeStreamerParams( $actual );
296
297 $streamer = new HTTPFileStreamer( 'Xyzzy.jpg', $params );
298
299 ob_start();
300 $ok = $streamer->stream();
301 $data = ob_get_clean();
302
303 $this->assertFalse( $ok );
304 $this->assertStringContainsString( '<h1>File not found</h1>', $data );
305 }
306
307 public function testStream_allowOB() {
308 $params = $this->makeStreamerParams( $actual );
309
310 $streamer = new HTTPFileStreamer( self::FILE, $params );
311 $streamer->stream( [], true, [], HTTPFileStreamer::STREAM_ALLOW_OB );
312
313 // Expect no buffer reset, even though the file was sent
314 $this->assertSame( 0, $actual['reset'] );
315 $this->assertBufferContainsFile();
316 }
317
318 public function testStream_headless() {
319 $params = $this->makeStreamerParams( $actual );
320
321 $streamer = new HTTPFileStreamer( self::FILE, $params );
322 $streamer->stream( [ 'Extra: yes' ], true, [], HTTPFileStreamer::STREAM_HEADLESS );
323
324 // Expect no headers, not even "extra"
325 $this->assertSame( [], $actual['headers'] );
326 $this->assertBufferContainsFile();
327 }
328
329 private function assertBufferContainsFile( ?int $offset = null, ?int $length = null ) {
330 $actual = ob_get_clean();
331 $expected = file_get_contents( self::FILE );
332
333 if ( $offset !== null || $length !== null ) {
334 $expected = substr( $expected, $offset ?? 0, $length );
335 }
336
337 $this->assertSame( $expected, $actual );
338 }
339
340 }