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
|
package middleware_test
import (
"context"
"io/ioutil"
"testing"
"github.com/aws/smithy-go/logging"
"github.com/aws/smithy-go/middleware"
)
type mockWithContextLogger struct {
logging.Logger
Context context.Context
}
func (m mockWithContextLogger) WithContext(ctx context.Context) logging.Logger {
m.Context = ctx
return m
}
func TestGetLogger(t *testing.T) {
if logger := middleware.GetLogger(context.Background()); logger == nil {
t.Fatal("expect logger to not be nil")
} else if _, ok := logger.(logging.Nop); !ok {
t.Fatal("expect GetLogger to fallback to Nop")
}
standardLogger := logging.NewStandardLogger(ioutil.Discard)
ctx := middleware.SetLogger(context.Background(), standardLogger)
if logger := middleware.GetLogger(ctx); logger == nil {
t.Fatal("expect logger to not be nil")
} else if logger != standardLogger {
t.Error("expect logger to be standard logger")
}
withContextLogger := mockWithContextLogger{}
ctx = middleware.SetLogger(context.Background(), withContextLogger)
if logger := middleware.GetLogger(ctx); logger == nil {
t.Fatal("expect logger to not be nil")
} else if mock, ok := logger.(mockWithContextLogger); !ok {
t.Error("expect logger to be context logger")
} else if mock.Context != ctx {
t.Error("expect logger context to match")
}
}
|