github.com/xmidt-org/webpa-common@v1.11.9/xhttp/error_test.go (about) 1 package xhttp 2 3 import ( 4 "context" 5 "io/ioutil" 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 ) 14 15 func testErrorState(t *testing.T) { 16 var ( 17 assert = assert.New(t) 18 httpError = &Error{Code: 503, Header: http.Header{"Foo": []string{"Bar"}}, Text: "fubar"} 19 ) 20 21 assert.Equal(503, httpError.StatusCode()) 22 assert.Equal(http.Header{"Foo": []string{"Bar"}}, httpError.Headers()) 23 assert.Equal("fubar", httpError.Error()) 24 25 json, err := httpError.MarshalJSON() 26 assert.NoError(err) 27 assert.JSONEq( 28 `{"code": 503, "text": "fubar"}`, 29 string(json), 30 ) 31 } 32 33 func testErrorDefaultEncoding(t *testing.T) { 34 var ( 35 assert = assert.New(t) 36 httpError = &Error{Code: 503, Header: http.Header{"Foo": []string{"Bar"}}, Text: "fubar"} 37 response = httptest.NewRecorder() 38 ) 39 40 gokithttp.DefaultErrorEncoder(context.Background(), httpError, response) 41 assert.Equal(503, httpError.Code) 42 assert.Equal("Bar", response.HeaderMap.Get("Foo")) 43 assert.JSONEq( 44 `{"code": 503, "text": "fubar"}`, 45 response.Body.String(), 46 ) 47 } 48 49 func TestError(t *testing.T) { 50 t.Run("State", testErrorState) 51 t.Run("DefaultEncoding", testErrorDefaultEncoding) 52 } 53 54 func TestWriteErrorf(t *testing.T) { 55 var ( 56 assert = assert.New(t) 57 require = require.New(t) 58 59 testData = []struct { 60 code int 61 format string 62 parameters []interface{} 63 expectedJSON string 64 }{ 65 { 66 http.StatusInternalServerError, 67 "some message followed by an int: %d", 68 []interface{}{47}, 69 `{"code": 500, "message": "some message followed by an int: 47"}`, 70 }, 71 { 72 412, 73 "this message has no parameters", 74 nil, 75 `{"code": 412, "message": "this message has no parameters"}`, 76 }, 77 } 78 ) 79 80 for _, record := range testData { 81 t.Logf("%v", record) 82 83 var ( 84 response = httptest.NewRecorder() 85 count, err = WriteErrorf(response, record.code, record.format, record.parameters...) 86 ) 87 88 assert.True(count > 0) 89 assert.NoError(err) 90 assert.Equal(record.code, response.Code) 91 assert.Equal("application/json", response.HeaderMap.Get("Content-Type")) 92 93 actualJSON, err := ioutil.ReadAll(response.Body) 94 require.NoError(err) 95 96 assert.JSONEq(record.expectedJSON, string(actualJSON)) 97 } 98 } 99 100 func TestWriteError(t *testing.T) { 101 var ( 102 assert = assert.New(t) 103 require = require.New(t) 104 105 testData = []struct { 106 code int 107 value interface{} 108 expectedJSON string 109 }{ 110 { 111 http.StatusInternalServerError, 112 "expected message", 113 `{"code": 500, "message": "expected message"}`, 114 }, 115 { 116 567, 117 "", 118 `{"code": 567, "message": ""}`, 119 }, 120 } 121 ) 122 123 for _, record := range testData { 124 t.Logf("%v", record) 125 126 var ( 127 response = httptest.NewRecorder() 128 count, err = WriteError(response, record.code, record.value) 129 ) 130 131 assert.True(count > 0) 132 assert.NoError(err) 133 assert.Equal(record.code, response.Code) 134 assert.Equal("application/json", response.HeaderMap.Get("Content-Type")) 135 136 actualJSON, err := ioutil.ReadAll(response.Body) 137 require.NoError(err) 138 139 assert.JSONEq(record.expectedJSON, string(actualJSON)) 140 } 141 }