github.com/xmidt-org/webpa-common@v1.11.9/xhttp/xcontext/setErrorEncoder_test.go (about) 1 package xcontext 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 gokithttp "github.com/go-kit/kit/transport/http" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 "github.com/xmidt-org/webpa-common/xhttp" 14 ) 15 16 func testSetErrorEncoderDefault(t *testing.T) { 17 var ( 18 assert = assert.New(t) 19 ctx = SetErrorEncoder(nil)(context.Background(), httptest.NewRequest("GET", "/", nil)) 20 ) 21 22 assert.NotNil(xhttp.GetErrorEncoder(ctx)) 23 } 24 25 func testSetErrorEncoderCustom(t *testing.T) { 26 var ( 27 assert = assert.New(t) 28 require = require.New(t) 29 30 expectedCalled = false 31 expected gokithttp.ErrorEncoder = func(context.Context, error, http.ResponseWriter) { 32 expectedCalled = true 33 } 34 35 actual = xhttp.GetErrorEncoder( 36 SetErrorEncoder(expected)(context.Background(), httptest.NewRequest("GET", "/", nil)), 37 ) 38 ) 39 40 require.NotNil(actual) 41 actual(context.Background(), errors.New("expected"), httptest.NewRecorder()) 42 assert.True(expectedCalled) 43 } 44 45 func TestSetErrorEncoder(t *testing.T) { 46 t.Run("Default", testSetErrorEncoderDefault) 47 t.Run("Custom", testSetErrorEncoderCustom) 48 }