Skip to content

Commit

Permalink
Merge pull request #240 from jasonlam604/feature/Issue_212_underscore…
Browse files Browse the repository at this point in the history
…_in_URL_makes_them_invalid

Fixe for _ in URL makes them invalid #212
  • Loading branch information
asaskevich authored Nov 9, 2017
2 parents 713392a + efdd542 commit 635ecdb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ func IsURL(str string) bool {
if str == "" || utf8.RuneCountInString(str) >= maxURLRuneCount || len(str) <= minURLRuneCount || strings.HasPrefix(str, ".") {
return false
}
u, err := url.Parse(str)
strTemp := str
if strings.Index(str, ":") >= 0 && strings.Index(str, "://") == -1 {
// support no indicated urlscheme but with colon for port number
// http:// is appended so url.Parse will succeed, strTemp used so it does not impact rxURL.MatchString
strTemp = "http://" + str
}
u, err := url.Parse(strTemp)
if err != nil {
return false
}
Expand All @@ -67,7 +73,6 @@ func IsURL(str string) bool {
return false
}
return rxURL.MatchString(str)

}

// IsRequestURL check if the string rawurl, assuming
Expand Down
4 changes: 4 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,10 @@ func TestIsURL(t *testing.T) {
{"foo_bar.example.com", true},
{"foo_bar_fizz_buzz.example.com", true},
{"http://hello_world.example.com", true},
// According to #212
{"foo_bar-fizz-buzz:1313", true},
{"foo_bar-fizz-buzz:13:13", false},
{"foo_bar-fizz-buzz://1313", false},
}
for _, test := range tests {
actual := IsURL(test.param)
Expand Down

0 comments on commit 635ecdb

Please sign in to comment.