Path: blob/main/components/public-api-server/middleware/logging_test.go
2498 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package middleware56import (7"bytes"8"net/http"9"net/http/httptest"10"testing"1112"github.com/sirupsen/logrus"13_ "github.com/sirupsen/logrus/hooks/test"14"github.com/stretchr/testify/require"15)1617func TestLoggingMiddleware(t *testing.T) {18logInMemory := &bytes.Buffer{}19logger := logrus.New()20logger.SetOutput(logInMemory)21logger.SetFormatter(&logrus.JSONFormatter{})2223expectedBody := `hello world`2425someHandler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {26_, _ = w.Write([]byte(expectedBody))27})28req := httptest.NewRequest("GET", "/", nil)29rec := httptest.NewRecorder() // this records the response3031m := NewLoggingMiddleware()32wrappedHandler := m(someHandler)33wrappedHandler.ServeHTTP(rec, req)3435require.HTTPStatusCode(t, someHandler, http.MethodGet, "/", nil, http.StatusOK)36require.HTTPBodyContains(t, someHandler, http.MethodGet, "/", nil, "hello")37}383940