worker_test.go 3.6 KB

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