update
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
This commit is contained in:
172
utils/req/req.go
Normal file
172
utils/req/req.go
Normal file
@@ -0,0 +1,172 @@
|
||||
package req
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/navidrome/navidrome/log"
|
||||
)
|
||||
|
||||
type Values struct {
|
||||
*http.Request
|
||||
}
|
||||
|
||||
func Params(r *http.Request) *Values {
|
||||
return &Values{r}
|
||||
}
|
||||
|
||||
var (
|
||||
ErrMissingParam = errors.New("missing parameter")
|
||||
ErrInvalidParam = errors.New("invalid parameter")
|
||||
)
|
||||
|
||||
func newError(err error, param string) error {
|
||||
return fmt.Errorf("%w: '%s'", err, param)
|
||||
}
|
||||
func (r *Values) String(param string) (string, error) {
|
||||
v := r.URL.Query().Get(param)
|
||||
if v == "" {
|
||||
return "", newError(ErrMissingParam, param)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (r *Values) StringPtr(param string) *string {
|
||||
var v *string
|
||||
if _, exists := r.URL.Query()[param]; exists {
|
||||
s := r.URL.Query().Get(param)
|
||||
v = &s
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Values) BoolPtr(param string) *bool {
|
||||
var v *bool
|
||||
if _, exists := r.URL.Query()[param]; exists {
|
||||
s := r.URL.Query().Get(param)
|
||||
b := strings.Contains("/true/on/1/", "/"+strings.ToLower(s)+"/")
|
||||
v = &b
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Values) StringOr(param, def string) string {
|
||||
v, _ := r.String(param)
|
||||
if v == "" {
|
||||
return def
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Values) Strings(param string) ([]string, error) {
|
||||
values := r.URL.Query()[param]
|
||||
if len(values) == 0 {
|
||||
return nil, newError(ErrMissingParam, param)
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func (r *Values) TimeOr(param string, def time.Time) time.Time {
|
||||
v, _ := r.String(param)
|
||||
if v == "" || v == "-1" {
|
||||
return def
|
||||
}
|
||||
value, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return def
|
||||
}
|
||||
t := time.UnixMilli(value)
|
||||
if t.Before(time.Date(1970, time.January, 2, 0, 0, 0, 0, time.UTC)) {
|
||||
return def
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func (r *Values) Times(param string) ([]time.Time, error) {
|
||||
pStr, err := r.Strings(param)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
times := make([]time.Time, len(pStr))
|
||||
for i, t := range pStr {
|
||||
ti, err := strconv.ParseInt(t, 10, 64)
|
||||
if err != nil {
|
||||
log.Warn(r.Context(), "Ignoring invalid time param", "time", t, err)
|
||||
times[i] = time.Now()
|
||||
continue
|
||||
}
|
||||
times[i] = time.UnixMilli(ti)
|
||||
}
|
||||
return times, nil
|
||||
}
|
||||
|
||||
func (r *Values) Int64(param string) (int64, error) {
|
||||
v, err := r.String(param)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
value, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("%w '%s': expected integer, got '%s'", ErrInvalidParam, param, v)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (r *Values) Int(param string) (int, error) {
|
||||
v, err := r.Int64(param)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(v), nil
|
||||
}
|
||||
|
||||
func (r *Values) IntOr(param string, def int) int {
|
||||
v, err := r.Int64(param)
|
||||
if err != nil {
|
||||
return def
|
||||
}
|
||||
return int(v)
|
||||
}
|
||||
|
||||
func (r *Values) Int64Or(param string, def int64) int64 {
|
||||
v, err := r.Int64(param)
|
||||
if err != nil {
|
||||
return def
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Values) Ints(param string) ([]int, error) {
|
||||
pStr, err := r.Strings(param)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ints := make([]int, 0, len(pStr))
|
||||
for _, s := range pStr {
|
||||
i, err := strconv.ParseInt(s, 10, 64)
|
||||
if err == nil {
|
||||
ints = append(ints, int(i))
|
||||
}
|
||||
}
|
||||
return ints, nil
|
||||
}
|
||||
|
||||
func (r *Values) Bool(param string) (bool, error) {
|
||||
v, err := r.String(param)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return strings.Contains("/true/on/1/", "/"+strings.ToLower(v)+"/"), nil
|
||||
}
|
||||
|
||||
func (r *Values) BoolOr(param string, def bool) bool {
|
||||
v, err := r.Bool(param)
|
||||
if err != nil {
|
||||
return def
|
||||
}
|
||||
return v
|
||||
}
|
||||
277
utils/req/req_test.go
Normal file
277
utils/req/req_test.go
Normal file
@@ -0,0 +1,277 @@
|
||||
package req_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/navidrome/navidrome/utils/req"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestUtils(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Request Helpers Suite")
|
||||
}
|
||||
|
||||
var _ = Describe("Request Helpers", func() {
|
||||
var r *req.Values
|
||||
|
||||
Describe("ParamString", func() {
|
||||
BeforeEach(func() {
|
||||
r = req.Params(httptest.NewRequest("GET", "/ping?a=123", nil))
|
||||
})
|
||||
|
||||
It("returns param as string", func() {
|
||||
Expect(r.String("a")).To(Equal("123"))
|
||||
})
|
||||
|
||||
It("returns empty string if param does not exist", func() {
|
||||
v, err := r.String("NON_EXISTENT_PARAM")
|
||||
Expect(err).To(MatchError(req.ErrMissingParam))
|
||||
Expect(err.Error()).To(ContainSubstring("NON_EXISTENT_PARAM"))
|
||||
Expect(v).To(BeEmpty())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("ParamStringDefault", func() {
|
||||
BeforeEach(func() {
|
||||
r = req.Params(httptest.NewRequest("GET", "/ping?a=123", nil))
|
||||
})
|
||||
|
||||
It("returns param as string", func() {
|
||||
Expect(r.StringOr("a", "default_value")).To(Equal("123"))
|
||||
})
|
||||
|
||||
It("returns default string if param does not exist", func() {
|
||||
Expect(r.StringOr("xx", "default_value")).To(Equal("default_value"))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("ParamStrings", func() {
|
||||
BeforeEach(func() {
|
||||
r = req.Params(httptest.NewRequest("GET", "/ping?a=123&a=456", nil))
|
||||
})
|
||||
|
||||
It("returns all param occurrences as []string", func() {
|
||||
Expect(r.Strings("a")).To(Equal([]string{"123", "456"}))
|
||||
})
|
||||
|
||||
It("returns empty array if param does not exist", func() {
|
||||
v, err := r.Strings("xx")
|
||||
Expect(err).To(MatchError(req.ErrMissingParam))
|
||||
Expect(v).To(BeEmpty())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("ParamTime", func() {
|
||||
d := time.Date(2002, 8, 9, 12, 11, 13, 1000000, time.Local)
|
||||
t := d.UnixMilli()
|
||||
now := time.Now()
|
||||
BeforeEach(func() {
|
||||
r = req.Params(httptest.NewRequest("GET", fmt.Sprintf("/ping?t=%d&inv=abc", t), nil))
|
||||
})
|
||||
|
||||
It("returns parsed time", func() {
|
||||
Expect(r.TimeOr("t", now)).To(Equal(d))
|
||||
})
|
||||
|
||||
It("returns default time if param does not exist", func() {
|
||||
Expect(r.TimeOr("xx", now)).To(Equal(now))
|
||||
})
|
||||
|
||||
It("returns default time if param is an invalid timestamp", func() {
|
||||
Expect(r.TimeOr("inv", now)).To(Equal(now))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("ParamTimes", func() {
|
||||
d1 := time.Date(2002, 8, 9, 12, 11, 13, 1000000, time.Local)
|
||||
d2 := time.Date(2002, 8, 9, 12, 13, 56, 0000000, time.Local)
|
||||
t1 := d1.UnixMilli()
|
||||
t2 := d2.UnixMilli()
|
||||
BeforeEach(func() {
|
||||
r = req.Params(httptest.NewRequest("GET", fmt.Sprintf("/ping?t=%d&t=%d", t1, t2), nil))
|
||||
})
|
||||
|
||||
It("returns all param occurrences as []time.Time", func() {
|
||||
Expect(r.Times("t")).To(Equal([]time.Time{d1, d2}))
|
||||
})
|
||||
|
||||
It("returns empty string if param does not exist", func() {
|
||||
v, err := r.Times("xx")
|
||||
Expect(err).To(MatchError(req.ErrMissingParam))
|
||||
Expect(v).To(BeEmpty())
|
||||
})
|
||||
|
||||
It("returns current time as default if param is invalid", func() {
|
||||
now := time.Now()
|
||||
r = req.Params(httptest.NewRequest("GET", "/ping?t=null", nil))
|
||||
times, err := r.Times("t")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(times).To(HaveLen(1))
|
||||
Expect(times[0]).To(BeTemporally(">=", now))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("ParamInt", func() {
|
||||
BeforeEach(func() {
|
||||
r = req.Params(httptest.NewRequest("GET", "/ping?i=123&inv=123.45", nil))
|
||||
})
|
||||
Context("int", func() {
|
||||
It("returns parsed int", func() {
|
||||
Expect(r.IntOr("i", 999)).To(Equal(123))
|
||||
})
|
||||
|
||||
It("returns default value if param does not exist", func() {
|
||||
Expect(r.IntOr("xx", 999)).To(Equal(999))
|
||||
})
|
||||
|
||||
It("returns default value if param is an invalid int", func() {
|
||||
Expect(r.IntOr("inv", 999)).To(Equal(999))
|
||||
})
|
||||
|
||||
It("returns error if param is an invalid int", func() {
|
||||
_, err := r.Int("inv")
|
||||
Expect(err).To(MatchError(req.ErrInvalidParam))
|
||||
})
|
||||
})
|
||||
Context("int64", func() {
|
||||
It("returns parsed int64", func() {
|
||||
Expect(r.Int64Or("i", 999)).To(Equal(int64(123)))
|
||||
})
|
||||
|
||||
It("returns default value if param does not exist", func() {
|
||||
Expect(r.Int64Or("xx", 999)).To(Equal(int64(999)))
|
||||
})
|
||||
|
||||
It("returns default value if param is an invalid int", func() {
|
||||
Expect(r.Int64Or("inv", 999)).To(Equal(int64(999)))
|
||||
})
|
||||
|
||||
It("returns error if param is an invalid int", func() {
|
||||
_, err := r.Int64("inv")
|
||||
Expect(err).To(MatchError(req.ErrInvalidParam))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Describe("ParamInts", func() {
|
||||
BeforeEach(func() {
|
||||
r = req.Params(httptest.NewRequest("GET", "/ping?i=123&i=456", nil))
|
||||
})
|
||||
|
||||
It("returns array of occurrences found", func() {
|
||||
Expect(r.Ints("i")).To(Equal([]int{123, 456}))
|
||||
})
|
||||
|
||||
It("returns empty array if param does not exist", func() {
|
||||
v, err := r.Ints("xx")
|
||||
Expect(err).To(MatchError(req.ErrMissingParam))
|
||||
Expect(v).To(BeEmpty())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("ParamBool", func() {
|
||||
Context("value is true", func() {
|
||||
BeforeEach(func() {
|
||||
r = req.Params(httptest.NewRequest("GET", "/ping?b=true&c=on&d=1&e=True", nil))
|
||||
})
|
||||
|
||||
It("parses 'true'", func() {
|
||||
Expect(r.BoolOr("b", false)).To(BeTrue())
|
||||
})
|
||||
|
||||
It("parses 'on'", func() {
|
||||
Expect(r.BoolOr("c", false)).To(BeTrue())
|
||||
})
|
||||
|
||||
It("parses '1'", func() {
|
||||
Expect(r.BoolOr("d", false)).To(BeTrue())
|
||||
})
|
||||
|
||||
It("parses 'True'", func() {
|
||||
Expect(r.BoolOr("e", false)).To(BeTrue())
|
||||
})
|
||||
})
|
||||
|
||||
Context("value is false", func() {
|
||||
BeforeEach(func() {
|
||||
r = req.Params(httptest.NewRequest("GET", "/ping?b=false&c=off&d=0", nil))
|
||||
})
|
||||
|
||||
It("parses 'false'", func() {
|
||||
Expect(r.BoolOr("b", true)).To(BeFalse())
|
||||
})
|
||||
|
||||
It("parses 'off'", func() {
|
||||
Expect(r.BoolOr("c", true)).To(BeFalse())
|
||||
})
|
||||
|
||||
It("parses '0'", func() {
|
||||
Expect(r.BoolOr("d", true)).To(BeFalse())
|
||||
})
|
||||
|
||||
It("returns default value if param does not exist", func() {
|
||||
Expect(r.BoolOr("xx", true)).To(BeTrue())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Describe("ParamStringPtr", func() {
|
||||
BeforeEach(func() {
|
||||
r = req.Params(httptest.NewRequest("GET", "/ping?a=123", nil))
|
||||
})
|
||||
|
||||
It("returns pointer to string if param exists", func() {
|
||||
ptr := r.StringPtr("a")
|
||||
Expect(ptr).ToNot(BeNil())
|
||||
Expect(*ptr).To(Equal("123"))
|
||||
})
|
||||
|
||||
It("returns nil if param does not exist", func() {
|
||||
ptr := r.StringPtr("xx")
|
||||
Expect(ptr).To(BeNil())
|
||||
})
|
||||
|
||||
It("returns pointer to empty string if param exists but is empty", func() {
|
||||
r = req.Params(httptest.NewRequest("GET", "/ping?a=", nil))
|
||||
ptr := r.StringPtr("a")
|
||||
Expect(ptr).ToNot(BeNil())
|
||||
Expect(*ptr).To(Equal(""))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("ParamBoolPtr", func() {
|
||||
Context("value is true", func() {
|
||||
BeforeEach(func() {
|
||||
r = req.Params(httptest.NewRequest("GET", "/ping?b=true", nil))
|
||||
})
|
||||
|
||||
It("returns pointer to true if param is 'true'", func() {
|
||||
ptr := r.BoolPtr("b")
|
||||
Expect(ptr).ToNot(BeNil())
|
||||
Expect(*ptr).To(BeTrue())
|
||||
})
|
||||
})
|
||||
|
||||
Context("value is false", func() {
|
||||
BeforeEach(func() {
|
||||
r = req.Params(httptest.NewRequest("GET", "/ping?b=false", nil))
|
||||
})
|
||||
|
||||
It("returns pointer to false if param is 'false'", func() {
|
||||
ptr := r.BoolPtr("b")
|
||||
Expect(ptr).ToNot(BeNil())
|
||||
Expect(*ptr).To(BeFalse())
|
||||
})
|
||||
})
|
||||
|
||||
It("returns nil if param does not exist", func() {
|
||||
ptr := r.BoolPtr("xx")
|
||||
Expect(ptr).To(BeNil())
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user