Speak the ntfy JSON API directly: subscribe, publish, group topics that share a server and credentials, reconnect with backoff and a since cursor, and list account subscriptions, reservations, and ACL grants.
38 lines
944 B
Go
38 lines
944 B
Go
package ntfy
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseLine(t *testing.T) {
|
|
ev, ok, err := ParseLine([]byte(`{"id":"abc","time":10,"event":"message","topic":"alerts","message":"hi","priority":5}`))
|
|
if err != nil || !ok {
|
|
t.Fatal(err, ok)
|
|
}
|
|
if ev.ID != "abc" || ev.Priority != 5 || ev.Message != "hi" {
|
|
t.Fatalf("%+v", ev)
|
|
}
|
|
if _, ok, err := ParseLine([]byte(" ")); err != nil || ok {
|
|
t.Fatalf("blank: %v %v", err, ok)
|
|
}
|
|
if _, _, err := ParseLine([]byte("{")); err == nil {
|
|
t.Fatal("expected json error")
|
|
}
|
|
}
|
|
|
|
func TestReadLinesSkipsGarbage(t *testing.T) {
|
|
var got []string
|
|
var bad int
|
|
err := ReadLines(strings.NewReader("nope\n{\"id\":\"a\",\"time\":1,\"event\":\"open\",\"topic\":\"t\"}\n"), func(ev Event) error {
|
|
got = append(got, ev.Event)
|
|
return nil
|
|
}, func(error) { bad++ })
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if bad != 1 || len(got) != 1 || got[0] != "open" {
|
|
t.Fatalf("bad=%d got=%v", bad, got)
|
|
}
|
|
}
|