github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/imaging/errors_test.go (about) 1 package imaging 2 3 import ( 4 "io/ioutil" 5 "net/http" 6 "strings" 7 "testing" 8 9 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v2/pkg/session" 10 "github.com/stretchr/testify/require" 11 "github.com/tj/assert" 12 ) 13 14 func TestNewError(t *testing.T) { 15 sess, err := session.New() 16 require.NoError(t, err) 17 18 req, err := http.NewRequest( 19 http.MethodHead, 20 "/", 21 nil) 22 require.NoError(t, err) 23 24 tests := map[string]struct { 25 response *http.Response 26 expected *Error 27 }{ 28 "valid response, status code 400, Bad Request": { 29 response: &http.Response{ 30 Status: "Bad Request", 31 StatusCode: http.StatusBadRequest, 32 Body: ioutil.NopCloser(strings.NewReader( 33 `{"type":"testType","title":"Bad Request","detail":"error","status":400, 34 "extensionFields":{"requestId":"123"},"problemId":"abc123","requestId":"123"}`), 35 ), 36 Request: req, 37 }, 38 expected: &Error{ 39 Type: "testType", 40 Title: "Bad Request", 41 Detail: "error", 42 Status: http.StatusBadRequest, 43 ExtensionFields: map[string]string{ 44 "requestId": "123", 45 }, 46 ProblemID: "abc123", 47 RequestID: "123", 48 }, 49 }, 50 "valid response, status code 400, Illegal parameter value": { 51 response: &http.Response{ 52 Status: "Bad Request", 53 StatusCode: http.StatusBadRequest, 54 Body: ioutil.NopCloser(strings.NewReader( 55 `{"type":"testType","title":"Illegal parameter value","detail":"error","status":400, 56 "extensionFields":{"illegalValue":"abc","parameterName":"param1"},"problemId":"abc123","illegalValue":"abc","parameterName":"param1"}`), 57 ), 58 Request: req, 59 }, 60 expected: &Error{ 61 Type: "testType", 62 Title: "Illegal parameter value", 63 Detail: "error", 64 Status: http.StatusBadRequest, 65 ExtensionFields: map[string]string{ 66 "illegalValue": "abc", 67 "parameterName": "param1", 68 }, 69 ProblemID: "abc123", 70 IllegalValue: "abc", 71 ParameterName: "param1", 72 }, 73 }, 74 "invalid response body, assign status code": { 75 response: &http.Response{ 76 Status: "Internal Server Error", 77 StatusCode: http.StatusInternalServerError, 78 Body: ioutil.NopCloser(strings.NewReader( 79 `test`), 80 ), 81 Request: req, 82 }, 83 expected: &Error{ 84 Title: "test", 85 Detail: "", 86 Status: http.StatusInternalServerError, 87 }, 88 }, 89 } 90 for name, test := range tests { 91 t.Run(name, func(t *testing.T) { 92 res := Client(sess).(*imaging).Error(test.response) 93 assert.Equal(t, test.expected, res) 94 }) 95 } 96 } 97 98 func TestAs(t *testing.T) { 99 tests := map[string]struct { 100 err Error 101 target Error 102 expected bool 103 }{ 104 "different error code": { 105 err: Error{Status: 404}, 106 target: Error{Status: 401}, 107 expected: false, 108 }, 109 "same error code": { 110 err: Error{Status: 404}, 111 target: Error{Status: 404}, 112 expected: true, 113 }, 114 "same error code and error message": { 115 err: Error{Status: 404, Title: "some error"}, 116 target: Error{Status: 404, Title: "some error"}, 117 expected: true, 118 }, 119 "same error code and different error message": { 120 err: Error{Status: 404, Title: "some error"}, 121 target: Error{Status: 404, Title: "other error"}, 122 expected: false, 123 }, 124 } 125 126 for name, test := range tests { 127 t.Run(name, func(t *testing.T) { 128 assert.Equal(t, test.err.Is(&test.target), test.expected) 129 }) 130 } 131 }