client_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package client_test
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/require"
  5. "heckel.io/ntfy/client"
  6. "heckel.io/ntfy/log"
  7. "heckel.io/ntfy/test"
  8. "os"
  9. "testing"
  10. "time"
  11. )
  12. func TestMain(m *testing.M) {
  13. log.SetLevel(log.ErrorLevel)
  14. os.Exit(m.Run())
  15. }
  16. func TestClient_Publish_Subscribe(t *testing.T) {
  17. s, port := test.StartServer(t)
  18. defer test.StopServer(t, s, port)
  19. c := client.New(newTestConfig(port))
  20. subscriptionID, _ := c.Subscribe("mytopic")
  21. time.Sleep(time.Second)
  22. msg, err := c.Publish("mytopic", "some message")
  23. require.Nil(t, err)
  24. require.Equal(t, "some message", msg.Message)
  25. msg, err = c.Publish("mytopic", "some other message",
  26. client.WithTitle("some title"),
  27. client.WithPriority("high"),
  28. client.WithTags([]string{"tag1", "tag 2"}))
  29. require.Nil(t, err)
  30. require.Equal(t, "some other message", msg.Message)
  31. require.Equal(t, "some title", msg.Title)
  32. require.Equal(t, []string{"tag1", "tag 2"}, msg.Tags)
  33. require.Equal(t, 4, msg.Priority)
  34. msg, err = c.Publish("mytopic", "some delayed message",
  35. client.WithDelay("25 hours"))
  36. require.Nil(t, err)
  37. require.Equal(t, "some delayed message", msg.Message)
  38. require.True(t, time.Now().Add(24*time.Hour).Unix() < msg.Time)
  39. time.Sleep(200 * time.Millisecond)
  40. msg = nextMessage(c)
  41. require.NotNil(t, msg)
  42. require.Equal(t, "some message", msg.Message)
  43. msg = nextMessage(c)
  44. require.NotNil(t, msg)
  45. require.Equal(t, "some other message", msg.Message)
  46. require.Equal(t, "some title", msg.Title)
  47. require.Equal(t, []string{"tag1", "tag 2"}, msg.Tags)
  48. require.Equal(t, 4, msg.Priority)
  49. msg = nextMessage(c)
  50. require.Nil(t, msg)
  51. c.Unsubscribe(subscriptionID)
  52. time.Sleep(200 * time.Millisecond)
  53. msg, err = c.Publish("mytopic", "a message that won't be received")
  54. require.Nil(t, err)
  55. require.Equal(t, "a message that won't be received", msg.Message)
  56. msg = nextMessage(c)
  57. require.Nil(t, msg)
  58. }
  59. func TestClient_Publish_Poll(t *testing.T) {
  60. s, port := test.StartServer(t)
  61. defer test.StopServer(t, s, port)
  62. c := client.New(newTestConfig(port))
  63. msg, err := c.Publish("mytopic", "some message", client.WithNoFirebase(), client.WithTagsList("tag1,tag2"))
  64. require.Nil(t, err)
  65. require.Equal(t, "some message", msg.Message)
  66. require.Equal(t, []string{"tag1", "tag2"}, msg.Tags)
  67. msg, err = c.Publish("mytopic", "this won't be cached", client.WithNoCache())
  68. require.Nil(t, err)
  69. require.Equal(t, "this won't be cached", msg.Message)
  70. msg, err = c.Publish("mytopic", "some delayed message", client.WithDelay("20 min"))
  71. require.Nil(t, err)
  72. require.Equal(t, "some delayed message", msg.Message)
  73. messages, err := c.Poll("mytopic")
  74. require.Nil(t, err)
  75. require.Equal(t, 1, len(messages))
  76. require.Equal(t, "some message", messages[0].Message)
  77. messages, err = c.Poll("mytopic", client.WithScheduled())
  78. require.Nil(t, err)
  79. require.Equal(t, 2, len(messages))
  80. require.Equal(t, "some message", messages[0].Message)
  81. require.Equal(t, "some delayed message", messages[1].Message)
  82. }
  83. func newTestConfig(port int) *client.Config {
  84. c := client.NewConfig()
  85. c.DefaultHost = fmt.Sprintf("http://127.0.0.1:%d", port)
  86. return c
  87. }
  88. func nextMessage(c *client.Client) *client.Message {
  89. select {
  90. case m := <-c.Messages:
  91. return m
  92. default:
  93. return nil
  94. }
  95. }