Skip to content

Commit e11a2a5

Browse files
SNOW-761744 Added URL Validator and URL Encoder (#757)
1 parent 72ad913 commit e11a2a5

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

url_util.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package gosnowflake
2+
3+
import (
4+
"net/url"
5+
"regexp"
6+
)
7+
8+
var (
9+
matcher, _ = regexp.Compile(`^http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z@:])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\&\(\)\/\\\+&%\$#_=@]*)?$`)
10+
)
11+
12+
func isValidURL(targetURL string) bool {
13+
if !matcher.MatchString(targetURL) {
14+
logger.Infof(" The provided URL is not a valid URL - " + targetURL)
15+
return false
16+
}
17+
return true
18+
}
19+
20+
func urlEncode(targetString string) string {
21+
// We use QueryEscape instead of PathEscape here
22+
// for consistency across Drivers. For example:
23+
// QueryEscape escapes space as "+" whereas PE
24+
// it as %20F. PE also does not escape @ or &
25+
// either but QE does.
26+
// The behavior of QE in Golang is more in sync
27+
// with URL encoders in Python and Java hence the choice
28+
return url.QueryEscape(targetString)
29+
}

util_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,44 @@ func TestGetMin(t *testing.T) {
230230
}
231231
}
232232
}
233+
234+
type tcURLList struct {
235+
in string
236+
out bool
237+
}
238+
239+
func TestValidURL(t *testing.T) {
240+
testcases := []tcURLList{
241+
{"https://ssoTestURL.okta.com", true},
242+
{"https://ssoTestURL.okta.com:8080", true},
243+
{"https://ssoTestURL.okta.com/testpathvalue", true},
244+
{"-a calculator", false},
245+
{"This is a random test", false},
246+
{"file://TestForFile", false},
247+
}
248+
for _, test := range testcases {
249+
result := isValidURL(test.in)
250+
if test.out != result {
251+
t.Errorf("Failed to validate URL, input :%v, expected: %v, got: %v", test.in, test.out, result)
252+
}
253+
}
254+
}
255+
256+
type tcEncodeList struct {
257+
in string
258+
out string
259+
}
260+
261+
func TestEncodeURL(t *testing.T) {
262+
testcases := []tcEncodeList{
263+
{"Hello @World", "Hello+%40World"},
264+
{"Test//String", "Test%2F%2FString"},
265+
}
266+
267+
for _, test := range testcases {
268+
result := urlEncode(test.in)
269+
if test.out != result {
270+
t.Errorf("Failed to encode string, input %v, expected: %v, got: %v", test.in, test.out, result)
271+
}
272+
}
273+
}

0 commit comments

Comments
 (0)