Skip to content
Snippets Groups Projects
Commit 8d1202fd authored by OZGCloud's avatar OZGCloud
Browse files

fixed test

parent f63b91ca
Branches
Tags
No related merge requests found
......@@ -95,7 +95,7 @@ func createRueckfrageClient(grpcAddress string) (pb.AntragraumServiceClient, fun
target := GetGrpcServerUrl(grpcAddress, conf)
conn, err := grpc.NewClient(target, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.WithError(err).Error("rueckfrage router failed to route: %v", err)
log.WithError(err).Error(fmt.Sprintf("rueckfrage router failed to route: %v", err))
return nil, nil, errors.New("rueckfrage router failed to route")
}
......
......@@ -31,46 +31,50 @@ 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()
SetUpGrpcRouter()
SetUpHttpGateway()
var buf bytes.Buffer
logger.BaseLogger.SetOutput(&buf)
originalFlags := logger.BaseLogger.Flags()
defer func() {
logger.BaseLogger.SetOutput(nil)
logger.BaseLogger.SetFlags(originalFlags)
}()
jsonData := []byte(`{"postfachId": "testPostfachId"}`)
req, _ := http.NewRequest("POST", "http://localhost:8082/api/v1/rueckfragen", bytes.NewBuffer(jsonData))
req.Header.Set(GrpcAddressHeader, "localhost")
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(nil)
}()
client := &http.Client{}
client.Do(req)
logOutput := buf.String()
assert.Contains(t, logOutput, "received POST request for /api/v1/rueckfragen with body: {\"postfachId\": \"testPostfachId\"}")
assert.Contains(t, logOutput, "successfully handled POST request for /api/v1/rueckfragen with body: {\"postfachId\": \"testPostfachId\"}")
assert.Contains(t, "successfully handled POST request for /api/v1/rueckfragen with body: {\"postfachId\": \"testPostfachId\"}", 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:8082/")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment