github.com/hashicorp/terraform-plugin-sdk@v1.17.2/httpclient/useragent_test.go (about)

     1  package httpclient
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform-plugin-sdk/meta"
     9  )
    10  
    11  func TestUserAgentAppendViaEnvVar(t *testing.T) {
    12  	if oldenv, isSet := os.LookupEnv(uaEnvVar); isSet {
    13  		defer os.Setenv(uaEnvVar, oldenv)
    14  	} else {
    15  		defer os.Unsetenv(uaEnvVar)
    16  	}
    17  
    18  	expectedBase := "HashiCorp Terraform/0.0.0 (+https://www.terraform.io) Terraform Plugin SDK/" + meta.SDKVersionString()
    19  
    20  	testCases := []struct {
    21  		envVarValue string
    22  		expected    string
    23  	}{
    24  		{"", expectedBase},
    25  		{" ", expectedBase},
    26  		{" \n", expectedBase},
    27  		{"test/1", expectedBase + " test/1"},
    28  		{"test/1 (comment)", expectedBase + " test/1 (comment)"},
    29  	}
    30  
    31  	for i, tc := range testCases {
    32  		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
    33  			os.Unsetenv(uaEnvVar)
    34  			os.Setenv(uaEnvVar, tc.envVarValue)
    35  			givenUA := TerraformUserAgent("0.0.0")
    36  			if givenUA != tc.expected {
    37  				t.Fatalf("Expected User-Agent '%s' does not match '%s'", tc.expected, givenUA)
    38  			}
    39  		})
    40  	}
    41  }