github.com/xmidt-org/webpa-common@v1.11.9/middleware/logging_test.go (about) 1 package middleware 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 "github.com/xmidt-org/webpa-common/logging" 10 ) 11 12 func testLoggingWithLoggable(t *testing.T) { 13 var ( 14 require = require.New(t) 15 assert = assert.New(t) 16 expectedLogger = logging.NewTestLogger(nil, t) 17 loggable = new(mockLoggable) 18 expectedResponse = "expected response" 19 20 logging = Logging(func(ctx context.Context, value interface{}) (interface{}, error) { 21 assert.Equal(loggable, value) 22 assert.Equal(expectedLogger, logging.GetLogger(ctx)) 23 return expectedResponse, nil 24 }) 25 ) 26 27 require.NotNil(logging) 28 29 loggable.On("Logger").Return(expectedLogger).Once() 30 31 actual, err := logging(context.Background(), loggable) 32 assert.Equal(expectedResponse, actual) 33 assert.NoError(err) 34 35 loggable.AssertExpectations(t) 36 } 37 38 func testLoggingWithoutLoggable(t *testing.T) { 39 var ( 40 require = require.New(t) 41 assert = assert.New(t) 42 43 expectedRequest = "expected request" 44 expectedResponse = "expected response" 45 46 logging = Logging(func(ctx context.Context, value interface{}) (interface{}, error) { 47 assert.Equal(expectedRequest, value) 48 assert.Equal(logging.DefaultLogger(), logging.GetLogger(ctx)) 49 return expectedResponse, nil 50 }) 51 ) 52 53 require.NotNil(logging) 54 55 actual, err := logging(context.Background(), expectedRequest) 56 assert.Equal(expectedResponse, actual) 57 assert.NoError(err) 58 } 59 60 func TestLogging(t *testing.T) { 61 t.Run("WithLoggable", testLoggingWithLoggable) 62 t.Run("WithoutLoggable", testLoggingWithoutLoggable) 63 }