*/ private function redirectMap(): array { $found = preg_match('/^map\s+"[^"]*"\s+\$luf_canonical_redirect\s*\{(.*?)^\}/ms', $this->config(), $match); $this->assertSame(1, $found, 'Die map $luf_canonical_redirect fehlt.'); return array_values(array_filter(array_map('trim', explode("\n", $match[1])))); } /** * @return list Zeilen der map, die weiterleiten (Wert 1) */ private function redirectRules(): array { return array_values(array_filter($this->redirectMap(), fn (string $line) => str_ends_with($line, '" 1;'))); } public function test_nginx_resolves_the_app_container_at_request_time(): void { $config = $this->config(); $this->assertMatchesRegularExpression('/^\s*resolver\s+127\.0\.0\.11\b[^;]*valid=\d+s[^;]*;/m', $config); $this->assertMatchesRegularExpression('/^\s*set\s+\$php_upstream\s+app:9000;/m', $config); $this->assertMatchesRegularExpression('/^\s*fastcgi_pass\s+\$php_upstream;/m', $config); $this->assertDoesNotMatchRegularExpression('/^\s*fastcgi_pass\s+app:9000;/m', $config); } public function test_nginx_keeps_the_fastcgi_and_timeout_settings(): void { $config = $this->config(); $this->assertMatchesRegularExpression('/^\s*fastcgi_read_timeout\s+3600s;/m', $config); $this->assertMatchesRegularExpression('/^\s*fastcgi_send_timeout\s+3600s;/m', $config); $this->assertMatchesRegularExpression('/^\s*client_max_body_size\s+2048m;/m', $config); $this->assertMatchesRegularExpression('/^\s*fastcgi_param\s+HTTP_X_FORWARDED_PROTO\s+\$forwarded_proto;/m', $config); $this->assertStringContainsString('try_files $uri $uri/ /index.php?$query_string;', $config); $this->assertStringContainsString('location ~ /\.(?!well-known) {', $config); } public function test_alias_hosts_are_redirected_to_the_fixed_canonical_domain(): void { $config = $this->config(); $this->assertStringContainsString('if ($luf_canonical_redirect) {', $config); $this->assertStringContainsString('return 301 https://lara-und-felix.de$request_uri;', $config, 'Fest verdrahtetes Ziel, Pfad und Query-String aus $request_uri.'); $this->assertSame(1, preg_match_all('/^\s*return\s+30\d\s/m', $config), 'Es gibt genau eine Weiterleitung.'); $this->assertDoesNotMatchRegularExpression('/return\s+30\d\s+[^;]*\$(host|http_host|http_x_forwarded_host|scheme)/', $config, 'Das Ziel darf nicht aus Anfragewerten entstehen.'); } public function test_redirect_rules_cover_both_www_aliases_and_the_http_apex_only(): void { $rules = implode("\n", $this->redirectRules()); $this->assertCount(3, $this->redirectRules()); $this->assertStringContainsString('www\.lara-und-felix\.de', $rules); $this->assertStringContainsString('www\.laraundfelix\.de', $rules); $this->assertStringContainsString('\|\s*http\s*(,[^|]*)?\|lara-und-felix\.de', $rules, 'Der Apex wird nur bei ausdrücklichem X-Forwarded-Proto http weitergeleitet.'); $this->assertStringNotContainsString('dev.', $rules, 'dev.laraundfelix.de bleibt unverändert.'); $this->assertStringNotContainsString('|laraundfelix', $rules, 'laraundfelix.de ohne www übernimmt die STRATO-Weiterleitung.'); $this->assertStringContainsString('default 0;', implode("\n", $this->redirectMap())); } public function test_only_get_and_head_are_redirected(): void { foreach ($this->redirectRules() as $rule) { $this->assertStringContainsString('"~*^(GET|HEAD)\|', $rule, 'POST (zum Beispiel der Stripe-Webhook) darf kein 301 bekommen: '.$rule); } } public function test_acme_challenge_is_excluded_before_any_redirect_rule(): void { $acme = null; $firstRedirect = null; foreach ($this->redirectMap() as $index => $line) { if ($acme === null && str_contains($line, '/\.well-known/acme-challenge/') && str_ends_with($line, '" 0;')) { $acme = $index; } if ($firstRedirect === null && str_ends_with($line, '" 1;')) { $firstRedirect = $index; } } $this->assertNotNull($acme, 'Die ACME-Ausnahme fehlt.'); $this->assertNotNull($firstRedirect); $this->assertLessThan($firstRedirect, $acme, 'nginx wertet map-Einträge der Reihe nach aus: die Ausnahme muss vorn stehen.'); } public function test_redirect_decision_uses_forwarded_headers_and_never_the_local_scheme(): void { $config = $this->config(); $this->assertStringContainsString('map "$request_method|$http_x_forwarded_proto|$luf_public_host|$uri" $luf_canonical_redirect {', $config); $this->assertStringContainsString('map $http_x_forwarded_host $luf_forwarded_host {', $config); $this->assertMatchesRegularExpression('/^\s*""\s+\$host;/m', $config, 'Ohne X-Forwarded-Host gilt der Host-Header.'); $this->assertStringNotContainsString('$scheme|', $config, 'Ohne X-Forwarded-Proto darf nie weitergeleitet werden (direkter Zugriff, Health-Check).'); } }