[8.x] Add assertDownloadOffered test method to TestResponse class #37532
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces a convenient
assertDownloadOffered($filename = null)
method which makes it easier to test different types of file responses such as:Unless explicitly overridden, most of these approaches add
attachment
as the disposition-type to the Content-Disposition header, which asks the HTTP client to download the file locally, as shown in the screenshot below:In order to test whether a route/controller action responds with a downloadable file or not and whether that file is indeed the correct one, currently one has to extract the header, split it and inspect the content in a hacky way. This kind of approach lacks chainability as well.
The proposed
TestResponse::assertDownloadOffered()
method ensures two things:$response->assertDownloadOffered()
)$response->assertDownloadOffered('image.png')
)Intent Resolution
As shown in the examples above, as long as the disposition-type parameter of the Content-Disposition header is set to
attachment
, it will be determined that the intention is to download the file. Hence, in cases where disposition-type is changed to something else like this:...even though the called method is download(), replacing
attachment
withinline
instructs the HTTP client to display the file instead of downloading. In such cases,$response->assertDownloadOffered()
will mark the test as failed.Examples
attachment
option is manually passed to the header.attachment
.Compatibility
This PR does not include breaking changes.