github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/imaging/errors_test.go (about) 1 package imaging 2 3 import ( 4 "context" 5 "io/ioutil" 6 "net/http" 7 "strings" 8 "testing" 9 10 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/session" 11 "github.com/stretchr/testify/require" 12 "github.com/tj/assert" 13 ) 14 15 func TestNewError(t *testing.T) { 16 sess, err := session.New() 17 require.NoError(t, err) 18 19 req, err := http.NewRequest( 20 http.MethodHead, 21 "/", 22 nil) 23 require.NoError(t, err) 24 25 tests := map[string]struct { 26 response *http.Response 27 expected *Error 28 }{ 29 "valid response, status code 400, Bad Request": { 30 response: &http.Response{ 31 Status: "Bad Request", 32 StatusCode: http.StatusBadRequest, 33 Body: ioutil.NopCloser(strings.NewReader( 34 `{"type":"testType","title":"Bad Request","detail":"error","status":400, 35 "extensionFields":{"requestId":"123"},"problemId":"abc123","requestId":"123"}`), 36 ), 37 Request: req, 38 }, 39 expected: &Error{ 40 Type: "testType", 41 Title: "Bad Request", 42 Detail: "error", 43 Status: http.StatusBadRequest, 44 ExtensionFields: map[string]string{ 45 "requestId": "123", 46 }, 47 ProblemID: "abc123", 48 RequestID: "123", 49 }, 50 }, 51 "valid response, status code 400, Illegal parameter value": { 52 response: &http.Response{ 53 Status: "Bad Request", 54 StatusCode: http.StatusBadRequest, 55 Body: ioutil.NopCloser(strings.NewReader( 56 `{"type":"testType","title":"Illegal parameter value","detail":"error","status":400, 57 "extensionFields":{"illegalValue":"abc","parameterName":"param1"},"problemId":"abc123","illegalValue":"abc","parameterName":"param1"}`), 58 ), 59 Request: req, 60 }, 61 expected: &Error{ 62 Type: "testType", 63 Title: "Illegal parameter value", 64 Detail: "error", 65 Status: http.StatusBadRequest, 66 ExtensionFields: map[string]string{ 67 "illegalValue": "abc", 68 "parameterName": "param1", 69 }, 70 ProblemID: "abc123", 71 IllegalValue: "abc", 72 ParameterName: "param1", 73 }, 74 }, 75 "invalid response body, assign status code": { 76 response: &http.Response{ 77 Status: "Internal Server Error", 78 StatusCode: http.StatusInternalServerError, 79 Body: ioutil.NopCloser(strings.NewReader( 80 `test`), 81 ), 82 Request: req, 83 }, 84 expected: &Error{ 85 Title: "Failed to unmarshal error body. Image & Video Manager API failed. Check details for more information.", 86 Detail: "test", 87 Status: http.StatusInternalServerError, 88 }, 89 }, 90 } 91 for name, test := range tests { 92 t.Run(name, func(t *testing.T) { 93 res := Client(sess).(*imaging).Error(test.response) 94 assert.Equal(t, test.expected, res) 95 }) 96 } 97 } 98 99 func TestAs(t *testing.T) { 100 tests := map[string]struct { 101 err Error 102 target Error 103 expected bool 104 }{ 105 "different error code": { 106 err: Error{Status: 404}, 107 target: Error{Status: 401}, 108 expected: false, 109 }, 110 "same error code": { 111 err: Error{Status: 404}, 112 target: Error{Status: 404}, 113 expected: true, 114 }, 115 "same error code and error message": { 116 err: Error{Status: 404, Title: "some error"}, 117 target: Error{Status: 404, Title: "some error"}, 118 expected: true, 119 }, 120 "same error code and different error message": { 121 err: Error{Status: 404, Title: "some error"}, 122 target: Error{Status: 404, Title: "other error"}, 123 expected: false, 124 }, 125 } 126 127 for name, test := range tests { 128 t.Run(name, func(t *testing.T) { 129 assert.Equal(t, test.err.Is(&test.target), test.expected) 130 }) 131 } 132 } 133 134 func TestJsonErrorUnmarshalling(t *testing.T) { 135 req, err := http.NewRequestWithContext( 136 context.TODO(), 137 http.MethodHead, 138 "/", 139 nil) 140 require.NoError(t, err) 141 tests := map[string]struct { 142 input *http.Response 143 expected *Error 144 }{ 145 "API failure with HTML response": { 146 input: &http.Response{ 147 Request: req, 148 Status: "OK", 149 Body: ioutil.NopCloser(strings.NewReader(`<HTML><HEAD>...</HEAD><BODY>...</BODY></HTML>`))}, 150 expected: &Error{ 151 Type: "", 152 Title: "Failed to unmarshal error body. Image & Video Manager API failed. Check details for more information.", 153 Detail: "<HTML><HEAD>...</HEAD><BODY>...</BODY></HTML>", 154 }, 155 }, 156 "API failure with plain text response": { 157 input: &http.Response{ 158 Request: req, 159 Status: "OK", 160 Body: ioutil.NopCloser(strings.NewReader("Your request did not succeed as this operation has reached the limit for your account. Please try after 2024-01-16T15:20:55.945Z"))}, 161 expected: &Error{ 162 Type: "", 163 Title: "Failed to unmarshal error body. Image & Video Manager API failed. Check details for more information.", 164 Detail: "Your request did not succeed as this operation has reached the limit for your account. Please try after 2024-01-16T15:20:55.945Z", 165 }, 166 }, 167 "API failure with XML response": { 168 input: &http.Response{ 169 Request: req, 170 Status: "OK", 171 Body: ioutil.NopCloser(strings.NewReader(`<Root><Item id="1" name="Example" /></Root>`))}, 172 expected: &Error{ 173 Type: "", 174 Title: "Failed to unmarshal error body. Image & Video Manager API failed. Check details for more information.", 175 Detail: "<Root><Item id=\"1\" name=\"Example\" /></Root>", 176 }, 177 }, 178 } 179 180 for name, test := range tests { 181 t.Run(name, func(t *testing.T) { 182 sess, _ := session.New() 183 g := imaging{ 184 Session: sess, 185 } 186 assert.Equal(t, test.expected, g.Error(test.input)) 187 }) 188 } 189 }