Merge 9921b587a7
into 9faaa4f0dc
This commit is contained in:
commit
43a5afd63a
2 changed files with 123 additions and 3 deletions
20
mastodon.go
20
mastodon.go
|
@ -24,11 +24,17 @@ type Config struct {
|
||||||
AccessToken string
|
AccessToken string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type WriterResetter interface {
|
||||||
|
io.Writer
|
||||||
|
Reset()
|
||||||
|
}
|
||||||
|
|
||||||
// Client is a API client for mastodon.
|
// Client is a API client for mastodon.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
http.Client
|
http.Client
|
||||||
Config *Config
|
Config *Config
|
||||||
UserAgent string
|
UserAgent string
|
||||||
|
JSONWriter io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) doAPI(ctx context.Context, method string, uri string, params interface{}, res interface{}, pg *Pagination) error {
|
func (c *Client) doAPI(ctx context.Context, method string, uri string, params interface{}, res interface{}, pg *Pagination) error {
|
||||||
|
@ -125,7 +131,15 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
|
||||||
*pg = *pg2
|
*pg = *pg2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return json.NewDecoder(resp.Body).Decode(&res)
|
|
||||||
|
if c.JSONWriter != nil {
|
||||||
|
if resetter, ok := c.JSONWriter.(WriterResetter); ok {
|
||||||
|
resetter.Reset()
|
||||||
|
}
|
||||||
|
return json.NewDecoder(io.TeeReader(resp.Body, c.JSONWriter)).Decode(&res)
|
||||||
|
} else {
|
||||||
|
return json.NewDecoder(resp.Body).Decode(&res)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient returns a new mastodon API client.
|
// NewClient returns a new mastodon API client.
|
||||||
|
|
106
status_test.go
106
status_test.go
|
@ -1,12 +1,14 @@
|
||||||
package mastodon
|
package mastodon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -37,6 +39,110 @@ func TestGetFavourites(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetFavouritesSavedJSONTwice(t *testing.T) {
|
||||||
|
ourJSON := `[{"content": "foo"}, {"content": "bar"}]`
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintln(w, ourJSON)
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
client := NewClient(&Config{
|
||||||
|
Server: ts.URL,
|
||||||
|
ClientID: "foo",
|
||||||
|
ClientSecret: "bar",
|
||||||
|
AccessToken: "zoo",
|
||||||
|
})
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
client.JSONWriter = &buf
|
||||||
|
|
||||||
|
favs, err := client.GetFavourites(context.Background(), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("should not be fail: %v", err)
|
||||||
|
}
|
||||||
|
if len(favs) != 2 {
|
||||||
|
t.Fatalf("result should be two: %d", len(favs))
|
||||||
|
}
|
||||||
|
if favs[0].Content != "foo" {
|
||||||
|
t.Fatalf("want %q but %q", "foo", favs[0].Content)
|
||||||
|
}
|
||||||
|
if favs[1].Content != "bar" {
|
||||||
|
t.Fatalf("want %q but %q", "bar", favs[1].Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We get a trailing `\n` from the API which we need to trim
|
||||||
|
// off before we compare it with our literal above.
|
||||||
|
theirJSON := strings.TrimSpace(string(buf.Bytes()))
|
||||||
|
|
||||||
|
if theirJSON != ourJSON {
|
||||||
|
t.Fatalf("want %q but %q", ourJSON, theirJSON)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now we call the API again to see if we get the same or doubled JSON.
|
||||||
|
|
||||||
|
favs, err = client.GetFavourites(context.Background(), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("should not be fail: %v", err)
|
||||||
|
}
|
||||||
|
if len(favs) != 2 {
|
||||||
|
t.Fatalf("result should be two: %d", len(favs))
|
||||||
|
}
|
||||||
|
if favs[0].Content != "foo" {
|
||||||
|
t.Fatalf("want %q but %q", "foo", favs[0].Content)
|
||||||
|
}
|
||||||
|
if favs[1].Content != "bar" {
|
||||||
|
t.Fatalf("want %q but %q", "bar", favs[1].Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We get a trailing `\n` from the API which we need to trim
|
||||||
|
// off before we compare it with our literal above.
|
||||||
|
theirJSON = strings.TrimSpace(string(buf.Bytes()))
|
||||||
|
|
||||||
|
if theirJSON != ourJSON {
|
||||||
|
t.Fatalf("want %q but %q", ourJSON, theirJSON)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetFavouritesSavedJSON(t *testing.T) {
|
||||||
|
ourJSON := `[{"content": "foo"}, {"content": "bar"}]`
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintln(w, ourJSON)
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
client := NewClient(&Config{
|
||||||
|
Server: ts.URL,
|
||||||
|
ClientID: "foo",
|
||||||
|
ClientSecret: "bar",
|
||||||
|
AccessToken: "zoo",
|
||||||
|
})
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
client.JSONWriter = &buf
|
||||||
|
|
||||||
|
favs, err := client.GetFavourites(context.Background(), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("should not be fail: %v", err)
|
||||||
|
}
|
||||||
|
if len(favs) != 2 {
|
||||||
|
t.Fatalf("result should be two: %d", len(favs))
|
||||||
|
}
|
||||||
|
if favs[0].Content != "foo" {
|
||||||
|
t.Fatalf("want %q but %q", "foo", favs[0].Content)
|
||||||
|
}
|
||||||
|
if favs[1].Content != "bar" {
|
||||||
|
t.Fatalf("want %q but %q", "bar", favs[1].Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We get a trailing `\n` from the API which we need to trim
|
||||||
|
// off before we compare it with our literal above.
|
||||||
|
theirJSON := strings.TrimSpace(string(buf.Bytes()))
|
||||||
|
|
||||||
|
if theirJSON != ourJSON {
|
||||||
|
t.Fatalf("want %q but %q", ourJSON, theirJSON)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetBookmarks(t *testing.T) {
|
func TestGetBookmarks(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintln(w, `[{"content": "foo"}, {"content": "bar"}]`)
|
fmt.Fprintln(w, `[{"content": "foo"}, {"content": "bar"}]`)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue