worker_test.go 4.6 KB

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