Add unit tests for checkResponseForErrors()

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-24 16:45:20 -04:00
parent 7fb7710c55
commit 1c3a1c665e

View File

@ -1,6 +1,7 @@
package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
@ -183,3 +184,50 @@ func Test_portainerClientImp_do(t *testing.T) {
})
}
}
func Test_checkResponseForErrors(t *testing.T) {
type args struct {
resp *http.Response
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "generic error",
args: args{
resp: func() (resp *http.Response) {
resp = &http.Response{
StatusCode: http.StatusNotFound,
}
bodyBytes, _ := json.Marshal(map[string]interface{}{
"Err": "Error",
"Details": "Not found",
})
resp.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
return
}(),
},
wantErr: true,
},
{
name: "non generic error",
args: args{
resp: func() (resp *http.Response) {
resp = &http.Response{
StatusCode: http.StatusNotFound,
Body: ioutil.NopCloser(bytes.NewReader([]byte("Err"))),
}
return
}(),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.wantErr, checkResponseForErrors(tt.args.resp) != nil)
})
}
}