Skip to content

Commit 1f3ce7a

Browse files
committed
clone the original response class before every curl request
1 parent 5e42d8f commit 1f3ce7a

4 files changed

Lines changed: 26 additions & 19 deletions

File tree

system/HTTP/CURLRequest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ class CURLRequest extends OutgoingRequest
2828
*/
2929
protected $response;
3030

31+
/**
32+
* The original response object associated with this request
33+
*
34+
* @var ResponseInterface|null
35+
*/
36+
protected $responseOrig;
37+
3138
/**
3239
* The URI associated with this request
3340
*
@@ -105,7 +112,7 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
105112

106113
parent::__construct('GET', $uri);
107114

108-
$this->response = $response;
115+
$this->responseOrig = $response;
109116
$this->baseURI = $uri->useRawQueryString();
110117
$this->defaultOptions = $options;
111118

@@ -125,6 +132,8 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
125132
*/
126133
public function request($method, string $url, array $options = []): ResponseInterface
127134
{
135+
$this->response = clone $this->responseOrig;
136+
128137
$this->parseOptions($options);
129138

130139
$url = $this->prepareURL($url);
@@ -469,10 +478,6 @@ protected function applyBody(array $curlOptions = []): array
469478
*/
470479
protected function setResponseHeaders(array $headers = [])
471480
{
472-
foreach ($this->response->headers() as $header) {
473-
$this->response->removeHeader($header->getName());
474-
}
475-
476481
foreach ($headers as $header) {
477482
if (($pos = strpos($header, ':')) !== false) {
478483
$title = substr($header, 0, $pos);

system/Test/Mock/MockCURLRequest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function setOutput($output)
3434

3535
protected function sendRequest(array $curlOptions = []): string
3636
{
37+
$this->response = clone $this->responseOrig;
38+
3739
// Save so we can access later.
3840
$this->curl_options = $curlOptions;
3941

tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -766,15 +766,15 @@ public function testSendContinuedWithManyHeaders()
766766
$this->assertSame('<title>Update success! config</title>', $response->getBody());
767767

768768
$responseHeaderKeys = [
769+
'Cache-Control',
770+
'Content-Type',
769771
'Server',
770772
'Connection',
771773
'Keep-Alive',
772774
'Set-Cookie',
773775
'Date',
774776
'Expires',
775-
'Cache-Control',
776777
'Pragma',
777-
'Content-Type',
778778
'Transfer-Encoding',
779779
];
780780
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
@@ -805,20 +805,20 @@ public function testResponseHeadersWithMultipleRequests()
805805
$this->assertSame('<title>Hello1</title>', $response->getBody());
806806

807807
$responseHeaderKeys = [
808+
'Cache-Control',
809+
'Content-Type',
808810
'Server',
809811
'Expires',
810-
'Cache-Control',
811812
'Pragma',
812-
'Content-Type',
813813
'Transfer-Encoding',
814814
];
815815
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
816816

817817
$this->assertSame(200, $response->getStatusCode());
818818

819819
$output = "HTTP/2.0 200 OK
820-
Server: ddos-guard
821820
Expires: Thu, 19 Nov 1982 08:52:00 GMT
821+
Cache-Control: no-store, no-cache, must-revalidate
822822
Content-Type: application/xml; charset=utf-8
823823
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Hello2</title>";
824824
$request->setOutput($output);
@@ -828,9 +828,9 @@ public function testResponseHeadersWithMultipleRequests()
828828
$this->assertSame('<title>Hello2</title>', $response->getBody());
829829

830830
$responseHeaderKeys = [
831-
'Server',
832-
'Expires',
831+
'Cache-Control',
833832
'Content-Type',
833+
'Expires',
834834
'Transfer-Encoding',
835835
];
836836
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));

tests/system/HTTP/CURLRequestTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -749,15 +749,15 @@ public function testSendContinuedWithManyHeaders()
749749
$this->assertSame('<title>Update success! config</title>', $response->getBody());
750750

751751
$responseHeaderKeys = [
752+
'Cache-Control',
753+
'Content-Type',
752754
'Server',
753755
'Connection',
754756
'Keep-Alive',
755757
'Set-Cookie',
756758
'Date',
757759
'Expires',
758-
'Cache-Control',
759760
'Pragma',
760-
'Content-Type',
761761
'Transfer-Encoding',
762762
];
763763
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
@@ -788,20 +788,20 @@ public function testResponseHeadersWithMultipleRequests()
788788
$this->assertSame('<title>Hello1</title>', $response->getBody());
789789

790790
$responseHeaderKeys = [
791+
'Cache-Control',
792+
'Content-Type',
791793
'Server',
792794
'Expires',
793-
'Cache-Control',
794795
'Pragma',
795-
'Content-Type',
796796
'Transfer-Encoding',
797797
];
798798
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
799799

800800
$this->assertSame(200, $response->getStatusCode());
801801

802802
$output = "HTTP/2.0 200 OK
803-
Server: ddos-guard
804803
Expires: Thu, 19 Nov 1982 08:52:00 GMT
804+
Cache-Control: no-store, no-cache, must-revalidate
805805
Content-Type: application/xml; charset=utf-8
806806
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Hello2</title>";
807807
$request->setOutput($output);
@@ -811,9 +811,9 @@ public function testResponseHeadersWithMultipleRequests()
811811
$this->assertSame('<title>Hello2</title>', $response->getBody());
812812

813813
$responseHeaderKeys = [
814-
'Server',
815-
'Expires',
814+
'Cache-Control',
816815
'Content-Type',
816+
'Expires',
817817
'Transfer-Encoding',
818818
];
819819
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));

0 commit comments

Comments
 (0)