go-mastodon/streaming_ws_test.go

306 lines
7.8 KiB
Go
Raw Normal View History

package mastodon
2017-04-23 10:28:31 +02:00
import (
"context"
"net/http"
"net/http/httptest"
"sync"
2017-04-23 10:28:31 +02:00
"testing"
"time"
"github.com/gorilla/websocket"
)
2017-04-30 15:16:50 +02:00
func TestStreamingWSUser(t *testing.T) {
2017-04-23 10:28:31 +02:00
ts := httptest.NewServer(http.HandlerFunc(wsMock))
defer ts.Close()
2017-04-24 06:55:07 +02:00
client := NewClient(&Config{Server: ts.URL}).NewWSClient()
2017-04-23 10:28:31 +02:00
ctx, cancel := context.WithCancel(context.Background())
2017-04-30 15:16:50 +02:00
q, err := client.StreamingWSUser(ctx)
2017-04-23 10:28:31 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
wsTest(t, q, cancel)
}
2017-04-30 15:16:50 +02:00
func TestStreamingWSPublic(t *testing.T) {
2017-04-23 10:28:31 +02:00
ts := httptest.NewServer(http.HandlerFunc(wsMock))
defer ts.Close()
2017-04-24 06:55:07 +02:00
client := NewClient(&Config{Server: ts.URL}).NewWSClient()
2017-04-23 10:28:31 +02:00
ctx, cancel := context.WithCancel(context.Background())
2017-04-30 15:16:50 +02:00
q, err := client.StreamingWSPublic(ctx, false)
2017-04-23 10:28:31 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
wsTest(t, q, cancel)
}
2017-04-23 11:42:13 +02:00
2017-04-23 10:28:31 +02:00
func TestStreamingWSHashtag(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(wsMock))
defer ts.Close()
2017-04-24 06:55:07 +02:00
client := NewClient(&Config{Server: ts.URL}).NewWSClient()
2017-04-23 10:28:31 +02:00
ctx, cancel := context.WithCancel(context.Background())
2017-04-28 07:46:19 +02:00
q, err := client.StreamingWSHashtag(ctx, "zzz", true)
2017-04-23 10:28:31 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
2017-04-28 07:46:19 +02:00
wsTest(t, q, cancel)
2017-04-23 10:28:31 +02:00
2017-04-28 07:46:19 +02:00
ctx, cancel = context.WithCancel(context.Background())
q, err = client.StreamingWSHashtag(ctx, "zzz", false)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
2017-04-23 10:28:31 +02:00
wsTest(t, q, cancel)
}
func wsMock(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/streaming" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
u := websocket.Upgrader{}
conn, err := u.Upgrade(w, r, nil)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
defer conn.Close()
err = conn.WriteMessage(websocket.TextMessage,
[]byte(`{"event":"update","payload":"{\"content\":\"foo\"}"}`))
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
err = conn.WriteMessage(websocket.TextMessage,
[]byte(`{"event":"status.update","payload":"{\"content\":\"bar\"}"}`))
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
2017-04-23 10:28:31 +02:00
err = conn.WriteMessage(websocket.TextMessage,
2017-04-23 12:47:03 +02:00
[]byte(`{"event":"notification","payload":"{\"id\":123}"}`))
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
err = conn.WriteMessage(websocket.TextMessage,
[]byte(`{"event":"delete","payload":1234567}`))
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
err = conn.WriteMessage(websocket.TextMessage,
[]byte(`{"event":"update","payload":"<html></html>"}`))
2017-04-23 10:28:31 +02:00
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
time.Sleep(10 * time.Second)
}
func wsTest(t *testing.T, q chan Event, cancel func()) {
time.AfterFunc(time.Second, func() {
cancel()
})
events := []Event{}
for e := range q {
events = append(events, e)
}
if len(events) != 7 {
t.Fatalf("result should be seven: %d", len(events))
2017-04-23 10:28:31 +02:00
}
if events[0].(*UpdateEvent).Status.Content != "foo" {
t.Fatalf("want %q but %q", "foo", events[0].(*UpdateEvent).Status.Content)
}
if events[1].(*UpdateEditEvent).Status.Content != "bar" {
t.Fatalf("want %q but %q", "bar", events[1].(*UpdateEditEvent).Status.Content)
2017-04-23 12:47:03 +02:00
}
if events[2].(*NotificationEvent).Notification.ID != "123" {
t.Fatalf("want %q but %q", "123", events[2].(*NotificationEvent).Notification.ID)
2017-04-23 10:28:31 +02:00
}
if events[3].(*DeleteEvent).ID != "1234567" {
t.Fatalf("want %q but %q", "1234567", events[3].(*DeleteEvent).ID)
2017-04-23 12:47:03 +02:00
}
2017-04-30 15:14:02 +02:00
if errorEvent, ok := events[4].(*ErrorEvent); !ok {
t.Fatalf("should be fail: %v", errorEvent.err)
}
if errorEvent, ok := events[5].(*ErrorEvent); !ok {
t.Fatalf("should be fail: %v", errorEvent.err)
}
if errorEvent, ok := events[6].(*ErrorEvent); !ok {
t.Fatalf("should be fail: %v", errorEvent.err)
}
2017-04-23 12:47:03 +02:00
}
2017-04-23 13:04:06 +02:00
func TestStreamingWS(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(wsMock))
defer ts.Close()
2017-04-24 06:55:07 +02:00
client := NewClient(&Config{Server: ":"}).NewWSClient()
2017-04-28 07:31:08 +02:00
_, err := client.StreamingWSPublic(context.Background(), true)
2017-04-23 13:04:06 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-04-24 06:55:07 +02:00
client = NewClient(&Config{Server: ts.URL}).NewWSClient()
2017-04-23 13:04:06 +02:00
ctx, cancel := context.WithCancel(context.Background())
cancel()
2017-04-28 07:31:08 +02:00
q, err := client.StreamingWSPublic(ctx, true)
2017-04-23 13:04:06 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
var wg sync.WaitGroup
wg.Add(1)
2017-04-23 13:04:06 +02:00
go func() {
defer wg.Done()
2017-04-23 13:04:06 +02:00
e := <-q
if errorEvent, ok := e.(*ErrorEvent); !ok {
t.Errorf("should be fail: %v", errorEvent.err)
2017-04-23 13:04:06 +02:00
}
}()
wg.Wait()
2017-04-23 13:04:06 +02:00
}
2017-04-23 12:47:03 +02:00
func TestHandleWS(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u := websocket.Upgrader{}
conn, err := u.Upgrade(w, r, nil)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
defer conn.Close()
err = conn.WriteMessage(websocket.TextMessage,
[]byte(`<html></html>`))
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
time.Sleep(10 * time.Second)
}))
defer ts.Close()
q := make(chan Event)
2017-04-24 06:55:07 +02:00
client := NewClient(&Config{}).NewWSClient()
2017-04-23 12:47:03 +02:00
var wg sync.WaitGroup
wg.Add(1)
2017-04-23 12:47:03 +02:00
go func() {
defer wg.Done()
2017-04-23 12:47:03 +02:00
e := <-q
if errorEvent, ok := e.(*ErrorEvent); !ok {
t.Errorf("should be fail: %v", errorEvent.err)
2017-04-23 12:47:03 +02:00
}
}()
err := client.handleWS(context.Background(), ":", q)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
wg.Add(1)
2017-04-23 12:47:03 +02:00
go func() {
defer wg.Done()
2017-04-23 12:47:03 +02:00
e := <-q
if errorEvent, ok := e.(*ErrorEvent); !ok {
t.Errorf("should be fail: %v", errorEvent.err)
2017-04-23 12:47:03 +02:00
}
}()
err = client.handleWS(ctx, "ws://"+ts.Listener.Addr().String(), q)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
wg.Add(1)
2017-04-23 12:47:03 +02:00
go func() {
defer wg.Done()
2017-04-23 12:47:03 +02:00
e := <-q
if errorEvent, ok := e.(*ErrorEvent); !ok {
t.Errorf("should be fail: %v", errorEvent.err)
2017-04-23 12:47:03 +02:00
}
}()
client.handleWS(context.Background(), "ws://"+ts.Listener.Addr().String(), q)
wg.Wait()
2017-04-23 10:28:31 +02:00
}
2017-04-23 11:46:41 +02:00
func TestDialRedirect(t *testing.T) {
2017-04-24 06:55:07 +02:00
client := NewClient(&Config{}).NewWSClient()
2017-04-23 11:46:41 +02:00
_, err := client.dialRedirect(":")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
}
2017-04-23 11:42:13 +02:00
func TestDial(t *testing.T) {
canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if canErr {
canErr = false
http.Redirect(w, r, ":", http.StatusMovedPermanently)
return
}
http.Redirect(w, r, "http://www.example.com/", http.StatusMovedPermanently)
}))
defer ts.Close()
2017-04-24 06:55:07 +02:00
client := NewClient(&Config{}).NewWSClient()
2017-04-23 11:42:13 +02:00
_, _, err := client.dial(":")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
_, _, err = client.dial("ws://" + ts.Listener.Addr().String())
2017-04-23 11:42:13 +02:00
if err == nil {
2017-04-24 10:25:56 +02:00
t.Fatalf("should be fail: %v", err)
2017-04-23 11:42:13 +02:00
}
_, rawurl, err := client.dial("ws://" + ts.Listener.Addr().String())
2017-04-23 11:42:13 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if rawurl != "ws://www.example.com/" {
t.Fatalf("want %q but %q", "ws://www.example.com/", rawurl)
}
}
2017-04-23 10:28:31 +02:00
func TestChangeWebSocketScheme(t *testing.T) {
_, err := changeWebSocketScheme(":")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
u, err := changeWebSocketScheme("http://example.com/")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if u.Scheme != "ws" {
t.Fatalf("want %q but %q", "ws", u.Scheme)
}
u, err = changeWebSocketScheme("https://example.com/")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if u.Scheme != "wss" {
t.Fatalf("want %q but %q", "wss", u.Scheme)
}
}