| // Copyright (c) HashiCorp, Inc. |
| // SPDX-License-Identifier: MPL-2.0 |
| package fwtransport |
| |
| import ( |
| "context" |
| "testing" |
| ) |
| |
| func TestCompileUserAgentString(t *testing.T) { |
| cases := map[string]struct { |
| Name string // Name of the provider |
| TerraformVersion string |
| ProviderVersion string |
| EnvValue string |
| ExpectedUserAgent string |
| }{ |
| "the expected user agent is returned for given inputs": { |
| Name: "terraform-provider-foobar", |
| TerraformVersion: "1.2.3", |
| ProviderVersion: "9.9.9", |
| ExpectedUserAgent: "Terraform/1.2.3 (+https://www.terraform.io) Terraform-Plugin-SDK/terraform-plugin-framework terraform-provider-foobar/9.9.9", |
| }, |
| "the user agent can have values appended via an environment variable": { |
| Name: "terraform-provider-foobar", |
| TerraformVersion: "1.2.3", |
| ProviderVersion: "9.9.9", |
| EnvValue: "I'm appended at the end!", |
| ExpectedUserAgent: "Terraform/1.2.3 (+https://www.terraform.io) Terraform-Plugin-SDK/terraform-plugin-framework terraform-provider-foobar/9.9.9 I'm appended at the end!", |
| }, |
| "values appended via an environment variable have whitespace trimmed": { |
| Name: "terraform-provider-foobar", |
| TerraformVersion: "1.2.3", |
| ProviderVersion: "9.9.9", |
| EnvValue: " my surrounding white space is removed ", |
| ExpectedUserAgent: "Terraform/1.2.3 (+https://www.terraform.io) Terraform-Plugin-SDK/terraform-plugin-framework terraform-provider-foobar/9.9.9 my surrounding white space is removed", |
| }, |
| } |
| |
| for tn, tc := range cases { |
| t.Run(tn, func(t *testing.T) { |
| // Arrange |
| ctx := context.Background() |
| |
| t.Setenv(uaEnvVar, tc.EnvValue) // Use same global const as the CompileUserAgentString function |
| |
| // Act |
| |
| ua := CompileUserAgentString(ctx, tc.Name, tc.TerraformVersion, tc.ProviderVersion) |
| |
| // Assert |
| if ua != tc.ExpectedUserAgent { |
| t.Fatalf("Incorrect user agent output: got %s, want %s", ua, tc.ExpectedUserAgent) |
| } |
| }) |
| } |
| } |