client_test.go 3.1 KB

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