2021-10-31 23:18:30 +00:00
|
|
|
package caddy_test
|
|
|
|
|
|
|
|
import (
|
2023-08-16 15:52:29 +00:00
|
|
|
"bytes"
|
2022-06-03 15:18:07 +00:00
|
|
|
"fmt"
|
2021-10-31 23:18:30 +00:00
|
|
|
"net/http"
|
2023-08-16 15:52:29 +00:00
|
|
|
"strings"
|
2022-06-03 15:18:07 +00:00
|
|
|
"sync"
|
2021-10-31 23:18:30 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2/caddytest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPHP(t *testing.T) {
|
2022-06-03 15:18:07 +00:00
|
|
|
var wg sync.WaitGroup
|
2021-10-31 23:18:30 +00:00
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`
|
2022-06-03 15:18:07 +00:00
|
|
|
{
|
2023-08-04 15:38:28 +00:00
|
|
|
skip_install_trust
|
2022-10-04 12:36:03 +00:00
|
|
|
admin localhost:2999
|
|
|
|
http_port 9080
|
|
|
|
https_port 9443
|
2023-08-04 15:38:28 +00:00
|
|
|
|
|
|
|
frankenphp
|
2022-06-03 15:18:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 12:36:03 +00:00
|
|
|
localhost:9080 {
|
2023-08-04 15:38:28 +00:00
|
|
|
route {
|
|
|
|
php {
|
|
|
|
root ../testdata
|
|
|
|
}
|
|
|
|
}
|
2021-10-31 23:18:30 +00:00
|
|
|
}
|
2022-06-03 15:18:07 +00:00
|
|
|
`, "caddyfile")
|
2021-10-31 23:18:30 +00:00
|
|
|
|
2022-06-03 15:18:07 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
2023-08-04 15:38:28 +00:00
|
|
|
wg.Add(1)
|
|
|
|
|
2022-06-03 15:18:07 +00:00
|
|
|
go func(i int) {
|
2023-08-04 15:38:28 +00:00
|
|
|
tester.AssertGetResponse(fmt.Sprintf("http://localhost:9080/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i))
|
2022-06-03 15:18:07 +00:00
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2021-10-31 23:18:30 +00:00
|
|
|
}
|
2023-08-16 15:52:29 +00:00
|
|
|
|
|
|
|
func TestLargeRequest(t *testing.T) {
|
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`
|
|
|
|
{
|
|
|
|
skip_install_trust
|
|
|
|
admin localhost:2999
|
|
|
|
http_port 9080
|
|
|
|
https_port 9443
|
|
|
|
|
|
|
|
frankenphp
|
|
|
|
}
|
|
|
|
|
|
|
|
localhost:9080 {
|
|
|
|
route {
|
|
|
|
php {
|
|
|
|
root ../testdata
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, "caddyfile")
|
|
|
|
|
|
|
|
tester.AssertPostResponseBody(
|
|
|
|
"http://localhost:9080/large-request.php",
|
|
|
|
[]string{},
|
|
|
|
bytes.NewBufferString(strings.Repeat("f", 1_048_576)),
|
|
|
|
http.StatusOK,
|
|
|
|
"Request body size: 1048576 (unknown)",
|
|
|
|
)
|
|
|
|
}
|
2023-09-15 10:59:43 +00:00
|
|
|
|
|
|
|
func TestWorker(t *testing.T) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`
|
|
|
|
{
|
|
|
|
skip_install_trust
|
|
|
|
admin localhost:2999
|
|
|
|
http_port 9080
|
|
|
|
https_port 9443
|
|
|
|
|
|
|
|
frankenphp {
|
|
|
|
worker ../testdata/index.php 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
localhost:9080 {
|
|
|
|
route {
|
|
|
|
php {
|
|
|
|
root ../testdata
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, "caddyfile")
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func(i int) {
|
|
|
|
tester.AssertGetResponse(fmt.Sprintf("http://localhost:9080/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i))
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnv(t *testing.T) {
|
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`
|
|
|
|
{
|
|
|
|
skip_install_trust
|
|
|
|
admin localhost:2999
|
|
|
|
http_port 9080
|
|
|
|
https_port 9443
|
|
|
|
|
|
|
|
frankenphp {
|
|
|
|
worker {
|
|
|
|
file ../testdata/env.php
|
|
|
|
num 1
|
|
|
|
env FOO bar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
localhost:9080 {
|
|
|
|
route {
|
|
|
|
php {
|
|
|
|
root ../testdata
|
|
|
|
env FOO baz
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, "caddyfile")
|
|
|
|
|
|
|
|
tester.AssertGetResponse("http://localhost:9080/env.php", http.StatusOK, "bazbar")
|
|
|
|
}
|
2023-10-03 13:44:55 +00:00
|
|
|
|
|
|
|
func TestPHPServerDirective(t *testing.T) {
|
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`
|
|
|
|
{
|
|
|
|
skip_install_trust
|
|
|
|
admin localhost:2999
|
|
|
|
http_port 9080
|
|
|
|
https_port 9443
|
|
|
|
|
|
|
|
frankenphp
|
|
|
|
}
|
|
|
|
|
|
|
|
localhost:9080 {
|
|
|
|
root * ../testdata
|
|
|
|
php_server
|
|
|
|
}
|
|
|
|
`, "caddyfile")
|
|
|
|
|
|
|
|
tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "I am by birth a Genevese (i not set)")
|
|
|
|
tester.AssertGetResponse("http://localhost:9080/hello.txt", http.StatusOK, "Hello")
|
|
|
|
tester.AssertGetResponse("http://localhost:9080/not-found.txt", http.StatusOK, "I am by birth a Genevese (i not set)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPHPServerDirectiveDisableFileServer(t *testing.T) {
|
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`
|
|
|
|
{
|
|
|
|
skip_install_trust
|
|
|
|
admin localhost:2999
|
|
|
|
http_port 9080
|
|
|
|
https_port 9443
|
|
|
|
|
|
|
|
frankenphp
|
|
|
|
order php_server before respond
|
|
|
|
}
|
|
|
|
|
|
|
|
localhost:9080 {
|
|
|
|
root * ../testdata
|
|
|
|
php_server {
|
|
|
|
file_server off
|
|
|
|
}
|
|
|
|
respond "Not found" 404
|
|
|
|
}
|
|
|
|
`, "caddyfile")
|
|
|
|
|
|
|
|
tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "I am by birth a Genevese (i not set)")
|
|
|
|
tester.AssertGetResponse("http://localhost:9080/hello.txt", http.StatusNotFound, "Not found")
|
|
|
|
}
|