From 4bc2ad8fa0359e75c9ecd39a2ee7b2f1f6f9ee02 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 1 Nov 2024 14:42:43 +0900 Subject: [PATCH] URL Requests basic tests file --- www/admin/UrlRequests.target.php | 9 +- www/admin/class_test.url-requests.curl.php | 229 ++++++++++++++++----- 2 files changed, 179 insertions(+), 59 deletions(-) diff --git a/www/admin/UrlRequests.target.php b/www/admin/UrlRequests.target.php index 1b5b8b2f..c127b97d 100644 --- a/www/admin/UrlRequests.target.php +++ b/www/admin/UrlRequests.target.php @@ -23,13 +23,16 @@ $file_get = file_get_contents('php://input') ?: '{"Error" => "file_get_contents $log->debug('SERVER', $log->prAr($_SERVER)); $log->debug('HEADERS', $log->prAr($http_headers)); +$log->debug('GET', $log->prAr($_GET)); $log->debug('POST', $log->prAr($_POST)); $log->debug('PHP-INPUT', $log->prAr($file_get)); +header("Content-Type: application/json; charset=UTF-8"); + print Json::jsonConvertArrayTo([ - 'HTTP_HEADERS' => $http_headers, - "_GET" => $_GET, - "_POST" => Json::jsonConvertToArray($file_get), + 'HEADERS' => $http_headers, + "PARAMS" => $_GET, + "BODY" => Json::jsonConvertToArray($file_get), ]); $log->debug('[END]', '=========================================>'); diff --git a/www/admin/class_test.url-requests.curl.php b/www/admin/class_test.url-requests.curl.php index 32d6931a..5b6b9ec7 100644 --- a/www/admin/class_test.url-requests.curl.php +++ b/www/admin/class_test.url-requests.curl.php @@ -26,8 +26,6 @@ $log = new CoreLibs\Logging\Logging([ 'log_per_date' => true, ]); -$client = new Curl(); - $PAGE_NAME = 'TEST CLASS: URL REQUESTS CURL'; print ""; print "" . $PAGE_NAME . ""; @@ -35,87 +33,206 @@ print ""; print '
Class Test Master
'; print '

' . $PAGE_NAME . '

'; +$client = new Curl(); + print "
"; -$data = $client->requestGet( - 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlReqeusts.target.php' - . '?other=get_a', - ['test-header: ABC', 'request-type: _GET'], - ['foo' => 'BAR'] +$data = $client->get( + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php' + . '?other=get_a', + [ + 'headers' => $client->prepareHeaders([ + 'test-header: ABC', + 'info-request-type: _GET', + 'Funk-pop' => 'Semlly god' + ]), + 'query' => ['foo' => 'BAR'] + ] ); print "_GET RESPONSE:
" . print_r($data, true) . "
"; print "
"; -$data = $client->requestPost( - 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlReqeusts.target.php' - . '?other=post_a', - ['payload' => 'data post'], - [ - 'Content-Type: application/json', - 'Accept: application/json', - 'test-header: ABC', - 'info-request-type: _POST' - ], - ['foo' => 'BAR post'], +$data = $client->request( + 'get', + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php' + . '?other=get_a', ); -print "_POST RESPONSE:
" . print_r($data, true) . "
"; +print "_GET RESPONSE, nothing set:
" . print_r($data, true) . "
"; print "
"; -$data = $client->requestPut( - 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlReqeusts.target.php' - . '?other=put_a', - ['payload' => 'data put'], +try { + $data = $client->request( + 'get', + 'soba54.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php' + . '?other=get_a', + ); + print "_GET RESPONSE, nothing set, invalid URL:
" . print_r($data, true) . "
"; +} catch (Exception $e) { + print "Exception:
" . print_r($e, true) . "

"; +} + + +print "
"; +$data = $client->request( + "get", + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/' + . 'trunk/www/admin/UrlRequests.target.php' + . '?other=get_a', [ - 'Content-Type: application/json', - 'Accept: application/json', - 'test-header: ABC', - 'info-request-type: _PUT' + "headers" => $client->prepareHeaders([ + 'test-header: ABC', + 'info-request-type: _GET', + 'Funk-pop' => 'Semlly god' + ]), + "query" => ['foo' => 'BAR'], ], - ['foo' => 'BAR put'], +); +print "[request] _GET RESPONSE:
" . print_r($data, true) . "
"; + +print "
"; +$data = $client->post( + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php' + . '?other=post_a', + [ + 'body' => ['payload' => 'data post'], + 'headers' => $client->prepareHeaders([ + 'Content-Type: application/json', + 'Accept: application/json', + 'test-header: ABC', + 'info-request-type: _POST' + ]), + 'query' => ['foo' => 'BAR post'], + ] +); +print "_POST RESPONSE:
" . print_r($data, true) . "
"; +print "
"; +$data = $client->request( + "post", + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php' + . '?other=post_a', + [ + "body" => ['payload' => 'data post', 'request' => 'I am the request body'], + "headers" => $client->prepareHeaders([ + 'Content-Type: application/json', + 'Accept: application/json', + 'test-header: ABC', + 'info-request-type: _POST' + ]), + "query" => ['foo' => 'BAR post'], + ] +); +print "[request] _POST RESPONSE:
" . print_r($data, true) . "
"; + +print "
"; +$data = $client->put( + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php' + . '?other=put_a', + [ + "body" => ['payload' => 'data put'], + "headers" => $client->prepareHeaders([ + 'Content-Type: application/json', + 'Accept: application/json', + 'test-header: ABC', + 'info-request-type: _PUT' + ]), + 'query' => ['foo' => 'BAR put'], + ] ); print "_PUT RESPONSE:
" . print_r($data, true) . "
"; print "
"; -$data = $client->requestPatch( - 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlReqeusts.target.php' +$data = $client->patch( + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php' . '?other=patch_a', - ['payload' => 'data patch'], [ - 'Content-Type: application/json', - 'Accept: application/json', - 'test-header: ABC', - 'info-request-type: _PATCH' - ], - ['foo' => 'BAR patch'], + "body" => ['payload' => 'data patch'], + "headers" => $client->prepareHeaders([ + 'Content-Type: application/json', + 'Accept: application/json', + 'test-header: ABC', + 'info-request-type: _PATCH' + ]), + 'query' => ['foo' => 'BAR patch'], + ] ); print "_PATCH RESPONSE:
" . print_r($data, true) . "
"; print "
"; -$data = $client->requestDelete( - 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlReqeusts.target.php' +$data = $client->delete( + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php' . '?other=delete_no_body_a', - null, [ - 'Content-Type: application/json', - 'Accept: application/json', - 'test-header: ABC', - 'info-request-type: _DELETE' - ], - ['foo' => 'BAR delete'], + "body" => null, + "headers" => $client->prepareHeaders([ + 'Content-Type: application/json', + 'Accept: application/json', + 'test-header: ABC', + 'info-request-type: _DELETE' + ]), + "query" => ['foo' => 'BAR delete'], + ] ); print "_DELETE RESPONSE:
" . print_r($data, true) . "
"; -$data = $client->requestDelete( - 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlReqeusts.target.php' + +print "
"; +$data = $client->delete( + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php' . '?other=delete_body_a', - ['payload' => 'data delete'], [ - 'Content-Type: application/json', - 'Accept: application/json', - 'test-header: ABC', - 'info-request-type: _DELETE' - ], - ['foo' => 'BAR delete'], + "body" => ['payload' => 'data delete'], + "headers" => $client->prepareHeaders([ + 'Content-Type: application/json', + 'Accept: application/json', + 'test-header: ABC', + 'info-request-type: _DELETE' + ]), + "query" => ['foo' => 'BAR delete'], + ] ); -print "_DELETE RESPONSE:
" . print_r($data, true) . "
"; +print "_DELETE RESPONSE BODY:
" . print_r($data, true) . "
"; + +print "
"; + +try { + $uc = new Curl([ + "base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/foo', + "headers" => [ + 'DEFAULT-master' => 'master-header', + 'default-header' => 'uc-get', + 'default-remove' => 'will be removed', + 'default-remove-array' => ['a', 'b'], + 'default-remove-array-part' => ['c', 'd'], + 'default-remove-array-part-alt' => ['c', 'd', 'e'], + 'default-overwrite' => 'will be overwritten', + 'default-add' => 'will be added', + ] + ]); + print "CONFIG:
" . print_r($uc->getConfig(), true) . "
"; + $uc->removeHeaders(['default-remove' => '']); + $uc->removeHeaders(['default-remove-array' => ['a', 'b']]); + $uc->removeHeaders(['default-remove-array-part' => 'c']); + $uc->removeHeaders(['default-remove-array-part-alt' => ['c', 'd']]); + $uc->setHeaders(['default-new' => 'Something new']); + $uc->setHeaders(['default-overwrite' => 'Something Overwritten']); + $uc->setHeaders(['default-add' => 'Something Added'], true); + print "CONFIG:
" . print_r($uc->getConfig(), true) . "
"; + $data = $uc->request( + 'get', + 'UrlRequests.target.php?other=get_a', + [ + 'headers' => [ + 'call-header' => 'call-get', + 'default-header' => 'overwrite-uc-get', + 'X-Foo' => ['bar', 'baz'], + ] + ] + ); + print "[uc] _GET RESPONSE, nothing set:
" . print_r($data, true) . "
"; + print "[uc] SENT URL: " . $uc->getUrlSent() . "
"; + print "[uc] SENT URL PARSED:
" . print_r($uc->getUrlParsedSent(), true) . "
"; + print "[uc] SENT HEADERS:
" . print_r($uc->getHeadersSent(), true) . "
"; +} catch (Exception $e) { + print "Exception:
" . print_r(json_decode($e->getMessage(), true), true) . "

"; +} print "";