From 705190e52e2f0a9362af7f39e6361c74dea0cd64 Mon Sep 17 00:00:00 2001 From: mrz1836 Date: Wed, 2 Dec 2020 14:25:56 -0500 Subject: [PATCH] Fix for www case insensitive --- sanitize.go | 5 +- sanitize_test.go | 133 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 109 insertions(+), 29 deletions(-) diff --git a/sanitize.go b/sanitize.go index c721d66..b5bdba7 100644 --- a/sanitize.go +++ b/sanitize.go @@ -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 @@ -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 @@ -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 diff --git a/sanitize_test.go b/sanitize_test.go index 9fbe92f..cbd66cf 100644 --- a/sanitize_test.go +++ b/sanitize_test.go @@ -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