This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
device-detector_test.go
84 lines (77 loc) · 2.74 KB
/
device-detector_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
package devicedetector
import (
"fmt"
"testing"
)
// TestDeviceDetector tests initialization and parsing errors dependent js lib.
func TestDeviceDetector(t *testing.T) {
userAgent := "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36"
detector, err := NewDeviceDetector(DeviceDetectorOptions{})
if err != nil {
t.Errorf("failed to create device detector: %v", err)
}
result, err := detector.Parse(userAgent)
if err != nil {
t.Errorf("failed to parse user agent: %v", err)
}
if result == nil {
t.Errorf("result is nil")
}
if result == "" {
t.Errorf("result is empty")
}
fmt.Printf("%s\n", result)
}
// BenchmarkDeviceDetector benchmarks the device detector.
func BenchmarkDeviceDetector(b *testing.B) {
userAgent := "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36"
detector, err := NewDeviceDetector(DeviceDetectorOptions{})
if err != nil {
b.Errorf("failed to create device detector: %v", err)
}
for i := 0; i < b.N; i++ {
_, err := detector.Parse(userAgent)
if err != nil {
b.Errorf("failed to parse user agent: %v", err)
}
}
}
// ExampleDeviceDetector shows how to use the device detector.
func ExampleNewDeviceDetector() {
userAgent := "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36"
detector, err := NewDeviceDetector(DeviceDetectorOptions{})
if err != nil {
fmt.Printf("failed to create device detector: %v", err)
}
result, err := detector.Parse(userAgent)
if err != nil {
fmt.Printf("failed to parse user agent: %v", err)
}
if result == nil {
fmt.Printf("result is nil")
}
if result == "" {
fmt.Printf("result is empty")
}
fmt.Printf("%s\n", result)
// Output:
// {"client":{"type":"browser","name":"Chrome Mobile","version":"41.0","engine":"Blink","engineVersion":""},"os":{"name":"Android","version":"6.0","platform":""},"device":{"type":"smartphone","brand":"Google","model":"Nexus 5X"},"bot":null}
}
func TestNewBotDetector(t *testing.T) {
userAgent := "Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Mobile/15E148 Safari/604.1 (compatible; AdsBot-Google-Mobile; +http://www.google.com/mobile/adsbot.html)"
detector, err := NewBotDetector(DeviceDetectorOptions{})
if err != nil {
t.Errorf("failed to create device detector: %v", err)
}
result, err := detector.Parse(userAgent)
if err != nil {
t.Errorf("failed to parse user agent: %v", err)
}
if result == nil {
t.Errorf("result is nil")
}
if result == "" {
t.Errorf("result is empty")
}
fmt.Printf("%s\n", result)
}