serve_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package cmd
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "testing"
  9. "time"
  10. "github.com/gorilla/websocket"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. "heckel.io/ntfy/client"
  14. "heckel.io/ntfy/test"
  15. "heckel.io/ntfy/util"
  16. )
  17. func init() {
  18. rand.Seed(time.Now().UnixMilli())
  19. }
  20. func TestCLI_Serve_Unix_Curl(t *testing.T) {
  21. sockFile := filepath.Join(t.TempDir(), "ntfy.sock")
  22. configFile := newEmptyFile(t) // Avoid issues with existing server.yml file on system
  23. go func() {
  24. app, _, _, _ := newTestApp()
  25. err := app.Run([]string{"ntfy", "serve", "--config=" + configFile, "--listen-http=-", "--listen-unix=" + sockFile})
  26. require.Nil(t, err)
  27. }()
  28. for i := 0; i < 40 && !util.FileExists(sockFile); i++ {
  29. time.Sleep(50 * time.Millisecond)
  30. }
  31. require.True(t, util.FileExists(sockFile))
  32. cmd := exec.Command("curl", "-s", "--unix-socket", sockFile, "-d", "this is a message", "localhost/mytopic")
  33. out, err := cmd.Output()
  34. require.Nil(t, err)
  35. m := toMessage(t, string(out))
  36. require.Equal(t, "this is a message", m.Message)
  37. }
  38. func TestCLI_Serve_WebSocket(t *testing.T) {
  39. port := 10000 + rand.Intn(20000)
  40. go func() {
  41. configFile := newEmptyFile(t) // Avoid issues with existing server.yml file on system
  42. app, _, _, _ := newTestApp()
  43. err := app.Run([]string{"ntfy", "serve", "--config=" + configFile, fmt.Sprintf("--listen-http=:%d", port)})
  44. require.Nil(t, err)
  45. }()
  46. test.WaitForPortUp(t, port)
  47. ws, _, err := websocket.DefaultDialer.Dial(fmt.Sprintf("ws://127.0.0.1:%d/mytopic/ws", port), nil)
  48. require.Nil(t, err)
  49. messageType, data, err := ws.ReadMessage()
  50. require.Nil(t, err)
  51. require.Equal(t, websocket.TextMessage, messageType)
  52. require.Equal(t, "open", toMessage(t, string(data)).Event)
  53. c := client.New(client.NewConfig())
  54. _, err = c.Publish(fmt.Sprintf("http://127.0.0.1:%d/mytopic", port), "my message")
  55. require.Nil(t, err)
  56. messageType, data, err = ws.ReadMessage()
  57. require.Nil(t, err)
  58. require.Equal(t, websocket.TextMessage, messageType)
  59. m := toMessage(t, string(data))
  60. require.Equal(t, "my message", m.Message)
  61. require.Equal(t, "mytopic", m.Topic)
  62. }
  63. func TestIP_Host_Parsing(t *testing.T) {
  64. cases := map[string]string{
  65. "1.1.1.1": "1.1.1.1/32",
  66. "fd00::1234": "fd00::1234/128",
  67. "192.168.0.3/24": "192.168.0.0/24",
  68. "10.1.2.3/8": "10.0.0.0/8",
  69. "201:be93::4a6/21": "201:b800::/21",
  70. }
  71. for q, expectedAnswer := range cases {
  72. ips, err := parseIPHostPrefix(q)
  73. require.Nil(t, err)
  74. assert.Equal(t, 1, len(ips))
  75. assert.Equal(t, expectedAnswer, ips[0].String())
  76. }
  77. }
  78. func newEmptyFile(t *testing.T) string {
  79. filename := filepath.Join(t.TempDir(), "empty")
  80. require.Nil(t, os.WriteFile(filename, []byte{}, 0600))
  81. return filename
  82. }