@@ -4,11 +4,13 @@ import (
4
4
"bufio"
5
5
"bytes"
6
6
"context"
7
+ "flag"
7
8
"fmt"
8
9
"net"
9
10
"net/http"
10
11
"os"
11
12
"path/filepath"
13
+ "regexp"
12
14
"strings"
13
15
"testing"
14
16
"time"
@@ -47,7 +49,10 @@ func (c *httpContext) beforeEach() {
47
49
_ = os .Setenv ("KUBECONFIG" , kubeConfig )
48
50
// Capture logging
49
51
c .klogState = klog .CaptureState ()
50
- klog .SetLogger (textlogger .NewLogger (textlogger .NewConfig (textlogger .Verbosity (1 ), textlogger .Output (& c .logBuffer ))))
52
+ flags := flag .NewFlagSet ("test" , flag .ContinueOnError )
53
+ klog .InitFlags (flags )
54
+ _ = flags .Set ("v" , "5" )
55
+ klog .SetLogger (textlogger .NewLogger (textlogger .NewConfig (textlogger .Verbosity (5 ), textlogger .Output (& c .logBuffer ))))
51
56
// Start server in random port
52
57
ln , err := net .Listen ("tcp" , "0.0.0.0:0" )
53
58
if err != nil {
@@ -247,3 +252,29 @@ func TestWellKnownOAuthProtectedResource(t *testing.T) {
247
252
})
248
253
})
249
254
}
255
+
256
+ func TestMiddlewareLogging (t * testing.T ) {
257
+ testCase (t , func (ctx * httpContext ) {
258
+ _ , _ = http .Get (fmt .Sprintf ("http://%s/.well-known/oauth-protected-resource" , ctx .httpAddress ))
259
+ t .Run ("Logs HTTP requests and responses" , func (t * testing.T ) {
260
+ if ! strings .Contains (ctx .logBuffer .String (), "GET /.well-known/oauth-protected-resource 200" ) {
261
+ t .Errorf ("Expected log entry for GET /.well-known/oauth-protected-resource, got: %s" , ctx .logBuffer .String ())
262
+ }
263
+ })
264
+ t .Run ("Logs HTTP request duration" , func (t * testing.T ) {
265
+ expected := `"GET /.well-known/oauth-protected-resource 200 (.+)"`
266
+ m := regexp .MustCompile (expected ).FindStringSubmatch (ctx .logBuffer .String ())
267
+ if len (m ) != 2 {
268
+ t .Fatalf ("Expected log entry to contain duration, got %s" , ctx .logBuffer .String ())
269
+ }
270
+ duration , err := time .ParseDuration (m [1 ])
271
+ if err != nil {
272
+ t .Fatalf ("Failed to parse duration from log entry: %v" , err )
273
+ }
274
+ if duration < 0 {
275
+ t .Errorf ("Expected duration to be non-negative, got %v" , duration )
276
+ }
277
+ })
278
+ })
279
+
280
+ }
0 commit comments