@@ -134,14 +134,17 @@ func TestHandleErrorResp(t *testing.T) {
134134 client := NewClient (mockToken )
135135
136136 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
141143 }{
142144 {
143- name : "401 Invalid Authentication" ,
144- httpCode : http .StatusUnauthorized ,
145+ name : "401 Invalid Authentication" ,
146+ httpCode : http .StatusUnauthorized ,
147+ contentType : "application/json" ,
145148 body : bytes .NewReader ([]byte (
146149 `{
147150 "error":{
@@ -152,11 +155,12 @@ func TestHandleErrorResp(t *testing.T) {
152155 }
153156 }` ,
154157 )),
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. ...." ,
156159 },
157160 {
158- name : "401 Azure Access Denied" ,
159- httpCode : http .StatusUnauthorized ,
161+ name : "401 Azure Access Denied" ,
162+ httpCode : http .StatusUnauthorized ,
163+ contentType : "application/json" ,
160164 body : bytes .NewReader ([]byte (
161165 `{
162166 "error":{
@@ -165,11 +169,12 @@ func TestHandleErrorResp(t *testing.T) {
165169 }
166170 }` ,
167171 )),
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." ,
169173 },
170174 {
171- name : "503 Model Overloaded" ,
172- httpCode : http .StatusServiceUnavailable ,
175+ name : "503 Model Overloaded" ,
176+ httpCode : http .StatusServiceUnavailable ,
177+ contentType : "application/json" ,
173178 body : bytes .NewReader ([]byte (`
174179 {
175180 "error":{
@@ -179,22 +184,53 @@ func TestHandleErrorResp(t *testing.T) {
179184 "code":null
180185 }
181186 }` )),
182- expected : "error, status code: 503, message: That model..." ,
187+ expected : "error, status code: 503, status: , message: That model..." ,
183188 },
184189 {
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" ,
187193 body : bytes .NewReader ([]byte (`
188194 {
189195 "error":{}
190196 }` )),
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" ,
192224 },
193225 }
194226
195227 for _ , tc := range testCases {
196228 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+ }
198234 testCase .StatusCode = tc .httpCode
199235 testCase .Body = io .NopCloser (tc .body )
200236 err := client .handleErrorResp (testCase )
@@ -203,12 +239,6 @@ func TestHandleErrorResp(t *testing.T) {
203239 t .Errorf ("Unexpected error: %v , expected: %s" , err , tc .expected )
204240 t .Fail ()
205241 }
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- }
212242 })
213243 }
214244}
0 commit comments