Skip to content

Commit 4376cc8

Browse files
committed
feat(logging): unit tests for new CGO franken_log_message method
1 parent a7ba434 commit 4376cc8

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

frankenphp_test.go

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

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)