This short lesson is about a rarely used assertion for file downloads.
In the Controller, you have a method where the response is to download a file.
use Symfony\Component\HttpFoundation\BinaryFileResponse; class ProductController extends Controller{ // ... public function download(): BinaryFileResponse { return response()->download(storage_path('files/product-specification.pdf')); }}
How can you test if this download was successful and if the download returned that exact file name to the browser?
In the test, you can assert the HTTP response header and check if it contains the attachment with the exact filename.
use function Pest\Laravel\get; test('download product success', function () { get('/products/download') ->assertOk() ->assertHeader('Content-Disposition', 'attachment; filename=product-specification.pdf');});
The idea for this test came from the BookStackApp/BookStack open-source project. There's a test for exporting a book in HTML format, and one of the assertions is checking the header for the filename.
class BooksApiTest extends TestCase{ // ... public function test_export_html_endpoint() { $this->actingAsApiEditor(); $book = $this->entities->book(); $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/html"); $resp->assertStatus(200); $resp->assertSee($book->name); $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"'); } // ...}