Skip to content

Commit 3708b41

Browse files
committed
add tests (worker and non-worker separately because the global worker would activate for the non-worker case too
1 parent 1eb691d commit 3708b41

2 files changed

Lines changed: 157 additions & 0 deletions

File tree

caddy/caddy_test.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,142 @@ func TestPHPServerDirectiveDisableFileServer(t *testing.T) {
486486
tester.AssertGetResponse("http://localhost:"+testPort+"/not-found.txt", http.StatusOK, "I am by birth a Genevese (i not set)")
487487
}
488488

489+
func TestPHPServerGlobals(t *testing.T) {
490+
documentRoot, _ := filepath.Abs("../testdata")
491+
scriptFilename := documentRoot + string(filepath.Separator) + "server-globals.php"
492+
493+
tester := caddytest.NewTester(t)
494+
initServer(t, tester, `
495+
{
496+
skip_install_trust
497+
admin localhost:2999
498+
http_port `+testPort+`
499+
https_port 9443
500+
}
501+
502+
localhost:`+testPort+` {
503+
root ../testdata
504+
php_server {
505+
index server-globals.php
506+
}
507+
}
508+
`, "caddyfile")
509+
510+
// Request to /en: no matching file, falls through to server-globals.php worker
511+
// SCRIPT_NAME should be /server-globals.php, PHP_SELF should be /server-globals.php (no /en), PATH_INFO empty
512+
tester.AssertGetResponse(
513+
"http://localhost:"+testPort+"/en",
514+
http.StatusOK,
515+
"SCRIPT_NAME: /server-globals.php<br>"+
516+
"SCRIPT_FILENAME: "+scriptFilename+"<br>"+
517+
"PHP_SELF: /server-globals.php<br>"+
518+
"PATH_INFO: <br>"+
519+
"DOCUMENT_ROOT: "+documentRoot+"<br>"+
520+
"REQUEST_URI: /en<br>",
521+
)
522+
523+
// Request to /server-globals.php/en: explicit PHP file with path info
524+
// SCRIPT_NAME should be /server-globals.php, PHP_SELF should be /server-globals.php/en, PATH_INFO should be /en
525+
tester.AssertGetResponse(
526+
"http://localhost:"+testPort+"/server-globals.php/en",
527+
http.StatusOK,
528+
"SCRIPT_NAME: /server-globals.php<br>"+
529+
"SCRIPT_FILENAME: "+scriptFilename+"<br>"+
530+
"PHP_SELF: /server-globals.php/en<br>"+
531+
"PATH_INFO: /en<br>"+
532+
"DOCUMENT_ROOT: "+documentRoot+"<br>"+
533+
"REQUEST_URI: /server-globals.php/en<br>",
534+
)
535+
}
536+
537+
func TestWorkerPHPServerGlobals(t *testing.T) {
538+
documentRoot, _ := filepath.Abs("../testdata")
539+
scriptFilename := documentRoot + string(filepath.Separator) + "server-globals.php"
540+
testPortNum, _ := strconv.Atoi(testPort)
541+
testPortTwo := strconv.Itoa(testPortNum + 1)
542+
543+
tester := caddytest.NewTester(t)
544+
initServer(t, tester, `
545+
{
546+
skip_install_trust
547+
admin localhost:2999
548+
549+
frankenphp {
550+
worker {
551+
file ../testdata/server-globals.php
552+
num 1
553+
}
554+
}
555+
}
556+
557+
http://localhost:`+testPort+` {
558+
php_server {
559+
root ../testdata
560+
index server-globals.php
561+
}
562+
}
563+
564+
http://localhost:`+testPortTwo+` {
565+
php_server {
566+
root ../testdata
567+
index server-globals.php
568+
worker {
569+
file server-globals.php
570+
num 1
571+
}
572+
}
573+
}
574+
`, "caddyfile")
575+
576+
// === Site 1: global worker with php_server ===
577+
// because we don't specify a php file, PATH_INFO should be empty
578+
tester.AssertGetResponse(
579+
"http://localhost:"+testPort+"/en",
580+
http.StatusOK,
581+
"SCRIPT_NAME: /server-globals.php<br>"+
582+
"SCRIPT_FILENAME: "+scriptFilename+"<br>"+
583+
"PHP_SELF: /server-globals.php<br>"+
584+
"PATH_INFO: <br>"+
585+
"DOCUMENT_ROOT: "+documentRoot+"<br>"+
586+
"REQUEST_URI: /en<br>",
587+
)
588+
589+
tester.AssertGetResponse(
590+
"http://localhost:"+testPort+"/server-globals.php/en",
591+
http.StatusOK,
592+
"SCRIPT_NAME: /server-globals.php<br>"+
593+
"SCRIPT_FILENAME: "+scriptFilename+"<br>"+
594+
"PHP_SELF: /server-globals.php/en<br>"+
595+
"PATH_INFO: /en<br>"+
596+
"DOCUMENT_ROOT: "+documentRoot+"<br>"+
597+
"REQUEST_URI: /server-globals.php/en<br>",
598+
)
599+
600+
// === Site 2: php_server with its own worker ===
601+
// because we specify a php file, PATH_INFO should be /en
602+
tester.AssertGetResponse(
603+
"http://localhost:"+testPortTwo+"/en",
604+
http.StatusOK,
605+
"SCRIPT_NAME: /server-globals.php<br>"+
606+
"SCRIPT_FILENAME: "+scriptFilename+"<br>"+
607+
"PHP_SELF: /server-globals.php<br>"+
608+
"PATH_INFO: <br>"+
609+
"DOCUMENT_ROOT: "+documentRoot+"<br>"+
610+
"REQUEST_URI: /en<br>",
611+
)
612+
613+
tester.AssertGetResponse(
614+
"http://localhost:"+testPortTwo+"/server-globals.php/en",
615+
http.StatusOK,
616+
"SCRIPT_NAME: /server-globals.php<br>"+
617+
"SCRIPT_FILENAME: "+scriptFilename+"<br>"+
618+
"PHP_SELF: /server-globals.php/en<br>"+
619+
"PATH_INFO: /en<br>"+
620+
"DOCUMENT_ROOT: "+documentRoot+"<br>"+
621+
"REQUEST_URI: /server-globals.php/en<br>",
622+
)
623+
}
624+
489625
func TestMetrics(t *testing.T) {
490626
var wg sync.WaitGroup
491627
tester := caddytest.NewTester(t)

testdata/server-globals.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
ignore_user_abort(true);
4+
5+
$handler = static function() {
6+
echo "SCRIPT_NAME: " . ($_SERVER['SCRIPT_NAME'] ?? '(not set)') . "<br>";
7+
echo "SCRIPT_FILENAME: " . ($_SERVER['SCRIPT_FILENAME'] ?? '(not set)') . "<br>";
8+
echo "PHP_SELF: " . ($_SERVER['PHP_SELF'] ?? '(not set)') . "<br>";
9+
echo "PATH_INFO: " . ($_SERVER['PATH_INFO'] ?? '(not set)') . "<br>";
10+
echo "DOCUMENT_ROOT: " . ($_SERVER['DOCUMENT_ROOT'] ?? '(not set)') . "<br>";
11+
echo "REQUEST_URI: " . ($_SERVER['REQUEST_URI'] ?? '(not set)') . "<br>";
12+
};
13+
14+
if (isset($_SERVER['FRANKENPHP_WORKER'])) {
15+
for ($nbRequests = 0, $running = true; $running; ++$nbRequests) {
16+
$running = \frankenphp_handle_request($handler);
17+
gc_collect_cycles();
18+
}
19+
} else {
20+
$handler();
21+
}

0 commit comments

Comments
 (0)