diff --git a/src/Classes/Api/V1/HttpRequest.php b/src/Classes/Api/V1/HttpRequest.php index 8717da65..a525a144 100644 --- a/src/Classes/Api/V1/HttpRequest.php +++ b/src/Classes/Api/V1/HttpRequest.php @@ -13,6 +13,7 @@ namespace RobinTheHood\ModifiedModuleLoaderClient\Api\V1; +use RuntimeException; use RobinTheHood\ModifiedModuleLoaderClient\Logger\LogLevel; use RobinTheHood\ModifiedModuleLoaderClient\Logger\StaticLogger; @@ -27,7 +28,107 @@ public function isServerAvailable(string $url): bool return false; } - public function sendPostRequest(string $url, $data) + public function sendPostRequest(string $url, $data): string + { + $result = $this->sendCurlPostRequest($url, $data); + return $result; + } + + + public function sendGetRequest(string $url): string + { + $result = $this->sendCurlGetRequest($url); + return $result; + } + + public static function createQuery(array $queryValues): string + { + $query = ''; + foreach ($queryValues as $name => $value) { + $query .= $name . '=' . urlencode($value) . '&'; + } + return $query; + } + + /** + * @throws RuntimeException + */ + private function sendCurlGetRequest(string $url): string + { + // HTTP GET-Request konfigurieren + $curl = curl_init($url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_USERAGENT, 'Modified Module Loader Client'); + + StaticLogger::log( + LogLevel::DEBUG, + "Send GET request to $url" + ); + + // Request senden und Ergebnis erhalten + $result = curl_exec($curl); + + // HTTP-Statuscode und Fehler prüfen + $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + if ($result === false) { + $error = curl_error($curl); + curl_close($curl); + StaticLogger::log(LogLevel::ERROR, "$httpCode Error-Response from $url\n$error"); + throw new RuntimeException('Fehler beim Senden des GET-Requests: ' . $error); + } elseif ($httpCode < 200 || $httpCode >= 300) { + curl_close($curl); + StaticLogger::log(LogLevel::ERROR, "$httpCode Error-Response from $url"); + throw new RuntimeException('Fehler beim Senden des GET-Requests: HTTP-Statuscode ' . $httpCode); + } + + // Request beenden + curl_close($curl); + + StaticLogger::log(LogLevel::DEBUG, "$httpCode Response from $url\n" . print_r($result, true)); + + return (string) $result; + } + + /** + * @throws RuntimeException + */ + private function sendCurlPostRequest(string $url, $data): string + { + // HTTP POST-Request konfigurieren + $curl = curl_init($url); + curl_setopt($curl, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_USERAGENT, 'Modified Module Loader Client'); + + StaticLogger::log( + LogLevel::DEBUG, + "Send POST request to $url\n[DATA]\n" . print_r($data, true) + ); + + // Request senden und Ergebnis erhalten + $result = curl_exec($curl); + + // HTTP-Statuscode und Fehler prüfen + $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + if ($result === false) { + $error = curl_error($curl); + curl_close($curl); + StaticLogger::log(LogLevel::ERROR, "$httpCode Error-Response from $url\n$error"); + throw new RuntimeException('Fehler beim Senden des POST-Requests: ' . $error); + } elseif ($httpCode < 200 || $httpCode >= 300) { + curl_close($curl); + StaticLogger::log(LogLevel::ERROR, "$httpCode Error-Response from $url"); + throw new RuntimeException('Fehler beim Senden des POST-Requests: HTTP-Statuscode ' . $httpCode); + } + + // Request beenden + curl_close($curl); + + return (string) $result; + } + + private function sendFileGetContentsPostRequest(string $url, $data): string { // http verwenden, auch wenn die Url mit https://... beginnt $options = [ @@ -56,7 +157,7 @@ public function sendPostRequest(string $url, $data) return $result; } - public function sendGetRequest($url) + private function sendFileGetContentsGetRequest(string $url): string { // http verwenden, auch wenn die Url mit https://... beginnt $options = [ @@ -82,13 +183,4 @@ public function sendGetRequest($url) return $result; } - - public static function createQuery(array $queryValues): string - { - $query = ''; - foreach ($queryValues as $name => $value) { - $query .= $name . '=' . urlencode($value) . '&'; - } - return $query; - } } diff --git a/tests/unit/HttpRequestTest.php b/tests/unit/HttpRequestTest.php new file mode 100644 index 00000000..e0c9bb5a --- /dev/null +++ b/tests/unit/HttpRequestTest.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace RobinTheHood\ModifiedModuleLoaderClient\Tests\Unit; + +use PHPUnit\Framework\TestCase; +use RobinTheHood\ModifiedModuleLoaderClient\Api\V1\HttpRequest; + +class HttpRequestTest extends TestCase +{ + public function testSendPostRequestSuccess() + { + $httpClient = new HttpRequest(); + $response = $httpClient->sendPostRequest('https://postman-echo.com/post', ['foo' => 'bar']); + + $this->assertNotNull($response); + $this->assertIsString($response); + $this->assertStringContainsString('application/x-www-form-urlencoded', $response); + $this->assertStringContainsString('"foo": "bar"', $response); + } + + public function testSendPostRequestFailure() + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage( + 'Fehler beim Senden des POST-Requests: Could not resolve host: this-url-does-not-exist.com' + ); + + $httpClient = new HttpRequest(); + $response = $httpClient->sendPostRequest('https://this-url-does-not-exist.com', []); + $this->assertIsString($response); + } +}