This change updates the underlying type of VagrantCloudErrors.Errors to match the expected errors response type and adds a simple test to verify the parsing of various error responses. Tests before change ``` --- FAIL: TestVagranCloudErrors (0.00s) client_test.go:23: failed to decode error response: json: cannot unmarshal array into Go struct field VagrantCloudErrors.errors of type map[string][]string client_test.go:23: failed to decode error response: json: cannot unmarshal array into Go struct field VagrantCloudErrors.errors of type map[string][]string client_test.go:26: failed to get expected response; expected "", got "error1. error2" client_test.go:23: failed to decode error response: json: cannot unmarshal array into Go struct field VagrantCloudErrors.errors of type map[string][]string client_test.go:26: failed to get expected response; expected "", got "message Bad credentials" ``` Test after change ``` --- PASS: TestVagranCloudErrors (0.00s) ```
31 lines
948 B
Go
31 lines
948 B
Go
package vagrantcloud
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestVagranCloudErrors(t *testing.T) {
|
|
testCases := []struct {
|
|
resp string
|
|
expected string
|
|
}{
|
|
{`{"Status":"422 Unprocessable Entity", "StatusCode":422, "errors":[]}`, ""},
|
|
{`{"Status":"404 Artifact not found", "StatusCode":404, "errors":["error1", "error2"]}`, "error1. error2"},
|
|
{`{"StatusCode":403, "errors":[{"message":"Bad credentials"}]}`, "message Bad credentials"},
|
|
{`{"StatusCode":500, "errors":[["error in unexpected format"]]}`, "[error in unexpected format]"},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
var cloudErrors VagrantCloudErrors
|
|
err := json.NewDecoder(strings.NewReader(tc.resp)).Decode(&cloudErrors)
|
|
if err != nil {
|
|
t.Errorf("failed to decode error response: %s", err)
|
|
}
|
|
if got := cloudErrors.FormatErrors(); got != tc.expected {
|
|
t.Errorf("failed to get expected response; expected %q, got %q", tc.expected, got)
|
|
}
|
|
}
|
|
}
|