@@ -284,15 +284,14 @@ func TestReadResponseNil(t *testing.T) {
284284}
285285
286286tests := []struct {
287- name string
288- r io.Reader
289- wantErr bool
290- wantValue string
287+ name string
288+ r io.Reader
289+ wantErr bool
291290}{
292- {"Empty" , bytes .NewReader (nil ), true , "" },
293- {"Nil" , getResponseBody (nil ), false , "blah" },
294- {"Response" , getResponseBody (TestStruct {"blah" }), false , "blah" },
295- {"Error" , getErrorBody ("blah" , http .StatusNotFound ), true , "" },
291+ {"Empty" , bytes .NewReader (nil ), true },
292+ {"Nil" , getResponseBody (nil ), false },
293+ {"Response" , getResponseBody (TestStruct {"blah" }), false },
294+ {"Error" , getErrorBody ("blah" , http .StatusNotFound ), true },
296295}
297296for _ , tt := range tests {
298297t .Run (tt .name , func (t * testing.T ) {
@@ -303,3 +302,42 @@ func TestReadResponseNil(t *testing.T) {
303302})
304303}
305304}
305+
306+ func TestReadError (t * testing.T ) {
307+ type TestStruct struct {
308+ Value string
309+ }
310+
311+ tests := []struct {
312+ name string
313+ r io.Reader
314+ wantErr bool
315+ wantMessage string
316+ wantCode int
317+ }{
318+ {"Empty" , bytes .NewReader (nil ), false , "" , 0 },
319+ {"Response" , getResponseBody (TestStruct {"blah" }), false , "" , 0 },
320+ {"Error" , getErrorBody ("blah" , http .StatusNotFound ), true , "blah" , http .StatusNotFound },
321+ }
322+ for _ , tt := range tests {
323+ t .Run (tt .name , func (t * testing.T ) {
324+ err := ReadError (tt .r )
325+ if (err != nil ) != tt .wantErr {
326+ t .Errorf ("ReadError() error = %v, wantErr %v" , err , tt .wantErr )
327+ }
328+
329+ if err != nil {
330+ err , ok := err .(* Error )
331+ if ! ok {
332+ t .Fatal ("invalid error type" )
333+ }
334+ if got , want := err .Message , tt .wantMessage ; got != want {
335+ t .Errorf ("got message %v, want %v" , got , want )
336+ }
337+ if got , want := err .Code , tt .wantCode ; got != want {
338+ t .Errorf ("got code %v, want %v" , got , want )
339+ }
340+ }
341+ })
342+ }
343+ }
0 commit comments