Add TestParseAPIError
This commit is contained in:
parent
5d0b16b367
commit
5714594344
|
@ -2,7 +2,9 @@ package mastodon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -67,3 +69,21 @@ func TestString(t *testing.T) {
|
||||||
t.Fatalf("want %q but %q", s, *sp)
|
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
Block a user