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.Empty(t, resp.Header.Get("Invalid"))
  204. assert.Equal(t, fmt.Sprintf("%d", i), resp.Header.Get("I"))
  205. }, opts)
  206. }
  207. func TestInput_module(t *testing.T) { testInput(t, nil) }
  208. func TestInput_worker(t *testing.T) { testInput(t, &testOptions{workerScript: "input.php"}) }
  209. func testInput(t *testing.T, opts *testOptions) {
  210. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  211. req := httptest.NewRequest("POST", "http://example.com/input.php", strings.NewReader(fmt.Sprintf("post data %d", i)))
  212. w := httptest.NewRecorder()
  213. handler(w, req)
  214. resp := w.Result()
  215. body, _ := io.ReadAll(resp.Body)
  216. assert.Equal(t, fmt.Sprintf("post data %d", i), string(body))
  217. assert.Equal(t, "bar", resp.Header.Get("Foo"))
  218. }, opts)
  219. }
  220. func TestPostSuperGlobals_module(t *testing.T) { testPostSuperGlobals(t, nil) }
  221. func TestPostSuperGlobals_worker(t *testing.T) {
  222. testPostSuperGlobals(t, &testOptions{workerScript: "super-globals.php"})
  223. }
  224. func testPostSuperGlobals(t *testing.T, opts *testOptions) {
  225. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  226. formData := url.Values{"baz": {"bat"}, "i": {fmt.Sprintf("%d", i)}}
  227. req := httptest.NewRequest("POST", fmt.Sprintf("http://example.com/super-globals.php?foo=bar&iG=%d", i), strings.NewReader(formData.Encode()))
  228. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  229. w := httptest.NewRecorder()
  230. handler(w, req)
  231. resp := w.Result()
  232. body, _ := io.ReadAll(resp.Body)
  233. assert.Contains(t, string(body), "'foo' => 'bar'")
  234. assert.Contains(t, string(body), fmt.Sprintf("'i' => '%d'", i))
  235. assert.Contains(t, string(body), "'baz' => 'bat'")
  236. assert.Contains(t, string(body), fmt.Sprintf("'iG' => '%d'", i))
  237. }, opts)
  238. }
  239. func TestCookies_module(t *testing.T) { testCookies(t, nil) }
  240. func TestCookies_worker(t *testing.T) { testCookies(t, &testOptions{workerScript: "cookies.php"}) }
  241. func testCookies(t *testing.T, opts *testOptions) {
  242. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  243. req := httptest.NewRequest("GET", "http://example.com/cookies.php", nil)
  244. req.AddCookie(&http.Cookie{Name: "foo", Value: "bar"})
  245. req.AddCookie(&http.Cookie{Name: "i", Value: fmt.Sprintf("%d", i)})
  246. w := httptest.NewRecorder()
  247. handler(w, req)
  248. resp := w.Result()
  249. body, _ := io.ReadAll(resp.Body)
  250. assert.Contains(t, string(body), "'foo' => 'bar'")
  251. assert.Contains(t, string(body), fmt.Sprintf("'i' => '%d'", i))
  252. }, opts)
  253. }
  254. func TestSession_module(t *testing.T) { testSession(t, nil) }
  255. func TestSession_worker(t *testing.T) {
  256. testSession(t, &testOptions{workerScript: "session.php"})
  257. }
  258. func testSession(t *testing.T, opts *testOptions) {
  259. if opts == nil {
  260. opts = &testOptions{}
  261. }
  262. opts.realServer = true
  263. runTest(t, func(_ func(http.ResponseWriter, *http.Request), ts *httptest.Server, i int) {
  264. jar, err := cookiejar.New(&cookiejar.Options{})
  265. if err != nil {
  266. panic(err)
  267. }
  268. client := &http.Client{Jar: jar}
  269. resp1, err := client.Get(ts.URL + "/session.php")
  270. if err != nil {
  271. panic(err)
  272. }
  273. body1, _ := io.ReadAll(resp1.Body)
  274. assert.Equal(t, "Count: 0\n", string(body1))
  275. resp2, err := client.Get(ts.URL + "/session.php")
  276. if err != nil {
  277. panic(err)
  278. }
  279. body2, _ := io.ReadAll(resp2.Body)
  280. assert.Equal(t, "Count: 1\n", string(body2))
  281. }, opts)
  282. }
  283. func TestPhpInfo_module(t *testing.T) { testPhpInfo(t, nil) }
  284. func TestPhpInfo_worker(t *testing.T) { testPhpInfo(t, &testOptions{workerScript: "phpinfo.php"}) }
  285. func testPhpInfo(t *testing.T, opts *testOptions) {
  286. var logOnce sync.Once
  287. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  288. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/phpinfo.php?i=%d", i), nil)
  289. w := httptest.NewRecorder()
  290. handler(w, req)
  291. resp := w.Result()
  292. body, _ := io.ReadAll(resp.Body)
  293. logOnce.Do(func() {
  294. t.Log(string(body))
  295. })
  296. assert.Contains(t, string(body), "frankenphp")
  297. assert.Contains(t, string(body), fmt.Sprintf("i=%d", i))
  298. }, opts)
  299. }
  300. func TestPersistentObject_module(t *testing.T) { testPersistentObject(t, nil) }
  301. func TestPersistentObject_worker(t *testing.T) {
  302. testPersistentObject(t, &testOptions{workerScript: "persistent-object.php"})
  303. }
  304. func testPersistentObject(t *testing.T, opts *testOptions) {
  305. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  306. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/persistent-object.php?i=%d", i), nil)
  307. w := httptest.NewRecorder()
  308. handler(w, req)
  309. resp := w.Result()
  310. body, _ := io.ReadAll(resp.Body)
  311. assert.Equal(t, fmt.Sprintf(`request: %d
  312. class exists: 1
  313. id: obj1
  314. object id: 1`, i), string(body))
  315. }, opts)
  316. }
  317. func TestAutoloader_module(t *testing.T) { testAutoloader(t, nil) }
  318. func TestAutoloader_worker(t *testing.T) {
  319. testAutoloader(t, &testOptions{workerScript: "autoloader.php"})
  320. }
  321. func testAutoloader(t *testing.T, opts *testOptions) {
  322. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  323. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/autoloader.php?i=%d", i), nil)
  324. w := httptest.NewRecorder()
  325. handler(w, req)
  326. resp := w.Result()
  327. body, _ := io.ReadAll(resp.Body)
  328. assert.Equal(t, fmt.Sprintf(`request %d
  329. my_autoloader`, i), string(body))
  330. }, opts)
  331. }
  332. func TestLog_module(t *testing.T) { testLog(t, &testOptions{}) }
  333. func TestLog_worker(t *testing.T) {
  334. testLog(t, &testOptions{workerScript: "log.php"})
  335. }
  336. func testLog(t *testing.T, opts *testOptions) {
  337. logger, logs := observer.New(zap.InfoLevel)
  338. opts.logger = zap.New(logger)
  339. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  340. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/log.php?i=%d", i), nil)
  341. w := httptest.NewRecorder()
  342. handler(w, req)
  343. for logs.FilterMessage(fmt.Sprintf("request %d", i)).Len() <= 0 {
  344. }
  345. }, opts)
  346. }
  347. func TestConnectionAbort_module(t *testing.T) { testConnectionAbort(t, &testOptions{}) }
  348. func TestConnectionAbort_worker(t *testing.T) {
  349. testConnectionAbort(t, &testOptions{workerScript: "connectionStatusLog.php"})
  350. }
  351. func testConnectionAbort(t *testing.T, opts *testOptions) {
  352. testFinish := func(finish string) {
  353. t.Run(fmt.Sprintf("finish=%s", finish), func(t *testing.T) {
  354. logger, logs := observer.New(zap.InfoLevel)
  355. opts.logger = zap.New(logger)
  356. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  357. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/connectionStatusLog.php?i=%d&finish=%s", i, finish), nil)
  358. w := httptest.NewRecorder()
  359. ctx, cancel := context.WithCancel(req.Context())
  360. req = req.WithContext(ctx)
  361. cancel()
  362. handler(w, req)
  363. for logs.FilterMessage(fmt.Sprintf("request %d: 1", i)).Len() <= 0 {
  364. }
  365. }, opts)
  366. })
  367. }
  368. testFinish("0")
  369. testFinish("1")
  370. }
  371. func TestException_module(t *testing.T) { testException(t, &testOptions{}) }
  372. func TestException_worker(t *testing.T) {
  373. testException(t, &testOptions{workerScript: "exception.php"})
  374. }
  375. func testException(t *testing.T, opts *testOptions) {
  376. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  377. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/exception.php?i=%d", i), nil)
  378. w := httptest.NewRecorder()
  379. handler(w, req)
  380. resp := w.Result()
  381. body, _ := io.ReadAll(resp.Body)
  382. assert.Contains(t, string(body), "hello")
  383. assert.Contains(t, string(body), fmt.Sprintf(`Uncaught Exception: request %d`, i))
  384. }, opts)
  385. }
  386. func TestEarlyHints_module(t *testing.T) { testEarlyHints(t, &testOptions{}) }
  387. func TestEarlyHints_worker(t *testing.T) {
  388. testEarlyHints(t, &testOptions{workerScript: "early-hints.php"})
  389. }
  390. func testEarlyHints(t *testing.T, opts *testOptions) {
  391. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  392. var earlyHintReceived bool
  393. trace := &httptrace.ClientTrace{
  394. Got1xxResponse: func(code int, header textproto.MIMEHeader) error {
  395. switch code {
  396. case http.StatusEarlyHints:
  397. assert.Equal(t, "</style.css>; rel=preload; as=style", header.Get("Link"))
  398. assert.Equal(t, strconv.Itoa(i), header.Get("Request"))
  399. earlyHintReceived = true
  400. }
  401. return nil
  402. },
  403. }
  404. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/early-hints.php?i=%d", i), nil)
  405. w := NewRecorder()
  406. w.ClientTrace = trace
  407. handler(w, req)
  408. assert.Equal(t, strconv.Itoa(i), w.Header().Get("Request"))
  409. assert.Equal(t, "", w.Header().Get("Link"))
  410. assert.True(t, earlyHintReceived)
  411. }, opts)
  412. }
  413. type streamResponseRecorder struct {
  414. *httptest.ResponseRecorder
  415. writeCallback func(buf []byte)
  416. }
  417. func (srr *streamResponseRecorder) Write(buf []byte) (int, error) {
  418. srr.writeCallback(buf)
  419. return srr.ResponseRecorder.Write(buf)
  420. }
  421. func TestFlush_module(t *testing.T) { testFlush(t, &testOptions{}) }
  422. func TestFlush_worker(t *testing.T) {
  423. testFlush(t, &testOptions{workerScript: "flush.php"})
  424. }
  425. func testFlush(t *testing.T, opts *testOptions) {
  426. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  427. var j int
  428. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/flush.php?i=%d", i), nil)
  429. w := &streamResponseRecorder{httptest.NewRecorder(), func(buf []byte) {
  430. if j == 0 {
  431. assert.Equal(t, []byte("He"), buf)
  432. } else {
  433. assert.Equal(t, []byte(fmt.Sprintf("llo %d", i)), buf)
  434. }
  435. j++
  436. }}
  437. handler(w, req)
  438. assert.Equal(t, 2, j)
  439. }, opts)
  440. }
  441. func TestLargeRequest_module(t *testing.T) {
  442. testLargeRequest(t, &testOptions{})
  443. }
  444. func TestLargeRequest_worker(t *testing.T) {
  445. testLargeRequest(t, &testOptions{workerScript: "large-request.php"})
  446. }
  447. func testLargeRequest(t *testing.T, opts *testOptions) {
  448. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  449. req := httptest.NewRequest(
  450. "POST",
  451. fmt.Sprintf("http://example.com/large-request.php?i=%d", i),
  452. strings.NewReader(strings.Repeat("f", 1_048_576)),
  453. )
  454. w := httptest.NewRecorder()
  455. handler(w, req)
  456. resp := w.Result()
  457. body, _ := io.ReadAll(resp.Body)
  458. assert.Contains(t, string(body), fmt.Sprintf("Request body size: 1048576 (%d)", i))
  459. }, opts)
  460. }
  461. func TestVersion(t *testing.T) {
  462. v := frankenphp.Version()
  463. assert.GreaterOrEqual(t, v.MajorVersion, 8)
  464. assert.GreaterOrEqual(t, v.MinorVersion, 0)
  465. assert.GreaterOrEqual(t, v.ReleaseVersion, 0)
  466. assert.GreaterOrEqual(t, v.VersionID, 0)
  467. assert.NotEmpty(t, v.Version, 0)
  468. }
  469. func ExampleServeHTTP() {
  470. if err := frankenphp.Init(); err != nil {
  471. panic(err)
  472. }
  473. defer frankenphp.Shutdown()
  474. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  475. req := frankenphp.NewRequestWithContext(r, "/path/to/document/root", nil)
  476. if err := frankenphp.ServeHTTP(w, req); err != nil {
  477. panic(err)
  478. }
  479. })
  480. log.Fatal(http.ListenAndServe(":8080", nil))
  481. }