diff --git a/src/Client.php b/src/Client.php index ad0baa7..ac5408f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -17,7 +17,6 @@ use Berlioz\Http\Client\Adapter\AdapterInterface; use Berlioz\Http\Client\Adapter\CurlAdapter; use Berlioz\Http\Client\Adapter\StreamAdapter; -use Berlioz\Http\Client\Components; use Berlioz\Http\Client\Cookies\CookiesManager; use Berlioz\Http\Client\Exception\HttpClientException; use Berlioz\Http\Client\Exception\HttpException; @@ -266,8 +265,8 @@ public function sendRequest(RequestInterface $request, Options|array $options = Response::HTTP_STATUS_MOVED_PERMANENTLY, Response::HTTP_STATUS_MOVED_TEMPORARILY, Response::HTTP_STATUS_SEE_OTHER, - 307, - 308 + Response::HTTP_STATUS_TEMPORARY_REDIRECT, + Response::HTTP_STATUS_PERMANENT_REDIRECT, ] )) { continue; @@ -300,9 +299,23 @@ public function sendRequest(RequestInterface $request, Options|array $options = } } + $redirectMethod = Request::HTTP_METHOD_GET; + $redirectBody = null; + // For 307 and 308 redirect cases, method and body not changed. + if (in_array( + $response->getStatusCode(), + [ + Response::HTTP_STATUS_TEMPORARY_REDIRECT, + Response::HTTP_STATUS_PERMANENT_REDIRECT, + ] + )) { + $redirectMethod = $request->getMethod(); + $redirectBody = $request->getBody(); + } + // Create request for redirection $request = $this->prepareRequest( - new Request(Request::HTTP_METHOD_GET, Uri::create($redirectUri, $request->getUri())), + new Request($redirectMethod, Uri::create($redirectUri, $request->getUri()), $redirectBody), $cookies, Options::make([ 'headers' => [ diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 6dee90b..c85ccd0 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -58,6 +58,19 @@ public function testGet_redirection() $this->assertEquals('GET', $bodyExploded[0]); } + public function testPost_307Redirection() + { + $uri = new Uri('http', 'localhost', 8080, '/request.php?redirect=1&response_code=307'); + $client = new Client(); + $response = $client->post($uri, http_build_query(['body' => 'test'])); + + $this->assertEquals(200, $response->getStatusCode()); + + $bodyExploded = preg_split('/\r?\n/', (string)$response->getBody()); + $this->assertEquals('POST', $bodyExploded[0]); + $this->assertEquals('test', end($bodyExploded)); + } + public function testGet_encodedHttpReason() { $uri = new Uri('http', 'localhost', 8080, '/request.php?test=encoded_http_reason'); diff --git a/tests/server/request.php b/tests/server/request.php index f5ff529..cd951ad 100644 --- a/tests/server/request.php +++ b/tests/server/request.php @@ -15,6 +15,7 @@ print PHP_EOL; print file_get_contents('php://stdin'); print_r(getallheaders()); +array_map(fn($postValue) => print $postValue, $_POST); $contents = ob_get_clean(); setcookie('test', 'value'); @@ -23,7 +24,7 @@ header( 'Location: /request.php?encoding=' . ($_GET['encoding'] ?? null) . '&redirect=' . ($redirect - 1), true, - 301 + $_GET['response_code'] ?? 301 ); exit; }