73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
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)
|
|
}
|
|
}
|