Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Commit

Permalink
Add Chromium support and Ubuntu specific tests
Browse files Browse the repository at this point in the history
This commit adds support for the Chromium user agent string and adds
user agent tests for the latest version of all major browsers available
on Ubuntu.

Fixes:
- Ignore revision field for localization (e.g. rv:50.0)
- Ignore browser name parameter without version (e.g. "Ubuntu")

Fixes #27
  • Loading branch information
dlebech committed Dec 7, 2016
1 parent 8e786bc commit 6e7843e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
25 changes: 25 additions & 0 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ var uastrings = []struct {
ua: "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0",
expected: "Mozilla:5.0 Platform:X11 OS:Linux x86_64 Browser:Firefox-17.0 Engine:Gecko-20100101 Bot:false Mobile:false",
},
{
title: "FirefoxLinux - Ubuntu V50",
ua: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0",
expected: "Mozilla:5.0 Platform:X11 OS:Ubuntu Browser:Firefox-50.0 Engine:Gecko-20100101 Bot:false Mobile:false",
},
{
title: "FirefoxWin",
ua: "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14",
Expand Down Expand Up @@ -265,6 +270,11 @@ var uastrings = []struct {
ua: "Opera/9.80 (X11; Linux x86_64) Presto/2.12.388 Version/12.10",
expected: "Platform:X11 OS:Linux x86_64 Browser:Opera-9.80 Engine:Presto-2.12.388 Bot:false Mobile:false",
},
{
title: "OperaLinux - Ubuntu V41",
ua: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36 OPR/41.0.2353.69",
expected: "Mozilla:5.0 Platform:X11 OS:Linux x86_64 Browser:Opera-41.0.2353.69 Engine:AppleWebKit-537.36 Bot:false Mobile:false",
},
{
title: "OperaAndroid",
ua: "Opera/9.80 (Android 4.2.1; Linux; Opera Mobi/ADR-1212030829) Presto/2.11.355 Version/12.10",
Expand Down Expand Up @@ -329,6 +339,11 @@ var uastrings = []struct {
ua: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11",
expected: "Mozilla:5.0 Platform:X11 OS:Linux x86_64 Browser:Chrome-23.0.1271.97 Engine:AppleWebKit-537.11 Bot:false Mobile:false",
},
{
title: "ChromeLinux - Ubuntu V55",
ua: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36",
expected: "Mozilla:5.0 Platform:X11 OS:Linux x86_64 Browser:Chrome-55.0.2883.75 Engine:AppleWebKit-537.36 Bot:false Mobile:false",
},
{
title: "ChromeWin7",
ua: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19",
Expand Down Expand Up @@ -429,6 +444,16 @@ var uastrings = []struct {
ua: "Mozilla/5.0 (SymbianOS/9.1; U; [en-us]) AppleWebKit/413 (KHTML, like Gecko) Safari/413",
expected: "Mozilla:5.0 Platform:Symbian OS:SymbianOS/9.1 Browser:Symbian-413 Engine:AppleWebKit-413 Bot:false Mobile:true",
},
{
title: "Chromium - Ubuntu V49",
ua: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36",
expected: "Mozilla:5.0 Platform:X11 OS:Linux x86_64 Browser:Chromium-49.0.2623.108 Engine:AppleWebKit-537.36 Bot:false Mobile:false",
},
{
title: "Chromium - Ubuntu V55",
ua: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/53.0.2785.143 Chrome/53.0.2785.143 Safari/537.36",
expected: "Mozilla:5.0 Platform:X11 OS:Linux x86_64 Browser:Chromium-53.0.2785.143 Engine:AppleWebKit-537.36 Bot:false Mobile:false",
},

// Dalvik
{
Expand Down
12 changes: 10 additions & 2 deletions browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ func (p *UserAgent) detectBrowser(sections []section) {
p.browser.Engine = engine.name
p.browser.EngineVersion = engine.version
if slen > 2 {
p.browser.Version = sections[2].version
sectionIndex := 2
// The version after the engine comment is empty on e.g. Ubuntu
// platforms so if this is the case, let's use the next in line.
if sections[2].version == "" && slen > 3 {
sectionIndex = 3
}
p.browser.Version = sections[sectionIndex].version
if engine.name == "AppleWebKit" {
switch sections[slen-1].name {
case "Edge":
Expand All @@ -63,8 +69,10 @@ func (p *UserAgent) detectBrowser(sections []section) {
p.browser.Name = "Opera"
p.browser.Version = sections[slen-1].version
default:
if sections[2].name == "Chrome" {
if sections[sectionIndex].name == "Chrome" {
p.browser.Name = "Chrome"
} else if sections[sectionIndex].name == "Chromium" {
p.browser.Name = "Chromium"
} else {
p.browser.Name = "Safari"
}
Expand Down
4 changes: 3 additions & 1 deletion operating_systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ func gecko(p *UserAgent, comment []string) {
}
}
}
if len(comment) > 3 {
// Only parse 4th comment as localization if it doesn't start with rv:.
// For example Firefox on Ubuntu contains "rv:XX.X" in this field.
if len(comment) > 3 && !strings.HasPrefix(comment[3], "rv:") {
p.localization = comment[3]
}
}
Expand Down

0 comments on commit 6e7843e

Please sign in to comment.