11
11
12
12
namespace Symfony \Component \HttpFoundation \Tests ;
13
13
14
+ use Symfony \Bridge \PhpUnit \ExpectDeprecationTrait ;
14
15
use Symfony \Component \HttpFoundation \BinaryFileResponse ;
15
16
use Symfony \Component \HttpFoundation \File \Stream ;
16
17
use Symfony \Component \HttpFoundation \Request ;
19
20
20
21
class BinaryFileResponseTest extends ResponseTestCase
21
22
{
23
+ use ExpectDeprecationTrait;
24
+
22
25
public function testConstruction ()
23
26
{
24
27
$ file = __DIR__ .'/../README.md ' ;
@@ -29,6 +32,26 @@ public function testConstruction()
29
32
$ this ->assertTrue ($ response ->headers ->has ('Last-Modified ' ));
30
33
$ this ->assertFalse ($ response ->headers ->has ('Content-Disposition ' ));
31
34
35
+ $ response = new BinaryFileResponse ($ 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
+ /**
42
+ * @group legacy
43
+ */
44
+ public function testConstructionLegacy ()
45
+ {
46
+ $ file = __DIR__ .'/../README.md ' ;
47
+ $ this ->expectDeprecation ('Since symfony/http-foundation 5.2: The "Symfony\Component\HttpFoundation\BinaryFileResponse::create()" method is deprecated, use "new Symfony\Component\HttpFoundation\BinaryFileResponse()" instead. ' );
48
+ $ response = BinaryFileResponse::create ($ file , 404 , ['X-Header ' => 'Foo ' ], true , null , true , true );
49
+ $ this ->assertEquals (404 , $ response ->getStatusCode ());
50
+ $ this ->assertEquals ('Foo ' , $ response ->headers ->get ('X-Header ' ));
51
+ $ this ->assertTrue ($ response ->headers ->has ('ETag ' ));
52
+ $ this ->assertTrue ($ response ->headers ->has ('Last-Modified ' ));
53
+ $ this ->assertFalse ($ response ->headers ->has ('Content-Disposition ' ));
54
+
32
55
$ response = BinaryFileResponse::create ($ file , 404 , [], true , ResponseHeaderBag::DISPOSITION_INLINE );
33
56
$ this ->assertEquals (404 , $ response ->getStatusCode ());
34
57
$ this ->assertFalse ($ response ->headers ->has ('ETag ' ));
@@ -83,7 +106,7 @@ public function testSetContentDispositionGeneratesSafeFallbackFilenameForWrongly
83
106
*/
84
107
public function testRequests ($ requestRange , $ offset , $ length , $ responseRange )
85
108
{
86
- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ])->setAutoEtag ();
109
+ $ response = ( new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]) )->setAutoEtag ();
87
110
88
111
// do a request to get the ETag
89
112
$ request = Request::create ('/ ' );
@@ -115,7 +138,7 @@ public function testRequests($requestRange, $offset, $length, $responseRange)
115
138
*/
116
139
public function testRequestsWithoutEtag ($ requestRange , $ offset , $ length , $ responseRange )
117
140
{
118
- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
141
+ $ response = new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
119
142
120
143
// do a request to get the LastModified
121
144
$ request = Request::create ('/ ' );
@@ -156,7 +179,7 @@ public function provideRanges()
156
179
public function testRangeRequestsWithoutLastModifiedDate ()
157
180
{
158
181
// prevent auto last modified
159
- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ], true , null , false , false );
182
+ $ response = new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ], true , null , false , false );
160
183
161
184
// prepare a request for a range of the testing file
162
185
$ request = Request::create ('/ ' );
@@ -177,7 +200,7 @@ public function testRangeRequestsWithoutLastModifiedDate()
177
200
*/
178
201
public function testFullFileRequests ($ requestRange )
179
202
{
180
- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ])->setAutoEtag ();
203
+ $ response = ( new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]) )->setAutoEtag ();
181
204
182
205
// prepare a request for a range of the testing file
183
206
$ request = Request::create ('/ ' );
@@ -213,7 +236,7 @@ public function testRangeOnPostMethod()
213
236
{
214
237
$ request = Request::create ('/ ' , 'POST ' );
215
238
$ request ->headers ->set ('Range ' , 'bytes=10-20 ' );
216
- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
239
+ $ response = new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
217
240
218
241
$ file = fopen (__DIR__ .'/File/Fixtures/test.gif ' , 'r ' );
219
242
$ data = fread ($ file , 35 );
@@ -231,7 +254,7 @@ public function testRangeOnPostMethod()
231
254
232
255
public function testUnpreparedResponseSendsFullFile ()
233
256
{
234
- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 );
257
+ $ response = new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 );
235
258
236
259
$ data = file_get_contents (__DIR__ .'/File/Fixtures/test.gif ' );
237
260
@@ -247,7 +270,7 @@ public function testUnpreparedResponseSendsFullFile()
247
270
*/
248
271
public function testInvalidRequests ($ requestRange )
249
272
{
250
- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ])->setAutoEtag ();
273
+ $ response = ( new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]) )->setAutoEtag ();
251
274
252
275
// prepare a request for a range of the testing file
253
276
$ request = Request::create ('/ ' );
@@ -278,7 +301,7 @@ public function testXSendfile($file)
278
301
$ request ->headers ->set ('X-Sendfile-Type ' , 'X-Sendfile ' );
279
302
280
303
BinaryFileResponse::trustXSendfileTypeHeader ();
281
- $ response = BinaryFileResponse:: create ($ file , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
304
+ $ response = new BinaryFileResponse ($ file , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
282
305
$ response ->prepare ($ request );
283
306
284
307
$ this ->expectOutputString ('' );
@@ -338,7 +361,7 @@ public function testDeleteFileAfterSend()
338
361
public function testAcceptRangeOnUnsafeMethods ()
339
362
{
340
363
$ request = Request::create ('/ ' , 'POST ' );
341
- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
364
+ $ response = new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
342
365
$ response ->prepare ($ request );
343
366
344
367
$ this ->assertEquals ('none ' , $ response ->headers ->get ('Accept-Ranges ' ));
@@ -347,7 +370,7 @@ public function testAcceptRangeOnUnsafeMethods()
347
370
public function testAcceptRangeNotOverriden ()
348
371
{
349
372
$ request = Request::create ('/ ' , 'POST ' );
350
- $ response = BinaryFileResponse:: create (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
373
+ $ response = new BinaryFileResponse (__DIR__ .'/File/Fixtures/test.gif ' , 200 , ['Content-Type ' => 'application/octet-stream ' ]);
351
374
$ response ->headers ->set ('Accept-Ranges ' , 'foo ' );
352
375
$ response ->prepare ($ request );
353
376
0 commit comments