Add TestHandleReader
parent
28598eb3ab
commit
c66c3aaa22
|
@ -5,10 +5,55 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestHandleReader(t *testing.T) {
|
||||||
|
q := make(chan Event)
|
||||||
|
r := strings.NewReader(`
|
||||||
|
event: update
|
||||||
|
data: {"content": "foo"}
|
||||||
|
event: notification
|
||||||
|
data: {"type": "mention"}
|
||||||
|
event: delete
|
||||||
|
data: 1234567
|
||||||
|
:thump
|
||||||
|
`)
|
||||||
|
go func() {
|
||||||
|
defer close(q)
|
||||||
|
err := handleReader(context.Background(), q, r)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("should not be fail: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
var passUpdate, passNotification, passDelete bool
|
||||||
|
for e := range q {
|
||||||
|
switch event := e.(type) {
|
||||||
|
case *UpdateEvent:
|
||||||
|
passUpdate = true
|
||||||
|
if event.Status.Content != "foo" {
|
||||||
|
t.Fatalf("want %q but %q", "foo", event.Status.Content)
|
||||||
|
}
|
||||||
|
case *NotificationEvent:
|
||||||
|
passNotification = true
|
||||||
|
if event.Notification.Type != "mention" {
|
||||||
|
t.Fatalf("want %q but %q", "mention", event.Notification.Type)
|
||||||
|
}
|
||||||
|
case *DeleteEvent:
|
||||||
|
passDelete = true
|
||||||
|
if event.ID != 1234567 {
|
||||||
|
t.Fatalf("want %d but %d", 1234567, event.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !passUpdate || !passNotification || !passDelete {
|
||||||
|
t.Fatalf("have not passed through somewhere: update %t, notification %t, delete %t",
|
||||||
|
passUpdate, passNotification, passDelete)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestStreamingPublic(t *testing.T) {
|
func TestStreamingPublic(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path != "/api/v1/streaming/public" {
|
if r.URL.Path != "/api/v1/streaming/public" {
|
||||||
|
|
Loading…
Reference in New Issue