...
1 package middleware_test
2
3 import (
4 "context"
5 "io/ioutil"
6 "testing"
7
8 "github.com/aws/smithy-go/logging"
9 "github.com/aws/smithy-go/middleware"
10 )
11
12 type mockWithContextLogger struct {
13 logging.Logger
14 Context context.Context
15 }
16
17 func (m mockWithContextLogger) WithContext(ctx context.Context) logging.Logger {
18 m.Context = ctx
19 return m
20 }
21
22 func TestGetLogger(t *testing.T) {
23 if logger := middleware.GetLogger(context.Background()); logger == nil {
24 t.Fatal("expect logger to not be nil")
25 } else if _, ok := logger.(logging.Nop); !ok {
26 t.Fatal("expect GetLogger to fallback to Nop")
27 }
28
29 standardLogger := logging.NewStandardLogger(ioutil.Discard)
30 ctx := middleware.SetLogger(context.Background(), standardLogger)
31
32 if logger := middleware.GetLogger(ctx); logger == nil {
33 t.Fatal("expect logger to not be nil")
34 } else if logger != standardLogger {
35 t.Error("expect logger to be standard logger")
36 }
37
38 withContextLogger := mockWithContextLogger{}
39 ctx = middleware.SetLogger(context.Background(), withContextLogger)
40 if logger := middleware.GetLogger(ctx); logger == nil {
41 t.Fatal("expect logger to not be nil")
42 } else if mock, ok := logger.(mockWithContextLogger); !ok {
43 t.Error("expect logger to be context logger")
44 } else if mock.Context != ctx {
45 t.Error("expect logger context to match")
46 }
47 }
48
View as plain text