go-mastodon/helper_test.go

90 lines
4.1 KiB
Go
Raw Permalink Normal View History

2017-04-15 19:00:30 +02:00
package mastodon
import (
2017-04-20 13:39:10 +02:00
"io/ioutil"
2017-04-20 13:52:50 +02:00
"net/http"
2017-04-20 13:39:10 +02:00
"os"
2017-04-20 13:52:50 +02:00
"strings"
2017-04-15 19:00:30 +02:00
"testing"
)
2017-04-20 13:55:45 +02:00
const wantBase64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHEAAABxCAYAAADifkzQAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAXoSURBVHhe7Zxbcts4EEWtWUp2MM5fqryDLMNVWdNMObuYHdiVP2d24O/sQsNL8WbacIMPkWh0QzgpFCVGfLiPugFIpE7ngbtOaG5C4ul0mh6tI1pImpYo5b1u+DM/T9tFCU2zEilwizwJRRLPYWpO4rXZN4f3zGxS4lHyUiDTY7j+mJZNUFIgkZnuhSYkIrAWArl/byKbKKcWAlM8lVbXmcgMY9PIrbeg5rElYTIxDRhPG+uts1DiYeQapk9EkNgA5HnIhNef9XMgbJ+oCayVkbWzMbTE89s/07MLp09fp0f/YyW25kAnpERNYA4ptqjQfweR93VEhpLIErpWoETLUnCo2Eoiw41OrxE4R04u2Cy4S5xnSwk9glTuKqGDRGAtMoRELQtlkEvL5bHG6cSf40OdLlFnTRnNlcRSpTcrs0vUObKMUsKe/WEf7yb4lDkJBF2ioMRgRsvarfuX+9A+sekSBRaDmT1SczK7xIkSWbgWytkis0tMqCmQbM1Qvh4yu8QBizJ6DVLsh/P7dVmcvkxZ3CX6lDjLJBFApGVYm7pQ6lbpEg/GOgtBl7gXlFFRSmvQJTZAl7gTlE+2WvTR6VEMJZUie58YmPOPOm+8LrEB3ErUPvbKseW1LeL6A/At/SJFmvelFT+pIc2UU8hDq5WVtQQCtxIRkGuEWGYiz6+mQOC2nBK3042pjNYWCProtAHcZyJANgIXGelgIJMSIhMRKLTq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cE2DKTDn6OFaE7BP3Bhbbag37jYgLicgM2TrbcJOJT3+dxwZqiYyajW76RIijRPD47b1I9lcWfZR8E+GccC5OwqTiamCTipRQqsXpaufhWaSrgQ2ClGagxGsQa+NudLok0gLtHJCZtfrqJVxOMTyIjITryX76zrc81Uj9ovtPbGrBNxBFehUIusQZvv99WXoWCLrEGZiNvkN0d/cfoIFu0rP2f7IAAAAASUVORK5CYII="
2017-04-20 13:21:24 +02:00
func TestBase64EncodeFileName(t *testing.T) {
2017-04-20 13:39:10 +02:00
// Error in os.Open.
2017-04-20 13:21:24 +02:00
uri, err := Base64EncodeFileName("fail")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-04-20 13:39:10 +02:00
// Success.
2017-04-20 13:21:24 +02:00
uri, err = Base64EncodeFileName("testdata/logo.png")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
2017-04-20 13:55:45 +02:00
if uri != wantBase64 {
t.Fatalf("want %q but %q", wantBase64, uri)
2017-04-20 13:21:24 +02:00
}
}
2017-04-20 13:39:10 +02:00
func TestBase64Encode(t *testing.T) {
// Error in file.Stat.
uri, err := Base64Encode(nil)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
// Error in file.Read.
logo, err := os.Open("testdata/logo.png")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
_, err = ioutil.ReadAll(logo)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
uri, err = Base64Encode(logo)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
// Success.
logo, err = os.Open("testdata/logo.png")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
uri, err = Base64Encode(logo)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
2017-04-20 13:55:45 +02:00
if uri != wantBase64 {
t.Fatalf("want %q but %q", wantBase64, uri)
2017-04-20 13:39:10 +02:00
}
}
2017-04-15 19:00:30 +02:00
func TestString(t *testing.T) {
s := "test"
sp := String(s)
if *sp != s {
t.Fatalf("want %q but %q", s, *sp)
}
}
2017-04-20 13:52:50 +02:00
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())
}
}