Add TestParseAPIError
parent
5d0b16b367
commit
5714594344
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue