Skip to content

Commit 23e8dd8

Browse files
authored
[9.x] Fix Precognition headers for Symfony responses (#44424)
* handle symfony responses for Vary header * support symfony reponse for precognition header
1 parent 26dd9bd commit 23e8dd8

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/Illuminate/Foundation/Http/Middleware/HandlePrecognitiveRequests.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public function handle($request, $next)
4545
$this->prepareForPrecognition($request);
4646

4747
return tap($next($request), function ($response) use ($request) {
48-
$this->appendVaryHeader($request, $response->header('Precognition', 'true'));
48+
$response->headers->set('Precognition', 'true');
49+
50+
$this->appendVaryHeader($request, $response);
4951
});
5052
}
5153

@@ -72,9 +74,9 @@ protected function prepareForPrecognition($request)
7274
*/
7375
protected function appendVaryHeader($request, $response)
7476
{
75-
return $response->header('Vary', implode(', ', array_filter([
77+
return tap($response, fn () => $response->headers->set('Vary', implode(', ', array_filter([
7678
$response->headers->get('Vary'),
7779
'Precognition',
78-
])));
80+
]))));
7981
}
8082
}

tests/Integration/Routing/PrecognitionTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,34 @@ public function testItDoesNotSetLastUrl()
743743
$response->assertOk();
744744
$this->assertSame('http://localhost/expected-route-2', session()->previousUrl());
745745
}
746+
747+
public function testItAppendsVaryHeaderToSymfonyResponse()
748+
{
749+
Route::get('test-route', function () {
750+
return response()->streamDownload(function () {
751+
echo 'foo';
752+
}, null, ['Expected' => 'Header']);
753+
})->middleware(HandlePrecognitiveRequests::class);
754+
755+
$response = $this->get('test-route');
756+
$response->assertOk();
757+
$response->assertHeader('Expected', 'Header');
758+
}
759+
760+
public function testItAppendsPrecognitionHeaderToSymfonyResponse()
761+
{
762+
Route::get('test-route', function () {
763+
//
764+
})->middleware([
765+
HandlePrecognitiveRequests::class,
766+
MiddlewareReturningSymfonyResponse::class,
767+
]);
768+
769+
$response = $this->get('test-route', ['Precognition' => 'true']);
770+
$response->assertOk();
771+
$response->assertHeader('Expected', 'Header');
772+
$response->assertHeader('Precognition', 'true');
773+
}
746774
}
747775

748776
class PrecognitionTestController
@@ -908,3 +936,13 @@ protected function prepareForPrecognition($request)
908936
app()->bind(ControllerDispatcherContract::class, fn ($app) => new ControllerDispatcher($app));
909937
}
910938
}
939+
940+
class MiddlewareReturningSymfonyResponse
941+
{
942+
public function handle($request, $next)
943+
{
944+
return response()->streamDownload(function () {
945+
//
946+
}, null, ['Expected' => 'Header']);
947+
}
948+
}

0 commit comments

Comments
 (0)