From 5bc93fc02b14d67381507d934bf75bb7f3945cb0 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Thu, 5 Dec 2024 15:45:25 +0100 Subject: [PATCH] fixed test --- internal/server/middleware_test.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/internal/server/middleware_test.go b/internal/server/middleware_test.go index d6c48e4..51d31a7 100644 --- a/internal/server/middleware_test.go +++ b/internal/server/middleware_test.go @@ -31,42 +31,47 @@ import ( "github.com/stretchr/testify/assert" "net/http" "testing" + "log" + "github.com/sirupsen/logrus" + "go.elastic.co/ecslogrus" ) func TestRequestLoggingMiddleware(t *testing.T) { + logger := logrus.New() + logger.SetFormatter(&ecslogrus.Formatter{}) + log.SetOutput(logger.Writer()) + t.Run("should log request", func(t *testing.T) { + logger.SetLevel(logrus.DebugLevel) mock.SetUpGrpcServer() SetUpHttpGateway() var buf bytes.Buffer - logger.BaseLogger.SetOutput(&buf) + log.SetOutput(&buf) - originalFlags := logger.BaseLogger.Flags() defer func() { - logger.BaseLogger.SetOutput(nil) - logger.BaseLogger.SetFlags(originalFlags) - }() + log.SetOutput(nil) + }() jsonData := []byte(`{"mukId": "testMukId"}`) http.Post("http://localhost:8080/api/fachstellen", "application/json", bytes.NewBuffer(jsonData)) logOutput := buf.String() - assert.Contains(t, logOutput, "received POST request for /api/fachstellen with body: {\"mukId\": \"testMukId\"}") - assert.Contains(t, logOutput, "successfully handled POST request for /api/fachstellen with body: {\"mukId\": \"testMukId\"}") + assert.Contains(t, "received POST request for /api/fachstellen with body: {\"mukId\": \"testMukId\"}", logOutput) + assert.Contains(t, "successfully handled POST request for /api/fachstellen with body: {\"mukId\": \"testMukId\"}", logOutput) }) t.Run("should not log request", func(t *testing.T) { + logger.SetLevel(logrus.InfoLevel) mock.SetUpGrpcServer() SetUpHttpGateway() var buf bytes.Buffer - logger.BaseLogger.SetOutput(&buf) + log.SetOutput(&buf) - originalFlags := logger.BaseLogger.Flags() defer func() { - logger.BaseLogger.SetOutput(nil) - logger.BaseLogger.SetFlags(originalFlags) - }() + log.SetOutput(nil) + }() http.Get("http://localhost:8080/") -- GitLab