-
Notifications
You must be signed in to change notification settings - Fork 0
/
slog_test.go
168 lines (144 loc) · 4.16 KB
/
slog_test.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package log
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"github.com/99designs/gqlgen/graphql"
. "github.com/onsi/ginkgo"
g "github.com/onsi/gomega"
"github.com/vektah/gqlparser/v2/gqlerror"
)
var _ = Describe("Logger", func() {
Describe("NewSlogHTTPMiddleware", func() {
It("should log http request information", func() {
var (
buf bytes.Buffer
logger = slog.New(slog.NewJSONHandler(&buf, nil))
mw = NewSLogChiMiddleware(logger)
res = "test"
hf = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(res))
})
)
ts := httptest.NewServer(mw(hf))
defer ts.Close()
_, err := http.Get(ts.URL)
g.Expect(err).To(g.Succeed())
type logOutput struct {
Msg string `json:"msg"`
Proto string `json:"proto"`
Path string `json:"path"`
Duration int `json:"duration"`
Status int `json:"status"`
Size int `json:"size"`
IP string `json:"ip"`
}
var lo logOutput
err = json.Unmarshal(buf.Bytes(), &lo)
g.Expect(err).To(g.Succeed())
g.Expect(lo.Msg).To(g.Equal("HTTP Request Served"))
g.Expect(lo.Proto).To(g.Equal("HTTP/1.1"))
g.Expect(lo.Path).To(g.Equal("/"))
g.Expect(lo.Duration).To(g.BeNumerically(">", 0))
g.Expect(lo.Status).To(g.Equal(200))
g.Expect(lo.Size).To(g.Equal(len(res)))
g.Expect(strings.Split(lo.IP, ":")[0]).To(g.Equal("127.0.0.1"))
})
})
Describe("noOpVariablesScrubber#Scrub", func() {
It("should return nil", func() {
s := noopVariablesScrubber{}
res := s.Scrub(map[string]any{"test": struct{}{}})
g.Expect(res).To(g.BeNil())
})
})
Describe("DevScrubber#Scrub", func() {
It("should return the same map", func() {
s := DevScrubber{}
in := map[string]any{"test": struct{ Data string }{Data: "data"}}
res := s.Scrub(in)
g.Expect(res).To(g.Equal(in))
})
})
Describe("NewGraphQLResponseMiddleware", func() {
It("should log GraphQL response and request info", func() {
var (
buf bytes.Buffer
logger = slog.New(slog.NewJSONHandler(&buf, nil))
query = "query"
vars = map[string]any{"token": "super secrect stuff"}
oc = &graphql.OperationContext{
RawQuery: query,
Variables: vars,
}
errMsg = "Testing Errors"
errors = gqlerror.List{
&gqlerror.Error{
Err: errors.New("error"),
Message: errMsg,
},
}
handler = func(ctx context.Context) *graphql.Response {
return &graphql.Response{
Errors: errors,
}
}
subject = NewSLogGraphQLResponseMiddleware(logger, nil)
)
subject(graphql.WithOperationContext(context.Background(), oc), handler)
type logOutput struct {
Msg string `json:"msg"`
Graphql struct {
Req struct {
Query string `json:"query"`
Variables map[string]any `json:"variables"`
} `json:"req"`
Res struct {
Errors string `json:"errors"`
} `json:"res"`
Duration int `json:"duration"`
} `json:"graphql"`
}
var lo logOutput
err := json.Unmarshal(buf.Bytes(), &lo)
g.Expect(err).To(g.Succeed())
g.Expect(lo.Msg).To(g.Equal("GraphQL Request Served"))
g.Expect(lo.Graphql.Req.Query).To(g.Equal(query))
g.Expect(lo.Graphql.Req.Variables).To(g.BeNil())
g.Expect(lo.Graphql.Res.Errors).To(g.Equal(fmt.Sprintf("input: %s\n", errMsg)))
g.Expect(lo.Graphql.Duration).To(g.BeNumerically(">", 0))
})
})
Describe("SLogReplaceAttr", func() {
It("should return a new slog.Attr with the error message", func() {
var (
buf bytes.Buffer
logger = slog.New(
slog.NewJSONHandler(&buf, &slog.HandlerOptions{ReplaceAttr: SLogReplaceAttr}),
)
errMsg = "error"
)
logger.ErrorContext(
context.Background(),
"An error occurred",
slog.Any("error", errors.New(errMsg)),
)
var lo struct {
Msg string `json:"msg"`
Error struct {
Message string `json:"message"`
} `json:"error"`
}
err := json.Unmarshal(buf.Bytes(), &lo)
g.Expect(err).To(g.Succeed())
g.Expect(lo.Msg).To(g.Equal("An error occurred"))
g.Expect(lo.Error.Message).To(g.Equal(errMsg))
})
})
})