]> git.mxchange.org Git - friendica.git/commitdiff
Add explicit status setting for PSR/ResponseInterface & add tests for OPTIONS endpoint
authorPhilipp <admin@philipp.info>
Sun, 2 Jan 2022 20:28:28 +0000 (21:28 +0100)
committerPhilipp <admin@philipp.info>
Tue, 4 Jan 2022 19:59:25 +0000 (20:59 +0100)
src/App.php
src/App/Page.php
src/Capabilities/ICanCreateResponses.php
src/Module/Response.php
src/Module/Special/Options.php
tests/src/Module/Special/OptionsTest.php [new file with mode: 0644]

index f7c929820d308640f7b3156d684b58d997417d70..c56222b63f5d7b52bfa9daf98b929b13842c68b7 100644 (file)
@@ -710,7 +710,8 @@ class App
                        $timestamp = microtime(true);
                        $response = $module->run($input);
                        $this->profiler->set(microtime(true) - $timestamp, 'content');
-                       if ($response->getHeaderLine(ICanCreateResponses::X_HEADER) === ICanCreateResponses::TYPE_HTML) {
+                       if ($response->getHeaderLine(ICanCreateResponses::X_HEADER) === ICanCreateResponses::TYPE_HTML &&
+                               $response->getStatusCode() == 200) {
                                $page->run($this, $this->baseURL, $this->args, $this->mode, $response, $this->l10n, $this->profiler, $this->config, $pconfig);
                        } else {
                                $page->exit($response);
index be5f8dc1e660664cd5d857d1ccbdb940509f4657..57468f8e5e988e90152db9703a8963dc01afe33e 100644 (file)
@@ -378,6 +378,12 @@ class Page implements ArrayAccess
         */
        public function exit(ResponseInterface $response)
        {
+               header(sprintf("HTTP/%s %i %s",
+                       $response->getProtocolVersion(),
+                       $response->getStatusCode(),
+                       $response->getReasonPhrase())
+               );
+
                foreach ($response->getHeaders() as $key => $header) {
                        if (is_array($header)) {
                                $header_str = implode(',', $header);
index f0dac471f5ba8f99a6fba447d2562f40f83cfbcb..96149bab108fa44010bb52abf602d823d2227d05 100644 (file)
@@ -70,6 +70,16 @@ interface ICanCreateResponses
         */
        public function setType(string $type, ?string $content_type = null): void;
 
+       /**
+        * Sets the status and the reason for the response
+        *
+        * @param int $status The HTTP status code
+        * @param null|string $reason Reason phrase (when empty a default will be used based on the status code)
+        *
+        * @return void
+        */
+       public function setStatus(int $status = 200, ?string $reason = null): void;
+
        /**
         * Creates a PSR-7 compliant interface
         * @see https://www.php-fig.org/psr/psr-7/
index dc11c9908df0c34c5a4fd10c167c7e92d90cef70..e4bfde7a3c6d949c6c0ad72dca685ef29e987f4c 100644 (file)
@@ -40,6 +40,10 @@ class Response implements ICanCreateResponses
         */
        protected $type = self::TYPE_HTML;
 
+       protected $status = 200;
+
+       protected $reason = null;
+
        /**
         * {@inheritDoc}
         */
@@ -111,6 +115,15 @@ class Response implements ICanCreateResponses
                $this->type = $type;
        }
 
+       /**
+        * {@inheritDoc}
+        */
+       public function setStatus(int $status = 200, ?string $reason = null): void
+       {
+               $this->status = $status;
+               $this->reason = $reason;
+       }
+
        /**
         * {@inheritDoc}
         */
@@ -127,6 +140,6 @@ class Response implements ICanCreateResponses
                // Setting the response type as an X-header for direct usage
                $this->headers[static::X_HEADER] = $this->type;
 
-               return new \GuzzleHttp\Psr7\Response(200, $this->headers, $this->content);
+               return new \GuzzleHttp\Psr7\Response($this->status, $this->headers, $this->content, $this->reason);
        }
 }
index 36bbe4fb137121b05b08d8cb2f8e3c6c4a4200a5..327f746b0c4ef84db228a5aa9259c551583aef88 100644 (file)
@@ -10,7 +10,7 @@ class Options extends BaseModule
        protected function options(array $request = [])
        {
                // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
-               $this->response->setHeader('Allow', implode(',', Router::ALLOWED_METHODS));
-               $this->response->setHeader(($this->server['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' 204 No Content');
+               $this->response->setHeader(implode(',', Router::ALLOWED_METHODS), 'Allow');
+               $this->response->setStatus(204);
        }
 }
diff --git a/tests/src/Module/Special/OptionsTest.php b/tests/src/Module/Special/OptionsTest.php
new file mode 100644 (file)
index 0000000..3f7024f
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+
+namespace Friendica\Test\src\Module\Special;
+
+use Friendica\App\Router;
+use Friendica\Capabilities\ICanCreateResponses;
+use Friendica\DI;
+use Friendica\Module\Special\Options;
+use Friendica\Test\FixtureTest;
+
+class OptionsTest extends FixtureTest
+{
+       public function testOptions()
+       {
+               $response = (new Options(DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::OPTIONS]))->run();
+
+               self::assertEmpty((string)$response->getBody());
+               self::assertEquals(204, $response->getStatusCode());
+               self::assertEquals('No Content', $response->getReasonPhrase());
+               self::assertEquals([
+                       'Allow'                       => [implode(',', Router::ALLOWED_METHODS)],
+                       ICanCreateResponses::X_HEADER => ['html'],
+               ], $response->getHeaders());
+               self::assertEquals(implode(',', Router::ALLOWED_METHODS), $response->getHeaderLine('Allow'));
+       }
+}