1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-12-29 13:12:36 +01:00
ntfy/client/client_test.go

111 lines
3.1 KiB
Go
Raw Normal View History

2021-12-22 14:17:50 +01:00
package client_test
import (
2021-12-22 23:20:43 +01:00
"fmt"
2021-12-22 14:17:50 +01:00
"github.com/stretchr/testify/require"
"heckel.io/ntfy/client"
2021-12-22 23:20:43 +01:00
"heckel.io/ntfy/test"
2021-12-22 14:17:50 +01:00
"testing"
"time"
)
2021-12-22 23:20:43 +01: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 14:17:50 +01:00
2021-12-22 23:20:43 +01:00
subscriptionID := c.Subscribe("mytopic")
time.Sleep(time.Second)
2021-12-22 14:17:50 +01:00
2021-12-22 23:20:43 +01:00
msg, err := c.Publish("mytopic", "some message")
2021-12-22 14:17:50 +01:00
require.Nil(t, err)
2021-12-22 23:20:43 +01: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-25 00:57:02 +01:00
time.Sleep(200 * time.Millisecond)
2021-12-22 23:20:43 +01: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 14:17:50 +01:00
}
2021-12-22 23:45:19 +01: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 23:20:43 +01:00
func newTestConfig(port int) *client.Config {
2021-12-22 14:17:50 +01:00
c := client.NewConfig()
2021-12-22 23:20:43 +01:00
c.DefaultHost = fmt.Sprintf("http://127.0.0.1:%d", port)
2021-12-22 14:17:50 +01:00
return c
}
2021-12-22 23:20:43 +01:00
func nextMessage(c *client.Client) *client.Message {
select {
case m := <-c.Messages:
return m
default:
return nil
2021-12-22 14:17:50 +01:00
}
}