Skip to content

Commit

Permalink
Merge pull request xojoc#10 from pendo-io/master
Browse files Browse the repository at this point in the history
Fix test failures introduced by interactions of PR xojoc#7 and xojoc#9
  • Loading branch information
xojoc authored Jan 30, 2017
2 parents ab9684e + 6153725 commit 4931ac5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
39 changes: 25 additions & 14 deletions browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package useragent

import (
"github.com/blang/semver"
"net/url"
)

Expand Down Expand Up @@ -95,9 +96,11 @@ func parseMozillaLike(l *lex, ua *UserAgent) bool {
return false
}
case l.match("Windows"):
ua.OS = OSWindows
// Windows has version before security
_ = parseOSVersion(l, ua)
l.span("; ")
ua.Security = parseSecurity(l)
ua.OS = OSWindows
case l.match("Macintosh"):
l.span("; ")
ua.Security = parseSecurity(l)
Expand Down Expand Up @@ -125,8 +128,10 @@ func parseMozillaLike(l *lex, ua *UserAgent) bool {
return false
}

// swallow the error to preserve backwards compatibility
_ = parseOSVersion(l, ua)
// OS Version is not required, and may be set above
if ua.OSVersion.Equals(semver.Version{}) {
_ = parseOSVersion(l, ua)
}

if _, ok := l.span(") "); !ok {
return false
Expand Down Expand Up @@ -154,7 +159,7 @@ func parseUnixLike(l *lex, ua *UserAgent) bool {
default:
// Various distros use "... Distro; Linux x86_64) "
if _, ok := l.spanBefore("Linux", ") "); ok {
ua.OS = "GNU/Linux"
ua.OS = OSLinux
return true
}
return false
Expand Down Expand Up @@ -238,7 +243,7 @@ func parseChromeSafari(l *lex) *UserAgent {
return nil
}
}
if ua.OS == "Android" {
if ua.OS == OSAndroid {
if l.match("Mobile") {
ua.Mobile = true
} else {
Expand Down Expand Up @@ -271,17 +276,11 @@ func parseIE1(l *lex) *UserAgent {
if !l.match("MSIE ") {
return nil
}

ua.Name = "MSIE"
if !parseVersion(l, ua, ";") {
return nil
}
if _, ok := l.span("Opera "); ok {
if !parseVersion(l, ua, " ") {
return nil
}
ua.Name = "Opera"
}

if !l.match(" Windows NT") {
return nil
}
Expand All @@ -290,6 +289,13 @@ func parseIE1(l *lex) *UserAgent {
// swallow the error to preserve backwards compatibility
_ = parseOSVersion(l, ua)

if _, ok := l.span("Opera "); ok {
if !parseVersion(l, ua, " ") {
return nil
}
ua.Name = "Opera"
}

return ua
}

Expand Down Expand Up @@ -341,15 +347,20 @@ func parseOperaClassic(l *lex) *UserAgent {
}
switch {
case l.match("Windows"):
ua.OS = "Windows"
ua.OS = OSWindows
case l.match("Macintosh"):
ua.OS = "Mac OS X"
ua.OS = OSMacOS
l.spanBefore("OS X", ")")
default:
if !parseUnixLike(l, ua) {
return nil
}
}

// swallow the error to preserve backwards compatibility
_ = parseOSVersion(l, ua)

// Get security if present, then get to the end of the parens block
if _, ok := l.spanBefore("; ", ")"); ok {
ua.Security = parseSecurity(l)
}
Expand Down
6 changes: 4 additions & 2 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,10 @@ func parseVersion(l *lex, ua *UserAgent, sep string) bool {
return true
}

var appleVersionRegexp = regexp.MustCompile(`(?:[^\)]+?)\b(\d+_\d+(_\d+)?)\b`)
var genericVersionRegexp = regexp.MustCompile(`(?:[^\)]*?) (\d+\.\d+(\.\d+)?)\b`)
// Regexps need to match start of string to prevent greedily finding
// version numbers from much later in the UA string
var appleVersionRegexp = regexp.MustCompile(`^(?:[^\)]+?)\b(\d+_\d+(_\d+)?)\b`)
var genericVersionRegexp = regexp.MustCompile(`^(?:[^\)]*?) (\d+\.\d+(\.\d+)?)\b`)

func parseOSVersion(l *lex, ua *UserAgent) bool {
switch ua.OS {
Expand Down
9 changes: 9 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ func TestOpera(t *testing.T) {
got = Parse(`Opera/4.02 (Windows 98; U) [de]`)
want.Type = Browser
want.OS = "Windows"
want.OSVersion = semver.Version{}
want.Name = "Opera"
want.Version = mustParse("4.2.0")
want.Security = SecurityStrong
Expand All @@ -483,6 +484,7 @@ func TestOpera(t *testing.T) {
got = Parse(`Opera/9.30 (Macintosh; PPC Mac OS X; U; ja)`)
want.Type = Browser
want.OS = "Mac OS X"
want.OSVersion = semver.Version{}
want.Name = "Opera"
want.Version = mustParse("9.30.0")
want.Security = SecurityStrong
Expand All @@ -493,6 +495,7 @@ func TestOpera(t *testing.T) {
got = Parse(`Opera/7.52 (FreeBSD 4.7-RELEASE i386; U) [fr]`)
want.Type = Browser
want.OS = "FreeBSD"
want.OSVersion = semver.Version{}
want.Name = "Opera"
want.Version = mustParse("7.52.0")
want.Security = SecurityStrong
Expand All @@ -503,6 +506,7 @@ func TestOpera(t *testing.T) {
got = Parse(`Opera/9.80 (Windows NT 6.1; U; en) Presto/2.10.229 Version/11.61`)
want.Type = Browser
want.OS = "Windows"
want.OSVersion = mustParse("6.1.0")
want.Name = "Opera"
want.Version = mustParse("11.61.0")
want.Security = SecurityStrong
Expand All @@ -513,6 +517,7 @@ func TestOpera(t *testing.T) {
got = Parse(`Opera/9.80 (Linux mips; U; HbbTV/1.1.1 (; TechniSat; DigiPal ISIO HD; 2.70.0.5; 57.0-6; ); CE-HTML/1.0 (); MB_BP/1.0 (TechniSat; DigiPal ISIO HD; ); TechniSat DigiPal ISIO HD BCM3 STB; de) Presto/2.12.407 Version/12.51`)
want.Type = Browser
want.OS = "GNU/Linux"
want.OSVersion = semver.Version{}
want.Name = "Opera"
want.Version = mustParse("12.51.0")
want.Security = SecurityStrong
Expand All @@ -523,6 +528,7 @@ func TestOpera(t *testing.T) {
got = Parse(`Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; zh-tw) Opera 11.00`)
want.Type = Browser
want.OS = "Windows"
want.OSVersion = mustParse("6.0.0")
want.Name = "Opera"
want.Version = mustParse("11.0.0")
want.Security = SecurityUnknown
Expand All @@ -533,6 +539,7 @@ func TestOpera(t *testing.T) {
got = Parse(`Mozilla/5.0 (Windows NT 6.1; U; nl; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 Opera 11.01`)
want.Type = Browser
want.OS = "Windows"
want.OSVersion = mustParse("6.1.0")
want.Name = "Opera"
want.Version = mustParse("11.1.0")
want.Security = SecurityStrong
Expand All @@ -543,6 +550,7 @@ func TestOpera(t *testing.T) {
got = Parse(`Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36 OPR/15.0.1147.100`)
want.Type = Browser
want.OS = "Windows"
want.OSVersion = mustParse("6.1.0")
want.Name = "Opera"
want.Version = mustParse("15.0.1147")
want.Security = SecurityUnknown
Expand All @@ -553,6 +561,7 @@ func TestOpera(t *testing.T) {
got = Parse(`Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Mobile Safari/537.36 OPR/15.0.1147.100`)
want.Type = Browser
want.OS = "Android"
want.OSVersion = mustParse("4.0.4")
want.Name = "Opera"
want.Version = mustParse("15.0.1147")
want.Security = SecurityUnknown
Expand Down

0 comments on commit 4931ac5

Please sign in to comment.