go-mastodon/streaming_test.go

385 lines
9.3 KiB
Go
Raw Permalink Normal View History

2017-04-16 13:05:42 +02:00
package mastodon
import (
"bufio"
2017-04-16 13:05:42 +02:00
"context"
"fmt"
"net/http"
"net/http/httptest"
2017-04-28 20:42:45 +02:00
"strings"
2017-04-16 13:05:42 +02:00
"testing"
"time"
)
2017-04-28 20:42:45 +02:00
func TestHandleReader(t *testing.T) {
large := "large"
largeContent := strings.Repeat(large, 2*(bufio.MaxScanTokenSize/len(large)))
2017-04-28 20:42:45 +02:00
q := make(chan Event)
r := strings.NewReader(fmt.Sprintf(`
2017-04-28 20:42:45 +02:00
event: update
2017-04-29 20:19:58 +02:00
data: {content: error}
event: update
2017-04-28 20:42:45 +02:00
data: {"content": "foo"}
event: update
data: {"content": "%s"}
2017-04-28 20:42:45 +02:00
event: notification
data: {"type": "mention"}
event: delete
data: 1234567
:thump
`, largeContent))
2017-04-28 20:42:45 +02:00
go func() {
defer close(q)
2017-04-29 20:12:55 +02:00
err := handleReader(q, r)
2017-04-28 20:42:45 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
}()
var passUpdate, passUpdateLarge, passNotification, passDelete, passError bool
2017-04-28 20:42:45 +02:00
for e := range q {
switch event := e.(type) {
case *UpdateEvent:
if event.Status.Content == "foo" {
passUpdate = true
} else if event.Status.Content == largeContent {
passUpdateLarge = true
} else {
t.Fatalf("bad update content: %q", event.Status.Content)
2017-04-28 20:42:45 +02:00
}
case *NotificationEvent:
passNotification = true
if event.Notification.Type != "mention" {
t.Fatalf("want %q but %q", "mention", event.Notification.Type)
}
case *DeleteEvent:
passDelete = true
2017-10-25 08:22:17 +02:00
if event.ID != "1234567" {
t.Fatalf("want %q but %q", "1234567", event.ID)
2017-04-28 20:42:45 +02:00
}
2017-04-29 20:19:58 +02:00
case *ErrorEvent:
passError = true
if event.err == nil {
t.Fatalf("should be fail: %v", event.err)
}
2017-04-28 20:42:45 +02:00
}
}
if !passUpdate || !passUpdateLarge || !passNotification || !passDelete || !passError {
2017-04-29 20:19:58 +02:00
t.Fatalf("have not passed through somewhere: "+
"update: %t, update (large): %t, notification: %t, delete: %t, error: %t",
passUpdate, passUpdateLarge, passNotification, passDelete, passError)
2017-04-28 20:42:45 +02:00
}
}
2017-04-29 20:12:55 +02:00
func TestStreaming(t *testing.T) {
2017-04-29 21:30:04 +02:00
var isEnd bool
2017-04-29 20:12:55 +02:00
canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2017-04-29 21:30:04 +02:00
if isEnd {
return
} else if canErr {
2017-04-29 20:12:55 +02:00
canErr = false
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
f := w.(http.Flusher)
fmt.Fprintln(w, `
event: update
data: {"content": "foo"}
`)
f.Flush()
2017-04-29 21:30:04 +02:00
isEnd = true
2017-04-29 20:12:55 +02:00
}))
defer ts.Close()
c := NewClient(&Config{Server: ":"})
_, err := c.streaming(context.Background(), "", nil)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
c = NewClient(&Config{Server: ts.URL})
ctx, cancel := context.WithCancel(context.Background())
2017-04-29 21:30:04 +02:00
time.AfterFunc(time.Second, cancel)
2017-04-29 20:12:55 +02:00
q, err := c.streaming(ctx, "", nil)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
2017-04-29 21:30:04 +02:00
var cnt int
2017-04-29 20:12:55 +02:00
var passError, passUpdate bool
for e := range q {
switch event := e.(type) {
case *ErrorEvent:
passError = true
if event.err == nil {
t.Fatalf("should be fail: %v", event.err)
}
case *UpdateEvent:
2017-04-29 21:30:04 +02:00
cnt++
2017-04-29 20:12:55 +02:00
passUpdate = true
if event.Status.Content != "foo" {
t.Fatalf("want %q but %q", "foo", event.Status.Content)
}
}
}
2017-04-29 21:30:04 +02:00
if cnt != 1 {
t.Fatalf("result should be one: %d", cnt)
}
2017-04-29 20:12:55 +02:00
if !passError || !passUpdate {
t.Fatalf("have not passed through somewhere: error %t, update %t", passError, passUpdate)
}
}
2017-04-29 21:30:04 +02:00
func TestDoStreaming(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.(http.Flusher).Flush()
time.Sleep(time.Second)
}))
defer ts.Close()
c := NewClient(&Config{Server: ts.URL})
req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(time.Millisecond, cancel)
req = req.WithContext(ctx)
q := make(chan Event)
go func() {
defer close(q)
c.doStreaming(req, q)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
}()
var passError bool
for e := range q {
if event, ok := e.(*ErrorEvent); ok {
passError = true
if event.err == nil {
t.Fatalf("should be fail: %v", event.err)
}
}
}
if !passError {
t.Fatalf("have not passed through: error %t", passError)
}
}
func TestStreamingUser(t *testing.T) {
2017-04-29 21:41:56 +02:00
var isEnd bool
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if isEnd {
return
2017-04-29 21:53:13 +02:00
} else if r.URL.Path != "/api/v1/streaming/user" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
2017-04-29 21:41:56 +02:00
}
f, _ := w.(http.Flusher)
fmt.Fprintln(w, `
event: update
data: {"content": "foo"}
`)
f.Flush()
isEnd = true
}))
defer ts.Close()
c := NewClient(&Config{Server: ts.URL})
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(time.Second, cancel)
q, err := c.StreamingUser(ctx)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
events := []Event{}
for e := range q {
if _, ok := e.(*ErrorEvent); !ok {
events = append(events, e)
}
}
if len(events) != 1 {
t.Fatalf("result should be one: %d", len(events))
}
if events[0].(*UpdateEvent).Status.Content != "foo" {
t.Fatalf("want %q but %q", "foo", events[0].(*UpdateEvent).Status.Content)
}
2017-04-29 21:30:04 +02:00
}
2017-04-16 13:05:42 +02:00
func TestStreamingPublic(t *testing.T) {
2017-04-29 20:12:55 +02:00
var isEnd bool
2017-04-16 13:05:42 +02:00
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2017-04-29 20:12:55 +02:00
if isEnd {
return
2017-04-29 21:53:13 +02:00
} else if r.URL.Path != "/api/v1/streaming/public/local" {
2017-04-16 13:05:42 +02:00
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
f, _ := w.(http.Flusher)
fmt.Fprintln(w, `
event: update
2017-04-20 12:14:24 +02:00
data: {"content": "foo"}
2017-04-16 13:05:42 +02:00
`)
f.Flush()
fmt.Fprintln(w, `
event: update
2017-04-20 12:14:24 +02:00
data: {"content": "bar"}
2017-04-16 13:05:42 +02:00
`)
f.Flush()
2017-04-29 20:12:55 +02:00
isEnd = true
2017-04-16 13:05:42 +02:00
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
ctx, cancel := context.WithCancel(context.Background())
2017-04-29 21:53:13 +02:00
q, err := client.StreamingPublic(ctx, true)
2017-04-16 13:05:42 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
2017-04-29 21:30:04 +02:00
time.AfterFunc(time.Second, cancel)
2017-04-16 13:05:42 +02:00
events := []Event{}
for e := range q {
2017-04-29 20:12:55 +02:00
if _, ok := e.(*ErrorEvent); !ok {
events = append(events, e)
}
2017-04-16 13:05:42 +02:00
}
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)
}
}
2017-04-29 21:30:04 +02:00
func TestStreamingHashtag(t *testing.T) {
2017-04-29 21:53:13 +02:00
var isEnd bool
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if isEnd {
return
} else if r.URL.Path != "/api/v1/streaming/hashtag/local" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
f, _ := w.(http.Flusher)
fmt.Fprintln(w, `
event: update
data: {"content": "foo"}
`)
f.Flush()
isEnd = true
}))
defer ts.Close()
client := NewClient(&Config{Server: ts.URL})
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(time.Second, cancel)
q, err := client.StreamingHashtag(ctx, "hashtag", true)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
events := []Event{}
for e := range q {
if _, ok := e.(*ErrorEvent); !ok {
events = append(events, e)
}
}
if len(events) != 1 {
t.Fatalf("result should be one: %d", len(events))
}
if events[0].(*UpdateEvent).Status.Content != "foo" {
t.Fatalf("want %q but %q", "foo", events[0].(*UpdateEvent).Status.Content)
}
2017-04-29 21:30:04 +02:00
}
2019-05-12 16:32:17 +02:00
func TestStreamingList(t *testing.T) {
var isEnd bool
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if isEnd {
return
} else if r.URL.Path != "/api/v1/streaming/list" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
f, _ := w.(http.Flusher)
fmt.Fprintln(w, `
event: update
data: {"content": "foo"}
`)
f.Flush()
isEnd = true
}))
defer ts.Close()
client := NewClient(&Config{Server: ts.URL})
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(time.Second, cancel)
q, err := client.StreamingList(ctx, "1")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
events := []Event{}
for e := range q {
if _, ok := e.(*ErrorEvent); !ok {
events = append(events, e)
}
}
if len(events) != 1 {
t.Fatalf("result should be one: %d", len(events))
}
if events[0].(*UpdateEvent).Status.Content != "foo" {
t.Fatalf("want %q but %q", "foo", events[0].(*UpdateEvent).Status.Content)
}
}
2021-02-25 15:26:45 +01:00
func TestStreamingDirect(t *testing.T) {
var isEnd bool
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if isEnd {
return
} else if r.URL.Path != "/api/v1/streaming/direct" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
f, _ := w.(http.Flusher)
fmt.Fprintln(w, `
event: update
data: {"content": "foo"}
`)
f.Flush()
isEnd = true
}))
defer ts.Close()
client := NewClient(&Config{Server: ts.URL})
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(time.Second, cancel)
q, err := client.StreamingDirect(ctx)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
events := []Event{}
for e := range q {
if _, ok := e.(*ErrorEvent); !ok {
events = append(events, e)
}
}
if len(events) != 1 {
t.Fatalf("result should be one: %d", len(events))
}
if events[0].(*UpdateEvent).Status.Content != "foo" {
t.Fatalf("want %q but %q", "foo", events[0].(*UpdateEvent).Status.Content)
}
}