]> git.mxchange.org Git - friendica.git/commitdiff
Fixing Response
authorPhilipp <admin@philipp.info>
Sun, 21 Nov 2021 23:07:09 +0000 (00:07 +0100)
committerPhilipp <admin@philipp.info>
Sat, 27 Nov 2021 11:40:57 +0000 (12:40 +0100)
src/App.php
src/Capabilities/ICanCreateResponses.php
src/Module/Api/ApiResponse.php
src/Module/Response.php
tests/src/Module/Api/ApiResponseTest.php
tests/src/Module/Api/Friendica/NotificationTest.php
tests/src/Module/Api/GnuSocial/GnuSocial/VersionTest.php
tests/src/Module/Api/GnuSocial/Help/TestTest.php
tests/src/Module/Api/Twitter/Account/RateLimitStatusTest.php
tests/src/Module/Api/Twitter/SavedSearchesTest.php
tests/src/Module/NodeInfoTest.php

index 30194af8e273e001b8ce72510c121b7f844f70af..c80518c1922b66770975d3b51c7e20a326d8fa2f 100644 (file)
@@ -704,7 +704,7 @@ class App
 
                        // Let the module run it's internal process (init, get, post, ...)
                        $response = $module->run($_POST, $_REQUEST);
-                       if ($response->getHeaderLine('X-RESPONSE-TYPE') === ICanCreateResponses::TYPE_HTML) {
+                       if ($response->getHeaderLine(ICanCreateResponses::X_HEADER) === ICanCreateResponses::TYPE_HTML) {
                                $page->run($this, $this->baseURL, $this->args, $this->mode, $response, $this->l10n, $this->profiler, $this->config, $pconfig);
                        } else {
                                $page->exit($response);
index 21a7b1bde925022202af533d8aa2862d7a119afe..0cd5348c5331fe3cb38d689fdc58c00a31ba0fd4 100644 (file)
@@ -7,6 +7,11 @@ use Psr\Http\Message\ResponseInterface;
 
 interface ICanCreateResponses
 {
+       /**
+        * This constant helps to find the specific return type of responses inside the headers array
+        */
+       const X_HEADER = 'X-RESPONSE-TYPE';
+
        const TYPE_HTML = 'html';
        const TYPE_XML  = 'xml';
        const TYPE_JSON = 'json';
index 8a28e5766bec9459b5267d4733794b7eb97d75ed..863e1d21d2591a7c042017eb2138ae3492f670bc 100644 (file)
@@ -199,10 +199,10 @@ class ApiResponse extends Response
 
                switch ($format) {
                        case 'xml':
-                               $this->setHeader('Content-Type: text/xml');
+                               $this->setType(static::TYPE_XML);
                                break;
                        case 'json':
-                               $this->setHeader('Content-Type: application/json');
+                               $this->setType(static::TYPE_JSON);
                                if (!empty($return)) {
                                        $json = json_encode(end($return));
                                        if (!empty($_GET['callback'])) {
@@ -212,10 +212,10 @@ class ApiResponse extends Response
                                }
                                break;
                        case 'rss':
-                               $this->setHeader('Content-Type: application/rss+xml');
+                               $this->setType(static::TYPE_RSS);
                                break;
                        case 'atom':
-                               $this->setHeader('Content-Type: application/atom+xml');
+                               $this->setType(static::TYPE_ATOM);
                                break;
                }
 
index 29df82c04432cf2f1398a9cac023a5bcf82f015d..d24ebe954a8317aaa39a1fbf6a0cc1b8fb310a8f 100644 (file)
@@ -19,7 +19,7 @@ class Response implements ICanCreateResponses
        /**
         * @var string
         */
-       protected $type = ICanCreateResponses::TYPE_HTML;
+       protected $type = self::TYPE_HTML;
 
        /**
         * {@inheritDoc}
@@ -68,7 +68,7 @@ class Response implements ICanCreateResponses
         */
        public function setType(string $type, ?string $content_type = null): void
        {
-               if (!in_array($type, ICanCreateResponses::ALLOWED_TYPES)) {
+               if (!in_array($type, static::ALLOWED_TYPES)) {
                        throw new InternalServerErrorException('wrong type');
                }
 
@@ -79,9 +79,14 @@ class Response implements ICanCreateResponses
                        case static::TYPE_XML:
                                $content_type = $content_type ?? 'text/xml';
                                break;
+                       case static::TYPE_RSS:
+                               $content_type = $content_type ?? 'application/rss+xml';
+                               break;
+                       case static::TYPE_ATOM:
+                               $content_type = $content_type ?? 'application/atom+xml';
+                               break;
                }
 
-
                $this->setHeader($content_type, 'Content-type');
 
                $this->type = $type;
@@ -101,7 +106,7 @@ class Response implements ICanCreateResponses
        public function generate(): ResponseInterface
        {
                // Setting the response type as an X-header for direct usage
-               $this->headers['X-RESPONSE-TYPE'] = $this->type;
+               $this->headers[static::X_HEADER] = $this->type;
 
                return new \GuzzleHttp\Psr7\Response(200, $this->headers, $this->content);
        }
index aba8c808a55b0a485353888be720ce6c0ae21a9e..99e3a8d5da0b2ad3af22ddd336f0549c435a4022 100644 (file)
@@ -37,6 +37,7 @@ class ApiResponseTest extends MockedTest
                $response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
                $response->error(200, 'OK', 'error_message', 'xml');
 
+               self::assertEquals(['Content-type' => 'text/xml', 'HTTP/1.1 200 OK'], $response->getHeaders());
                self::assertEquals('<?xml version="1.0"?>' . "\n" .
                                                   '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
                                                   'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
@@ -59,6 +60,7 @@ class ApiResponseTest extends MockedTest
                $response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
                $response->error(200, 'OK', 'error_message', 'rss');
 
+               self::assertEquals(['Content-type' => 'application/rss+xml', 'HTTP/1.1 200 OK'], $response->getHeaders());
                self::assertEquals(
                        '<?xml version="1.0"?>' . "\n" .
                        '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
@@ -82,6 +84,7 @@ class ApiResponseTest extends MockedTest
                $response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
                $response->error(200, 'OK', 'error_message', 'atom');
 
+               self::assertEquals(['Content-type' => 'application/atom+xml', 'HTTP/1.1 200 OK'], $response->getHeaders());
                self::assertEquals(
                        '<?xml version="1.0"?>' . "\n" .
                        '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
index 22c498c44bf8ee7611f713fdf0005a48c03d57c2..ef93de3ef13ef3f3edd22bee464b1926e7fa0c2a 100644 (file)
@@ -21,6 +21,7 @@
 
 namespace Friendica\Test\src\Module\Api\Friendica;
 
+use Friendica\Capabilities\ICanCreateResponses;
 use Friendica\DI;
 use Friendica\Module\Api\Friendica\Notification;
 use Friendica\Test\src\Module\Api\ApiTest;
@@ -68,7 +69,10 @@ XML;
                $notification = new Notification(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'xml']);
                $response = $notification->run();
 
+               print_r($response->getHeaders());
+
                self::assertXmlStringEqualsXmlString($assertXml, (string)$response->getBody());
+               self::assertEquals(['Content-type' => ['text/xml'], ICanCreateResponses::X_HEADER => ['xml']], $response->getHeaders());
        }
 
        public function testWithJsonResult()
@@ -77,5 +81,6 @@ XML;
                $response = $notification->run();
 
                self::assertJson($response->getBody());
+               self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
        }
 }
index e5057f09beadd170a4c353644f151455190ce6f2..0f65318319dba6c7cdac5f6da7f810268f7f3613 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Friendica\Test\src\Module\Api\GnuSocial\GnuSocial;
 
+use Friendica\Capabilities\ICanCreateResponses;
 use Friendica\DI;
 use Friendica\Module\Api\GNUSocial\GNUSocial\Version;
 use Friendica\Test\src\Module\Api\ApiTest;
@@ -13,6 +14,7 @@ class VersionTest extends ApiTest
                $version = new Version(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json']);
                $response = $version->run();
 
+               self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
                self::assertEquals('"0.9.7"', $response->getBody());
        }
 }
index 85bc89e00336d1f60d46bfe2f1ad158692b77f6a..5aa4286812c7dd4a61f5389a0e240b173740c57d 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Friendica\Test\src\Module\Api\GnuSocial\Help;
 
+use Friendica\Capabilities\ICanCreateResponses;
 use Friendica\DI;
 use Friendica\Module\Api\GNUSocial\Help\Test;
 use Friendica\Test\src\Module\Api\ApiTest;
@@ -13,6 +14,7 @@ class TestTest extends ApiTest
                $test = new Test(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json']);
                $response = $test->run();
 
+               self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
                self::assertEquals('"ok"', $response->getBody());
        }
 
@@ -21,6 +23,7 @@ class TestTest extends ApiTest
                $test = new Test(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'xml']);
                $response = $test->run();
 
+               self::assertEquals(['Content-type' => ['text/xml'], ICanCreateResponses::X_HEADER => ['xml']], $response->getHeaders());
                self::assertxml($response->getBody(), 'ok');
        }
 }
index aa76c1bf53e59f24b88af4982e9e4ba0f99a00f1..64becb75c52d382762e850e831d9aee7822816d0 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Friendica\Test\src\Module\Api\Twitter\Account;
 
+use Friendica\Capabilities\ICanCreateResponses;
 use Friendica\DI;
 use Friendica\Module\Api\Twitter\Account\RateLimitStatus;
 use Friendica\Test\src\Module\Api\ApiTest;
@@ -15,6 +16,7 @@ class RateLimitStatusTest extends ApiTest
 
                $result = json_decode($response->getBody());
 
+               self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
                self::assertEquals(150, $result->remaining_hits);
                self::assertEquals(150, $result->hourly_limit);
                self::assertIsInt($result->reset_time_in_seconds);
@@ -25,6 +27,7 @@ class RateLimitStatusTest extends ApiTest
                $rateLimitStatus = new RateLimitStatus(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'xml']);
                $response = $rateLimitStatus->run();
 
+               self::assertEquals(['Content-type' => ['text/xml'], ICanCreateResponses::X_HEADER => ['xml']], $response->getHeaders());
                self::assertXml($response->getBody(), 'hash');
        }
 }
index 0b20335c48b10caadd015f0a449be389f570496e..14973b3912a726110e5788f087d2d67a139cacb2 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Friendica\Test\src\Module\Api\Twitter;
 
+use Friendica\Capabilities\ICanCreateResponses;
 use Friendica\DI;
 use Friendica\Module\Api\Twitter\SavedSearches;
 use Friendica\Test\src\Module\Api\ApiTest;
@@ -15,6 +16,7 @@ class SavedSearchesTest extends ApiTest
 
                $result = json_decode($response->getBody());
 
+               self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
                self::assertEquals(1, $result[0]->id);
                self::assertEquals(1, $result[0]->id_str);
                self::assertEquals('Saved search', $result[0]->name);
index cb167053306c70d7a709960998f13a0e3d7f640d..53a2926f9f8cc4a558a95d697f6d6ade96296346 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Friendica\Test\src\Module;
 
+use Friendica\Capabilities\ICanCreateResponses;
 use Friendica\DI;
 use Friendica\Module\NodeInfo110;
 use Friendica\Module\NodeInfo120;
@@ -19,7 +20,7 @@ class NodeInfoTest extends FixtureTest
                $response = $nodeinfo->run();
 
                self::assertJson($response->getBody());
-               self::assertEquals(['Content-type' => ['application/json']], $response->getHeaders());
+               self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
 
                $json = json_decode($response->getBody());
 
@@ -42,7 +43,7 @@ class NodeInfoTest extends FixtureTest
                $response = $nodeinfo->run();
 
                self::assertJson($response->getBody());
-               self::assertEquals(['Content-type' => ['application/json; charset=utf-8']], $response->getHeaders());
+               self::assertEquals(['Content-type' => ['application/json; charset=utf-8'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
 
                $json = json_decode($response->getBody());
 
@@ -64,7 +65,7 @@ class NodeInfoTest extends FixtureTest
                $response = $nodeinfo->run();
 
                self::assertJson($response->getBody());
-               self::assertEquals(['Content-type' => ['application/json; charset=utf-8']], $response->getHeaders());
+               self::assertEquals(['Content-type' => ['application/json; charset=utf-8'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
 
                $json = json_decode($response->getBody());