From 69c1c629927cd78286fc6c8d61b5b5ad78245508 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 24 Oct 2023 22:27:27 +0300 Subject: [PATCH] add a workaround for make_self_url() when invoked off /api/ endpoint, add unit tests for this method --- .vscode/tasks.json | 28 ++++++---- classes/config.php | 6 +-- tests/SelfUrlPathTest.php | 106 ++++++++++++++++++++++++++++++++++++++ utils/phpunit.sh | 6 +++ 4 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 tests/SelfUrlPathTest.php create mode 100755 utils/phpunit.sh diff --git a/.vscode/tasks.json b/.vscode/tasks.json index d59e3ff10..11fd2c305 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -6,22 +6,30 @@ "label": "phpstan (watcher)", "isBackground": true, "problemMatcher": { - "fileLocation": ["relative", "${workspaceRoot}"], + "fileLocation": [ + "relative", + "${workspaceRoot}" + ], "owner": "phpstan-watcher", "pattern": { - "regexp": "^/app/(.*?):([0-9\\?]*):(.*)$", - "file": 1, - "line": 2, - "message": 3 + "regexp": "^/app/(.*?):([0-9\\?]*):(.*)$", + "file": 1, + "line": 2, + "message": 3 }, "background": { "activeOnStart": true, "beginsPattern": "Using configuration file", "endsPattern": "All done" - } - }, - "command": "chmod +x ${workspaceRoot}/utils/phpstan-watcher.sh && ${workspaceRoot}/utils/phpstan-watcher.sh", + }, + "command": "chmod +x ${workspaceRoot}/utils/phpstan-watcher.sh && ${workspaceRoot}/utils/phpstan-watcher.sh" + }, + { + "type": "shell", + "label": "phpunit", + "command": "chmod +x ${workspaceRoot}/utils/phpunit.sh && ${workspaceRoot}/utils/phpunit.sh", + "problemMatcher": [] }, { "type": "gulp", @@ -30,9 +38,9 @@ "label": "gulp: default", "options": { "env": { - "PATH": "${env:PATH}:/usr/lib/sdk/node16/bin/" + "PATH": "${env:PATH}:/usr/lib/sdk/node16/bin/" } - } + } } ] } diff --git a/classes/config.php b/classes/config.php index 17c555fa9..72d6c5106 100644 --- a/classes/config.php +++ b/classes/config.php @@ -479,14 +479,14 @@ class Config { /** returns fully-qualified external URL to tt-rss (no trailing slash) * SELF_URL_PATH configuration variable is used as a fallback for the CLI SAPI * */ - static function get_self_url() : string { - if (php_sapi_name() == "cli") { + static function get_self_url(bool $always_detect = false) : string { + if (!$always_detect && php_sapi_name() == "cli") { return self::get(Config::SELF_URL_PATH); } else { $proto = self::is_server_https() ? 'https' : 'http'; $self_url_path = $proto . '://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH); - $self_url_path = preg_replace("/\w+\.php(\?.*$)?$/", "", $self_url_path); + $self_url_path = preg_replace("/(\/api\/{1,})?(\w+\.php)?(\?.*$)?$/", "", $self_url_path); if (substr($self_url_path, -1) === "/") { return substr($self_url_path, 0, -1); diff --git a/tests/SelfUrlPathTest.php b/tests/SelfUrlPathTest.php new file mode 100644 index 000000000..b8892f84b --- /dev/null +++ b/tests/SelfUrlPathTest.php @@ -0,0 +1,106 @@ +assertEquals( + 'http://example.com/tt-rss', + Config::get_self_url(true) + ); + + } + + public function test_b(): void { + $_SERVER = []; + + $_SERVER["HTTP_X_FORWARDED_PROTO"] = "https"; + $_SERVER["HTTP_HOST"] = "example.com"; + $_SERVER["REQUEST_URI"] = "/api/"; + + $this->assertEquals( + 'https://example.com', + Config::get_self_url(true) + ); + } + + public function test_c(): void { + $_SERVER = []; + + $_SERVER["HTTP_X_FORWARDED_PROTO"] = "https"; + $_SERVER["HTTP_HOST"] = "example.com"; + $_SERVER["REQUEST_URI"] = "/api/index.php"; + + $this->assertEquals( + 'https://example.com', + Config::get_self_url(true) + ); + } + + public function test_d(): void { + $_SERVER = []; + + $_SERVER["HTTP_X_FORWARDED_PROTO"] = "https"; + $_SERVER["HTTP_HOST"] = "example.com"; + $_SERVER["REQUEST_URI"] = "/api//"; + + $this->assertEquals( + 'https://example.com', + Config::get_self_url(true) + ); + } + + public function test_e(): void { + $_SERVER = []; + + $_SERVER["HTTP_X_FORWARDED_PROTO"] = "https"; + $_SERVER["HTTP_HOST"] = "example.com"; + $_SERVER["REQUEST_URI"] = "/"; + + $this->assertEquals( + 'https://example.com', + Config::get_self_url(true) + ); + } + + public function test_f(): void { + $_SERVER = []; + + $_SERVER["HTTP_HOST"] = "example.com"; + $_SERVER["REQUEST_URI"] = "/tt-rss/index.php"; + + $this->assertEquals( + 'http://example.com/tt-rss', + Config::get_self_url(true) + ); + } + + public function test_g(): void { + $_SERVER = []; + + $_SERVER["HTTP_HOST"] = "example.com"; + $_SERVER["REQUEST_URI"] = "/tt-rss/"; + + $this->assertEquals( + 'http://example.com/tt-rss', + Config::get_self_url(true) + ); + } + + public function test_h(): void { + $_SERVER = []; + + $_SERVER["HTTP_HOST"] = "example.com"; + $_SERVER["REQUEST_URI"] = "/tt-rss"; + + $this->assertEquals( + 'http://example.com/tt-rss', + Config::get_self_url(true) + ); + } +} diff --git a/utils/phpunit.sh b/utils/phpunit.sh new file mode 100755 index 000000000..2aa166283 --- /dev/null +++ b/utils/phpunit.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +docker run --rm -v $(pwd):/app \ + --workdir /app registry.fakecake.org/ci/php8.2-alpine:3.18 php82 -d memory_limit=-1 ./vendor/bin/phpunit + +