diff --git a/helper_test.go b/helper_test.go index de43ebd..104ae60 100644 --- a/helper_test.go +++ b/helper_test.go @@ -2,7 +2,9 @@ package mastodon import ( "io/ioutil" + "net/http" "os" + "strings" "testing" ) @@ -67,3 +69,21 @@ func TestString(t *testing.T) { t.Fatalf("want %q but %q", s, *sp) } } + +func TestParseAPIError(t *testing.T) { + // No api error. + r := ioutil.NopCloser(strings.NewReader(`404`)) + err := parseAPIError("bad request", &http.Response{Status: "404 Not Found", Body: r}) + want := "bad request: 404 Not Found" + if err.Error() != want { + t.Fatalf("want %q but %q", want, err.Error()) + } + + // With api error. + r = ioutil.NopCloser(strings.NewReader(`{"error":"Record not found"}`)) + err = parseAPIError("bad request", &http.Response{Status: "404 Not Found", Body: r}) + want = "bad request: 404 Not Found: Record not found" + if err.Error() != want { + t.Fatalf("want %q but %q", want, err.Error()) + } +}