Initial cloud-services repo - gateway service + pkg modules
This commit is contained in:
69
pkg/utils/urlhelper/urlhelper.go
Normal file
69
pkg/utils/urlhelper/urlhelper.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package urlhelper
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// BuildQuery builds querystring from map
|
||||
func BuildQuery(queries map[string]string) string {
|
||||
var qs []string
|
||||
|
||||
for key, value := range queries {
|
||||
qs = append(qs, key+"="+url.QueryEscape(value))
|
||||
}
|
||||
|
||||
return strings.Join(qs, "&")
|
||||
}
|
||||
|
||||
// BuildURL builds url with base url and querystring
|
||||
func BuildURL(base string, queries map[string]string) string {
|
||||
return strings.Join([]string{base, BuildQuery(queries)}, "?")
|
||||
}
|
||||
|
||||
func GetQueryInt(query url.Values, key string) int {
|
||||
i, _ := strconv.Atoi(query.Get(key))
|
||||
return i
|
||||
}
|
||||
|
||||
// Parse a epoch unix time number from the URL
|
||||
func GetQueryUnix(query url.Values, key string) (t time.Time) {
|
||||
return time.UnixMilli(GetQueryInt64(query, key))
|
||||
}
|
||||
|
||||
func GetQueryTimeStamp(query url.Values, key string) (t time.Time) {
|
||||
t, _ = time.Parse(time.RFC3339Nano, query.Get(key))
|
||||
return t
|
||||
}
|
||||
|
||||
// GetQueryBool returns val, ok.
|
||||
func GetQueryBool(query url.Values, key string) (bool, bool) {
|
||||
b, err := strconv.ParseBool(query.Get(key))
|
||||
return b, err == nil
|
||||
}
|
||||
|
||||
func GetQueryInt64(query url.Values, key string) int64 {
|
||||
i, _ := strconv.ParseInt(query.Get(key), 0, 64)
|
||||
return i
|
||||
}
|
||||
|
||||
func GetQueryFloat32(query url.Values, key string) float32 {
|
||||
i, _ := strconv.ParseFloat(query.Get(key), 32)
|
||||
return float32(i)
|
||||
}
|
||||
|
||||
func GetQueryUUID(query url.Values, key string) uuid.UUID {
|
||||
val := query.Get(key)
|
||||
if val != "" {
|
||||
id, err := uuid.Parse(val)
|
||||
if err == nil {
|
||||
return id
|
||||
}
|
||||
}
|
||||
|
||||
return uuid.Nil
|
||||
}
|
||||
72
pkg/utils/urlhelper/urlhelper_test.go
Normal file
72
pkg/utils/urlhelper/urlhelper_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package urlhelper
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"fiskerinc.com/modules/testhelper"
|
||||
)
|
||||
|
||||
func checkQueryString(t *testing.T, querystring string, data map[string]string) {
|
||||
for key, value := range data {
|
||||
compare := strings.Join([]string{key, url.QueryEscape(value)}, "=")
|
||||
if strings.Index(querystring, compare) == -1 {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "TestBuildQuery", compare, querystring)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildQuery(t *testing.T) {
|
||||
qs := map[string]string{
|
||||
"a": "A",
|
||||
"b": "B",
|
||||
"c": "https://www.fiskerinc.com/?c=C",
|
||||
}
|
||||
|
||||
result := BuildQuery(qs)
|
||||
checkQueryString(t, result, qs)
|
||||
}
|
||||
|
||||
func TestBuildURL(t *testing.T) {
|
||||
domain := "https://testing.com"
|
||||
qs := map[string]string{
|
||||
"a": "A",
|
||||
"b": "B",
|
||||
"c": "https://www.fiskerinc.com/?c=C",
|
||||
}
|
||||
|
||||
result := BuildURL("https://testing.com", qs)
|
||||
|
||||
if strings.Index(result, domain+"?") != 0 {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "TestBuildURL", domain, result)
|
||||
}
|
||||
|
||||
checkQueryString(t, result, qs)
|
||||
}
|
||||
|
||||
func TestGetQueryInt(t *testing.T) {
|
||||
r, _ := http.NewRequest(http.MethodGet, "http://example.com?limit=50&offset=5&text=XXXXXX", nil)
|
||||
q := r.URL.Query()
|
||||
|
||||
i := GetQueryInt(q, "nonexistent")
|
||||
if i != 0 {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "Non-existing query", 0, i)
|
||||
}
|
||||
|
||||
i = GetQueryInt(q, "text")
|
||||
if i != 0 {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "Text query", 0, i)
|
||||
}
|
||||
|
||||
i = GetQueryInt(q, "limit")
|
||||
if i != 50 {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "Limit query", 50, i)
|
||||
}
|
||||
|
||||
i = GetQueryInt(q, "offset")
|
||||
if i != 5 {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "Offset query", 5, i)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user