1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2025-05-17 20:44:31 +02:00

Some tests

This commit is contained in:
binwiederhier 2022-12-31 16:08:49 -05:00
parent 0bb3c84b9e
commit 598d0bdda3
4 changed files with 61 additions and 22 deletions

View file

@ -1,9 +1,11 @@
package util
import (
"io"
"net/netip"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
@ -161,3 +163,40 @@ func TestQuoteCommand(t *testing.T) {
require.Equal(t, `rsync -av /home/phil/ root@example.com:/home/phil/`, QuoteCommand([]string{"rsync", "-av", "/home/phil/", "root@example.com:/home/phil/"}))
require.Equal(t, `/home/sweet/home "Äöü this is a test" "\a\b"`, QuoteCommand([]string{"/home/sweet/home", "Äöü this is a test", "\\a\\b"}))
}
func TestBasicAuth(t *testing.T) {
require.Equal(t, "Basic cGhpbDpwaGls", BasicAuth("phil", "phil"))
}
func TestBearerAuth(t *testing.T) {
require.Equal(t, "Bearer sometoken", BearerAuth("sometoken"))
}
type testJSON struct {
Name string `json:"name"`
Something int `json:"something"`
}
func TestReadJSON_Success(t *testing.T) {
v, err := UnmarshalJSON[testJSON](io.NopCloser(strings.NewReader(`{"name":"some name","something":99}`)))
require.Nil(t, err)
require.Equal(t, "some name", v.Name)
require.Equal(t, 99, v.Something)
}
func TestReadJSON_Failure(t *testing.T) {
_, err := UnmarshalJSON[testJSON](io.NopCloser(strings.NewReader(`{"na`)))
require.Equal(t, ErrUnmarshalJSON, err)
}
func TestReadJSONWithLimit_Success(t *testing.T) {
v, err := UnmarshalJSONWithLimit[testJSON](io.NopCloser(strings.NewReader(`{"name":"some name","something":99}`)), 100)
require.Nil(t, err)
require.Equal(t, "some name", v.Name)
require.Equal(t, 99, v.Something)
}
func TestReadJSONWithLimit_FailureTooLong(t *testing.T) {
_, err := UnmarshalJSONWithLimit[testJSON](io.NopCloser(strings.NewReader(`{"name":"some name","something":99}`)), 10)
require.Equal(t, ErrTooLargeJSON, err)
}