2017-04-14 10:10:20 +02:00
|
|
|
package mastodon
|
|
|
|
|
|
|
|
import (
|
2017-04-17 04:10:29 +02:00
|
|
|
"context"
|
2017-04-14 10:10:20 +02:00
|
|
|
"fmt"
|
2017-04-14 10:30:27 +02:00
|
|
|
"io"
|
2017-04-14 10:10:20 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-05-04 16:35:05 +02:00
|
|
|
"net/url"
|
2017-04-14 10:10:20 +02:00
|
|
|
"testing"
|
2017-04-19 11:26:12 +02:00
|
|
|
"time"
|
2017-04-14 10:10:20 +02:00
|
|
|
)
|
|
|
|
|
2017-05-04 16:35:05 +02:00
|
|
|
func TestDoAPI(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
q := r.URL.Query()
|
|
|
|
if q.Get("max_id") == "123" && q.Get("since_id") == "789" && q.Get("limit") == "10" {
|
|
|
|
w.Header().Set("Link", `<http://example.com?max_id=234>; rel="next", <http://example.com?since_id=890>; rel="prev"`)
|
|
|
|
fmt.Fprintln(w, `[{"username": "foo"}, {"username": "bar"}]`)
|
|
|
|
}
|
|
|
|
w.Header().Set("Link", `<:>; rel="next"`)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
c := NewClient(&Config{Server: ts.URL})
|
|
|
|
_, err := c.doAPI(context.Background(), http.MethodGet, "/", nil, nil, &Pagination{
|
|
|
|
MaxID: Int64(999),
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should be fail: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var accounts []Account
|
|
|
|
pg, err := c.doAPI(context.Background(), http.MethodGet, "/", url.Values{}, &accounts, &Pagination{
|
|
|
|
MaxID: Int64(123),
|
|
|
|
SinceID: Int64(789),
|
|
|
|
Limit: Int64(10),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not be fail: %v", err)
|
|
|
|
}
|
|
|
|
if *pg.MaxID != 234 {
|
|
|
|
t.Fatalf("want %d but %d", 234, *pg.MaxID)
|
|
|
|
}
|
|
|
|
if *pg.SinceID != 890 {
|
|
|
|
t.Fatalf("want %d but %d", 890, *pg.SinceID)
|
|
|
|
}
|
|
|
|
if accounts[0].Username != "foo" {
|
|
|
|
t.Fatalf("want %q but %q", "foo", accounts[0].Username)
|
|
|
|
}
|
|
|
|
if accounts[1].Username != "bar" {
|
|
|
|
t.Fatalf("want %q but %q", "bar", accounts[1].Username)
|
|
|
|
}
|
|
|
|
|
|
|
|
pg, err = c.doAPI(context.Background(), http.MethodGet, "/", nil, &accounts, &Pagination{
|
|
|
|
MaxID: Int64(123),
|
|
|
|
SinceID: Int64(789),
|
|
|
|
Limit: Int64(10),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not be fail: %v", err)
|
|
|
|
}
|
|
|
|
if *pg.MaxID != 234 {
|
|
|
|
t.Fatalf("want %d but %d", 234, *pg.MaxID)
|
|
|
|
}
|
|
|
|
if *pg.SinceID != 890 {
|
|
|
|
t.Fatalf("want %d but %d", 890, *pg.SinceID)
|
|
|
|
}
|
|
|
|
if accounts[0].Username != "foo" {
|
|
|
|
t.Fatalf("want %q but %q", "foo", accounts[0].Username)
|
|
|
|
}
|
|
|
|
if accounts[1].Username != "bar" {
|
|
|
|
t.Fatalf("want %q but %q", "bar", accounts[1].Username)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 10:10:20 +02:00
|
|
|
func TestAuthenticate(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.FormValue("username") != "valid" || r.FormValue("password") != "user" {
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2017-04-20 12:14:24 +02:00
|
|
|
fmt.Fprintln(w, `{"access_token": "zoo"}`)
|
2017-04-14 10:10:20 +02:00
|
|
|
return
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := NewClient(&Config{
|
|
|
|
Server: ts.URL,
|
|
|
|
ClientID: "foo",
|
|
|
|
ClientSecret: "bar",
|
|
|
|
})
|
2017-04-17 04:10:29 +02:00
|
|
|
err := client.Authenticate(context.Background(), "invalid", "user")
|
2017-04-14 10:10:20 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should be fail: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client = NewClient(&Config{
|
|
|
|
Server: ts.URL,
|
|
|
|
ClientID: "foo",
|
|
|
|
ClientSecret: "bar",
|
|
|
|
})
|
2017-04-17 04:10:29 +02:00
|
|
|
err = client.Authenticate(context.Background(), "valid", "user")
|
2017-04-14 10:10:20 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not be fail: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 11:26:12 +02:00
|
|
|
func TestAuthenticateWithCancel(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
return
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := NewClient(&Config{
|
|
|
|
Server: ts.URL,
|
|
|
|
ClientID: "foo",
|
|
|
|
ClientSecret: "bar",
|
|
|
|
})
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-04-19 12:12:09 +02:00
|
|
|
cancel()
|
2017-04-19 11:26:12 +02:00
|
|
|
err := client.Authenticate(ctx, "invalid", "user")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should be fail: %v", err)
|
|
|
|
}
|
|
|
|
if want := "Post " + ts.URL + "/oauth/token: context canceled"; want != err.Error() {
|
|
|
|
t.Fatalf("want %q but %q", want, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 10:10:20 +02:00
|
|
|
func TestPostStatus(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Header.Get("Authorization") != "Bearer zoo" {
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2017-04-20 12:14:24 +02:00
|
|
|
fmt.Fprintln(w, `{"access_token": "zoo"}`)
|
2017-04-14 10:10:20 +02:00
|
|
|
return
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := NewClient(&Config{
|
|
|
|
Server: ts.URL,
|
|
|
|
ClientID: "foo",
|
|
|
|
ClientSecret: "bar",
|
|
|
|
})
|
2017-04-17 04:10:29 +02:00
|
|
|
_, err := client.PostStatus(context.Background(), &Toot{
|
2017-04-14 10:10:20 +02:00
|
|
|
Status: "foobar",
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should be fail: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client = NewClient(&Config{
|
|
|
|
Server: ts.URL,
|
|
|
|
ClientID: "foo",
|
|
|
|
ClientSecret: "bar",
|
|
|
|
AccessToken: "zoo",
|
|
|
|
})
|
2017-04-17 04:10:29 +02:00
|
|
|
_, err = client.PostStatus(context.Background(), &Toot{
|
2017-04-14 10:10:20 +02:00
|
|
|
Status: "foobar",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not be fail: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2017-04-14 10:21:31 +02:00
|
|
|
|
2017-04-19 11:26:12 +02:00
|
|
|
func TestPostStatusWithCancel(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
return
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := NewClient(&Config{
|
|
|
|
Server: ts.URL,
|
|
|
|
ClientID: "foo",
|
|
|
|
ClientSecret: "bar",
|
|
|
|
})
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-04-19 12:12:09 +02:00
|
|
|
cancel()
|
2017-04-19 11:26:12 +02:00
|
|
|
_, err := client.PostStatus(ctx, &Toot{
|
|
|
|
Status: "foobar",
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should be fail: %v", err)
|
|
|
|
}
|
|
|
|
if want := "Post " + ts.URL + "/api/v1/statuses: context canceled"; want != err.Error() {
|
|
|
|
t.Fatalf("want %q but %q", want, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 10:26:22 +02:00
|
|
|
func TestGetTimelineHome(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-04-20 12:14:24 +02:00
|
|
|
fmt.Fprintln(w, `[{"content": "foo"}, {"content": "bar"}]`)
|
2017-04-14 10:26:22 +02:00
|
|
|
return
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := NewClient(&Config{
|
|
|
|
Server: ts.URL,
|
|
|
|
ClientID: "foo",
|
|
|
|
ClientSecret: "bar",
|
|
|
|
})
|
2017-04-17 04:10:29 +02:00
|
|
|
_, err := client.PostStatus(context.Background(), &Toot{
|
2017-04-14 10:26:22 +02:00
|
|
|
Status: "foobar",
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should be fail: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client = NewClient(&Config{
|
|
|
|
Server: ts.URL,
|
|
|
|
ClientID: "foo",
|
|
|
|
ClientSecret: "bar",
|
|
|
|
AccessToken: "zoo",
|
|
|
|
})
|
2017-05-04 14:56:54 +02:00
|
|
|
tl, _, err := client.GetTimelineHome(context.Background(), nil)
|
2017-04-14 10:26:22 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not be fail: %v", err)
|
|
|
|
}
|
|
|
|
if len(tl) != 2 {
|
|
|
|
t.Fatalf("result should be two: %d", len(tl))
|
|
|
|
}
|
|
|
|
if tl[0].Content != "foo" {
|
|
|
|
t.Fatalf("want %q but %q", "foo", tl[0].Content)
|
|
|
|
}
|
|
|
|
if tl[1].Content != "bar" {
|
2017-04-14 11:53:14 +02:00
|
|
|
t.Fatalf("want %q but %q", "bar", tl[1].Content)
|
2017-04-14 10:26:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 11:26:12 +02:00
|
|
|
func TestGetTimelineHomeWithCancel(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
return
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := NewClient(&Config{
|
|
|
|
Server: ts.URL,
|
|
|
|
ClientID: "foo",
|
|
|
|
ClientSecret: "bar",
|
|
|
|
AccessToken: "zoo",
|
|
|
|
})
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-04-19 12:12:09 +02:00
|
|
|
cancel()
|
2017-05-04 14:56:54 +02:00
|
|
|
_, _, err := client.GetTimelineHome(ctx, nil)
|
2017-04-19 11:26:12 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should be fail: %v", err)
|
|
|
|
}
|
|
|
|
if want := "Get " + ts.URL + "/api/v1/timelines/home: context canceled"; want != err.Error() {
|
|
|
|
t.Fatalf("want %q but %q", want, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 10:21:31 +02:00
|
|
|
func TestForTheCoverages(t *testing.T) {
|
|
|
|
(*UpdateEvent)(nil).event()
|
|
|
|
(*NotificationEvent)(nil).event()
|
|
|
|
(*DeleteEvent)(nil).event()
|
|
|
|
(*ErrorEvent)(nil).event()
|
2017-04-14 12:20:23 +02:00
|
|
|
_ = (&ErrorEvent{io.EOF}).Error()
|
2017-04-14 10:21:31 +02:00
|
|
|
}
|
2017-05-04 16:35:05 +02:00
|
|
|
|
|
|
|
func TestNewPagination(t *testing.T) {
|
|
|
|
_, err := newPagination("")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should be fail: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = newPagination(`<:>; rel="next"`)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should be fail: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = newPagination(`<:>; rel="prev"`)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should be fail: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pg, err := newPagination(`<http://example.com?max_id=123>; rel="next", <http://example.com?since_id=789>; rel="prev"`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not be fail: %v", err)
|
|
|
|
}
|
|
|
|
if *pg.MaxID != 123 {
|
|
|
|
t.Fatalf("want %d but %d", 123, *pg.MaxID)
|
|
|
|
}
|
|
|
|
if *pg.SinceID != 789 {
|
|
|
|
t.Fatalf("want %d but %d", 789, *pg.SinceID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPaginationID(t *testing.T) {
|
|
|
|
_, err := getPaginationID(":", "max_id")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should be fail: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = getPaginationID("http://example.com?max_id=abc", "max_id")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should be fail: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := getPaginationID("http://example.com?max_id=123", "max_id")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not be fail: %v", err)
|
|
|
|
}
|
|
|
|
if id != 123 {
|
|
|
|
t.Fatalf("want %d but %d", 123, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPaginationSetValues(t *testing.T) {
|
|
|
|
p := &Pagination{
|
|
|
|
MaxID: Int64(123),
|
|
|
|
SinceID: Int64(789),
|
|
|
|
Limit: Int64(10),
|
|
|
|
}
|
|
|
|
before := url.Values{"key": {"value"}}
|
|
|
|
after := p.setValues(before)
|
|
|
|
if after.Get("key") != "value" {
|
|
|
|
t.Fatalf("want %q but %q", "value", after.Get("key"))
|
|
|
|
}
|
|
|
|
if after.Get("max_id") != "123" {
|
|
|
|
t.Fatalf("want %q but %q", "123", after.Get("max_id"))
|
|
|
|
}
|
|
|
|
if after.Get("since_id") != "789" {
|
|
|
|
t.Fatalf("want %q but %q", "789", after.Get("since_id"))
|
|
|
|
}
|
|
|
|
if after.Get("limit") != "10" {
|
|
|
|
t.Fatalf("want %q but %q", "10", after.Get("limit"))
|
|
|
|
}
|
|
|
|
}
|