mirror of
https://github.com/eduardolat/pgbackweb.git
synced 2026-01-22 12:30:48 -06:00
Add AddQueryParamToUrl function to strutil package
This commit is contained in:
27
internal/util/strutil/add_query_param_to_url.go
Normal file
27
internal/util/strutil/add_query_param_to_url.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package strutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
nurl "net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func AddQueryParamToUrl(url, key, value string) string {
|
||||
if url == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
if key == "" || value == "" {
|
||||
return url
|
||||
}
|
||||
|
||||
value = nurl.PathEscape(value)
|
||||
|
||||
if !strings.Contains(url, "?") {
|
||||
return fmt.Sprintf("%s?%s=%s", url, key, value)
|
||||
}
|
||||
if strings.HasSuffix(url, "?") || strings.HasSuffix(url, "&") {
|
||||
return fmt.Sprintf("%s%s=%s", url, key, value)
|
||||
}
|
||||
return fmt.Sprintf("%s&%s=%s", url, key, value)
|
||||
}
|
||||
50
internal/util/strutil/add_query_param_to_url_test.go
Normal file
50
internal/util/strutil/add_query_param_to_url_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package strutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAddQueryParamToUrl(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
// Test case when url, key or value is empty
|
||||
assert.Equal("", AddQueryParamToUrl("", "key", "value"))
|
||||
assert.Equal("http://example.com", AddQueryParamToUrl("http://example.com", "", "value"))
|
||||
assert.Equal("http://example.com", AddQueryParamToUrl("http://example.com", "key", ""))
|
||||
|
||||
// Test case when url does not contain "?"
|
||||
assert.Equal("http://example.com?key=value", AddQueryParamToUrl("http://example.com", "key", "value"))
|
||||
|
||||
// Test case when url ends with "?"
|
||||
assert.Equal("http://example.com?key=value", AddQueryParamToUrl("http://example.com?", "key", "value"))
|
||||
|
||||
// Test case when url ends with "&"
|
||||
assert.Equal("http://example.com&?key=value", AddQueryParamToUrl("http://example.com&", "key", "value"))
|
||||
|
||||
// Test case when url contains "?" but does not end with "?" or "&"
|
||||
assert.Equal("http://example.com/path?key=value", AddQueryParamToUrl("http://example.com/path?", "key", "value"))
|
||||
|
||||
// Test case when multiple query params are added
|
||||
expected := "http://example.com/path?key1=value1&key2=value2"
|
||||
got := AddQueryParamToUrl("http://example.com/path", "key1", "value1")
|
||||
got = AddQueryParamToUrl(got, "key2", "value2")
|
||||
assert.Equal(expected, got)
|
||||
|
||||
// Test case when value contains special characters
|
||||
expected = "http://example.com/path?key1=value%20with%20spaces"
|
||||
got = AddQueryParamToUrl("http://example.com/path", "key1", "value with spaces")
|
||||
assert.Equal(expected, got)
|
||||
|
||||
// Test case when the key is empty
|
||||
expected = "http://example.com/path"
|
||||
got = AddQueryParamToUrl("http://example.com/path", "", "value")
|
||||
assert.Equal(expected, got)
|
||||
|
||||
// Test case when the value is empty
|
||||
expected = "http://example.com/path?key2=val2"
|
||||
got = AddQueryParamToUrl("http://example.com/path", "key1", "")
|
||||
got = AddQueryParamToUrl(got, "key2", "val2")
|
||||
assert.Equal(expected, got)
|
||||
}
|
||||
Reference in New Issue
Block a user