24 $file = __DIR__.
'/../README.md';
25 $response =
new BinaryFileResponse($file, 404, array(
'X-Header' =>
'Foo'),
true,
null,
true,
true);
26 $this->assertEquals(404, $response->getStatusCode());
27 $this->assertEquals(
'Foo', $response->headers->get(
'X-Header'));
28 $this->assertTrue($response->headers->has(
'ETag'));
29 $this->assertTrue($response->headers->has(
'Last-Modified'));
30 $this->assertFalse($response->headers->has(
'Content-Disposition'));
33 $this->assertEquals(404, $response->getStatusCode());
34 $this->assertFalse($response->headers->has(
'ETag'));
35 $this->assertEquals(
'inline; filename="README.md"', $response->headers->get(
'Content-Disposition'));
40 touch(sys_get_temp_dir().
'/fööö.html');
42 $response =
new BinaryFileResponse(sys_get_temp_dir().
'/fööö.html', 200, array(),
true,
'attachment');
44 @unlink(sys_get_temp_dir().
'/fööö.html');
46 $this->assertSame(
'fööö.html', $response->getFile()->getFilename());
55 $response->setContent(
'foo');
61 $this->assertFalse($response->getContent());
69 $this->assertSame(
'attachment; filename="f__.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html', $response->headers->get(
'Content-Disposition'));
75 public function testRequests($requestRange, $offset, $length, $responseRange)
77 $response =
BinaryFileResponse::create(__DIR__.
'/File/Fixtures/test.gif', 200, array(
'Content-Type' =>
'application/octet-stream'))->setAutoEtag();
81 $response->prepare($request);
82 $etag = $response->headers->get(
'ETag');
86 $request->headers->set(
'If-Range', $etag);
87 $request->headers->set(
'Range', $requestRange);
89 $file = fopen(__DIR__.
'/File/Fixtures/test.gif',
'r');
90 fseek($file, $offset);
91 $data = fread($file, $length);
94 $this->expectOutputString($data);
95 $response = clone $response;
96 $response->prepare($request);
97 $response->sendContent();
99 $this->assertEquals(206, $response->getStatusCode());
100 $this->assertEquals($responseRange, $response->headers->get(
'Content-Range'));
101 $this->assertSame($length, $response->headers->get(
'Content-Length'));
109 $response =
BinaryFileResponse::create(__DIR__.
'/File/Fixtures/test.gif', 200, array(
'Content-Type' =>
'application/octet-stream'));
113 $response->prepare($request);
114 $lastModified = $response->headers->get(
'Last-Modified');
118 $request->headers->set(
'If-Range', $lastModified);
119 $request->headers->set(
'Range', $requestRange);
121 $file = fopen(__DIR__.
'/File/Fixtures/test.gif',
'r');
122 fseek($file, $offset);
123 $data = fread($file, $length);
126 $this->expectOutputString($data);
127 $response = clone $response;
128 $response->prepare($request);
129 $response->sendContent();
131 $this->assertEquals(206, $response->getStatusCode());
132 $this->assertEquals($responseRange, $response->headers->get(
'Content-Range'));
138 array(
'bytes=1-4', 1, 4,
'bytes 1-4/35'),
139 array(
'bytes=-5', 30, 5,
'bytes 30-34/35'),
140 array(
'bytes=30-', 30, 5,
'bytes 30-34/35'),
141 array(
'bytes=30-30', 30, 1,
'bytes 30-30/35'),
142 array(
'bytes=30-34', 30, 5,
'bytes 30-34/35'),
149 $response =
BinaryFileResponse::create(__DIR__.
'/File/Fixtures/test.gif', 200, array(
'Content-Type' =>
'application/octet-stream'),
true,
null,
false,
false);
153 $request->headers->set(
'If-Range', date(
'D, d M Y H:i:s').
' GMT');
154 $request->headers->set(
'Range',
'bytes=1-4');
156 $this->expectOutputString(file_get_contents(__DIR__.
'/File/Fixtures/test.gif'));
157 $response = clone $response;
158 $response->prepare($request);
159 $response->sendContent();
161 $this->assertEquals(200, $response->getStatusCode());
162 $this->assertNull($response->headers->get(
'Content-Range'));
170 $response =
BinaryFileResponse::create(__DIR__.
'/File/Fixtures/test.gif', 200, array(
'Content-Type' =>
'application/octet-stream'))->setAutoEtag();
174 $request->headers->set(
'Range', $requestRange);
176 $file = fopen(__DIR__.
'/File/Fixtures/test.gif',
'r');
177 $data = fread($file, 35);
180 $this->expectOutputString($data);
181 $response = clone $response;
182 $response->prepare($request);
183 $response->sendContent();
185 $this->assertEquals(200, $response->getStatusCode());
195 array(
'bytes=20-10'),
196 array(
'bytes=50-40'),
205 $response =
BinaryFileResponse::create(__DIR__.
'/File/Fixtures/test.gif', 200, array(
'Content-Type' =>
'application/octet-stream'))->setAutoEtag();
209 $request->headers->set(
'Range', $requestRange);
211 $response = clone $response;
212 $response->prepare($request);
213 $response->sendContent();
215 $this->assertEquals(416, $response->getStatusCode());
216 $this->assertEquals(
'bytes */35', $response->headers->get(
'Content-Range'));
223 array(
'bytes=30-40'),
233 $request->headers->set(
'X-Sendfile-Type',
'X-Sendfile');
237 $response->prepare($request);
239 $this->expectOutputString(
'');
240 $response->sendContent();
242 $this->assertContains(
'README.md', $response->headers->get(
'X-Sendfile'));
248 array(__DIR__.
'/../README.md'),
249 array(
'file://'.__DIR__.
'/../README.md'),
259 $request->headers->set(
'X-Sendfile-Type',
'X-Accel-Redirect');
260 $request->headers->set(
'X-Accel-Mapping', $mapping);
262 $file =
new FakeFile($realpath, __DIR__.
'/File/Fixtures/test');
265 $response =
new BinaryFileResponse($file, 200, array(
'Content-Type' =>
'application/octet-stream'));
266 $reflection = new \ReflectionObject($response);
267 $property = $reflection->getProperty(
'file');
268 $property->setAccessible(
true);
269 $property->setValue($response, $file);
271 $response->prepare($request);
272 $this->assertEquals($virtual, $response->headers->get(
'X-Accel-Redirect'));
279 $path = __DIR__.
'/File/Fixtures/to_delete';
281 $realPath = realpath($path);
282 $this->assertFileExists($realPath);
284 $response =
new BinaryFileResponse($realPath, 200, array(
'Content-Type' =>
'application/octet-stream'));
285 $response->deleteFileAfterSend(
true);
287 $response->prepare($request);
288 $response->sendContent();
290 $this->assertFileNotExists($path);
296 $response =
BinaryFileResponse::create(__DIR__.
'/File/Fixtures/test.gif', 200, array(
'Content-Type' =>
'application/octet-stream'));
297 $response->prepare($request);
299 $this->assertEquals(
'none', $response->headers->get(
'Accept-Ranges'));
305 $response =
BinaryFileResponse::create(__DIR__.
'/File/Fixtures/test.gif', 200, array(
'Content-Type' =>
'application/octet-stream'));
306 $response->headers->set(
'Accept-Ranges',
'foo');
307 $response->prepare($request);
309 $this->assertEquals(
'foo', $response->headers->get(
'Accept-Ranges'));
315 array(
'/var/www/var/www/files/foo.txt',
'/var/www/=/files/',
'/files/var/www/files/foo.txt'),
316 array(
'/home/foo/bar.txt',
'/var/www/=/files/,/home/foo/=/baz/',
'/baz/bar.txt'),
323 $response =
new BinaryFileResponse(
new Stream(__DIR__.
'/../README.md'), 200, array(
'Content-Type' =>
'text/plain'));
324 $response->prepare($request);
326 $this->assertNull($response->headers->get(
'Content-Length'));
331 return new BinaryFileResponse(__DIR__.
'/../README.md', 200, array(
'Content-Type' =>
'application/octet-stream'));
336 $path = __DIR__.
'/../Fixtures/to_delete';
337 if (file_exists($path)) {