Do not print ugly WS error; tests
This commit is contained in:
parent
846ee0fb2d
commit
707c58a120
9 changed files with 204 additions and 65 deletions
|
@ -5,8 +5,6 @@ import (
|
|||
"encoding/json"
|
||||
"github.com/urfave/cli/v2"
|
||||
"heckel.io/ntfy/client"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
@ -15,7 +13,7 @@ import (
|
|||
// This only contains helpers so far
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
log.SetOutput(io.Discard)
|
||||
// log.SetOutput(io.Discard)
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
|
|
|
@ -34,3 +34,39 @@ func TestCLI_Publish_Subscribe_Poll(t *testing.T) {
|
|||
m = toMessage(t, stdout.String())
|
||||
require.Equal(t, "some message", m.Message)
|
||||
}
|
||||
|
||||
func TestCLI_Publish_All_The_Things(t *testing.T) {
|
||||
s, port := test.StartServer(t)
|
||||
defer test.StopServer(t, s, port)
|
||||
topic := fmt.Sprintf("http://127.0.0.1:%d/mytopic", port)
|
||||
|
||||
app, _, stdout, _ := newTestApp()
|
||||
require.Nil(t, app.Run([]string{
|
||||
"ntfy", "publish",
|
||||
"--title", "this is a title",
|
||||
"--priority", "high",
|
||||
"--tags", "tag1,tag2",
|
||||
// No --delay, --email
|
||||
"--click", "https://ntfy.sh",
|
||||
"--attach", "https://f-droid.org/F-Droid.apk",
|
||||
"--filename", "fdroid.apk",
|
||||
"--no-cache",
|
||||
"--no-firebase",
|
||||
topic,
|
||||
"some message",
|
||||
}))
|
||||
m := toMessage(t, stdout.String())
|
||||
require.Equal(t, "message", m.Event)
|
||||
require.Equal(t, "mytopic", m.Topic)
|
||||
require.Equal(t, "some message", m.Message)
|
||||
require.Equal(t, "this is a title", m.Title)
|
||||
require.Equal(t, 4, m.Priority)
|
||||
require.Equal(t, []string{"tag1", "tag2"}, m.Tags)
|
||||
require.Equal(t, "https://ntfy.sh", m.Click)
|
||||
require.Equal(t, "https://f-droid.org/F-Droid.apk", m.Attachment.URL)
|
||||
require.Equal(t, "fdroid.apk", m.Attachment.Name)
|
||||
require.Equal(t, int64(0), m.Attachment.Size)
|
||||
require.Equal(t, "", m.Attachment.Owner)
|
||||
require.Equal(t, int64(0), m.Attachment.Expires)
|
||||
require.Equal(t, "", m.Attachment.Type)
|
||||
}
|
||||
|
|
68
cmd/serve_test.go
Normal file
68
cmd/serve_test.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/stretchr/testify/require"
|
||||
"heckel.io/ntfy/client"
|
||||
"heckel.io/ntfy/test"
|
||||
"heckel.io/ntfy/util"
|
||||
"math/rand"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UnixMilli())
|
||||
}
|
||||
|
||||
func TestCLI_Serve_Unix_Curl(t *testing.T) {
|
||||
sockFile := filepath.Join(t.TempDir(), "ntfy.sock")
|
||||
go func() {
|
||||
app, _, _, _ := newTestApp()
|
||||
err := app.Run([]string{"ntfy", "serve", "--listen-http=-", "--listen-unix=" + sockFile})
|
||||
require.Nil(t, err)
|
||||
}()
|
||||
for i := 0; i < 40 && !util.FileExists(sockFile); i++ {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
require.True(t, util.FileExists(sockFile))
|
||||
|
||||
cmd := exec.Command("curl", "-s", "--unix-socket", sockFile, "-d", "this is a message", "localhost/mytopic")
|
||||
out, err := cmd.Output()
|
||||
require.Nil(t, err)
|
||||
m := toMessage(t, string(out))
|
||||
require.Equal(t, "this is a message", m.Message)
|
||||
}
|
||||
|
||||
func TestCLI_Serve_WebSocket(t *testing.T) {
|
||||
port := 10000 + rand.Intn(20000)
|
||||
go func() {
|
||||
app, _, _, _ := newTestApp()
|
||||
err := app.Run([]string{"ntfy", "serve", fmt.Sprintf("--listen-http=:%d", port)})
|
||||
require.Nil(t, err)
|
||||
}()
|
||||
test.WaitForPortUp(t, port)
|
||||
|
||||
ws, _, err := websocket.DefaultDialer.Dial(fmt.Sprintf("ws://127.0.0.1:%d/mytopic/ws", port), nil)
|
||||
require.Nil(t, err)
|
||||
|
||||
messageType, data, err := ws.ReadMessage()
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, websocket.TextMessage, messageType)
|
||||
require.Equal(t, "open", toMessage(t, string(data)).Event)
|
||||
|
||||
c := client.New(client.NewConfig())
|
||||
_, err = c.Publish(fmt.Sprintf("http://127.0.0.1:%d/mytopic", port), "my message")
|
||||
require.Nil(t, err)
|
||||
|
||||
messageType, data, err = ws.ReadMessage()
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, websocket.TextMessage, messageType)
|
||||
|
||||
m := toMessage(t, string(data))
|
||||
require.Equal(t, "my message", m.Message)
|
||||
require.Equal(t, "mytopic", m.Topic)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue