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

@ -31,10 +31,10 @@ var (
noQuotesRegex = regexp.MustCompile(`^[-_./:@a-zA-Z0-9]+$`)
)
// Errors for ReadJSON and ReadJSONWithLimit functions
// Errors for UnmarshalJSON and UnmarshalJSONWithLimit functions
var (
ErrInvalidJSON = errors.New("invalid JSON")
ErrTooLargeJSON = errors.New("too large JSON")
ErrUnmarshalJSON = errors.New("unmarshalling JSON failed")
ErrTooLargeJSON = errors.New("too large JSON")
)
// FileExists checks if a file exists, and returns true if it does
@ -295,17 +295,17 @@ func QuoteCommand(command []string) string {
return strings.Join(quoted, " ")
}
// ReadJSON reads the given io.ReadCloser into a struct
func ReadJSON[T any](body io.ReadCloser) (*T, error) {
// UnmarshalJSON reads the given io.ReadCloser into a struct
func UnmarshalJSON[T any](body io.ReadCloser) (*T, error) {
var obj T
if err := json.NewDecoder(body).Decode(&obj); err != nil {
return nil, ErrInvalidJSON
return nil, ErrUnmarshalJSON
}
return &obj, nil
}
// ReadJSONWithLimit reads the given io.ReadCloser into a struct, but only until limit is reached
func ReadJSONWithLimit[T any](r io.ReadCloser, limit int) (*T, error) {
// UnmarshalJSONWithLimit reads the given io.ReadCloser into a struct, but only until limit is reached
func UnmarshalJSONWithLimit[T any](r io.ReadCloser, limit int) (*T, error) {
defer r.Close()
p, err := Peek(r, limit)
if err != nil {
@ -315,7 +315,7 @@ func ReadJSONWithLimit[T any](r io.ReadCloser, limit int) (*T, error) {
}
var obj T
if err := json.NewDecoder(p).Decode(&obj); err != nil {
return nil, ErrInvalidJSON
return nil, ErrUnmarshalJSON
}
return &obj, nil
}

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)
}