PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 3.3.1
GiveWP – Donation Plugin and Fundraising Platform v3.3.1
4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / vendor / vendor-prefixed / symfony / http-foundation / Tests / BinaryFileResponseTest.php
give / vendor / vendor-prefixed / symfony / http-foundation / Tests Last commit date
File 2 years ago Fixtures 2 years ago Session 2 years ago schema 2 years ago AcceptHeaderItemTest.php 2 years ago AcceptHeaderTest.php 2 years ago ApacheRequestTest.php 2 years ago BinaryFileResponseTest.php 2 years ago CookieTest.php 2 years ago ExpressionRequestMatcherTest.php 2 years ago FileBagTest.php 2 years ago HeaderBagTest.php 2 years ago IpUtilsTest.php 2 years ago JsonResponseTest.php 2 years ago ParameterBagTest.php 2 years ago RedirectResponseTest.php 2 years ago RequestMatcherTest.php 2 years ago RequestStackTest.php 2 years ago RequestTest.php 2 years ago ResponseFunctionalTest.php 2 years ago ResponseHeaderBagTest.php 2 years ago ResponseTest.php 2 years ago ResponseTestCase.php 2 years ago ServerBagTest.php 2 years ago StreamedResponseTest.php 2 years ago
BinaryFileResponseTest.php
391 lines
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 *
11 * Modified by impress-org on 23-January-2024 using Strauss.
12 * @see https://github.com/BrianHenryIE/strauss
13 */
14
15 namespace Give\Vendors\Symfony\Component\HttpFoundation\Tests;
16
17 use Give\Vendors\Symfony\Component\HttpFoundation\BinaryFileResponse;
18 use Give\Vendors\Symfony\Component\HttpFoundation\File\Stream;
19 use Give\Vendors\Symfony\Component\HttpFoundation\Request;
20 use Give\Vendors\Symfony\Component\HttpFoundation\ResponseHeaderBag;
21 use Give\Vendors\Symfony\Component\HttpFoundation\Tests\File\FakeFile;
22
23 class BinaryFileResponseTest extends ResponseTestCase
24 {
25 public function testConstruction()
26 {
27 $file = __DIR__.'/../README.md';
28 $response = new BinaryFileResponse($file, 404, ['X-Header' => 'Foo'], true, null, true, true);
29 $this->assertEquals(404, $response->getStatusCode());
30 $this->assertEquals('Foo', $response->headers->get('X-Header'));
31 $this->assertTrue($response->headers->has('ETag'));
32 $this->assertTrue($response->headers->has('Last-Modified'));
33 $this->assertFalse($response->headers->has('Content-Disposition'));
34
35 $response = BinaryFileResponse::create($file, 404, [], true, ResponseHeaderBag::DISPOSITION_INLINE);
36 $this->assertEquals(404, $response->getStatusCode());
37 $this->assertFalse($response->headers->has('ETag'));
38 $this->assertEquals('inline; filename="README.md"', $response->headers->get('Content-Disposition'));
39 }
40
41 public function testConstructWithNonAsciiFilename()
42 {
43 touch(sys_get_temp_dir().'/fööö.html');
44
45 $response = new BinaryFileResponse(sys_get_temp_dir().'/fööö.html', 200, [], true, 'attachment');
46
47 @unlink(sys_get_temp_dir().'/fööö.html');
48
49 $this->assertSame('fööö.html', $response->getFile()->getFilename());
50 }
51
52 public function testSetContent()
53 {
54 $this->expectException('LogicException');
55 $response = new BinaryFileResponse(__FILE__);
56 $response->setContent('foo');
57 }
58
59 public function testGetContent()
60 {
61 $response = new BinaryFileResponse(__FILE__);
62 $this->assertFalse($response->getContent());
63 }
64
65 public function testSetContentDispositionGeneratesSafeFallbackFilename()
66 {
67 $response = new BinaryFileResponse(__FILE__);
68 $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'föö.html');
69
70 $this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html', $response->headers->get('Content-Disposition'));
71 }
72
73 public function testSetContentDispositionGeneratesSafeFallbackFilenameForWronglyEncodedFilename()
74 {
75 $response = new BinaryFileResponse(__FILE__);
76
77 $iso88591EncodedFilename = utf8_decode('föö.html');
78 $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $iso88591EncodedFilename);
79
80 // the parameter filename* is invalid in this case (rawurldecode('f%F6%F6') does not provide a UTF-8 string but an ISO-8859-1 encoded one)
81 $this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%F6%F6.html', $response->headers->get('Content-Disposition'));
82 }
83
84 /**
85 * @dataProvider provideRanges
86 */
87 public function testRequests($requestRange, $offset, $length, $responseRange)
88 {
89 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
90
91 // do a request to get the ETag
92 $request = Request::create('/');
93 $response->prepare($request);
94 $etag = $response->headers->get('ETag');
95
96 // prepare a request for a range of the testing file
97 $request = Request::create('/');
98 $request->headers->set('If-Range', $etag);
99 $request->headers->set('Range', $requestRange);
100
101 $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
102 fseek($file, $offset);
103 $data = fread($file, $length);
104 fclose($file);
105
106 $this->expectOutputString($data);
107 $response = clone $response;
108 $response->prepare($request);
109 $response->sendContent();
110
111 $this->assertEquals(206, $response->getStatusCode());
112 $this->assertEquals($responseRange, $response->headers->get('Content-Range'));
113 $this->assertSame((string) $length, $response->headers->get('Content-Length'));
114 }
115
116 /**
117 * @dataProvider provideRanges
118 */
119 public function testRequestsWithoutEtag($requestRange, $offset, $length, $responseRange)
120 {
121 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
122
123 // do a request to get the LastModified
124 $request = Request::create('/');
125 $response->prepare($request);
126 $lastModified = $response->headers->get('Last-Modified');
127
128 // prepare a request for a range of the testing file
129 $request = Request::create('/');
130 $request->headers->set('If-Range', $lastModified);
131 $request->headers->set('Range', $requestRange);
132
133 $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
134 fseek($file, $offset);
135 $data = fread($file, $length);
136 fclose($file);
137
138 $this->expectOutputString($data);
139 $response = clone $response;
140 $response->prepare($request);
141 $response->sendContent();
142
143 $this->assertEquals(206, $response->getStatusCode());
144 $this->assertEquals($responseRange, $response->headers->get('Content-Range'));
145 }
146
147 public function provideRanges()
148 {
149 return [
150 ['bytes=1-4', 1, 4, 'bytes 1-4/35'],
151 ['bytes=-5', 30, 5, 'bytes 30-34/35'],
152 ['bytes=30-', 30, 5, 'bytes 30-34/35'],
153 ['bytes=30-30', 30, 1, 'bytes 30-30/35'],
154 ['bytes=30-34', 30, 5, 'bytes 30-34/35'],
155 ['bytes=30-40', 30, 5, 'bytes 30-34/35'],
156 ];
157 }
158
159 public function testRangeRequestsWithoutLastModifiedDate()
160 {
161 // prevent auto last modified
162 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'], true, null, false, false);
163
164 // prepare a request for a range of the testing file
165 $request = Request::create('/');
166 $request->headers->set('If-Range', date('D, d M Y H:i:s').' GMT');
167 $request->headers->set('Range', 'bytes=1-4');
168
169 $this->expectOutputString(file_get_contents(__DIR__.'/File/Fixtures/test.gif'));
170 $response = clone $response;
171 $response->prepare($request);
172 $response->sendContent();
173
174 $this->assertEquals(200, $response->getStatusCode());
175 $this->assertNull($response->headers->get('Content-Range'));
176 }
177
178 /**
179 * @dataProvider provideFullFileRanges
180 */
181 public function testFullFileRequests($requestRange)
182 {
183 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
184
185 // prepare a request for a range of the testing file
186 $request = Request::create('/');
187 $request->headers->set('Range', $requestRange);
188
189 $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
190 $data = fread($file, 35);
191 fclose($file);
192
193 $this->expectOutputString($data);
194 $response = clone $response;
195 $response->prepare($request);
196 $response->sendContent();
197
198 $this->assertEquals(200, $response->getStatusCode());
199 }
200
201 public function provideFullFileRanges()
202 {
203 return [
204 ['bytes=0-'],
205 ['bytes=0-34'],
206 ['bytes=-35'],
207 // Syntactical invalid range-request should also return the full resource
208 ['bytes=20-10'],
209 ['bytes=50-40'],
210 // range units other than bytes must be ignored
211 ['unknown=10-20'],
212 ];
213 }
214
215 public function testRangeOnPostMethod()
216 {
217 $request = Request::create('/', 'POST');
218 $request->headers->set('Range', 'bytes=10-20');
219 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
220
221 $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
222 $data = fread($file, 35);
223 fclose($file);
224
225 $this->expectOutputString($data);
226 $response = clone $response;
227 $response->prepare($request);
228 $response->sendContent();
229
230 $this->assertSame(200, $response->getStatusCode());
231 $this->assertSame('35', $response->headers->get('Content-Length'));
232 $this->assertNull($response->headers->get('Content-Range'));
233 }
234
235 public function testUnpreparedResponseSendsFullFile()
236 {
237 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200);
238
239 $data = file_get_contents(__DIR__.'/File/Fixtures/test.gif');
240
241 $this->expectOutputString($data);
242 $response = clone $response;
243 $response->sendContent();
244
245 $this->assertEquals(200, $response->getStatusCode());
246 }
247
248 /**
249 * @dataProvider provideInvalidRanges
250 */
251 public function testInvalidRequests($requestRange)
252 {
253 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
254
255 // prepare a request for a range of the testing file
256 $request = Request::create('/');
257 $request->headers->set('Range', $requestRange);
258
259 $response = clone $response;
260 $response->prepare($request);
261 $response->sendContent();
262
263 $this->assertEquals(416, $response->getStatusCode());
264 $this->assertEquals('bytes */35', $response->headers->get('Content-Range'));
265 }
266
267 public function provideInvalidRanges()
268 {
269 return [
270 ['bytes=-40'],
271 ['bytes=40-50'],
272 ];
273 }
274
275 /**
276 * @dataProvider provideXSendfileFiles
277 */
278 public function testXSendfile($file)
279 {
280 $request = Request::create('/');
281 $request->headers->set('X-Sendfile-Type', 'X-Sendfile');
282
283 BinaryFileResponse::trustXSendfileTypeHeader();
284 $response = BinaryFileResponse::create($file, 200, ['Content-Type' => 'application/octet-stream']);
285 $response->prepare($request);
286
287 $this->expectOutputString('');
288 $response->sendContent();
289
290 $this->assertStringContainsString('README.md', $response->headers->get('X-Sendfile'));
291 }
292
293 public function provideXSendfileFiles()
294 {
295 return [
296 [__DIR__.'/../README.md'],
297 ['file://'.__DIR__.'/../README.md'],
298 ];
299 }
300
301 /**
302 * @dataProvider getSampleXAccelMappings
303 */
304 public function testXAccelMapping($realpath, $mapping, $virtual)
305 {
306 $request = Request::create('/');
307 $request->headers->set('X-Sendfile-Type', 'X-Accel-Redirect');
308 $request->headers->set('X-Accel-Mapping', $mapping);
309
310 $file = new FakeFile($realpath, __DIR__.'/File/Fixtures/test');
311
312 BinaryFileResponse::trustXSendfileTypeHeader();
313 $response = new BinaryFileResponse($file, 200, ['Content-Type' => 'application/octet-stream']);
314 $reflection = new \ReflectionObject($response);
315 $property = $reflection->getProperty('file');
316 $property->setAccessible(true);
317 $property->setValue($response, $file);
318
319 $response->prepare($request);
320 $this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
321 }
322
323 public function testDeleteFileAfterSend()
324 {
325 $request = Request::create('/');
326
327 $path = __DIR__.'/File/Fixtures/to_delete';
328 touch($path);
329 $realPath = realpath($path);
330 $this->assertFileExists($realPath);
331
332 $response = new BinaryFileResponse($realPath, 200, ['Content-Type' => 'application/octet-stream']);
333 $response->deleteFileAfterSend(true);
334
335 $response->prepare($request);
336 $response->sendContent();
337
338 $this->assertFileDoesNotExist($path);
339 }
340
341 public function testAcceptRangeOnUnsafeMethods()
342 {
343 $request = Request::create('/', 'POST');
344 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
345 $response->prepare($request);
346
347 $this->assertEquals('none', $response->headers->get('Accept-Ranges'));
348 }
349
350 public function testAcceptRangeNotOverriden()
351 {
352 $request = Request::create('/', 'POST');
353 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
354 $response->headers->set('Accept-Ranges', 'foo');
355 $response->prepare($request);
356
357 $this->assertEquals('foo', $response->headers->get('Accept-Ranges'));
358 }
359
360 public function getSampleXAccelMappings()
361 {
362 return [
363 ['/var/www/var/www/files/foo.txt', '/var/www/=/files/', '/files/var/www/files/foo.txt'],
364 ['/home/foo/bar.txt', '/var/www/=/files/,/home/foo/=/baz/', '/baz/bar.txt'],
365 ['/tmp/bar.txt', '"/var/www/"="/files/", "/home/Foo/"="/baz/"', null],
366 ];
367 }
368
369 public function testStream()
370 {
371 $request = Request::create('/');
372 $response = new BinaryFileResponse(new Stream(__DIR__.'/../README.md'), 200, ['Content-Type' => 'text/plain']);
373 $response->prepare($request);
374
375 $this->assertNull($response->headers->get('Content-Length'));
376 }
377
378 protected function provideResponse()
379 {
380 return new BinaryFileResponse(__DIR__.'/../README.md', 200, ['Content-Type' => 'application/octet-stream']);
381 }
382
383 public static function tearDownAfterClass()
384 {
385 $path = __DIR__.'/../Fixtures/to_delete';
386 if (file_exists($path)) {
387 @unlink($path);
388 }
389 }
390 }
391