Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
laushinka committed Apr 29, 2022
1 parent fb36164 commit 77c0af4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
3 changes: 2 additions & 1 deletion components/public-api-server/middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ func NewLoggingMiddleware(l *logrus.Entry) Middleware {
uri := r.RequestURI
method := r.Method
duration := time.Since(start)
next.ServeHTTP(w, r)

l.WithFields(logrus.Fields{
"uri": uri,
"method": method,
"duration": duration,
}).Info("Method call successful")
next.ServeHTTP(w, r)
})

return logging
Expand Down
20 changes: 7 additions & 13 deletions components/public-api-server/middleware/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package middleware

import (
"bytes"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
_ "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"testing"
)

func TestLoggingMiddleware(t *testing.T) {
logInMemory := &bytes.Buffer{}
logger := log.New()
logger := logrus.New()
logger.SetOutput(logInMemory)
logger.SetFormatter(&logrus.JSONFormatter{})

expectedBody := `hello world`

Expand All @@ -23,17 +24,10 @@ func TestLoggingMiddleware(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
rec := httptest.NewRecorder() // this records the response

m := NewLoggingMiddleware(logger)
m := NewLoggingMiddleware(logrus.NewEntry(logger))
wrappedHandler := m(someHandler)
wrappedHandler.ServeHTTP(rec, req)

if status := rec.Code; status != http.StatusOK {
t.Errorf("Something went wrong with status code %v", status)
}

if rec.Body.String() != expectedBody {
t.Errorf("Unexpected body: %v", rec.Body.String())
}

assert.Equal(t, logInMemory, logInMemory)
require.HTTPStatusCode(t, someHandler, http.MethodGet, "/", nil, http.StatusOK)
require.HTTPBodyContains(t, someHandler, http.MethodGet, "/", nil, "hello")
}

0 comments on commit 77c0af4

Please sign in to comment.