2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-02-15 23:30:31 +00:00
|
|
|
package useragent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-08-06 16:02:04 +00:00
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2018-02-15 23:30:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserAgent(t *testing.T) {
|
|
|
|
projectURL = "https://vault-test.com"
|
|
|
|
rt = "go5.0"
|
|
|
|
|
2022-03-31 17:31:11 +00:00
|
|
|
type args struct {
|
|
|
|
comments []string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "User agent",
|
|
|
|
args: args{},
|
2023-02-08 17:30:27 +00:00
|
|
|
want: "Vault (+https://vault-test.com; go5.0)",
|
2022-03-31 17:31:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "User agent with additional comment",
|
|
|
|
args: args{
|
|
|
|
comments: []string{"pid-abcdefg"},
|
|
|
|
},
|
2023-02-08 17:30:27 +00:00
|
|
|
want: "Vault (+https://vault-test.com; go5.0; pid-abcdefg)",
|
2022-03-31 17:31:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "User agent with additional comments",
|
|
|
|
args: args{
|
|
|
|
comments: []string{"pid-abcdefg", "cloud-provider"},
|
|
|
|
},
|
2023-02-08 17:30:27 +00:00
|
|
|
want: "Vault (+https://vault-test.com; go5.0; pid-abcdefg; cloud-provider)",
|
2022-03-31 17:31:11 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := String(tt.args.comments...); got != tt.want {
|
|
|
|
t.Errorf("String() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
2018-02-15 23:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-06 16:02:04 +00:00
|
|
|
|
|
|
|
func TestUserAgentPlugin(t *testing.T) {
|
|
|
|
projectURL = "https://vault-test.com"
|
|
|
|
rt = "go5.0"
|
|
|
|
|
2022-03-31 17:31:11 +00:00
|
|
|
type args struct {
|
|
|
|
pluginName string
|
2022-04-05 17:07:33 +00:00
|
|
|
pluginEnv *logical.PluginEnvironment
|
2022-03-31 17:31:11 +00:00
|
|
|
comments []string
|
2018-08-06 16:02:04 +00:00
|
|
|
}
|
2022-03-31 17:31:11 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{
|
2022-04-05 17:07:33 +00:00
|
|
|
{
|
|
|
|
name: "Plugin user agent with nil plugin env",
|
|
|
|
args: args{
|
|
|
|
pluginEnv: nil,
|
|
|
|
},
|
|
|
|
want: "",
|
|
|
|
},
|
2022-03-31 17:31:11 +00:00
|
|
|
{
|
|
|
|
name: "Plugin user agent without plugin name",
|
2022-04-05 17:07:33 +00:00
|
|
|
args: args{
|
|
|
|
pluginEnv: &logical.PluginEnvironment{
|
|
|
|
VaultVersion: "1.2.3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "Vault/1.2.3 (+https://vault-test.com; go5.0)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Plugin user agent without plugin name",
|
|
|
|
args: args{
|
|
|
|
pluginEnv: &logical.PluginEnvironment{
|
|
|
|
VaultVersion: "1.2.3",
|
|
|
|
},
|
|
|
|
},
|
2022-03-31 17:31:11 +00:00
|
|
|
want: "Vault/1.2.3 (+https://vault-test.com; go5.0)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Plugin user agent with plugin name",
|
|
|
|
args: args{
|
|
|
|
pluginName: "azure-auth",
|
2022-04-05 17:07:33 +00:00
|
|
|
pluginEnv: &logical.PluginEnvironment{
|
|
|
|
VaultVersion: "1.2.3",
|
|
|
|
},
|
2022-03-31 17:31:11 +00:00
|
|
|
},
|
|
|
|
want: "Vault/1.2.3 (+https://vault-test.com; azure-auth; go5.0)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Plugin user agent with plugin name and additional comment",
|
|
|
|
args: args{
|
|
|
|
pluginName: "azure-auth",
|
2022-04-05 17:07:33 +00:00
|
|
|
pluginEnv: &logical.PluginEnvironment{
|
|
|
|
VaultVersion: "1.2.3",
|
|
|
|
},
|
|
|
|
comments: []string{"pid-abcdefg"},
|
2022-03-31 17:31:11 +00:00
|
|
|
},
|
|
|
|
want: "Vault/1.2.3 (+https://vault-test.com; azure-auth; go5.0; pid-abcdefg)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Plugin user agent with plugin name and additional comments",
|
|
|
|
args: args{
|
|
|
|
pluginName: "azure-auth",
|
2022-04-05 17:07:33 +00:00
|
|
|
pluginEnv: &logical.PluginEnvironment{
|
|
|
|
VaultVersion: "1.2.3",
|
|
|
|
},
|
|
|
|
comments: []string{"pid-abcdefg", "cloud-provider"},
|
2022-03-31 17:31:11 +00:00
|
|
|
},
|
|
|
|
want: "Vault/1.2.3 (+https://vault-test.com; azure-auth; go5.0; pid-abcdefg; cloud-provider)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Plugin user agent with no plugin name and additional comments",
|
|
|
|
args: args{
|
2022-04-05 17:07:33 +00:00
|
|
|
pluginEnv: &logical.PluginEnvironment{
|
|
|
|
VaultVersion: "1.2.3",
|
|
|
|
},
|
2022-03-31 17:31:11 +00:00
|
|
|
comments: []string{"pid-abcdefg", "cloud-provider"},
|
|
|
|
},
|
|
|
|
want: "Vault/1.2.3 (+https://vault-test.com; go5.0; pid-abcdefg; cloud-provider)",
|
|
|
|
},
|
2022-04-05 17:07:33 +00:00
|
|
|
{
|
|
|
|
name: "Plugin user agent with version prerelease",
|
|
|
|
args: args{
|
|
|
|
pluginName: "azure-auth",
|
|
|
|
pluginEnv: &logical.PluginEnvironment{
|
|
|
|
VaultVersion: "1.2.3",
|
|
|
|
VaultVersionPrerelease: "dev",
|
|
|
|
},
|
|
|
|
comments: []string{"pid-abcdefg", "cloud-provider"},
|
|
|
|
},
|
|
|
|
want: "Vault/1.2.3-dev (+https://vault-test.com; azure-auth; go5.0; pid-abcdefg; cloud-provider)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Plugin user agent with version metadata",
|
|
|
|
args: args{
|
|
|
|
pluginName: "azure-auth",
|
|
|
|
pluginEnv: &logical.PluginEnvironment{
|
|
|
|
VaultVersion: "1.2.3",
|
|
|
|
VaultVersionMetadata: "ent",
|
|
|
|
},
|
|
|
|
comments: []string{"pid-abcdefg", "cloud-provider"},
|
|
|
|
},
|
|
|
|
want: "Vault/1.2.3+ent (+https://vault-test.com; azure-auth; go5.0; pid-abcdefg; cloud-provider)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Plugin user agent with version prerelease and metadata",
|
|
|
|
args: args{
|
|
|
|
pluginName: "azure-auth",
|
|
|
|
pluginEnv: &logical.PluginEnvironment{
|
|
|
|
VaultVersion: "1.2.3",
|
|
|
|
VaultVersionPrerelease: "dev",
|
|
|
|
VaultVersionMetadata: "ent",
|
|
|
|
},
|
|
|
|
comments: []string{"pid-abcdefg", "cloud-provider"},
|
|
|
|
},
|
|
|
|
want: "Vault/1.2.3-dev+ent (+https://vault-test.com; azure-auth; go5.0; pid-abcdefg; cloud-provider)",
|
|
|
|
},
|
2022-03-31 17:31:11 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-04-05 17:07:33 +00:00
|
|
|
if got := PluginString(tt.args.pluginEnv, tt.args.pluginName, tt.args.comments...); got != tt.want {
|
2022-03-31 17:31:11 +00:00
|
|
|
t.Errorf("PluginString() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
2018-08-06 16:02:04 +00:00
|
|
|
}
|
|
|
|
}
|