worker_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package frankenphp_test
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "net/http"
  7. "net/http/httptest"
  8. "net/url"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "testing"
  13. "github.com/dunglas/frankenphp"
  14. "github.com/stretchr/testify/assert"
  15. "go.uber.org/zap"
  16. "go.uber.org/zap/zapcore"
  17. "go.uber.org/zap/zaptest/observer"
  18. )
  19. func TestWorker(t *testing.T) {
  20. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  21. formData := url.Values{"baz": {"bat"}}
  22. req := httptest.NewRequest("POST", "http://example.com/worker.php?foo=bar", strings.NewReader(formData.Encode()))
  23. req.Header.Set("Content-Type", strings.Clone("application/x-www-form-urlencoded"))
  24. w := httptest.NewRecorder()
  25. handler(w, req)
  26. resp := w.Result()
  27. body, _ := io.ReadAll(resp.Body)
  28. assert.Contains(t, string(body), fmt.Sprintf("Requests handled: %d", i*2))
  29. formData2 := url.Values{"baz2": {"bat2"}}
  30. req2 := httptest.NewRequest("POST", "http://example.com/worker.php?foo2=bar2", strings.NewReader(formData2.Encode()))
  31. req2.Header.Set("Content-Type", strings.Clone("application/x-www-form-urlencoded"))
  32. w2 := httptest.NewRecorder()
  33. handler(w2, req2)
  34. resp2 := w2.Result()
  35. body2, _ := io.ReadAll(resp2.Body)
  36. assert.Contains(t, string(body2), fmt.Sprintf("Requests handled: %d", i*2+1))
  37. }, &testOptions{workerScript: "worker.php", nbWorkers: 1, nbParrallelRequests: 1})
  38. }
  39. func TestWorkerDie(t *testing.T) {
  40. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  41. req := httptest.NewRequest("GET", "http://example.com/die.php", nil)
  42. w := httptest.NewRecorder()
  43. handler(w, req)
  44. }, &testOptions{workerScript: "die.php", nbWorkers: 1, nbParrallelRequests: 10})
  45. }
  46. func TestNonWorkerModeAlwaysWorks(t *testing.T) {
  47. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  48. req := httptest.NewRequest("GET", "http://example.com/index.php", nil)
  49. w := httptest.NewRecorder()
  50. handler(w, req)
  51. resp := w.Result()
  52. body, _ := io.ReadAll(resp.Body)
  53. assert.Contains(t, string(body), "I am by birth a Genevese")
  54. }, &testOptions{workerScript: "phpinfo.php"})
  55. }
  56. func TestCannotCallHandleRequestInNonWorkerMode(t *testing.T) {
  57. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  58. req := httptest.NewRequest("GET", "http://example.com/non-worker.php", nil)
  59. w := httptest.NewRecorder()
  60. handler(w, req)
  61. resp := w.Result()
  62. body, _ := io.ReadAll(resp.Body)
  63. assert.Contains(t, string(body), "<b>Fatal error</b>: Uncaught RuntimeException: frankenphp_handle_request() called while not in worker mode")
  64. }, nil)
  65. }
  66. func TestWorkerEnv(t *testing.T) {
  67. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  68. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/worker-env.php?i=%d", i), nil)
  69. w := httptest.NewRecorder()
  70. handler(w, req)
  71. resp := w.Result()
  72. body, _ := io.ReadAll(resp.Body)
  73. assert.Equal(t, fmt.Sprintf("bar%d", i), string(body))
  74. }, &testOptions{workerScript: "worker-env.php", nbWorkers: 1, env: map[string]string{"FOO": "bar"}, nbParrallelRequests: 10})
  75. }
  76. func TestWorkerGetOpt(t *testing.T) {
  77. obs, logs := observer.New(zapcore.InfoLevel)
  78. logger := zap.New(obs)
  79. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  80. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/worker-getopt.php?i=%d", i), nil)
  81. req.Header.Add("Request", strconv.Itoa(i))
  82. w := httptest.NewRecorder()
  83. handler(w, req)
  84. resp := w.Result()
  85. body, _ := io.ReadAll(resp.Body)
  86. assert.Contains(t, string(body), fmt.Sprintf("[HTTP_REQUEST] => %d", i))
  87. assert.Contains(t, string(body), fmt.Sprintf("[REQUEST_URI] => /worker-getopt.php?i=%d", i))
  88. }, &testOptions{logger: logger, workerScript: "worker-getopt.php", env: map[string]string{"FOO": "bar"}})
  89. for _, l := range logs.FilterFieldKey("exit_status").All() {
  90. assert.Failf(t, "unexpected exit status", "exit status: %d", l.ContextMap()["exit_status"])
  91. }
  92. }
  93. func ExampleServeHTTP_workers() {
  94. if err := frankenphp.Init(
  95. frankenphp.WithWorkers("worker1.php", 4, map[string]string{"ENV1": "foo"}, []string{}),
  96. frankenphp.WithWorkers("worker2.php", 2, map[string]string{"ENV2": "bar"}, []string{}),
  97. ); err != nil {
  98. panic(err)
  99. }
  100. defer frankenphp.Shutdown()
  101. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  102. req, err := frankenphp.NewRequestWithContext(r, frankenphp.WithRequestDocumentRoot("/path/to/document/root", false))
  103. if err != nil {
  104. panic(err)
  105. }
  106. if err := frankenphp.ServeHTTP(w, req); err != nil {
  107. panic(err)
  108. }
  109. })
  110. log.Fatal(http.ListenAndServe(":8080", nil))
  111. }
  112. func TestWorkerHasOSEnvironmentVariableInSERVER(t *testing.T) {
  113. os.Setenv("CUSTOM_OS_ENV_VARIABLE", "custom_env_variable_value")
  114. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  115. req := httptest.NewRequest("GET", "http://example.com/worker.php", nil)
  116. w := httptest.NewRecorder()
  117. handler(w, req)
  118. resp := w.Result()
  119. body, _ := io.ReadAll(resp.Body)
  120. assert.Contains(t, string(body), "CUSTOM_OS_ENV_VARIABLE")
  121. assert.Contains(t, string(body), "custom_env_variable_value")
  122. }, &testOptions{workerScript: "worker.php", nbWorkers: 1, nbParrallelRequests: 1})
  123. }