serve_test.go 2.7 KB

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