From 5d0b16b367e1f0525d6d074164ad09be0cdfc33e Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Thu, 20 Apr 2017 20:39:10 +0900 Subject: [PATCH] Add TestBase64Encode --- helper_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/helper_test.go b/helper_test.go index 84b2b19..de43ebd 100644 --- a/helper_test.go +++ b/helper_test.go @@ -1,17 +1,21 @@ package mastodon import ( + "io/ioutil" + "os" "testing" ) const wantURI = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHEAAABxCAYAAADifkzQAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAXoSURBVHhe7Zxbcts4EEWtWUp2MM5fqryDLMNVWdNMObuYHdiVP2d24O/sQsNL8WbacIMPkWh0QzgpFCVGfLiPugFIpE7ngbtOaG5C4ul0mh6tI1pImpYo5b1u+DM/T9tFCU2zEilwizwJRRLPYWpO4rXZN4f3zGxS4lHyUiDTY7j+mJZNUFIgkZnuhSYkIrAWArl/byKbKKcWAlM8lVbXmcgMY9PIrbeg5rElYTIxDRhPG+uts1DiYeQapk9EkNgA5HnIhNef9XMgbJ+oCayVkbWzMbTE89s/07MLp09fp0f/YyW25kAnpERNYA4ptqjQfweR93VEhpLIErpWoETLUnCo2Eoiw41OrxE4R04u2Cy4S5xnSwk9glTuKqGDRGAtMoRELQtlkEvL5bHG6cSf40OdLlFnTRnNlcRSpTcrs0vUObKMUsKe/WEf7yb4lDkJBF2ioMRgRsvarfuX+9A+sekSBRaDmT1SczK7xIkSWbgWytkis0tMqCmQbM1Qvh4yu8QBizJ6DVLsh/P7dVmcvkxZ3CX6lDjLJBFApGVYm7pQ6lbpEg/GOgtBl7gXlFFRSmvQJTZAl7gTlE+2WvTR6VEMJZUie58YmPOPOm+8LrEB3ErUPvbKseW1LeL6A/At/SJFmvelFT+pIc2UU8hDq5WVtQQCt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PNKByn4lLWUiBEmzDrJRSc5nK18jjaPv1SmiJc4FmJsn/5zopEwKv2b8nwkrcE2DKTDn6OFaE7BP3Bhbbag37jYgLicgM2TrbcJOJT3+dxwZqiYyajW76RIijRPD47b1I9lcWfZR8E+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) @@ -21,6 +25,41 @@ func TestBase64EncodeFileName(t *testing.T) { } } +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 != wantURI { + t.Fatalf("want %q but %q", wantURI, uri) + } +} + func TestString(t *testing.T) { s := "test" sp := String(s)