Skip to content
Snippets Groups Projects
Commit 5bc93fc0 authored by OZGCloud's avatar OZGCloud
Browse files

fixed test

parent 6f3e336d
No related branches found
No related tags found
No related merge requests found
...@@ -31,41 +31,46 @@ import ( ...@@ -31,41 +31,46 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"net/http" "net/http"
"testing" "testing"
"log"
"github.com/sirupsen/logrus"
"go.elastic.co/ecslogrus"
) )
func TestRequestLoggingMiddleware(t *testing.T) { 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) { t.Run("should log request", func(t *testing.T) {
logger.SetLevel(logrus.DebugLevel)
mock.SetUpGrpcServer() mock.SetUpGrpcServer()
SetUpHttpGateway() SetUpHttpGateway()
var buf bytes.Buffer var buf bytes.Buffer
logger.BaseLogger.SetOutput(&buf) log.SetOutput(&buf)
originalFlags := logger.BaseLogger.Flags()
defer func() { defer func() {
logger.BaseLogger.SetOutput(nil) log.SetOutput(nil)
logger.BaseLogger.SetFlags(originalFlags)
}() }()
jsonData := []byte(`{"mukId": "testMukId"}`) jsonData := []byte(`{"mukId": "testMukId"}`)
http.Post("http://localhost:8080/api/fachstellen", "application/json", bytes.NewBuffer(jsonData)) http.Post("http://localhost:8080/api/fachstellen", "application/json", bytes.NewBuffer(jsonData))
logOutput := buf.String() logOutput := buf.String()
assert.Contains(t, logOutput, "received 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, logOutput, "successfully handled POST request for /api/fachstellen with body: {\"mukId\": \"testMukId\"}") 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) { t.Run("should not log request", func(t *testing.T) {
logger.SetLevel(logrus.InfoLevel)
mock.SetUpGrpcServer() mock.SetUpGrpcServer()
SetUpHttpGateway() SetUpHttpGateway()
var buf bytes.Buffer var buf bytes.Buffer
logger.BaseLogger.SetOutput(&buf) log.SetOutput(&buf)
originalFlags := logger.BaseLogger.Flags()
defer func() { defer func() {
logger.BaseLogger.SetOutput(nil) log.SetOutput(nil)
logger.BaseLogger.SetFlags(originalFlags)
}() }()
http.Get("http://localhost:8080/") http.Get("http://localhost:8080/")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment