caddy_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/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/env.php", http.StatusOK, "bazbar")
  121. }
  122. func TestPHPServerDirective(t *testing.T) {
  123. tester := caddytest.NewTester(t)
  124. tester.InitServer(`
  125. {
  126. skip_install_trust
  127. admin localhost:2999
  128. http_port 9080
  129. https_port 9443
  130. frankenphp
  131. order php_server before reverse_proxy
  132. }
  133. localhost:9080 {
  134. root * ../testdata
  135. php_server
  136. }
  137. `, "caddyfile")
  138. tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "I am by birth a Genevese (i not set)")
  139. tester.AssertGetResponse("http://localhost:9080/hello.txt", http.StatusOK, "Hello")
  140. tester.AssertGetResponse("http://localhost:9080/not-found.txt", http.StatusOK, "I am by birth a Genevese (i not set)")
  141. }
  142. func TestPHPServerDirectiveDisableFileServer(t *testing.T) {
  143. tester := caddytest.NewTester(t)
  144. tester.InitServer(`
  145. {
  146. skip_install_trust
  147. admin localhost:2999
  148. http_port 9080
  149. https_port 9443
  150. frankenphp
  151. order php_server before respond
  152. }
  153. localhost:9080 {
  154. root * ../testdata
  155. php_server {
  156. file_server off
  157. }
  158. respond "Not found" 404
  159. }
  160. `, "caddyfile")
  161. tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "I am by birth a Genevese (i not set)")
  162. tester.AssertGetResponse("http://localhost:9080/hello.txt", http.StatusNotFound, "Not found")
  163. }