[HttpFoundation] Deprecate setting public properties of Request and Response objects directly

This commit is contained in:
Nicolas Grekas
2026-03-02 22:03:34 +01:00
parent 5f20e4e1f7
commit b70e5ee6b8
2 changed files with 15 additions and 10 deletions

View File

@@ -55,20 +55,25 @@ final class HttpClientKernel implements HttpKernelInterface
'body' => $body,
] + $request->attributes->get('http_client_options', []));
$response = new Response($response->getContent(!$catch), $response->getStatusCode(), $response->getHeaders(!$catch));
$response->headers->remove('X-Body-File');
$response->headers->remove('X-Body-Eval');
$response->headers->remove('X-Content-Digest');
$response->headers = new class($response->headers->all()) extends ResponseHeaderBag {
$headers = new class($response->getHeaders(!$catch)) extends ResponseHeaderBag {
protected function computeCacheControlValue(): string
{
return $this->getCacheControlHeader(); // preserve the original value
}
};
$headers->remove('X-Body-File');
$headers->remove('X-Body-Eval');
$headers->remove('X-Content-Digest');
return $response;
try {
return new Response($response->getContent(!$catch), $response->getStatusCode(), $headers);
} catch (\TypeError) {
// BC with Symfony < 8.1
$response = new Response($response->getContent(!$catch), $response->getStatusCode());
$response->headers = $headers;
return $response;
}
}
private function getBody(Request $request): ?AbstractPart

View File

@@ -24,8 +24,8 @@ class HttpClientKernelTest extends TestCase
$request = new Request();
$request->attributes->set('http_client_options', ['max_redirects' => 50]);
$response = $this->createMock(ResponseInterface::class);
$response->expects($this->once())->method('getStatusCode')->willReturn(200);
$response = $this->createStub(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(200);
$client = $this->createMock(HttpClientInterface::class);
$client