Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-761744 Added URL Validator and URL Encoder #757

Merged
merged 6 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions url_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gosnowflake

import (
"net/url"
"regexp"
)

var (
matcher, _ = regexp.Compile(`^http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z@:])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\&\(\)\/\\\+&%\$#_=@]*)?$`)
)

func isValidURL(targetURL string) bool {
if !matcher.MatchString(targetURL) {
logger.Infof(" The provided URL is not a valid URL - " + targetURL)
return false
}
return true
}

func urlEncode(targetString string) string {
// We use QueryEscape instead of PathEscape here
// for consistency across Drivers. For example:
// QueryEscape escapes space as "+" whereas PE
// it as %20F. PE also does not escape @ or &
// either but QE does.
// The behavior of QE in Golang is more in sync
// with URL encoders in Python and Java hence the choice
return url.QueryEscape(targetString)
}
41 changes: 41 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,44 @@ func TestGetMin(t *testing.T) {
}
}
}

type tcURLList struct {
in string
out bool
}

func TestValidURL(t *testing.T) {
testcases := []tcURLList{
{"https://ssoTestURL.okta.com", true},
{"https://ssoTestURL.okta.com:8080", true},
{"https://ssoTestURL.okta.com/testpathvalue", true},
{"-a calculator", false},
{"This is a random test", false},
{"file://TestForFile", false},
}
for _, test := range testcases {
result := isValidURL(test.in)
if test.out != result {
t.Errorf("Failed to validate URL, input :%v, expected: %v, got: %v", test.in, test.out, result)
}
}
}

type tcEncodeList struct {
in string
out string
}

func TestEncodeURL(t *testing.T) {
testcases := []tcEncodeList{
{"Hello @World", "Hello+%40World"},
{"Test//String", "Test%2F%2FString"},
}

for _, test := range testcases {
result := urlEncode(test.in)
if test.out != result {
t.Errorf("Failed to encode string, input %v, expected: %v, got: %v", test.in, test.out, result)
}
}
}