This commit is contained in:
Yasuhiro Matsumoto 2017-04-14 18:53:14 +09:00
parent dc7a04a5b5
commit 0eccbb0514

View File

@ -1,11 +1,13 @@
package mastodon
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestAuthenticate(t *testing.T) {
@ -113,7 +115,7 @@ func TestGetTimelineHome(t *testing.T) {
t.Fatalf("want %q but %q", "foo", tl[0].Content)
}
if tl[1].Content != "bar" {
t.Fatalf("want %q but %q", "bar", tl[0].Content)
t.Fatalf("want %q but %q", "bar", tl[1].Content)
}
}
@ -224,3 +226,55 @@ func TestRegisterApp(t *testing.T) {
t.Fatalf("want %q but %q", "bar", app.ClientSecret)
}
}
func TestStreamingPublic(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/streaming/public" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
f, _ := w.(http.Flusher)
fmt.Fprintln(w, `
event: update
data: {"Content": "foo"}
`)
f.Flush()
fmt.Fprintln(w, `
event: update
data: {"Content": "bar"}
`)
f.Flush()
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
ctx, cancel := context.WithCancel(context.Background())
q, err := client.StreamingPublic(ctx)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
time.AfterFunc(3*time.Second, func() {
cancel()
close(q)
})
events := []Event{}
for e := range q {
events = append(events, e)
}
if len(events) != 2 {
t.Fatalf("result should be two: %d", len(events))
}
if events[0].(*UpdateEvent).Status.Content != "foo" {
t.Fatalf("want %q but %q", "foo", events[0].(*UpdateEvent).Status.Content)
}
if events[1].(*UpdateEvent).Status.Content != "bar" {
t.Fatalf("want %q but %q", "bar", events[1].(*UpdateEvent).Status.Content)
}
}