mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-02 12:17:39 -06:00
4c254cc2be
This is part of a general effort to move all of Terraform's non-library package surface under internal in order to reinforce that these are for internal use within Terraform only. If you were previously importing packages under this prefix into an external codebase, you could pin to an earlier release tag as an interim solution until you've make a plan to achieve the same functionality some other way.
79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|