diff --git a/mastodon_test.go b/mastodon_test.go index 9c2f2ac..4815d5f 100644 --- a/mastodon_test.go +++ b/mastodon_test.go @@ -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) + } +}