Просмотр исходного кода

test: add tests for exception handling (#11)

Kévin Dunglas 2 лет назад
Родитель
Сommit
3712d0d69b
2 измененных файлов с 19 добавлено и 3 удалено
  1. 18 0
      frankenphp_test.go
  2. 1 3
      testdata/exception.php

+ 18 - 0
frankenphp_test.go

@@ -368,6 +368,24 @@ func testLog(t *testing.T, opts *testOptions) {
 	}, opts)
 }
 
+func TestException_module(t *testing.T) { testException(t, &testOptions{}) }
+func TestException_worker(t *testing.T) {
+	testException(t, &testOptions{workerScript: "exception.php"})
+}
+func testException(t *testing.T, opts *testOptions) {
+	runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
+		req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/exception.php?i=%d", i), nil)
+		w := httptest.NewRecorder()
+		handler(w, req)
+
+		resp := w.Result()
+		body, _ := io.ReadAll(resp.Body)
+
+		assert.Contains(t, string(body), "hello")
+		assert.Contains(t, string(body), fmt.Sprintf(`Uncaught Exception: request %d`, i))
+	}, opts)
+}
+
 func ExampleExecuteScript() {
 	if err := frankenphp.Init(); err != nil {
 		panic(err)

+ 1 - 3
testdata/error.php → testdata/exception.php

@@ -2,9 +2,7 @@
 
 require_once __DIR__.'/_executor.php';
 
-throw new Exception('unexpected');
-
 return function () {
     echo 'hello';
-    throw new Exception('error');
+    throw new Exception("request {$_GET['i']}");
 };