frankenphp_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. package frankenphp_test
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "log"
  7. "net/http"
  8. "net/http/cookiejar"
  9. "net/http/httptest"
  10. "net/http/httptrace"
  11. "net/textproto"
  12. "net/url"
  13. "os"
  14. "strconv"
  15. "strings"
  16. "sync"
  17. "testing"
  18. "github.com/dunglas/frankenphp"
  19. "github.com/stretchr/testify/assert"
  20. "github.com/stretchr/testify/require"
  21. "go.uber.org/zap"
  22. "go.uber.org/zap/zaptest"
  23. "go.uber.org/zap/zaptest/observer"
  24. )
  25. type testOptions struct {
  26. workerScript string
  27. nbWorkers int
  28. nbParrallelRequests int
  29. realServer bool
  30. logger *zap.Logger
  31. initOpts []frankenphp.Option
  32. }
  33. func runTest(t *testing.T, test func(func(http.ResponseWriter, *http.Request), *httptest.Server, int), opts *testOptions) {
  34. if opts == nil {
  35. opts = &testOptions{}
  36. }
  37. if opts.nbParrallelRequests == 0 {
  38. opts.nbParrallelRequests = 100
  39. }
  40. cwd, _ := os.Getwd()
  41. testDataDir := cwd + "/testdata/"
  42. if opts.logger == nil {
  43. opts.logger = zaptest.NewLogger(t)
  44. }
  45. initOpts := []frankenphp.Option{frankenphp.WithLogger(opts.logger)}
  46. if opts.workerScript != "" {
  47. initOpts = append(initOpts, frankenphp.WithWorkers(testDataDir+opts.workerScript, opts.nbWorkers))
  48. }
  49. initOpts = append(initOpts, opts.initOpts...)
  50. err := frankenphp.Init(initOpts...)
  51. require.Nil(t, err)
  52. defer frankenphp.Shutdown()
  53. handler := func(w http.ResponseWriter, r *http.Request) {
  54. req := frankenphp.NewRequestWithContext(r, testDataDir, nil)
  55. if err := frankenphp.ServeHTTP(w, req); err != nil {
  56. panic(err)
  57. }
  58. }
  59. var ts *httptest.Server
  60. if opts.realServer {
  61. ts = httptest.NewServer(http.HandlerFunc(handler))
  62. defer ts.Close()
  63. }
  64. var wg sync.WaitGroup
  65. wg.Add(opts.nbParrallelRequests)
  66. for i := 0; i < opts.nbParrallelRequests; i++ {
  67. go func(i int) {
  68. test(handler, ts, i)
  69. wg.Done()
  70. }(i)
  71. }
  72. wg.Wait()
  73. }
  74. func BenchmarkHelloWorld(b *testing.B) {
  75. if err := frankenphp.Init(frankenphp.WithLogger(zap.NewNop())); err != nil {
  76. panic(err)
  77. }
  78. defer frankenphp.Shutdown()
  79. cwd, _ := os.Getwd()
  80. testDataDir := cwd + "/testdata/"
  81. handler := func(w http.ResponseWriter, r *http.Request) {
  82. req := frankenphp.NewRequestWithContext(r, testDataDir, nil)
  83. if err := frankenphp.ServeHTTP(w, req); err != nil {
  84. panic(err)
  85. }
  86. }
  87. req := httptest.NewRequest("GET", "http://example.com/index.php", nil)
  88. w := httptest.NewRecorder()
  89. for i := 0; i < b.N; i++ {
  90. handler(w, req)
  91. }
  92. }
  93. func TestHelloWorld_module(t *testing.T) { testHelloWorld(t, nil) }
  94. func TestHelloWorld_worker(t *testing.T) {
  95. testHelloWorld(t, &testOptions{workerScript: "index.php"})
  96. }
  97. func testHelloWorld(t *testing.T, opts *testOptions) {
  98. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  99. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/index.php?i=%d", i), nil)
  100. w := httptest.NewRecorder()
  101. handler(w, req)
  102. resp := w.Result()
  103. body, _ := io.ReadAll(resp.Body)
  104. assert.Equal(t, fmt.Sprintf("I am by birth a Genevese (%d)", i), string(body))
  105. }, opts)
  106. }
  107. func TestFinishRequest_module(t *testing.T) { testFinishRequest(t, nil) }
  108. func TestFinishRequest_worker(t *testing.T) {
  109. testFinishRequest(t, &testOptions{workerScript: "finish-request.php"})
  110. }
  111. func testFinishRequest(t *testing.T, opts *testOptions) {
  112. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  113. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/finish-request.php?i=%d", i), nil)
  114. w := httptest.NewRecorder()
  115. handler(w, req)
  116. resp := w.Result()
  117. body, _ := io.ReadAll(resp.Body)
  118. assert.Equal(t, fmt.Sprintf("This is output %d\n", i), string(body))
  119. }, opts)
  120. }
  121. func TestServerVariable_module(t *testing.T) { testServerVariable(t, nil) }
  122. func TestServerVariable_worker(t *testing.T) {
  123. testServerVariable(t, &testOptions{workerScript: "server-variable.php"})
  124. }
  125. func testServerVariable(t *testing.T, opts *testOptions) {
  126. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  127. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/server-variable.php/baz/bat?foo=a&bar=b&i=%d#hash", i), nil)
  128. req.SetBasicAuth("kevin", "password")
  129. w := httptest.NewRecorder()
  130. handler(w, req)
  131. resp := w.Result()
  132. body, _ := io.ReadAll(resp.Body)
  133. strBody := string(body)
  134. assert.Contains(t, strBody, "[REMOTE_HOST]")
  135. assert.Contains(t, strBody, "[REMOTE_USER] => kevin")
  136. assert.Contains(t, strBody, "[PHP_AUTH_USER] => kevin")
  137. assert.Contains(t, strBody, "[PHP_AUTH_PW] => password")
  138. assert.Contains(t, strBody, "[HTTP_AUTHORIZATION] => Basic a2V2aW46cGFzc3dvcmQ=")
  139. assert.Contains(t, strBody, "[DOCUMENT_ROOT]")
  140. assert.Contains(t, strBody, "[PHP_SELF] => /server-variable.php/baz/bat")
  141. assert.Contains(t, strBody, "[CONTENT_TYPE]")
  142. assert.Contains(t, strBody, fmt.Sprintf("[QUERY_STRING] => foo=a&bar=b&i=%d#hash", i))
  143. assert.Contains(t, strBody, fmt.Sprintf("[REQUEST_URI] => /server-variable.php/baz/bat?foo=a&bar=b&i=%d#hash", i))
  144. assert.Contains(t, strBody, "[CONTENT_LENGTH]")
  145. assert.Contains(t, strBody, "[REMOTE_ADDR]")
  146. assert.Contains(t, strBody, "[REMOTE_PORT]")
  147. assert.Contains(t, strBody, "[REQUEST_SCHEME] => http")
  148. assert.Contains(t, strBody, "[DOCUMENT_URI]")
  149. assert.Contains(t, strBody, "[AUTH_TYPE]")
  150. assert.Contains(t, strBody, "[REMOTE_IDENT]")
  151. assert.Contains(t, strBody, "[REQUEST_METHOD] => GET")
  152. assert.Contains(t, strBody, "[SERVER_NAME] => example.com")
  153. assert.Contains(t, strBody, "[SERVER_PROTOCOL] => HTTP/1.1")
  154. assert.Contains(t, strBody, "[SCRIPT_FILENAME]")
  155. assert.Contains(t, strBody, "[SERVER_SOFTWARE] => FrankenPHP")
  156. assert.Contains(t, strBody, "[REQUEST_TIME_FLOAT]")
  157. assert.Contains(t, strBody, "[REQUEST_TIME]")
  158. assert.Contains(t, strBody, "[REQUEST_TIME]")
  159. }, opts)
  160. }
  161. func TestPathInfo_module(t *testing.T) { testPathInfo(t, nil) }
  162. func TestPathInfo_worker(t *testing.T) {
  163. testPathInfo(t, &testOptions{workerScript: "server-variable.php"})
  164. }
  165. func testPathInfo(t *testing.T, opts *testOptions) {
  166. runTest(t, func(_ func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  167. handler := func(w http.ResponseWriter, r *http.Request) {
  168. cwd, _ := os.Getwd()
  169. testDataDir := cwd + "/testdata/"
  170. requestURI := r.URL.RequestURI()
  171. rewriteRequest := frankenphp.NewRequestWithContext(r, testDataDir, nil)
  172. rewriteRequest.URL.Path = "/server-variable.php/pathinfo"
  173. fc, _ := frankenphp.FromContext(rewriteRequest.Context())
  174. fc.Env["REQUEST_URI"] = requestURI
  175. err := frankenphp.ServeHTTP(w, rewriteRequest)
  176. assert.NoError(t, err)
  177. }
  178. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/pathinfo/%d", i), nil)
  179. w := httptest.NewRecorder()
  180. handler(w, req)
  181. resp := w.Result()
  182. body, _ := io.ReadAll(resp.Body)
  183. strBody := string(body)
  184. assert.Contains(t, strBody, "[PATH_INFO] => /pathinfo")
  185. assert.Contains(t, strBody, fmt.Sprintf("[REQUEST_URI] => /pathinfo/%d", i))
  186. assert.Contains(t, strBody, "[PATH_TRANSLATED] =>")
  187. assert.Contains(t, strBody, "[SCRIPT_NAME] => /server-variable.php")
  188. }, opts)
  189. }
  190. func TestHeaders_module(t *testing.T) { testHeaders(t, nil) }
  191. func TestHeaders_worker(t *testing.T) { testHeaders(t, &testOptions{workerScript: "headers.php"}) }
  192. func testHeaders(t *testing.T, opts *testOptions) {
  193. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  194. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/headers.php?i=%d", i), nil)
  195. w := httptest.NewRecorder()
  196. handler(w, req)
  197. resp := w.Result()
  198. body, _ := io.ReadAll(resp.Body)
  199. assert.Equal(t, "Hello", string(body))
  200. assert.Equal(t, 201, resp.StatusCode)
  201. assert.Equal(t, "bar", resp.Header.Get("Foo"))
  202. assert.Equal(t, "bar2", resp.Header.Get("Foo2"))
  203. assert.Equal(t, fmt.Sprintf("%d", i), resp.Header.Get("I"))
  204. }, opts)
  205. }
  206. func TestInput_module(t *testing.T) { testInput(t, nil) }
  207. func TestInput_worker(t *testing.T) { testInput(t, &testOptions{workerScript: "input.php"}) }
  208. func testInput(t *testing.T, opts *testOptions) {
  209. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  210. req := httptest.NewRequest("POST", "http://example.com/input.php", strings.NewReader(fmt.Sprintf("post data %d", i)))
  211. w := httptest.NewRecorder()
  212. handler(w, req)
  213. resp := w.Result()
  214. body, _ := io.ReadAll(resp.Body)
  215. assert.Equal(t, fmt.Sprintf("post data %d", i), string(body))
  216. assert.Equal(t, "bar", resp.Header.Get("Foo"))
  217. }, opts)
  218. }
  219. func TestPostSuperGlobals_module(t *testing.T) { testPostSuperGlobals(t, nil) }
  220. func TestPostSuperGlobals_worker(t *testing.T) {
  221. testPostSuperGlobals(t, &testOptions{workerScript: "super-globals.php"})
  222. }
  223. func testPostSuperGlobals(t *testing.T, opts *testOptions) {
  224. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  225. formData := url.Values{"baz": {"bat"}, "i": {fmt.Sprintf("%d", i)}}
  226. req := httptest.NewRequest("POST", fmt.Sprintf("http://example.com/super-globals.php?foo=bar&iG=%d", i), strings.NewReader(formData.Encode()))
  227. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  228. w := httptest.NewRecorder()
  229. handler(w, req)
  230. resp := w.Result()
  231. body, _ := io.ReadAll(resp.Body)
  232. assert.Contains(t, string(body), "'foo' => 'bar'")
  233. assert.Contains(t, string(body), fmt.Sprintf("'i' => '%d'", i))
  234. assert.Contains(t, string(body), "'baz' => 'bat'")
  235. assert.Contains(t, string(body), fmt.Sprintf("'iG' => '%d'", i))
  236. }, opts)
  237. }
  238. func TestCookies_module(t *testing.T) { testCookies(t, nil) }
  239. func TestCookies_worker(t *testing.T) { testCookies(t, &testOptions{workerScript: "cookies.php"}) }
  240. func testCookies(t *testing.T, opts *testOptions) {
  241. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  242. req := httptest.NewRequest("GET", "http://example.com/cookies.php", nil)
  243. req.AddCookie(&http.Cookie{Name: "foo", Value: "bar"})
  244. req.AddCookie(&http.Cookie{Name: "i", Value: fmt.Sprintf("%d", i)})
  245. w := httptest.NewRecorder()
  246. handler(w, req)
  247. resp := w.Result()
  248. body, _ := io.ReadAll(resp.Body)
  249. assert.Contains(t, string(body), "'foo' => 'bar'")
  250. assert.Contains(t, string(body), fmt.Sprintf("'i' => '%d'", i))
  251. }, opts)
  252. }
  253. func TestSession_module(t *testing.T) { testSession(t, nil) }
  254. func TestSession_worker(t *testing.T) {
  255. testSession(t, &testOptions{workerScript: "session.php"})
  256. }
  257. func testSession(t *testing.T, opts *testOptions) {
  258. if opts == nil {
  259. opts = &testOptions{}
  260. }
  261. opts.realServer = true
  262. runTest(t, func(_ func(http.ResponseWriter, *http.Request), ts *httptest.Server, i int) {
  263. jar, err := cookiejar.New(&cookiejar.Options{})
  264. if err != nil {
  265. panic(err)
  266. }
  267. client := &http.Client{Jar: jar}
  268. resp1, err := client.Get(ts.URL + "/session.php")
  269. if err != nil {
  270. panic(err)
  271. }
  272. body1, _ := io.ReadAll(resp1.Body)
  273. assert.Equal(t, "Count: 0\n", string(body1))
  274. resp2, err := client.Get(ts.URL + "/session.php")
  275. if err != nil {
  276. panic(err)
  277. }
  278. body2, _ := io.ReadAll(resp2.Body)
  279. assert.Equal(t, "Count: 1\n", string(body2))
  280. }, opts)
  281. }
  282. func TestPhpInfo_module(t *testing.T) { testPhpInfo(t, nil) }
  283. func TestPhpInfo_worker(t *testing.T) { testPhpInfo(t, &testOptions{workerScript: "phpinfo.php"}) }
  284. func testPhpInfo(t *testing.T, opts *testOptions) {
  285. var logOnce sync.Once
  286. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  287. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/phpinfo.php?i=%d", i), nil)
  288. w := httptest.NewRecorder()
  289. handler(w, req)
  290. resp := w.Result()
  291. body, _ := io.ReadAll(resp.Body)
  292. logOnce.Do(func() {
  293. t.Log(string(body))
  294. })
  295. assert.Contains(t, string(body), "frankenphp")
  296. assert.Contains(t, string(body), fmt.Sprintf("i=%d", i))
  297. }, opts)
  298. }
  299. func TestPersistentObject_module(t *testing.T) { testPersistentObject(t, nil) }
  300. func TestPersistentObject_worker(t *testing.T) {
  301. testPersistentObject(t, &testOptions{workerScript: "persistent-object.php"})
  302. }
  303. func testPersistentObject(t *testing.T, opts *testOptions) {
  304. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  305. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/persistent-object.php?i=%d", i), nil)
  306. w := httptest.NewRecorder()
  307. handler(w, req)
  308. resp := w.Result()
  309. body, _ := io.ReadAll(resp.Body)
  310. assert.Equal(t, fmt.Sprintf(`request: %d
  311. class exists: 1
  312. id: obj1
  313. object id: 1`, i), string(body))
  314. }, opts)
  315. }
  316. func TestAutoloader_module(t *testing.T) { testAutoloader(t, nil) }
  317. func TestAutoloader_worker(t *testing.T) {
  318. testAutoloader(t, &testOptions{workerScript: "autoloader.php"})
  319. }
  320. func testAutoloader(t *testing.T, opts *testOptions) {
  321. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  322. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/autoloader.php?i=%d", i), nil)
  323. w := httptest.NewRecorder()
  324. handler(w, req)
  325. resp := w.Result()
  326. body, _ := io.ReadAll(resp.Body)
  327. assert.Equal(t, fmt.Sprintf(`request %d
  328. my_autoloader`, i), string(body))
  329. }, opts)
  330. }
  331. func TestLog_module(t *testing.T) { testLog(t, &testOptions{}) }
  332. func TestLog_worker(t *testing.T) {
  333. testLog(t, &testOptions{workerScript: "log.php"})
  334. }
  335. func testLog(t *testing.T, opts *testOptions) {
  336. logger, logs := observer.New(zap.InfoLevel)
  337. opts.logger = zap.New(logger)
  338. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  339. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/log.php?i=%d", i), nil)
  340. w := httptest.NewRecorder()
  341. handler(w, req)
  342. for logs.FilterMessage(fmt.Sprintf("request %d", i)).Len() <= 0 {
  343. }
  344. }, opts)
  345. }
  346. func TestConnectionAbort_module(t *testing.T) { testConnectionAbort(t, &testOptions{}) }
  347. func TestConnectionAbort_worker(t *testing.T) {
  348. testConnectionAbort(t, &testOptions{workerScript: "connectionStatusLog.php"})
  349. }
  350. func testConnectionAbort(t *testing.T, opts *testOptions) {
  351. testFinish := func(finish string) {
  352. t.Run(fmt.Sprintf("finish=%s", finish), func(t *testing.T) {
  353. logger, logs := observer.New(zap.InfoLevel)
  354. opts.logger = zap.New(logger)
  355. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  356. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/connectionStatusLog.php?i=%d&finish=%s", i, finish), nil)
  357. w := httptest.NewRecorder()
  358. ctx, cancel := context.WithCancel(req.Context())
  359. req = req.WithContext(ctx)
  360. cancel()
  361. handler(w, req)
  362. for logs.FilterMessage(fmt.Sprintf("request %d: 1", i)).Len() <= 0 {
  363. }
  364. }, opts)
  365. })
  366. }
  367. testFinish("0")
  368. testFinish("1")
  369. }
  370. func TestException_module(t *testing.T) { testException(t, &testOptions{}) }
  371. func TestException_worker(t *testing.T) {
  372. testException(t, &testOptions{workerScript: "exception.php"})
  373. }
  374. func testException(t *testing.T, opts *testOptions) {
  375. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  376. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/exception.php?i=%d", i), nil)
  377. w := httptest.NewRecorder()
  378. handler(w, req)
  379. resp := w.Result()
  380. body, _ := io.ReadAll(resp.Body)
  381. assert.Contains(t, string(body), "hello")
  382. assert.Contains(t, string(body), fmt.Sprintf(`Uncaught Exception: request %d`, i))
  383. }, opts)
  384. }
  385. func TestEarlyHints_module(t *testing.T) { testEarlyHints(t, &testOptions{}) }
  386. func TestEarlyHints_worker(t *testing.T) {
  387. testEarlyHints(t, &testOptions{workerScript: "early-hints.php"})
  388. }
  389. func testEarlyHints(t *testing.T, opts *testOptions) {
  390. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  391. var earlyHintReceived bool
  392. trace := &httptrace.ClientTrace{
  393. Got1xxResponse: func(code int, header textproto.MIMEHeader) error {
  394. switch code {
  395. case http.StatusEarlyHints:
  396. assert.Equal(t, "</style.css>; rel=preload; as=style", header.Get("Link"))
  397. assert.Equal(t, strconv.Itoa(i), header.Get("Request"))
  398. earlyHintReceived = true
  399. }
  400. return nil
  401. },
  402. }
  403. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/early-hints.php?i=%d", i), nil)
  404. w := NewRecorder()
  405. w.ClientTrace = trace
  406. handler(w, req)
  407. assert.Equal(t, strconv.Itoa(i), w.Header().Get("Request"))
  408. assert.Equal(t, "", w.Header().Get("Link"))
  409. assert.True(t, earlyHintReceived)
  410. }, opts)
  411. }
  412. type streamResponseRecorder struct {
  413. *httptest.ResponseRecorder
  414. writeCallback func(buf []byte)
  415. }
  416. func (srr *streamResponseRecorder) Write(buf []byte) (int, error) {
  417. srr.writeCallback(buf)
  418. return srr.ResponseRecorder.Write(buf)
  419. }
  420. func TestFlush_module(t *testing.T) { testFlush(t, &testOptions{}) }
  421. func TestFlush_worker(t *testing.T) {
  422. testFlush(t, &testOptions{workerScript: "flush.php"})
  423. }
  424. func testFlush(t *testing.T, opts *testOptions) {
  425. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  426. var j int
  427. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/flush.php?i=%d", i), nil)
  428. w := &streamResponseRecorder{httptest.NewRecorder(), func(buf []byte) {
  429. if j == 0 {
  430. assert.Equal(t, []byte("He"), buf)
  431. } else {
  432. assert.Equal(t, []byte(fmt.Sprintf("llo %d", i)), buf)
  433. }
  434. j++
  435. }}
  436. handler(w, req)
  437. assert.Equal(t, 2, j)
  438. }, opts)
  439. }
  440. func TestTimeout_module(t *testing.T) {
  441. testTimeout(t, &testOptions{})
  442. }
  443. func TestTimeout_worker(t *testing.T) {
  444. testTimeout(t, &testOptions{workerScript: "timeout.php"})
  445. }
  446. func testTimeout(t *testing.T, opts *testOptions) {
  447. config := frankenphp.Config()
  448. if !config.ZendMaxExecutionTimers {
  449. t.Skip("Zend Timer is not enabled")
  450. }
  451. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  452. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/timeout.php?i=%d", i), nil)
  453. w := httptest.NewRecorder()
  454. handler(w, req)
  455. resp := w.Result()
  456. body, _ := io.ReadAll(resp.Body)
  457. assert.Contains(t, string(body), fmt.Sprintf("request: %d\n<br />\n<b>Fatal error</b>: Maximum execution time of 1 second exceeded in", i))
  458. }, opts)
  459. }
  460. func TestVersion(t *testing.T) {
  461. v := frankenphp.Version()
  462. assert.GreaterOrEqual(t, v.MajorVersion, 8)
  463. assert.GreaterOrEqual(t, v.MinorVersion, 0)
  464. assert.GreaterOrEqual(t, v.ReleaseVersion, 0)
  465. assert.GreaterOrEqual(t, v.VersionID, 0)
  466. assert.NotEmpty(t, v.Version, 0)
  467. }
  468. func ExampleServeHTTP() {
  469. if err := frankenphp.Init(); err != nil {
  470. panic(err)
  471. }
  472. defer frankenphp.Shutdown()
  473. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  474. req := frankenphp.NewRequestWithContext(r, "/path/to/document/root", nil)
  475. if err := frankenphp.ServeHTTP(w, req); err != nil {
  476. panic(err)
  477. }
  478. })
  479. log.Fatal(http.ListenAndServe(":8080", nil))
  480. }