add tests for parseMajorMinor func

This commit is contained in:
Danny van Kooten 2016-12-11 15:33:23 +01:00
parent d8f1eb20ef
commit 7104397afa
3 changed files with 24 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import (
"log"
"net/http"
"strconv"
"strings"
"time"
)
@ -42,3 +43,11 @@ func getRequestedPeriods(r *http.Request) (int64, int64) {
return before, after
}
func parseMajorMinor(v string) string {
parts := strings.SplitN(v, ".", 3)
if len(parts) > 1 {
v = parts[0] + "." + parts[1]
}
return v
}

View File

@ -48,3 +48,17 @@ func TestGetRequestIp(t *testing.T) {
t.Errorf("Expected IP address of %s does not match %s", ipAddress, result)
}
}
func TestParseMajorMinor(t *testing.T) {
actual := parseMajorMinor("50.0.0")
expected := "50.0"
if actual != expected {
t.Errorf("Return value should be %s, is %s instead", expected, actual)
}
actual = parseMajorMinor("1.1")
expected = "1.1"
if actual != expected {
t.Errorf("Return value should be %s is %s instead", expected, actual)
}
}

View File

@ -55,10 +55,7 @@ func CollectHandler(w http.ResponseWriter, r *http.Request) {
// add browser details
visitor.BrowserName, visitor.BrowserVersion = ua.Browser()
versionParts := strings.SplitN(visitor.BrowserVersion, ".", 3)
if len(versionParts) > 1 {
visitor.BrowserVersion = versionParts[0] + "." + versionParts[1]
}
visitor.BrowserName = parseMajorMinor(visitor.BrowserName)
// query by unique visitor key
visitor.GenerateKey()