Skip to content

Commit 54cb5f6

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: Remove headers with internal meaning from HttpClient responses
2 parents edf00ab + 60d0e02 commit 54cb5f6

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

Tests/CachingHttpClientTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpClient\MockHttpClient;
1717
use Symfony\Component\HttpClient\Response\MockResponse;
1818
use Symfony\Component\HttpKernel\HttpCache\Store;
19+
use Symfony\Contracts\HttpClient\ResponseInterface;
1920

2021
class CachingHttpClientTest extends TestCase
2122
{
@@ -39,4 +40,71 @@ public function testRequestHeaders()
3940
self::assertSame($response->getRequestOptions()['normalized_headers']['application-name'][0], 'Application-Name: test1234');
4041
self::assertSame($response->getRequestOptions()['normalized_headers']['test-name-header'][0], 'Test-Name-Header: test12345');
4142
}
43+
44+
public function testDoesNotEvaluateResponseBody()
45+
{
46+
$body = file_get_contents(__DIR__.'/Fixtures/assertion_failure.php');
47+
$response = $this->runRequest(new MockResponse($body, ['response_headers' => ['X-Body-Eval' => true]]));
48+
$headers = $response->getHeaders();
49+
50+
$this->assertSame($body, $response->getContent());
51+
$this->assertArrayNotHasKey('x-body-eval', $headers);
52+
}
53+
54+
public function testDoesNotIncludeFile()
55+
{
56+
$file = __DIR__.'/Fixtures/assertion_failure.php';
57+
58+
$response = $this->runRequest(new MockResponse(
59+
'test', ['response_headers' => [
60+
'X-Body-Eval' => true,
61+
'X-Body-File' => $file,
62+
]]
63+
));
64+
$headers = $response->getHeaders();
65+
66+
$this->assertSame('test', $response->getContent());
67+
$this->assertArrayNotHasKey('x-body-eval', $headers);
68+
$this->assertArrayNotHasKey('x-body-file', $headers);
69+
}
70+
71+
public function testDoesNotReadFile()
72+
{
73+
$file = __DIR__.'/Fixtures/assertion_failure.php';
74+
75+
$response = $this->runRequest(new MockResponse(
76+
'test', ['response_headers' => [
77+
'X-Body-File' => $file,
78+
]]
79+
));
80+
$headers = $response->getHeaders();
81+
82+
$this->assertSame('test', $response->getContent());
83+
$this->assertArrayNotHasKey('x-body-file', $headers);
84+
}
85+
86+
public function testRemovesXContentDigest()
87+
{
88+
$response = $this->runRequest(new MockResponse(
89+
'test', [
90+
'response_headers' => [
91+
'X-Content-Digest' => 'some-hash',
92+
]
93+
]));
94+
$headers = $response->getHeaders();
95+
96+
$this->assertArrayNotHasKey('x-content-digest', $headers);
97+
}
98+
99+
private function runRequest(MockResponse $mockResponse): ResponseInterface
100+
{
101+
$mockClient = new MockHttpClient($mockResponse);
102+
103+
$store = new Store(sys_get_temp_dir() . '/sf_http_cache');
104+
$client = new CachingHttpClient($mockClient, $store);
105+
106+
$response = $client->request('GET', 'http://test');
107+
108+
return $response;
109+
}
42110
}

Tests/Fixtures/assertion_failure.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
throw new \PHPUnit\Framework\AssertionFailedError('Response body should not be evaluated.');

0 commit comments

Comments
 (0)