Skip to content

Commit

Permalink
Fix for www case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Dec 2, 2020
1 parent 50a5580 commit 705190e
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 29 deletions.
5 changes: 3 additions & 2 deletions sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ var (
alphaNumericWithSpacesRegExp = regexp.MustCompile(`[^a-zA-Z0-9\s]`) // Alpha numeric (with spaces)
alphaRegExp = regexp.MustCompile(`[^a-zA-Z]`) // Alpha characters
alphaWithSpacesRegExp = regexp.MustCompile(`[^a-zA-Z\s]`) // Alpha characters (with spaces)
bitcoinRegExp = regexp.MustCompile(`[^a-km-zA-HJ-NP-Z1-9]`) // Bitcoin address accepted characters
bitcoinCashAddrRegExp = regexp.MustCompile(`[^ac-hj-np-zAC-HJ-NP-Z02-9]`) // Bitcoin `cashaddr` address accepted characters
bitcoinRegExp = regexp.MustCompile(`[^a-km-zA-HJ-NP-Z1-9]`) // Bitcoin address accepted characters
decimalRegExp = regexp.MustCompile(`[^0-9.-]`) // Decimals (positive and negative)
domainRegExp = regexp.MustCompile(`[^a-zA-Z0-9-.]`) // Domain accepted characters
emailRegExp = regexp.MustCompile(`[^a-zA-Z0-9-_.@+]`) // Email address characters
Expand All @@ -35,6 +35,7 @@ var (
timeRegExp = regexp.MustCompile(`[^0-9:]`) // Time allowed characters
uriRegExp = regexp.MustCompile(`[^a-zA-Z0-9-_/?&=#%]`) // URI allowed characters
urlRegExp = regexp.MustCompile(`[^a-zA-Z0-9-_/:.,?&@=#%]`) // URL allowed characters
wwwRegExp = regexp.MustCompile(`(?i)www.`) // For removing www
)

// emptySpace is an empty space for replacing
Expand Down Expand Up @@ -114,7 +115,7 @@ func Domain(original string, preserveCase bool, removeWww bool) (string, error)

// Remove leading www.
if removeWww {
u.Host = strings.Replace(u.Host, "www.", "", -1)
u.Host = wwwRegExp.ReplaceAllString(u.Host, "")
}

// Keeps the exact case of the original input string
Expand Down
133 changes: 106 additions & 27 deletions sanitize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,34 +279,113 @@ func ExampleDecimal_negative() {
func TestDomain(t *testing.T) {
t.Parallel()

var tests = []struct {
input string
expected string
expectedError bool
preserveCase bool
removeWww bool
}{
{"http://www.I am a domain.com", "Iamadomain.com", true, false, false},
{"!I_am a domain.com", "", true, true, false},
{"", "", false, true, false},
{"http://IAmaDomain.com", "IAmaDomain.com", false, true, false},
{"http://IAmaDomain.com", "iamadomain.com", false, false, false},
{"http://IAmaDomaiN.Com", "iamadomain.com", false, false, false},
{"https://IAmaDomain.com/?this=that#plusThis", "iamadomain.com", false, false, false},
{"http://www.IAmaDomain.com/?this=that#plusThis", "iamadomain.com", false, false, true},
{"IAmaDomain.com/?this=that#plusThis", "iamadomain.com", false, false, true},
{"www.IAmaDomain.com/?this=that#plusThis", "www.iamadomain.com", false, false, false},
}

for _, test := range tests {
if output, err := Domain(test.input, test.preserveCase, test.removeWww); output != test.expected && !test.expectedError {
t.Errorf("%s Failed: [%s] inputted and [%s] expected, received: [%s]", t.Name(), test.input, test.expected, output)
} else if err == nil && test.expectedError {
t.Errorf("%s Failed: expected to throw an error, no error [%s] inputted and [%s] expected", t.Name(), test.input, test.expected)
} else if err != nil && !test.expectedError {
t.Errorf("%s Failed: [%s] inputted and [%s] expected, received: [%s] error [%s]", t.Name(), test.input, test.expected, output, err.Error())
t.Run("valid cases", func(t *testing.T) {

var tests = []struct {
name string
input string
expected string
preserveCase bool
removeWww bool
}{
{
"no domain name",
"",
"",
true,
true,
},
{
"remove leading http",
"http://IAmaDomain.com",
"IAmaDomain.com",
true,
false,
},
{
"remove leading http and lowercase",
"http://IAmaDomain.com",
"iamadomain.com",
false,
false,
},
{
"full url with params",
"https://IAmaDomain.com/?this=that#plusThis",
"iamadomain.com",
false,
false,
},
{
"full url with params, remove www",
"http://www.IAmaDomain.com/?this=that#plusThis",
"iamadomain.com",
false,
true,
},
{
"full url with params, leave www",
"http://www.IAmaDomain.com/?this=that#plusThis",
"www.iamadomain.com",
false,
false,
},
{
"caps domain, remove www",
"WWW.DOMAIN.COM",
"domain.com",
false,
true,
},
{
"mixed caps domain, remove www",
"WwW.DOMAIN.COM",
"domain.com",
false,
true,
},
}
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output, err := Domain(test.input, test.preserveCase, test.removeWww)
assert.NoError(t, err)
assert.Equal(t, test.expected, output)
})
}
})

t.Run("invalid cases", func(t *testing.T) {

var tests = []struct {
name string
input string
expected string
preserveCase bool
removeWww bool
}{
{
"spaces in domain",
"http://www.I am a domain.com",
"http://www.I am a domain.com",
true,
true,
},
{
"symbol in domain",
"!I_am a domain.com",
"http://!I_am a domain.com",
true,
true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output, err := Domain(test.input, test.preserveCase, test.removeWww)
assert.Error(t, err)
assert.Equal(t, test.expected, output)
})
}
})
}

// BenchmarkDomain benchmarks the Domain method
Expand Down

0 comments on commit 705190e

Please sign in to comment.