@@ -32,10 +32,6 @@ import (
3232 "github.com/dunglas/frankenphp/internal/fastabs"
3333 "github.com/stretchr/testify/assert"
3434 "github.com/stretchr/testify/require"
35- "go.uber.org/zap/exp/zapslog"
36- "go.uber.org/zap/zapcore"
37- "go.uber.org/zap/zaptest"
38- "go.uber.org/zap/zaptest/observer"
3935)
4036
4137type testOptions struct {
@@ -62,10 +58,6 @@ func runTest(t *testing.T, test func(func(http.ResponseWriter, *http.Request), *
6258 cwd , _ := os .Getwd ()
6359 testDataDir := cwd + "/testdata/"
6460
65- if opts .logger == nil {
66- opts .logger = slog .New (zapslog .NewHandler (zaptest .NewLogger (t ).Core ()))
67- }
68-
6961 initOpts := []frankenphp.Option {frankenphp .WithLogger (opts .logger )}
7062 if opts .workerScript != "" {
7163 workerOpts := []frankenphp.WorkerOption {
@@ -423,44 +415,68 @@ my_autoloader`, i), body)
423415 }, opts )
424416}
425417
426- func TestLog_module (t * testing.T ) { testLog (t , & testOptions {}) }
427- func TestLog_worker (t * testing.T ) {
428- testLog (t , & testOptions {workerScript : "log.php" })
418+ func TestLog_error_log_module (t * testing.T ) { testLog_error_log (t , & testOptions {}) }
419+ func TestLog_error_log_worker (t * testing.T ) {
420+ testLog_error_log (t , & testOptions {workerScript : "log-error_log.php" })
421+ }
422+ func testLog_error_log (t * testing.T , opts * testOptions ) {
423+ var buf fmt.Stringer
424+ opts .logger , buf = newTestLogger (t )
425+
426+ runTest (t , func (handler func (http.ResponseWriter , * http.Request ), _ * httptest.Server , i int ) {
427+ req := httptest .NewRequest ("GET" , fmt .Sprintf ("http://example.com/log-error_log.php?i=%d" , i ), nil )
428+ w := httptest .NewRecorder ()
429+ handler (w , req )
430+
431+ assert .Contains (t , buf .String (), fmt .Sprintf ("request %d" , i ))
432+ }, opts )
433+ }
434+
435+ func TestLog_frankenphp_log_module (t * testing.T ) { testLog_frankenphp_log (t , & testOptions {}) }
436+ func TestLog_frankenphp_log_worker (t * testing.T ) {
437+ testLog_frankenphp_log (t , & testOptions {workerScript : "log-frankenphp_log.php" })
429438}
430- func testLog (t * testing.T , opts * testOptions ) {
431- logger , logs := observer . New ( zapcore . InfoLevel )
432- opts .logger = slog . New ( zapslog . NewHandler ( logger ) )
439+ func testLog_frankenphp_log (t * testing.T , opts * testOptions ) {
440+ var buf fmt. Stringer
441+ opts .logger , buf = newTestLogger ( t )
433442
434443 runTest (t , func (handler func (http.ResponseWriter , * http.Request ), _ * httptest.Server , i int ) {
435- req := httptest .NewRequest ("GET" , fmt .Sprintf ("http://example.com/log.php?i=%d" , i ), nil )
444+ req := httptest .NewRequest ("GET" , fmt .Sprintf ("http://example.com/log-frankenphp_log .php?i=%d" , i ), nil )
436445 w := httptest .NewRecorder ()
437446 handler (w , req )
438447
439- for logs .FilterMessage (fmt .Sprintf ("request %d" , i )).Len () <= 0 {
448+ logs := buf .String ()
449+ for _ , message := range []string {
450+ fmt .Sprintf (`level=DEBUG msg="some debug message %d" "key int"=1` , i ),
451+ fmt .Sprintf (`level=INFO msg="some info message %d" "key string"=string` , i ),
452+ fmt .Sprintf (`level=WARN msg="some warn message %d"` , i ),
453+ fmt .Sprintf (`level=ERROR msg="some error message %d" err="[a v]"` , i ),
454+ } {
455+ assert .Contains (t , logs , message )
440456 }
441457 }, opts )
442458}
443459
444460func TestConnectionAbort_module (t * testing.T ) { testConnectionAbort (t , & testOptions {}) }
445461func TestConnectionAbort_worker (t * testing.T ) {
446- testConnectionAbort (t , & testOptions {workerScript : "connectionStatusLog .php" })
462+ testConnectionAbort (t , & testOptions {workerScript : "connection_status .php" })
447463}
448464func testConnectionAbort (t * testing.T , opts * testOptions ) {
449465 testFinish := func (finish string ) {
450466 t .Run (fmt .Sprintf ("finish=%s" , finish ), func (t * testing.T ) {
451- logger , logs := observer . New ( zapcore . InfoLevel )
452- opts .logger = slog .New (zapslog . NewHandler ( logger ))
467+ var buf syncBuffer
468+ opts .logger = slog .New (slog . NewTextHandler ( & buf , & slog. HandlerOptions { Level : slog . LevelInfo } ))
453469
454470 runTest (t , func (handler func (http.ResponseWriter , * http.Request ), _ * httptest.Server , i int ) {
455- req := httptest .NewRequest ("GET" , fmt .Sprintf ("http://example.com/connectionStatusLog .php?i=%d&finish=%s" , i , finish ), nil )
471+ req := httptest .NewRequest ("GET" , fmt .Sprintf ("http://example.com/connection_status .php?i=%d&finish=%s" , i , finish ), nil )
456472 w := httptest .NewRecorder ()
457473
458474 ctx , cancel := context .WithCancel (req .Context ())
459475 req = req .WithContext (ctx )
460476 cancel ()
461477 handler (w , req )
462478
463- for logs . FilterMessage ( fmt .Sprintf ("request %d: 1" , i )). Len () <= 0 {
479+ for ! strings . Contains ( buf . String (), fmt .Sprintf ("request %d: 1" , i )) {
464480 }
465481 }, opts )
466482 })
@@ -1058,7 +1074,6 @@ func FuzzRequest(f *testing.F) {
10581074 // Headers should always be present even if empty
10591075 assert .Contains (t , body , fmt .Sprintf ("[CONTENT_TYPE] => %s" , fuzzedString ))
10601076 assert .Contains (t , body , fmt .Sprintf ("[HTTP_FUZZED] => %s" , fuzzedString ))
1061-
10621077 }, & testOptions {workerScript : "request-headers.php" })
10631078 })
10641079}
0 commit comments