go-mastodon/streaming_test.go

63 lines
1.3 KiB
Go

package mastodon
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)
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, false)
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)
}
}