Add helper

This commit is contained in:
178inaba 2017-04-16 02:00:30 +09:00
parent 62318331a3
commit e62f1adaae
3 changed files with 49 additions and 2 deletions

View File

@ -34,8 +34,6 @@ func TestAccountUpdate(t *testing.T) {
}
}
func String(v string) *string { return &v }
func TestGetBlocks(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`)

36
helper.go Normal file
View File

@ -0,0 +1,36 @@
package mastodon
import (
"encoding/base64"
"net/http"
"os"
)
func Base64EncodeFileName(filename string) (string, error) {
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
return Base64Encode(file)
}
func Base64Encode(file *os.File) (string, error) {
fi, err := file.Stat()
if err != nil {
return "", err
}
d := make([]byte, fi.Size())
_, err = file.Read(d)
if err != nil {
return "", err
}
return "data:" + http.DetectContentType(d) +
";base64," + base64.StdEncoding.EncodeToString(d), nil
}
// String is a helper function to get the pointer value of a string.
func String(v string) *string { return &v }

13
helper_test.go Normal file
View File

@ -0,0 +1,13 @@
package mastodon
import (
"testing"
)
func TestString(t *testing.T) {
s := "test"
sp := String(s)
if *sp != s {
t.Fatalf("want %q but %q", s, *sp)
}
}