123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- package caddy_test
- import (
- "bytes"
- "fmt"
- "net/http"
- "strings"
- "sync"
- "testing"
- "github.com/caddyserver/caddy/v2/caddytest"
- )
- func TestPHP(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
- }
- 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 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)",
- )
- }
- 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/worker-env.php
- num 1
- env FOO bar
- }
- }
- }
- localhost:9080 {
- route {
- php {
- root ../testdata
- env FOO baz
- }
- }
- }
- `, "caddyfile")
- tester.AssertGetResponse("http://localhost:9080/worker-env.php", http.StatusOK, "bazbar")
- }
- func TestJsonEnv(t *testing.T) {
- tester := caddytest.NewTester(t)
- tester.InitServer(`
- {
- "admin": {
- "listen": "localhost:2999"
- },
- "apps": {
- "frankenphp": {
- "workers": [
- {
- "env": {
- "FOO": "bar"
- },
- "file_name": "../testdata/worker-env.php",
- "num": 1
- }
- ]
- },
- "http": {
- "http_port": 9080,
- "https_port": 9443,
- "servers": {
- "srv0": {
- "listen": [
- ":9080"
- ],
- "routes": [
- {
- "handle": [
- {
- "handler": "subroute",
- "routes": [
- {
- "handle": [
- {
- "handler": "subroute",
- "routes": [
- {
- "handle": [
- {
- "env": {
- "FOO": "baz"
- },
- "handler": "php",
- "root": "../testdata"
- }
- ]
- }
- ]
- }
- ]
- }
- ]
- }
- ],
- "match": [
- {
- "host": [
- "localhost"
- ]
- }
- ],
- "terminal": true
- }
- ]
- }
- }
- },
- "pki": {
- "certificate_authorities": {
- "local": {
- "install_trust": false
- }
- }
- }
- }
- }
- `, "json")
- tester.AssertGetResponse("http://localhost:9080/worker-env.php", http.StatusOK, "bazbar")
- }
- 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")
- }
|