ntfy/server/util_test.go

65 lines
2.4 KiB
Go
Raw Normal View History

2022-01-16 07:17:46 +03:00
package server
import (
"github.com/stretchr/testify/require"
2022-02-01 16:47:01 +03:00
"net/http"
2022-01-16 07:17:46 +03:00
"testing"
)
2022-02-01 16:47:01 +03:00
func TestReadBoolParam(t *testing.T) {
r, _ := http.NewRequest("GET", "https://ntfy.sh/mytopic?up=1&firebase=no", nil)
up := readBoolParam(r, false, "x-up", "up")
firebase := readBoolParam(r, true, "x-firebase", "firebase")
require.Equal(t, true, up)
require.Equal(t, false, firebase)
2022-01-16 07:17:46 +03:00
2022-02-01 16:47:01 +03:00
r, _ = http.NewRequest("GET", "https://ntfy.sh/mytopic", nil)
r.Header.Set("X-Up", "yes")
r.Header.Set("X-Firebase", "0")
up = readBoolParam(r, false, "x-up", "up")
firebase = readBoolParam(r, true, "x-firebase", "firebase")
require.Equal(t, true, up)
require.Equal(t, false, firebase)
2022-01-16 07:17:46 +03:00
2022-02-01 16:47:01 +03:00
r, _ = http.NewRequest("GET", "https://ntfy.sh/mytopic", nil)
up = readBoolParam(r, false, "x-up", "up")
firebase = readBoolParam(r, true, "x-up", "up")
require.Equal(t, false, up)
require.Equal(t, true, firebase)
2022-01-16 07:17:46 +03:00
}
2022-04-19 16:14:32 +03:00
func TestParseActions(t *testing.T) {
actions, err := parseActions("[]")
require.Nil(t, err)
require.Empty(t, actions)
actions, err = parseActions("action=http, label=Open door, url=https://door.lan/open; view, Show portal, https://door.lan")
require.Nil(t, err)
require.Equal(t, 2, len(actions))
require.Equal(t, "http", actions[0].Action)
require.Equal(t, "Open door", actions[0].Label)
require.Equal(t, "https://door.lan/open", actions[0].URL)
require.Equal(t, "view", actions[1].Action)
require.Equal(t, "Show portal", actions[1].Label)
require.Equal(t, "https://door.lan", actions[1].URL)
actions, err = parseActions(`[{"action":"http","label":"Open door","url":"https://door.lan/open"}, {"action":"view","label":"Show portal","url":"https://door.lan"}]`)
require.Nil(t, err)
require.Equal(t, 2, len(actions))
require.Equal(t, "http", actions[0].Action)
require.Equal(t, "Open door", actions[0].Label)
require.Equal(t, "https://door.lan/open", actions[0].URL)
require.Equal(t, "view", actions[1].Action)
require.Equal(t, "Show portal", actions[1].Label)
require.Equal(t, "https://door.lan", actions[1].URL)
actions, err = parseActions("action=http, label=Open door, url=https://door.lan/open, body=this is a body, method=PUT")
require.Nil(t, err)
require.Equal(t, 1, len(actions))
require.Equal(t, "http", actions[0].Action)
require.Equal(t, "Open door", actions[0].Label)
require.Equal(t, "https://door.lan/open", actions[0].URL)
require.Equal(t, "PUT", actions[0].Method)
require.Equal(t, "this is a body", actions[0].Body)
}