Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Fix Precognition headers for Symfony responses #44424

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public function handle($request, $next)
$this->prepareForPrecognition($request);

return tap($next($request), function ($response) use ($request) {
$this->appendVaryHeader($request, $response->header('Precognition', 'true'));
$response->headers->set('Precognition', 'true');

$this->appendVaryHeader($request, $response);
});
}

Expand All @@ -72,9 +74,9 @@ protected function prepareForPrecognition($request)
*/
protected function appendVaryHeader($request, $response)
{
return $response->header('Vary', implode(', ', array_filter([
return tap($response, fn () => $response->headers->set('Vary', implode(', ', array_filter([
$response->headers->get('Vary'),
'Precognition',
])));
]))));
}
}
38 changes: 38 additions & 0 deletions tests/Integration/Routing/PrecognitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,34 @@ public function testItDoesNotSetLastUrl()
$response->assertOk();
$this->assertSame('http://localhost/expected-route-2', session()->previousUrl());
}

public function testItAppendsVaryHeaderToSymfonyResponse()
{
Route::get('test-route', function () {
return response()->streamDownload(function () {
echo 'foo';
}, null, ['Expected' => 'Header']);
})->middleware(HandlePrecognitiveRequests::class);

$response = $this->get('test-route');
$response->assertOk();
$response->assertHeader('Expected', 'Header');
}

public function testItAppendsPrecognitionHeaderToSymfonyResponse()
{
Route::get('test-route', function () {
//
})->middleware([
HandlePrecognitiveRequests::class,
MiddlewareReturningSymfonyResponse::class,
]);

$response = $this->get('test-route', ['Precognition' => 'true']);
$response->assertOk();
$response->assertHeader('Expected', 'Header');
$response->assertHeader('Precognition', 'true');
}
}

class PrecognitionTestController
Expand Down Expand Up @@ -908,3 +936,13 @@ protected function prepareForPrecognition($request)
app()->bind(ControllerDispatcherContract::class, fn ($app) => new ControllerDispatcher($app));
}
}

class MiddlewareReturningSymfonyResponse
{
public function handle($request, $next)
{
return response()->streamDownload(function () {
//
}, null, ['Expected' => 'Header']);
}
}