caddy_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package caddy_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "sync"
  8. "testing"
  9. "github.com/caddyserver/caddy/v2/caddytest"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. func TestPHP(t *testing.T) {
  14. var wg sync.WaitGroup
  15. tester := caddytest.NewTester(t)
  16. tester.InitServer(`
  17. {
  18. skip_install_trust
  19. admin localhost:2999
  20. http_port 9080
  21. https_port 9443
  22. frankenphp
  23. }
  24. localhost:9080 {
  25. route {
  26. php {
  27. root ../testdata
  28. }
  29. }
  30. }
  31. `, "caddyfile")
  32. for i := 0; i < 100; i++ {
  33. wg.Add(1)
  34. go func(i int) {
  35. defer wg.Done()
  36. tester.AssertGetResponse(fmt.Sprintf("http://localhost:9080/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i))
  37. }(i)
  38. }
  39. wg.Wait()
  40. }
  41. func TestLargeRequest(t *testing.T) {
  42. tester := caddytest.NewTester(t)
  43. tester.InitServer(`
  44. {
  45. skip_install_trust
  46. admin localhost:2999
  47. http_port 9080
  48. https_port 9443
  49. frankenphp
  50. }
  51. localhost:9080 {
  52. route {
  53. php {
  54. root ../testdata
  55. }
  56. }
  57. }
  58. `, "caddyfile")
  59. tester.AssertPostResponseBody(
  60. "http://localhost:9080/large-request.php",
  61. []string{},
  62. bytes.NewBufferString(strings.Repeat("f", 1_048_576)),
  63. http.StatusOK,
  64. "Request body size: 1048576 (unknown)",
  65. )
  66. }
  67. func TestWorker(t *testing.T) {
  68. var wg sync.WaitGroup
  69. tester := caddytest.NewTester(t)
  70. tester.InitServer(`
  71. {
  72. skip_install_trust
  73. admin localhost:2999
  74. http_port 9080
  75. https_port 9443
  76. frankenphp {
  77. worker ../testdata/index.php 2
  78. }
  79. }
  80. localhost:9080 {
  81. route {
  82. php {
  83. root ../testdata
  84. }
  85. }
  86. }
  87. `, "caddyfile")
  88. for i := 0; i < 100; i++ {
  89. wg.Add(1)
  90. go func(i int) {
  91. defer wg.Done()
  92. tester.AssertGetResponse(fmt.Sprintf("http://localhost:9080/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i))
  93. }(i)
  94. }
  95. wg.Wait()
  96. }
  97. func TestEnv(t *testing.T) {
  98. tester := caddytest.NewTester(t)
  99. tester.InitServer(`
  100. {
  101. skip_install_trust
  102. admin localhost:2999
  103. http_port 9080
  104. https_port 9443
  105. frankenphp {
  106. worker {
  107. file ../testdata/env.php
  108. num 1
  109. env FOO bar
  110. }
  111. }
  112. }
  113. localhost:9080 {
  114. route {
  115. php {
  116. root ../testdata
  117. env FOO baz
  118. }
  119. }
  120. }
  121. `, "caddyfile")
  122. tester.AssertGetResponse("http://localhost:9080/env.php", http.StatusOK, "bazbar")
  123. }
  124. func TestPHPServerDirective(t *testing.T) {
  125. tester := caddytest.NewTester(t)
  126. tester.InitServer(`
  127. {
  128. skip_install_trust
  129. admin localhost:2999
  130. http_port 9080
  131. https_port 9443
  132. frankenphp
  133. order php_server before reverse_proxy
  134. }
  135. localhost:9080 {
  136. root * ../testdata
  137. php_server
  138. }
  139. `, "caddyfile")
  140. tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "I am by birth a Genevese (i not set)")
  141. tester.AssertGetResponse("http://localhost:9080/hello.txt", http.StatusOK, "Hello")
  142. tester.AssertGetResponse("http://localhost:9080/not-found.txt", http.StatusOK, "I am by birth a Genevese (i not set)")
  143. }
  144. func TestPHPServerDirectiveDisableFileServer(t *testing.T) {
  145. tester := caddytest.NewTester(t)
  146. tester.InitServer(`
  147. {
  148. skip_install_trust
  149. admin localhost:2999
  150. http_port 9080
  151. https_port 9443
  152. frankenphp
  153. order php_server before respond
  154. }
  155. localhost:9080 {
  156. root * ../testdata
  157. php_server {
  158. file_server off
  159. }
  160. respond "Not found" 404
  161. }
  162. `, "caddyfile")
  163. tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "I am by birth a Genevese (i not set)")
  164. tester.AssertGetResponse("http://localhost:9080/hello.txt", http.StatusNotFound, "Not found")
  165. }
  166. // TestReload sends many concurrent reload requests, as done by Laravel Octane.
  167. // Better run this test with -race.
  168. func TestReload(t *testing.T) {
  169. tester := caddytest.NewTester(t)
  170. tester.InitServer(`
  171. {
  172. skip_install_trust
  173. admin localhost:2999
  174. http_port 9080
  175. https_port 9443
  176. frankenphp {
  177. worker ../testdata/index.php
  178. }
  179. order php_server before respond
  180. }
  181. localhost:9080 {
  182. root * ../testdata
  183. php_server
  184. }
  185. `, "caddyfile")
  186. const configURL = "http://localhost:2999/config/apps/frankenphp"
  187. var wg sync.WaitGroup
  188. for i := 0; i < 20; i++ {
  189. wg.Add(1)
  190. go func() {
  191. defer wg.Done()
  192. resp1, err := tester.Client.Get(configURL)
  193. require.NoError(t, err)
  194. r, err := http.NewRequest("PATCH", configURL, resp1.Body)
  195. require.NoError(t, err)
  196. r.Header.Add("Content-Type", "application/json")
  197. r.Header.Add("Cache-Control", "must-revalidate")
  198. resp, err := tester.Client.Do(r)
  199. require.NoError(t, err)
  200. assert.Equal(t, http.StatusOK, resp.StatusCode)
  201. }()
  202. }
  203. wg.Wait()
  204. tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "I am by birth a Genevese (i not set)")
  205. }