separate test file
parent
9e5ec3b4dc
commit
9f84e175f1
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue