Add unit tests for logger Closes #10

This commit is contained in:
AlphaIru 2026-02-26 16:18:06 -08:00
parent 64236c99c9
commit 1c181f3400

View file

@ -0,0 +1,35 @@
package logger
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/rs/zerolog"
)
func TestInjectAndFromContext(t *testing.T) {
var buf bytes.Buffer
testLogger := zerolog.New(&buf)
req := httptest.NewRequest(http.MethodGet, "/", nil)
// Inject logger
req = Inject(req, &testLogger)
// Retrieve logger
l := FromContext(req.Context())
l.Info().Msg("hello")
output := buf.String()
if output == "" {
t.Fatal("expected log output, got empty string")
}
if !bytes.Contains([]byte(output), []byte("hello")) {
t.Errorf("expected log to contain 'hello', got %s", output)
}
}