From 9f84e175f15c714d59a109e6332ae3d92580b793 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Sun, 16 Apr 2017 20:05:42 +0900 Subject: [PATCH] separate test file --- mastodon_test.go | 54 ----------------------------------------- streaming_test.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 54 deletions(-) create mode 100644 streaming_test.go diff --git a/mastodon_test.go b/mastodon_test.go index bc020f2..c5bbf58 100644 --- a/mastodon_test.go +++ b/mastodon_test.go @@ -1,13 +1,11 @@ package mastodon import ( - "context" "fmt" "io" "net/http" "net/http/httptest" "testing" - "time" ) func TestAuthenticate(t *testing.T) { @@ -226,55 +224,3 @@ 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) - } -} diff --git a/streaming_test.go b/streaming_test.go new file mode 100644 index 0000000..7227c12 --- /dev/null +++ b/streaming_test.go @@ -0,0 +1,62 @@ +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) + 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) + } +}