-
Notifications
You must be signed in to change notification settings - Fork 27
/
browscap_test.go
146 lines (127 loc) · 4.22 KB
/
browscap_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package browscap_go
import (
"io/ioutil"
"strings"
"testing"
)
const (
TEST_INI_FILE = "./test-data/full_php_browscap.ini"
TEST_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36"
TEST_IPHONE_AGENT = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5"
)
func TestInitBrowsCap(t *testing.T) {
if err := InitBrowsCap(TEST_INI_FILE, false); err != nil {
t.Fatalf("%v", err)
}
}
func TestGetBrowser(t *testing.T) {
if browser, ok := GetBrowser(TEST_USER_AGENT); !ok {
t.Error("Browser not found")
} else if browser.Browser != "Chrome" {
t.Errorf("Expected Chrome but got %q", browser.Browser)
} else if browser.Platform != "MacOSX" {
t.Errorf("Expected MacOSX but got %q", browser.Platform)
} else if browser.BrowserVersion != "37.0" {
t.Errorf("Expected 37.0 but got %q", browser.BrowserVersion)
} else if browser.RenderingEngineName != "Blink" {
t.Errorf("Expected Blink but got %q", browser.RenderingEngineName)
} else if browser.Crawler != "false" {
t.Errorf("Expected false but got %q", browser.Crawler)
}
}
func TestGetBrowserIPhone(t *testing.T) {
if browser, ok := GetBrowser(TEST_IPHONE_AGENT); !ok {
t.Error("Browser not found")
} else if browser.DeviceName != "iPhone" {
t.Errorf("Expected iPhone but got %q", browser.DeviceName)
} else if browser.Platform != "iOS" {
t.Errorf("Expected iOS but got %q", browser.Platform)
} else if browser.PlatformVersion != "4.3" {
t.Errorf("Expected 4.3 but got %q", browser.PlatformVersion)
} else if browser.IsMobile() != true {
t.Errorf("Expected true but got %t", browser.IsMobile())
}
}
func TestGetBrowserYandex(t *testing.T) {
if browser, ok := GetBrowser("Yandex Browser 1.1"); !ok {
t.Error("Browser not found")
} else if browser.Browser != "Yandex Browser" {
t.Errorf("Expected Yandex Browser but got %q", browser.Browser)
} else if browser.IsCrawler() != false {
t.Errorf("Expected false but got %t", browser.IsCrawler())
}
}
func TestGetBrowser360Spider(t *testing.T) {
if browser, ok := GetBrowser("360Spider"); !ok {
t.Error("Browser not found")
} else if browser.Browser != "360Spider" {
t.Errorf("Expected Chrome but got %q", browser.Browser)
} else if browser.IsCrawler() != true {
t.Errorf("Expected true but got %t", browser.IsCrawler())
}
}
func TestGetBrowserIssues(t *testing.T) {
// https://github.com/digitalcrab/browscap_go/issues/4
ua := "Mozilla/5.0 (iPad; CPU OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3"
if browser, ok := GetBrowser(ua); !ok {
t.Error("Browser not found")
} else if browser.DeviceType != "Tablet" {
t.Errorf("Expected tablet %q", browser.DeviceType)
}
}
func TestLastVersion(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
version, err := LastVersion()
if err != nil {
t.Fatalf("%v", err)
}
if version == "" {
t.Fatalf("Version not found")
}
t.Logf("Last version is %q, current version: %q", version, InitializedVersion())
}
func TestDownload(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
version, err := LastVersion()
if err != nil {
t.Fatalf("%v", err)
}
if version != InitializedVersion() {
t.Logf("Start downloading version %q", version)
tmpFile := "/tmp/browscap_go_TestDownload.ini"
if err = DownloadFile(tmpFile); err != nil {
t.Fatalf("%v", err)
}
t.Logf("Initializing with new version")
InitBrowsCap(tmpFile, true)
if version != InitializedVersion() {
t.Fatalf("New file is wrong")
}
}
}
func BenchmarkInit(b *testing.B) {
for i := 0; i < b.N; i++ {
InitBrowsCap(TEST_INI_FILE, true)
}
}
func BenchmarkGetBrowser(b *testing.B) {
data, err := ioutil.ReadFile("test-data/user_agents_sample.txt")
if err != nil {
b.Error(err)
}
uas := strings.Split(strings.TrimSpace(string(data)), "\n")
b.ResetTimer()
for i := 0; i < b.N; i++ {
idx := i % len(uas)
_, ok := GetBrowser(uas[idx])
if !ok {
b.Errorf("User agent not recognized: %s", uas[idx])
}
}
}