Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/public-api-server/middleware/logging_test.go
2498 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package middleware
6
7
import (
8
"bytes"
9
"net/http"
10
"net/http/httptest"
11
"testing"
12
13
"github.com/sirupsen/logrus"
14
_ "github.com/sirupsen/logrus/hooks/test"
15
"github.com/stretchr/testify/require"
16
)
17
18
func TestLoggingMiddleware(t *testing.T) {
19
logInMemory := &bytes.Buffer{}
20
logger := logrus.New()
21
logger.SetOutput(logInMemory)
22
logger.SetFormatter(&logrus.JSONFormatter{})
23
24
expectedBody := `hello world`
25
26
someHandler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
27
_, _ = w.Write([]byte(expectedBody))
28
})
29
req := httptest.NewRequest("GET", "/", nil)
30
rec := httptest.NewRecorder() // this records the response
31
32
m := NewLoggingMiddleware()
33
wrappedHandler := m(someHandler)
34
wrappedHandler.ServeHTTP(rec, req)
35
36
require.HTTPStatusCode(t, someHandler, http.MethodGet, "/", nil, http.StatusOK)
37
require.HTTPBodyContains(t, someHandler, http.MethodGet, "/", nil, "hello")
38
}
39
40