Merge pull request #33 from 178inaba/helper_test

Add helper test
pull/37/head
mattn 2017-04-21 00:19:34 +09:00 committed by GitHub
commit fc6bfd56fa
1 changed files with 76 additions and 0 deletions

View File

@ -1,9 +1,67 @@
package mastodon
import (
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
)
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+mQOC2nBK3042pjNYWCProtAHcZyJANgIXGelgIJMSIhMRKLTqUwmHAkEvp2txKhCEKKcSllZgVl4dCwThJBIpExwmVAhL8SgQhJUoOUTogjzgN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RPD47b1I9lcWfZR8E+GccC5OwqTiamCTipRQqsXpaufhWaSrgQ2ClGagxGsQa+NudLok0gLtHJCZtfrqJVxOMTyIjITryX76zrc81Uj9ovtPbGrBNxBFehUIusQZvv99WXoWCLrEGZiNvkN0d/cfoIFu0rP2f7IAAAAASUVORK5CYII="
func TestBase64EncodeFileName(t *testing.T) {
// Error in os.Open.
uri, err := Base64EncodeFileName("fail")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
// Success.
uri, err = Base64EncodeFileName("testdata/logo.png")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if uri != wantBase64 {
t.Fatalf("want %q but %q", wantBase64, uri)
}
}
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)
}
if uri != wantBase64 {
t.Fatalf("want %q but %q", wantBase64, uri)
}
}
func TestString(t *testing.T) {
s := "test"
sp := String(s)
@ -11,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())
}
}