forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseragent_test.go
78 lines (67 loc) · 1.81 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 httpclient
import (
"fmt"
"os"
"testing"
"github.com/hashicorp/terraform/version"
)
func TestUserAgentString_env(t *testing.T) {
expectedBase := fmt.Sprintf(userAgentFormat, version.Version)
if oldenv, isSet := os.LookupEnv(uaEnvVar); isSet {
defer os.Setenv(uaEnvVar, oldenv)
} else {
defer os.Unsetenv(uaEnvVar)
}
for i, c := range []struct {
expected string
additional string
}{
{expectedBase, ""},
{expectedBase, " "},
{expectedBase, " \n"},
{fmt.Sprintf("%s test/1", expectedBase), "test/1"},
{fmt.Sprintf("%s test/2", expectedBase), "test/2 "},
{fmt.Sprintf("%s test/3", expectedBase), " test/3 "},
{fmt.Sprintf("%s test/4", expectedBase), "test/4 \n"},
} {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if c.additional == "" {
os.Unsetenv(uaEnvVar)
} else {
os.Setenv(uaEnvVar, c.additional)
}
actual := UserAgentString()
if c.expected != actual {
t.Fatalf("Expected User-Agent '%s' does not match '%s'", c.expected, actual)
}
})
}
}
func TestUserAgentAppendViaEnvVar(t *testing.T) {
if oldenv, isSet := os.LookupEnv(uaEnvVar); isSet {
defer os.Setenv(uaEnvVar, oldenv)
} else {
defer os.Unsetenv(uaEnvVar)
}
expectedBase := "HashiCorp Terraform/0.0.0 (+https://www.terraform.io)"
testCases := []struct {
envVarValue string
expected string
}{
{"", expectedBase},
{" ", expectedBase},
{" \n", expectedBase},
{"test/1", expectedBase + " test/1"},
{"test/1 (comment)", expectedBase + " test/1 (comment)"},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
os.Unsetenv(uaEnvVar)
os.Setenv(uaEnvVar, tc.envVarValue)
givenUA := TerraformUserAgent("0.0.0")
if givenUA != tc.expected {
t.Fatalf("Expected User-Agent '%s' does not match '%s'", tc.expected, givenUA)
}
})
}
}