File: logging_test.go

package info (click to toggle)
relic 7.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,108 kB
  • sloc: sh: 230; makefile: 10
file content (79 lines) | stat: -rw-r--r-- 2,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package zhttp

import (
	"bytes"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

	"github.com/rs/zerolog"
	"github.com/rs/zerolog/hlog"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestLoggingMiddleware(t *testing.T) {
	h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch r.URL.Path {
		case "/dontlog":
			DontLog(r)
		case "/changeme":
			r.URL.Path = "/changed"
		default:
			http.NotFound(w, r)
			return
		}
		// updates to the log context affect every message
		hlog.FromRequest(r).UpdateContext(func(c zerolog.Context) zerolog.Context {
			return c.Str("append1", "always")
		})
		// append only to the access log without affecting the context
		AppendAccessLog(r, func(e *zerolog.Event) {
			e.Str("append2", "access")
		})
		hlog.FromRequest(r).Info().Msg("a message")
	})
	var buf bytes.Buffer
	logger := zerolog.New(&buf)
	now := fakeTime()
	mw := LoggingMiddleware(
		WithLogger(logger),
		func(lc *loggingConfig) { lc.now = now },
	)
	newReq := func(path string) *http.Request {
		r := httptest.NewRequest(http.MethodGet, path, nil)
		r.RemoteAddr = "192.168.1.1:12345"
		r.Header.Set("X-Request-Id", "00000000")
		r.Header.Set("User-Agent", "unittest")
		return r
	}

	t.Run("Changed", func(t *testing.T) {
		buf.Reset()
		r, w := newReq("/changeme"), httptest.NewRecorder()
		mw(h).ServeHTTP(w, r)
		resp := w.Result()
		require.Equal(t, http.StatusOK, resp.StatusCode)
		assert.Equal(t, `{"level":"info","ip":"192.168.1.1","req_id":"00000000","append1":"always","message":"a message"}
{"level":"info","ip":"192.168.1.1","req_id":"00000000","append1":"always","method":"GET","url":"/changed","status":200,"len":0,"dur":1000,"ttfb":2000,"ua":"unittest","append2":"access"}
`, buf.String())
	})
	t.Run("DontLog", func(t *testing.T) {
		buf.Reset()
		r, w := newReq("/dontlog"), httptest.NewRecorder()
		mw(h).ServeHTTP(w, r)
		resp := w.Result()
		require.Equal(t, http.StatusOK, resp.StatusCode)
		assert.Equal(t, `{"level":"info","ip":"192.168.1.1","req_id":"00000000","append1":"always","message":"a message"}
`, buf.String())
	})
}

func fakeTime() func() time.Time {
	ts := time.Date(2001, 1, 1, 0, 0, 0, 0, time.UTC)
	return func() time.Time {
		ts = ts.Add(time.Second)
		return ts
	}
}