2017-04-20 03:39:20 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mattn/go-mastodon"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCmdStream(t *testing.T) {
|
|
|
|
var ts *httptest.Server
|
|
|
|
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path != "/api/v1/streaming/public/local" {
|
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f, _ := w.(http.Flusher)
|
|
|
|
fmt.Fprintln(w, `
|
|
|
|
event: update
|
|
|
|
data: {"content": "foo", "account":{"acct":"FOO"}}
|
|
|
|
`)
|
|
|
|
f.Flush()
|
|
|
|
|
|
|
|
fmt.Fprintln(w, `
|
|
|
|
event: update
|
|
|
|
data: {"content": "bar", "account":{"acct":"BAR"}}
|
|
|
|
`)
|
|
|
|
f.Flush()
|
|
|
|
return
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
config := &mastodon.Config{
|
|
|
|
Server: ts.URL,
|
|
|
|
ClientID: "foo",
|
|
|
|
ClientSecret: "bar",
|
|
|
|
AccessToken: "zoo",
|
|
|
|
}
|
|
|
|
client := mastodon.NewClient(config)
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
app := makeApp()
|
|
|
|
app.Writer = &buf
|
|
|
|
app.Metadata = map[string]interface{}{
|
|
|
|
"client": client,
|
|
|
|
"config": config,
|
|
|
|
}
|
|
|
|
|
2017-04-20 17:57:28 +02:00
|
|
|
stop := func() {
|
2017-04-20 03:39:20 +02:00
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
if sig, ok := app.Metadata["signal"]; ok {
|
|
|
|
sig.(chan os.Signal) <- os.Interrupt
|
|
|
|
return
|
|
|
|
}
|
|
|
|
panic("timeout")
|
2017-04-20 17:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var out string
|
2017-04-20 03:39:20 +02:00
|
|
|
|
2017-04-20 17:57:28 +02:00
|
|
|
go stop()
|
2017-04-20 03:39:20 +02:00
|
|
|
app.Run([]string{"mstdn", "stream"})
|
2017-04-20 17:57:28 +02:00
|
|
|
out = buf.String()
|
|
|
|
if !strings.Contains(out, "FOO@") {
|
|
|
|
t.Fatalf("%q should be contained in output of command: %v", "FOO@", out)
|
|
|
|
}
|
|
|
|
if !strings.Contains(out, "foo") {
|
|
|
|
t.Fatalf("%q should be contained in output of command: %v", "foo", out)
|
|
|
|
}
|
2017-04-20 03:39:20 +02:00
|
|
|
|
2017-04-20 17:57:28 +02:00
|
|
|
go stop()
|
|
|
|
app.Run([]string{"mstdn", "stream", "--simplejson"})
|
|
|
|
out = buf.String()
|
2017-04-20 03:39:20 +02:00
|
|
|
if !strings.Contains(out, "FOO@") {
|
|
|
|
t.Fatalf("%q should be contained in output of command: %v", "FOO@", out)
|
|
|
|
}
|
|
|
|
if !strings.Contains(out, "foo") {
|
|
|
|
t.Fatalf("%q should be contained in output of command: %v", "foo", out)
|
|
|
|
}
|
|
|
|
}
|