Add TestParseAPIError

pull/33/head
178inaba 2017-04-20 20:52:50 +09:00
parent 5d0b16b367
commit 5714594344
1 changed files with 20 additions and 0 deletions

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())
}
}