Skip to content

Commit 9cc7b0d

Browse files
committed
feat(logging): unit tests for new CGO franken_log_message method
1 parent 8e172c5 commit 9cc7b0d

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

frankenphp_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,35 @@ func FuzzRequest(f *testing.F) {
10021002
// Headers should always be present even if empty
10031003
assert.Contains(t, body, fmt.Sprintf("[CONTENT_TYPE] => %s", fuzzedString))
10041004
assert.Contains(t, body, fmt.Sprintf("[HTTP_FUZZED] => %s", fuzzedString))
1005-
10061005
}, &testOptions{workerScript: "request-headers.php"})
10071006
})
10081007
}
1008+
1009+
func TestFrankenPHPLog(t *testing.T) {
1010+
var buf bytes.Buffer
1011+
handler := slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
1012+
logger := slog.New(handler)
1013+
1014+
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
1015+
body, _ := testGet("http://example.com/logging.php", handler, t)
1016+
assert.Empty(t, body)
1017+
}, &testOptions{
1018+
logger: logger,
1019+
nbParallelRequests: 1,
1020+
})
1021+
1022+
logOutput := buf.String()
1023+
1024+
for level, needle := range map[string]string{
1025+
"emerg": "level=ERROR msg=\"testing emerg as ERROR\" syslog_level=emerg\n",
1026+
"alert": "level=ERROR msg=\"testing alert as ERROR\" syslog_level=alert\n",
1027+
"crit": "level=ERROR msg=\"testing crit as ERROR\" syslog_level=crit\n",
1028+
"error": "level=ERROR msg=\"testing error as ERROR\" syslog_level=err\n",
1029+
"warn": "level=WARN msg=\"testing warning as WARN\" syslog_level=warning\n",
1030+
"notice": "level=INFO msg=\"testing notice as INFO\" syslog_level=notice\n",
1031+
"info": "level=INFO msg=\"testing info as INFO\" syslog_level=info\n",
1032+
"debug": "level=DEBUG msg=\"testing debug as DEBUG\" syslog_level=debug\n",
1033+
} {
1034+
assert.Containsf(t, logOutput, needle, "should contains %q log", level)
1035+
}
1036+
}

testdata/logging.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
frankenphp_log_message("testing emerg as ERROR", 0);
4+
frankenphp_log_message("testing alert as ERROR", 1);
5+
frankenphp_log_message("testing crit as ERROR", 2);
6+
frankenphp_log_message("testing error as ERROR", 3);
7+
frankenphp_log_message("testing warning as WARN", 4);
8+
frankenphp_log_message("testing notice as INFO", 5);
9+
frankenphp_log_message("testing info as INFO", 6);
10+
frankenphp_log_message("testing debug as DEBUG", 7);

0 commit comments

Comments
 (0)