Add TestParseAPIError

This commit is contained in:
178inaba 2017-04-20 20:52:50 +09:00
parent 5d0b16b367
commit 5714594344

View File

@ -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(`<html><head><title>404</title></head></html>`))
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())
}
}