Debian Codegraph @

v2/internallog/internallog_test.go

raw
1 // Copyright 2024, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 package internallog
31
32 import (
33 "bytes"
34 "context"
35 "flag"
36 "io"
37 "log/slog"
38 "net/http"
39 "net/url"
40 "os"
41 "strings"
42 "testing"
43
44 "github.com/googleapis/gax-go/v2/internallog/internal"
45 "github.com/googleapis/gax-go/v2/internallog/internal/logtest"
46 )
47
48 // To update conformance tests in this package run `go test -update_golden`
49 func TestMain(m *testing.M) {
50 flag.Parse()
51 os.Exit(m.Run())
52 }
53
54 func TestLog_off(t *testing.T) {
55 golden := "off.log"
56 logger, f := setupLogger(t, golden)
57 logger.Error("one")
58 logger.Info("two")
59 logger.Warn("three")
60 logger.Debug("four")
61 f.Close()
62 logtest.DiffTest(t, f.Name(), golden)
63 }
64
65 func TestLog_envarError(t *testing.T) {
66 golden := "envar-error.log"
67 t.Setenv(internal.LoggingLevelEnvVar, "eRrOr")
68 logger, f := setupLogger(t, golden)
69 logger.Error("one")
70 logger.Info("two")
71 logger.Warn("three")
72 logger.Debug("four")
73 f.Close()
74 logtest.DiffTest(t, f.Name(), golden)
75 }
76
77 func TestLog_envarInfo(t *testing.T) {
78 golden := "envar-info.log"
79 t.Setenv(internal.LoggingLevelEnvVar, "info")
80 logger, f := setupLogger(t, golden)
81 logger.Error("one")
82 logger.Info("two")
83 logger.Warn("three")
84 logger.Debug("four")
85 f.Close()
86 logtest.DiffTest(t, f.Name(), golden)
87 }
88
89 func TestLog_envarWarn(t *testing.T) {
90 golden := "envar-warn.log"
91 t.Setenv(internal.LoggingLevelEnvVar, "warn")
92 logger, f := setupLogger(t, golden)
93 logger.Error("one")
94 logger.Info("two")
95 logger.Warn("three")
96 logger.Debug("four")
97 f.Close()
98 logtest.DiffTest(t, f.Name(), golden)
99 }
100
101 func TestLog_envarDebug(t *testing.T) {
102 golden := "envar-debug.log"
103 t.Setenv(internal.LoggingLevelEnvVar, "debug")
104 logger, f := setupLogger(t, golden)
105 logger.Error("one")
106 logger.Info("two")
107 logger.Warn("three")
108 logger.Debug("four")
109 f.Close()
110 logtest.DiffTest(t, f.Name(), golden)
111 }
112
113 func TestLog_HTTPRequest(t *testing.T) {
114 golden := "httpRequest.log"
115 t.Setenv(internal.LoggingLevelEnvVar, "debug")
116 logger, f := setupLogger(t, golden)
117 ctx := context.Background()
118 body := []byte(`{"secret":"shh, it's a secret"}`)
119 request, err := http.NewRequest(http.MethodPost, "https://example.com", bytes.NewReader(body))
120 if err != nil {
121 t.Fatal(err)
122 }
123 request.Header.Add("foo", "bar")
124 logger.DebugContext(ctx, "msg", "request", HTTPRequest(request, body))
125 f.Close()
126 logtest.DiffTest(t, f.Name(), golden)
127 }
128
129 func TestLog_HTTPResponse(t *testing.T) {
130 golden := "httpResponse.log"
131 t.Setenv(internal.LoggingLevelEnvVar, "debug")
132 logger, f := setupLogger(t, golden)
133 ctx := context.Background()
134 body := []byte(`{"secret":"shh, it's a secret"}`)
135 response := &http.Response{
136 StatusCode: 200,
137 Header: http.Header{"Foo": []string{"bar"}},
138 Body: io.NopCloser(bytes.NewReader(body)),
139 }
140 logger.DebugContext(ctx, "msg", "response", HTTPResponse(response, body))
141 f.Close()
142 logtest.DiffTest(t, f.Name(), golden)
143 }
144
145 func TestLog_HTTPRequest_formData(t *testing.T) {
146 golden := "httpRequest-form.log"
147 t.Setenv(internal.LoggingLevelEnvVar, "debug")
148 logger, f := setupLogger(t, golden)
149 ctx := context.Background()
150 form := url.Values{}
151 form.Add("foo", "bar")
152 form.Add("baz", "qux")
153 request, err := http.NewRequest(http.MethodPost, "https://example.com", strings.NewReader(form.Encode()))
154 if err != nil {
155 t.Fatal(err)
156 }
157 request.Header.Add("foo", "bar")
158 logger.DebugContext(ctx, "msg", "request", HTTPRequest(request, []byte(form.Encode())))
159 f.Close()
160 logtest.DiffTest(t, f.Name(), golden)
161 }
162
163 func TestLog_HTTPRequest_jsonArray(t *testing.T) {
164 golden := "httpRequest-array.log"
165 t.Setenv(internal.LoggingLevelEnvVar, "debug")
166 logger, f := setupLogger(t, golden)
167 ctx := context.Background()
168 body := []byte(`[{"secret":"shh, it's a secret"},{"secret":"and, another"}]`)
169 request, err := http.NewRequest(http.MethodPost, "https://example.com", bytes.NewReader(body))
170 if err != nil {
171 t.Fatal(err)
172 }
173 request.Header.Add("foo", "bar")
174 logger.DebugContext(ctx, "msg", "request", HTTPRequest(request, body))
175 f.Close()
176 logtest.DiffTest(t, f.Name(), golden)
177 }
178
179 func setupLogger(t *testing.T, golden string) (*slog.Logger, *os.File) {
180 t.Helper()
181 f, err := os.CreateTemp(t.TempDir(), golden)
182 if err != nil {
183 t.Fatal(err)
184 }
185 logger := internal.NewLoggerWithWriter(f)
186 return logger, f
187 }