add a workaround for make_self_url() when invoked off /api/ endpoint, add unit tests for this method

This commit is contained in:
Andrew Dolgov 2023-10-24 22:27:27 +03:00
parent de2830b241
commit 69c1c62992
No known key found for this signature in database
GPG Key ID: 1A56B4FA25D4AF2A
4 changed files with 133 additions and 13 deletions

14
.vscode/tasks.json vendored
View File

@ -6,7 +6,10 @@
"label": "phpstan (watcher)",
"isBackground": true,
"problemMatcher": {
"fileLocation": ["relative", "${workspaceRoot}"],
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"owner": "phpstan-watcher",
"pattern": {
"regexp": "^/app/(.*?):([0-9\\?]*):(.*)$",
@ -18,10 +21,15 @@
"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",

View File

@ -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);

106
tests/SelfUrlPathTest.php Normal file
View File

@ -0,0 +1,106 @@
<?php
use PHPUnit\Framework\TestCase;
final class SelfUrlPathTest extends TestCase {
public function test_a(): void {
$_SERVER = [];
$_SERVER["HTTP_X_FORWARDED_PROTO"] = "http";
$_SERVER["HTTP_HOST"] = "example.com";
$_SERVER["REQUEST_URI"] = "/tt-rss/api/index.php";
$this->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)
);
}
}

6
utils/phpunit.sh Executable file
View File

@ -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