@@ -134,14 +134,17 @@ func TestHandleErrorResp(t *testing.T) {
134
134
client := NewClient (mockToken )
135
135
136
136
testCases := []struct {
137
- name string
138
- httpCode int
139
- body io.Reader
140
- expected string
137
+ name string
138
+ httpCode int
139
+ httpStatus string
140
+ contentType string
141
+ body io.Reader
142
+ expected string
141
143
}{
142
144
{
143
- name : "401 Invalid Authentication" ,
144
- httpCode : http .StatusUnauthorized ,
145
+ name : "401 Invalid Authentication" ,
146
+ httpCode : http .StatusUnauthorized ,
147
+ contentType : "application/json" ,
145
148
body : bytes .NewReader ([]byte (
146
149
`{
147
150
"error":{
@@ -152,11 +155,12 @@ func TestHandleErrorResp(t *testing.T) {
152
155
}
153
156
}` ,
154
157
)),
155
- expected : "error, status code: 401, message: You didn't provide an API key. ...." ,
158
+ expected : "error, status code: 401, status: , message: You didn't provide an API key. ...." ,
156
159
},
157
160
{
158
- name : "401 Azure Access Denied" ,
159
- httpCode : http .StatusUnauthorized ,
161
+ name : "401 Azure Access Denied" ,
162
+ httpCode : http .StatusUnauthorized ,
163
+ contentType : "application/json" ,
160
164
body : bytes .NewReader ([]byte (
161
165
`{
162
166
"error":{
@@ -165,11 +169,12 @@ func TestHandleErrorResp(t *testing.T) {
165
169
}
166
170
}` ,
167
171
)),
168
- expected : "error, status code: 401, message: Access denied due to Virtual Network/Firewall rules." ,
172
+ expected : "error, status code: 401, status: , message: Access denied due to Virtual Network/Firewall rules." ,
169
173
},
170
174
{
171
- name : "503 Model Overloaded" ,
172
- httpCode : http .StatusServiceUnavailable ,
175
+ name : "503 Model Overloaded" ,
176
+ httpCode : http .StatusServiceUnavailable ,
177
+ contentType : "application/json" ,
173
178
body : bytes .NewReader ([]byte (`
174
179
{
175
180
"error":{
@@ -179,22 +184,53 @@ func TestHandleErrorResp(t *testing.T) {
179
184
"code":null
180
185
}
181
186
}` )),
182
- expected : "error, status code: 503, message: That model..." ,
187
+ expected : "error, status code: 503, status: , message: That model..." ,
183
188
},
184
189
{
185
- name : "503 no message (Unknown response)" ,
186
- httpCode : http .StatusServiceUnavailable ,
190
+ name : "503 no message (Unknown response)" ,
191
+ httpCode : http .StatusServiceUnavailable ,
192
+ contentType : "application/json" ,
187
193
body : bytes .NewReader ([]byte (`
188
194
{
189
195
"error":{}
190
196
}` )),
191
- expected : "error, status code: 503, message: " ,
197
+ expected : "error, status code: 503, status: , message: " ,
198
+ },
199
+ {
200
+ name : "413 Request Entity Too Large" ,
201
+ httpCode : http .StatusRequestEntityTooLarge ,
202
+ contentType : "text/html" ,
203
+ body : bytes .NewReader ([]byte (`<html>
204
+ <head><title>413 Request Entity Too Large</title></head>
205
+ <body>
206
+ <center><h1>413 Request Entity Too Large</h1></center>
207
+ <hr><center>nginx</center>
208
+ </body>
209
+ </html>` )),
210
+ expected : `error, status code: 413, status: , body: <html>
211
+ <head><title>413 Request Entity Too Large</title></head>
212
+ <body>
213
+ <center><h1>413 Request Entity Too Large</h1></center>
214
+ <hr><center>nginx</center>
215
+ </body>
216
+ </html>` ,
217
+ },
218
+ {
219
+ name : "errorReader" ,
220
+ httpCode : http .StatusRequestEntityTooLarge ,
221
+ contentType : "text/html" ,
222
+ body : & errorReader {err : errors .New ("errorReader" )},
223
+ expected : "error, reading response body: errorReader" ,
192
224
},
193
225
}
194
226
195
227
for _ , tc := range testCases {
196
228
t .Run (tc .name , func (t * testing.T ) {
197
- testCase := & http.Response {}
229
+ testCase := & http.Response {
230
+ Header : map [string ][]string {
231
+ "Content-Type" : {tc .contentType },
232
+ },
233
+ }
198
234
testCase .StatusCode = tc .httpCode
199
235
testCase .Body = io .NopCloser (tc .body )
200
236
err := client .handleErrorResp (testCase )
@@ -203,12 +239,6 @@ func TestHandleErrorResp(t *testing.T) {
203
239
t .Errorf ("Unexpected error: %v , expected: %s" , err , tc .expected )
204
240
t .Fail ()
205
241
}
206
-
207
- e := & APIError {}
208
- if ! errors .As (err , & e ) {
209
- t .Errorf ("(%s) Expected error to be of type APIError" , tc .name )
210
- t .Fail ()
211
- }
212
242
})
213
243
}
214
244
}
0 commit comments