From c1e9c80d7d65ad795c0806aa0a537686030bd4be Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Fri, 28 Apr 2017 14:31:08 +0900 Subject: [PATCH] Fix streaming_ws local --- streaming_ws.go | 32 ++++++++++++++++---------------- streaming_ws_test.go | 36 ++++-------------------------------- 2 files changed, 20 insertions(+), 48 deletions(-) diff --git a/streaming_ws.go b/streaming_ws.go index 915465f..022928c 100644 --- a/streaming_ws.go +++ b/streaming_ws.go @@ -24,29 +24,29 @@ type Stream struct { Payload interface{} `json:"payload"` } -// StreamingWSPublic return channel to read events on public using WebSocket. -func (c *WSClient) StreamingWSPublic(ctx context.Context) (chan Event, error) { - return c.streamingWS(ctx, "public", "") -} - -// StreamingWSPublicLocal return channel to read events on public local using WebSocket. -func (c *WSClient) StreamingWSPublicLocal(ctx context.Context) (chan Event, error) { - return c.streamingWS(ctx, "public:local", "") -} - // StreamingWSUser return channel to read events on home using WebSocket. func (c *WSClient) StreamingWSUser(ctx context.Context) (chan Event, error) { return c.streamingWS(ctx, "user", "") } -// StreamingWSHashtag return channel to read events on tagged timeline using WebSocket. -func (c *WSClient) StreamingWSHashtag(ctx context.Context, tag string) (chan Event, error) { - return c.streamingWS(ctx, "hashtag", tag) +// StreamingWSPublic return channel to read events on public using WebSocket. +func (c *WSClient) StreamingWSPublic(ctx context.Context, isLocal bool) (chan Event, error) { + s := "public" + if isLocal { + s += ":local" + } + + return c.streamingWS(ctx, s, "") } -// StreamingWSHashtagLocal return channel to read events on tagged local timeline using WebSocket. -func (c *WSClient) StreamingWSHashtagLocal(ctx context.Context, tag string) (chan Event, error) { - return c.streamingWS(ctx, "hashtag:local", tag) +// StreamingWSHashtag return channel to read events on tagged timeline using WebSocket. +func (c *WSClient) StreamingWSHashtag(ctx context.Context, tag string, isLocal bool) (chan Event, error) { + s := "hashtag" + if isLocal { + s += ":local" + } + + return c.streamingWS(ctx, s, tag) } func (c *WSClient) streamingWS(ctx context.Context, stream, tag string) (chan Event, error) { diff --git a/streaming_ws_test.go b/streaming_ws_test.go index 5456a5d..e82bd48 100644 --- a/streaming_ws_test.go +++ b/streaming_ws_test.go @@ -16,21 +16,7 @@ func TestStreamingWSPublic(t *testing.T) { client := NewClient(&Config{Server: ts.URL}).NewWSClient() ctx, cancel := context.WithCancel(context.Background()) - q, err := client.StreamingWSPublic(ctx) - if err != nil { - t.Fatalf("should not be fail: %v", err) - } - - wsTest(t, q, cancel) -} - -func TestStreamingWSPublicLocal(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(wsMock)) - defer ts.Close() - - client := NewClient(&Config{Server: ts.URL}).NewWSClient() - ctx, cancel := context.WithCancel(context.Background()) - q, err := client.StreamingWSPublicLocal(ctx) + q, err := client.StreamingWSPublic(ctx, false) if err != nil { t.Fatalf("should not be fail: %v", err) } @@ -58,21 +44,7 @@ func TestStreamingWSHashtag(t *testing.T) { client := NewClient(&Config{Server: ts.URL}).NewWSClient() ctx, cancel := context.WithCancel(context.Background()) - q, err := client.StreamingWSHashtag(ctx, "zzz") - if err != nil { - t.Fatalf("should not be fail: %v", err) - } - - wsTest(t, q, cancel) -} - -func TestStreamingWSHashtagLocal(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(wsMock)) - defer ts.Close() - - client := NewClient(&Config{Server: ts.URL}).NewWSClient() - ctx, cancel := context.WithCancel(context.Background()) - q, err := client.StreamingWSHashtagLocal(ctx, "zzz") + q, err := client.StreamingWSHashtag(ctx, "zzz", false) if err != nil { t.Fatalf("should not be fail: %v", err) } @@ -156,7 +128,7 @@ func TestStreamingWS(t *testing.T) { defer ts.Close() client := NewClient(&Config{Server: ":"}).NewWSClient() - _, err := client.StreamingWSPublicLocal(context.Background()) + _, err := client.StreamingWSPublic(context.Background(), true) if err == nil { t.Fatalf("should be fail: %v", err) } @@ -164,7 +136,7 @@ func TestStreamingWS(t *testing.T) { client = NewClient(&Config{Server: ts.URL}).NewWSClient() ctx, cancel := context.WithCancel(context.Background()) cancel() - q, err := client.StreamingWSPublicLocal(ctx) + q, err := client.StreamingWSPublic(ctx, true) if err != nil { t.Fatalf("should not be fail: %v", err) }