From 8057c7cfb01fe0994be136af4f2a1bd26e181dbd Mon Sep 17 00:00:00 2001 From: Kurt Thiemann Date: Thu, 5 Dec 2024 14:35:19 +0100 Subject: [PATCH 1/2] [HttpClient] Test POST to GET redirects --- Tests/HttpClientTestCase.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Tests/HttpClientTestCase.php b/Tests/HttpClientTestCase.php index f572908..79763bc 100644 --- a/Tests/HttpClientTestCase.php +++ b/Tests/HttpClientTestCase.php @@ -673,4 +673,26 @@ public function testHeadRequestWithClosureBody() $this->assertIsArray($vars); $this->assertSame('HEAD', $vars['REQUEST_METHOD']); } + + /** + * @testWith [301] + * [302] + * [303] + */ + public function testPostToGetRedirect(int $status) + { + $p = TestHttpServer::start(8067); + + try { + $client = $this->getHttpClient(__FUNCTION__); + + $response = $client->request('POST', 'http://localhost:8057/custom?status=' . $status . '&headers[]=Location%3A%20%2F'); + $body = $response->toArray(); + } finally { + $p->stop(); + } + + $this->assertSame('GET', $body['REQUEST_METHOD']); + $this->assertSame('/', $body['REQUEST_URI']); + } } From 88898d842eb29d7e1a903724c94e90a6ca9c0509 Mon Sep 17 00:00:00 2001 From: Jontsa Date: Wed, 18 Dec 2024 14:18:31 +0200 Subject: [PATCH 2/2] [HttpClient] Fix a typo in NoPrivateNetworkHttpClient --- NoPrivateNetworkHttpClient.php | 2 +- Tests/NoPrivateNetworkHttpClientTest.php | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/NoPrivateNetworkHttpClient.php b/NoPrivateNetworkHttpClient.php index 1a81155..4094f98 100644 --- a/NoPrivateNetworkHttpClient.php +++ b/NoPrivateNetworkHttpClient.php @@ -138,7 +138,7 @@ public function request(string $method, string $url, array $options = []): Respo $filterContentHeaders = static function ($h) { return 0 !== stripos($h, 'Content-Length:') && 0 !== stripos($h, 'Content-Type:') && 0 !== stripos($h, 'Transfer-Encoding:'); }; - $options['header'] = array_filter($options['header'], $filterContentHeaders); + $options['headers'] = array_filter($options['headers'], $filterContentHeaders); $redirectHeaders['no_auth'] = array_filter($redirectHeaders['no_auth'], $filterContentHeaders); $redirectHeaders['with_auth'] = array_filter($redirectHeaders['with_auth'], $filterContentHeaders); } diff --git a/Tests/NoPrivateNetworkHttpClientTest.php b/Tests/NoPrivateNetworkHttpClientTest.php index fb94079..06ffc12 100644 --- a/Tests/NoPrivateNetworkHttpClientTest.php +++ b/Tests/NoPrivateNetworkHttpClientTest.php @@ -173,6 +173,27 @@ public function testNonCallableOnProgressCallback() $client->request('GET', $url, ['on_progress' => $customCallback]); } + public function testHeadersArePassedOnRedirect() + { + $ipAddr = '104.26.14.6'; + $url = sprintf('http://%s/', $ipAddr); + $content = 'foo'; + + $callback = function ($method, $url, $options) use ($content): MockResponse { + $this->assertArrayHasKey('headers', $options); + $this->assertNotContains('content-type: application/json', $options['headers']); + $this->assertContains('foo: bar', $options['headers']); + return new MockResponse($content); + }; + $responses = [ + new MockResponse('', ['http_code' => 302, 'redirect_url' => 'http://104.26.14.7']), + $callback, + ]; + $client = new NoPrivateNetworkHttpClient(new MockHttpClient($responses)); + $response = $client->request('POST', $url, ['headers' => ['foo' => 'bar', 'content-type' => 'application/json']]); + $this->assertEquals($content, $response->getContent()); + } + private function getMockHttpClient(string $ipAddr, string $content) { return new MockHttpClient(new MockResponse($content, ['primary_ip' => $ipAddr]));