-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuseragent_test.go
78 lines (73 loc) · 1.64 KB
/
useragent_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
package useragent
import (
"testing"
ver "github.com/hashicorp/go-version"
"github.com/stretchr/testify/assert"
)
func TestString(t *testing.T) {
ua := UA{
Version: ver.Must(ver.NewSemver("1.2.3")),
OS: "linux",
Arch: "amd64",
EnvType: ENV_DOCKER,
}
assert.Equal(t, "postman-insights-agent/1.2.3 (linux; amd64; docker)", ua.String())
}
func TestFromString(t *testing.T) {
testCases := []struct {
name string
input string
expected UA
expectErr bool
}{
{
name: "good case",
input: "postman-insights-agent/1.2.3 (linux; amd64; docker)",
expected: UA{
Version: ver.Must(ver.NewSemver("1.2.3")),
OS: "linux",
Arch: "amd64",
EnvType: ENV_DOCKER,
},
},
{
name: "empty os is ok",
input: "postman-insights-agent/1.2.3 (; amd64; docker)",
expected: UA{
Version: ver.Must(ver.NewSemver("1.2.3")),
OS: "",
Arch: "amd64",
EnvType: ENV_DOCKER,
},
},
{
name: "empty arch is ok",
input: "postman-insights-agent/1.2.3 (linux; ; docker)",
expected: UA{
Version: ver.Must(ver.NewSemver("1.2.3")),
OS: "linux",
Arch: "",
EnvType: ENV_DOCKER,
},
},
{
name: "bad version",
input: "postman-insights-agent/1.2x.3 (linux; amd64; docker)",
expectErr: true,
},
{
name: "bad env",
input: "postman-insights-agent/1.2.3 (linux; amd64; i-dont-exist)",
expectErr: true,
},
}
for _, c := range testCases {
actual, err := FromString(c.input)
if c.expectErr {
assert.Error(t, err, c.name)
} else {
assert.NoError(t, err, c.name)
assert.Equal(t, c.expected, actual, c.name)
}
}
}