caddy_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. )
  11. func TestPHP(t *testing.T) {
  12. var wg sync.WaitGroup
  13. tester := caddytest.NewTester(t)
  14. tester.InitServer(`
  15. {
  16. skip_install_trust
  17. admin localhost:2999
  18. http_port 9080
  19. https_port 9443
  20. frankenphp
  21. }
  22. localhost:9080 {
  23. route {
  24. php {
  25. root ../testdata
  26. }
  27. }
  28. }
  29. `, "caddyfile")
  30. for i := 0; i < 100; i++ {
  31. wg.Add(1)
  32. go func(i int) {
  33. tester.AssertGetResponse(fmt.Sprintf("http://localhost:9080/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i))
  34. wg.Done()
  35. }(i)
  36. }
  37. wg.Wait()
  38. }
  39. func TestLargeRequest(t *testing.T) {
  40. tester := caddytest.NewTester(t)
  41. tester.InitServer(`
  42. {
  43. skip_install_trust
  44. admin localhost:2999
  45. http_port 9080
  46. https_port 9443
  47. frankenphp
  48. }
  49. localhost:9080 {
  50. route {
  51. php {
  52. root ../testdata
  53. }
  54. }
  55. }
  56. `, "caddyfile")
  57. tester.AssertPostResponseBody(
  58. "http://localhost:9080/large-request.php",
  59. []string{},
  60. bytes.NewBufferString(strings.Repeat("f", 1_048_576)),
  61. http.StatusOK,
  62. "Request body size: 1048576 (unknown)",
  63. )
  64. }
  65. func TestWorker(t *testing.T) {
  66. var wg sync.WaitGroup
  67. tester := caddytest.NewTester(t)
  68. tester.InitServer(`
  69. {
  70. skip_install_trust
  71. admin localhost:2999
  72. http_port 9080
  73. https_port 9443
  74. frankenphp {
  75. worker ../testdata/index.php 2
  76. }
  77. }
  78. localhost:9080 {
  79. route {
  80. php {
  81. root ../testdata
  82. }
  83. }
  84. }
  85. `, "caddyfile")
  86. for i := 0; i < 100; i++ {
  87. wg.Add(1)
  88. go func(i int) {
  89. tester.AssertGetResponse(fmt.Sprintf("http://localhost:9080/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i))
  90. wg.Done()
  91. }(i)
  92. }
  93. wg.Wait()
  94. }
  95. func TestEnv(t *testing.T) {
  96. tester := caddytest.NewTester(t)
  97. tester.InitServer(`
  98. {
  99. skip_install_trust
  100. admin localhost:2999
  101. http_port 9080
  102. https_port 9443
  103. frankenphp {
  104. worker {
  105. file ../testdata/worker-env.php
  106. num 1
  107. env FOO bar
  108. }
  109. }
  110. }
  111. localhost:9080 {
  112. route {
  113. php {
  114. root ../testdata
  115. env FOO baz
  116. }
  117. }
  118. }
  119. `, "caddyfile")
  120. tester.AssertGetResponse("http://localhost:9080/worker-env.php", http.StatusOK, "bazbar")
  121. }
  122. func TestJsonEnv(t *testing.T) {
  123. tester := caddytest.NewTester(t)
  124. tester.InitServer(`
  125. {
  126. "admin": {
  127. "listen": "localhost:2999"
  128. },
  129. "apps": {
  130. "frankenphp": {
  131. "workers": [
  132. {
  133. "env": {
  134. "FOO": "bar"
  135. },
  136. "file_name": "../testdata/worker-env.php",
  137. "num": 1
  138. }
  139. ]
  140. },
  141. "http": {
  142. "http_port": 9080,
  143. "https_port": 9443,
  144. "servers": {
  145. "srv0": {
  146. "listen": [
  147. ":9080"
  148. ],
  149. "routes": [
  150. {
  151. "handle": [
  152. {
  153. "handler": "subroute",
  154. "routes": [
  155. {
  156. "handle": [
  157. {
  158. "handler": "subroute",
  159. "routes": [
  160. {
  161. "handle": [
  162. {
  163. "env": {
  164. "FOO": "baz"
  165. },
  166. "handler": "php",
  167. "root": "../testdata"
  168. }
  169. ]
  170. }
  171. ]
  172. }
  173. ]
  174. }
  175. ]
  176. }
  177. ],
  178. "match": [
  179. {
  180. "host": [
  181. "localhost"
  182. ]
  183. }
  184. ],
  185. "terminal": true
  186. }
  187. ]
  188. }
  189. }
  190. },
  191. "pki": {
  192. "certificate_authorities": {
  193. "local": {
  194. "install_trust": false
  195. }
  196. }
  197. }
  198. }
  199. }
  200. `, "json")
  201. tester.AssertGetResponse("http://localhost:9080/worker-env.php", http.StatusOK, "bazbar")
  202. }
  203. func TestPHPServerDirective(t *testing.T) {
  204. tester := caddytest.NewTester(t)
  205. tester.InitServer(`
  206. {
  207. skip_install_trust
  208. admin localhost:2999
  209. http_port 9080
  210. https_port 9443
  211. frankenphp
  212. }
  213. localhost:9080 {
  214. root * ../testdata
  215. php_server
  216. }
  217. `, "caddyfile")
  218. tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "I am by birth a Genevese (i not set)")
  219. tester.AssertGetResponse("http://localhost:9080/hello.txt", http.StatusOK, "Hello")
  220. tester.AssertGetResponse("http://localhost:9080/not-found.txt", http.StatusOK, "I am by birth a Genevese (i not set)")
  221. }
  222. func TestPHPServerDirectiveDisableFileServer(t *testing.T) {
  223. tester := caddytest.NewTester(t)
  224. tester.InitServer(`
  225. {
  226. skip_install_trust
  227. admin localhost:2999
  228. http_port 9080
  229. https_port 9443
  230. frankenphp
  231. order php_server before respond
  232. }
  233. localhost:9080 {
  234. root * ../testdata
  235. php_server {
  236. file_server off
  237. }
  238. respond "Not found" 404
  239. }
  240. `, "caddyfile")
  241. tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "I am by birth a Genevese (i not set)")
  242. tester.AssertGetResponse("http://localhost:9080/hello.txt", http.StatusNotFound, "Not found")
  243. }