From f440e07d53fc08167e83550508af43029a4a8c70 Mon Sep 17 00:00:00 2001 From: Ibrahim Date: Wed, 27 Nov 2024 18:00:30 +0300 Subject: [PATCH 1/2] feat: Added Ability to Mimic HTTP Request Headers --- webfiori/http/APITestCase.php | 52 +++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/webfiori/http/APITestCase.php b/webfiori/http/APITestCase.php index 2dcb63f..3a3c8fe 100644 --- a/webfiori/http/APITestCase.php +++ b/webfiori/http/APITestCase.php @@ -69,10 +69,14 @@ public function addFile(string $fileIdx, string $filePath, bool $reset = false) * @param array $parameters A dictionary thar represents the parameters that * will be sent to the endpoint. The name is parameter name as it appears in * service implementation and its value is the value of the parameter. + * + * @param array $httpHeaders An optional associative array that can be used + * to mimic HTTP request headers. The keys of the array are names of headers + * and the value of each key represents the value of the header. * * @return string The method will return the output of the endpoint. */ - public function callEndpoint(WebServicesManager $manager, string $requestMethod, string $apiEndpointName, array $parameters = []) : string { + public function callEndpoint(WebServicesManager $manager, string $requestMethod, string $apiEndpointName, array $parameters = [], array $httpHeaders = []) : string { $manager->setOutputStream(fopen(self::OUTPUT_STREAM,'w')); $method = strtoupper($requestMethod); putenv('REQUEST_METHOD='.$method); @@ -90,13 +94,13 @@ public function callEndpoint(WebServicesManager $manager, string $requestMethod, } $_POST['service'] = $apiEndpointName; $_SERVER['CONTENT_TYPE'] = 'multipart/form-data'; - $this->unset($_POST, $parameters, $manager); + $this->unset($_POST, $parameters, $manager, $httpHeaders); } else { foreach ($parameters as $key => $val) { $_GET[$key] = $this->parseVal($val); } $_GET['service'] = $apiEndpointName; - $this->unset($_GET, $parameters, $manager); + $this->unset($_GET, $parameters, $manager, $httpHeaders); } $retVal = $manager->readOutputStream(); @@ -141,11 +145,15 @@ private function parseVal($val) { * @param array $parameters An optional array of request parameters that can be * passed to the endpoint. * + * @param array $httpHeaders An optional associative array that can be used + * to mimic HTTP request headers. The keys of the array are names of headers + * and the value of each key represents the value of the header. + * * @return string The method will return the output that was produced by * the endpoint as string. */ - public function deletRequest(WebServicesManager $manager, string $endpoint, array $parameters = []) : string { - return $this->callEndpoint($manager, RequestMethod::DELETE, $endpoint, $parameters); + public function deletRequest(WebServicesManager $manager, string $endpoint, array $parameters = [], array $httpHeaders = []) : string { + return $this->callEndpoint($manager, RequestMethod::DELETE, $endpoint, $parameters, $httpHeaders); } /** * Sends a GET request to specific endpoint. @@ -160,8 +168,8 @@ public function deletRequest(WebServicesManager $manager, string $endpoint, arra * @return string The method will return the output that was produced by * the endpoint as string. */ - public function getRequest(WebServicesManager $manager, string $endpoint, array $parameters = []) : string { - return $this->callEndpoint($manager, RequestMethod::GET, $endpoint, $parameters); + public function getRequest(WebServicesManager $manager, string $endpoint, array $parameters = [], array $httpHeaders = []) : string { + return $this->callEndpoint($manager, RequestMethod::GET, $endpoint, $parameters, $httpHeaders); } /** * Sends a POST request to specific endpoint. @@ -173,11 +181,15 @@ public function getRequest(WebServicesManager $manager, string $endpoint, array * @param array $parameters An optional array of request parameters that can be * passed to the endpoint. * + * @param array $httpHeaders An optional associative array that can be used + * to mimic HTTP request headers. The keys of the array are names of headers + * and the value of each key represents the value of the header. + * * @return string The method will return the output that was produced by * the endpoint as string. */ - public function postRequest(WebServicesManager $manager, string $endpoint, array $parameters = []) : string { - return $this->callEndpoint($manager, RequestMethod::POST, $endpoint, $parameters); + public function postRequest(WebServicesManager $manager, string $endpoint, array $parameters = [], array $httpHeaders = []) : string { + return $this->callEndpoint($manager, RequestMethod::POST, $endpoint, $parameters, $httpHeaders); } /** * Sends a PUT request to specific endpoint. @@ -189,11 +201,15 @@ public function postRequest(WebServicesManager $manager, string $endpoint, array * @param array $parameters An optional array of request parameters that can be * passed to the endpoint. * + * @param array $httpHeaders An optional associative array that can be used + * to mimic HTTP request headers. The keys of the array are names of headers + * and the value of each key represents the value of the header. + * * @return string The method will return the output that was produced by * the endpoint as string. */ - public function putRequest(WebServicesManager $manager, string $endpoint, array $parameters = []) : string { - return $this->callEndpoint($manager, RequestMethod::PUT, $endpoint, $parameters); + public function putRequest(WebServicesManager $manager, string $endpoint, array $parameters = [], array $httpHeaders = []) : string { + return $this->callEndpoint($manager, RequestMethod::PUT, $endpoint, $parameters, $httpHeaders); } private function extractPathAndName($absPath): array { $DS = DIRECTORY_SEPARATOR; @@ -219,11 +235,23 @@ private function extractPathAndName($absPath): array { 'path' => '' ]; } - private function unset(array &$arr, array $params, WebServicesManager $m) { + private function unset(array &$arr, array $params, WebServicesManager $m, array $httpHeaders) { + foreach ($httpHeaders as $header => $value) { + $trHeader = trim($header.''); + $trVal = trim($value.''); + if (strlen($trHeader) != 0) { + $_SERVER['HTTP_'.strtoupper($trHeader)] = $trVal; + } + } $m->process(); foreach ($params as $key => $val) { unset($arr[$key]); } + + foreach ($httpHeaders as $header => $value) { + $trHeader = trim($header.''); + unset($_SERVER['HTTP_'.strtoupper($trHeader)]); + } } } From fa38055c3aceec01ffb070562f50022e0ea0fa16 Mon Sep 17 00:00:00 2001 From: Ibrahim Date: Wed, 27 Nov 2024 18:05:59 +0300 Subject: [PATCH 2/2] ci: Added Release Please Integration --- .github/workflows/php83.yml | 17 ++++++++++++++++- composer.json | 1 + release-please-config.json | 26 ++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 release-please-config.json diff --git a/.github/workflows/php83.yml b/.github/workflows/php83.yml index feb86a9..f67c9dc 100644 --- a/.github/workflows/php83.yml +++ b/.github/workflows/php83.yml @@ -41,4 +41,19 @@ jobs: - name: CodeCov uses: codecov/codecov-action@v4 with: - token: ${{ secrets.CODECOV_TOKEN }} \ No newline at end of file + token: ${{ secrets.CODECOV_TOKEN }} + + + release_prod: + name: Publish Release + needs: + - "test" + runs-on: ubuntu-latest + if: github.ref == 'refs/heads/master' + steps: + - uses: actions/checkout@v4 + - uses: google-github-actions/release-please-action@v3 + with: + release-type: php + config-file: release-please-config.json + token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/composer.json b/composer.json index 5eaa8ee..e5add0c 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "keywords": [ "JSON","PHP","API","Web APIs","Library","HTTP","PSR-7", "JSONx" ], + "version":"3.3.15", "license": "MIT", "require": { "php": ">=7.0", diff --git a/release-please-config.json b/release-please-config.json new file mode 100644 index 0000000..3fe3029 --- /dev/null +++ b/release-please-config.json @@ -0,0 +1,26 @@ +{ + "include-v-in-tag":true, + "tag-separator": "-", + "packages":{ + ".":{ + "changelog-path": "CHANGELOG.md", + "changelog-sections": [ + { "type": "feat", "section": "Features" }, + { "type": "feature", "section": "Features" }, + { "type": "fix", "section": "Bug Fixes" }, + { "type": "perf", "section": "Performance Improvements" }, + { "type": "revert", "section": "Reverts" }, + { "type": "docs", "section": "Documentation" }, + { "type": "style", "section": "Styles" }, + { "type": "chore", "section": "Miscellaneous Chores" }, + { "type": "refactor", "section": "Code Refactoring" }, + { "type": "test", "section": "Testing" }, + { "type": "build", "section": "Build System" }, + { "type": "ci", "section": "Continuous Integration" }, + { "type": "ui", "section": "User Interface" }, + { "type": "database", "section": "Database Changes" }, + { "type": "email", "section": "Email Notifications Changes" } + ] + } + } +} \ No newline at end of file