2021-12-22 13:17:50 +00:00
|
|
|
package client_test
|
|
|
|
|
|
|
|
import (
|
2021-12-22 22:20:43 +00:00
|
|
|
"fmt"
|
2021-12-22 13:17:50 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"heckel.io/ntfy/client"
|
2023-02-06 04:34:27 +00:00
|
|
|
"heckel.io/ntfy/log"
|
2021-12-22 22:20:43 +00:00
|
|
|
"heckel.io/ntfy/test"
|
2023-02-06 04:34:27 +00:00
|
|
|
"os"
|
2021-12-22 13:17:50 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-02-06 04:34:27 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
log.SetLevel(log.ErrorLevel)
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2021-12-22 22:20:43 +00:00
|
|
|
func TestClient_Publish_Subscribe(t *testing.T) {
|
|
|
|
s, port := test.StartServer(t)
|
|
|
|
defer test.StopServer(t, s, port)
|
|
|
|
c := client.New(newTestConfig(port))
|
2021-12-22 13:17:50 +00:00
|
|
|
|
2021-12-22 22:20:43 +00:00
|
|
|
subscriptionID := c.Subscribe("mytopic")
|
|
|
|
time.Sleep(time.Second)
|
2021-12-22 13:17:50 +00:00
|
|
|
|
2021-12-22 22:20:43 +00:00
|
|
|
msg, err := c.Publish("mytopic", "some message")
|
2021-12-22 13:17:50 +00:00
|
|
|
require.Nil(t, err)
|
2021-12-22 22:20:43 +00:00
|
|
|
require.Equal(t, "some message", msg.Message)
|
|
|
|
|
|
|
|
msg, err = c.Publish("mytopic", "some other message",
|
|
|
|
client.WithTitle("some title"),
|
|
|
|
client.WithPriority("high"),
|
|
|
|
client.WithTags([]string{"tag1", "tag 2"}))
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "some other message", msg.Message)
|
|
|
|
require.Equal(t, "some title", msg.Title)
|
|
|
|
require.Equal(t, []string{"tag1", "tag 2"}, msg.Tags)
|
|
|
|
require.Equal(t, 4, msg.Priority)
|
|
|
|
|
|
|
|
msg, err = c.Publish("mytopic", "some delayed message",
|
|
|
|
client.WithDelay("25 hours"))
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "some delayed message", msg.Message)
|
|
|
|
require.True(t, time.Now().Add(24*time.Hour).Unix() < msg.Time)
|
|
|
|
|
2021-12-24 23:57:02 +00:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
2021-12-22 22:20:43 +00:00
|
|
|
msg = nextMessage(c)
|
|
|
|
require.NotNil(t, msg)
|
|
|
|
require.Equal(t, "some message", msg.Message)
|
|
|
|
|
|
|
|
msg = nextMessage(c)
|
|
|
|
require.NotNil(t, msg)
|
|
|
|
require.Equal(t, "some other message", msg.Message)
|
|
|
|
require.Equal(t, "some title", msg.Title)
|
|
|
|
require.Equal(t, []string{"tag1", "tag 2"}, msg.Tags)
|
|
|
|
require.Equal(t, 4, msg.Priority)
|
|
|
|
|
|
|
|
msg = nextMessage(c)
|
|
|
|
require.Nil(t, msg)
|
|
|
|
|
|
|
|
c.Unsubscribe(subscriptionID)
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
|
|
|
msg, err = c.Publish("mytopic", "a message that won't be received")
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "a message that won't be received", msg.Message)
|
|
|
|
|
|
|
|
msg = nextMessage(c)
|
|
|
|
require.Nil(t, msg)
|
2021-12-22 13:17:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 22:45:19 +00:00
|
|
|
func TestClient_Publish_Poll(t *testing.T) {
|
|
|
|
s, port := test.StartServer(t)
|
|
|
|
defer test.StopServer(t, s, port)
|
|
|
|
c := client.New(newTestConfig(port))
|
|
|
|
|
|
|
|
msg, err := c.Publish("mytopic", "some message", client.WithNoFirebase(), client.WithTagsList("tag1,tag2"))
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "some message", msg.Message)
|
|
|
|
require.Equal(t, []string{"tag1", "tag2"}, msg.Tags)
|
|
|
|
|
|
|
|
msg, err = c.Publish("mytopic", "this won't be cached", client.WithNoCache())
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "this won't be cached", msg.Message)
|
|
|
|
|
|
|
|
msg, err = c.Publish("mytopic", "some delayed message", client.WithDelay("20 min"))
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "some delayed message", msg.Message)
|
|
|
|
|
|
|
|
messages, err := c.Poll("mytopic")
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, 1, len(messages))
|
|
|
|
require.Equal(t, "some message", messages[0].Message)
|
|
|
|
|
|
|
|
messages, err = c.Poll("mytopic", client.WithScheduled())
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, 2, len(messages))
|
|
|
|
require.Equal(t, "some message", messages[0].Message)
|
|
|
|
require.Equal(t, "some delayed message", messages[1].Message)
|
|
|
|
}
|
|
|
|
|
2021-12-22 22:20:43 +00:00
|
|
|
func newTestConfig(port int) *client.Config {
|
2021-12-22 13:17:50 +00:00
|
|
|
c := client.NewConfig()
|
2021-12-22 22:20:43 +00:00
|
|
|
c.DefaultHost = fmt.Sprintf("http://127.0.0.1:%d", port)
|
2021-12-22 13:17:50 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-12-22 22:20:43 +00:00
|
|
|
func nextMessage(c *client.Client) *client.Message {
|
|
|
|
select {
|
|
|
|
case m := <-c.Messages:
|
|
|
|
return m
|
|
|
|
default:
|
|
|
|
return nil
|
2021-12-22 13:17:50 +00:00
|
|
|
}
|
|
|
|
}
|